Class: Channel
- Inherits:
-
Object
show all
- Defined in:
- lib/channel.rb
Defined Under Namespace
Modules: Runtime
Classes: Closed, Conversion, Direction, ReceiveOnly, SendOnly, Timer
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(size = nil) ⇒ Channel
Returns a new instance of Channel.
4
5
6
7
8
9
|
# File 'lib/channel.rb', line 4
def initialize(size = nil)
@q = size ? SizedQueue.new(size) : Queue.new
@closed = false
@mutex = Mutex.new
@waiting = []
end
|
Class Method Details
.select(*channels) ⇒ Object
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/channel.rb', line 95
def select(*channels)
selector = new
threads = channels.map do |c|
Thread.new { selector << [c.recv, c] }
end
yield selector.recv
ensure
selector.close
threads.each(&:kill).each(&:join)
end
|
Instance Method Details
#close ⇒ Object
54
55
56
57
58
59
60
|
# File 'lib/channel.rb', line 54
def close
lock! do
return if closed?
@closed = true
all!
end
end
|
#closed? ⇒ Boolean
62
63
64
|
# File 'lib/channel.rb', line 62
def closed?
@closed
end
|
#each ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/channel.rb', line 70
def each
return enum_for(:each) unless block_given?
loop do
begin
e = recv
rescue Channel::Closed
return
else
yield e
end
end
end
|
#receive_only! ⇒ Object
Also known as:
r!
84
85
86
|
# File 'lib/channel.rb', line 84
def receive_only!
ReceiveOnly.new(self)
end
|
#recv ⇒ Object
Also known as:
pop
33
34
35
36
37
38
39
40
41
|
# File 'lib/channel.rb', line 33
def recv
lock! do
loop do
closed! if closed? && @q.empty?
wait! && next if @q.empty?
break @q.pop
end
end
end
|
#send(val) ⇒ Object
Also known as:
push
44
45
46
47
48
49
50
|
# File 'lib/channel.rb', line 44
def send(val)
lock! do
fail Closed if closed?
@q << val
next!
end
end
|
#send_only! ⇒ Object
Also known as:
s!
89
90
91
|
# File 'lib/channel.rb', line 89
def send_only!
SendOnly.new(self)
end
|