Class: Rubinius::Channel
- Inherits:
-
Object
- Object
- Rubinius::Channel
- Defined in:
- lib/rubinius/kernel/common/channel.rb,
lib/rubinius/kernel/bootstrap/channel.rb
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns nil if there are no values, otherwise a List object containing all values the Channel contains.
-
#waiting ⇒ Object
readonly
Returns nil if nothing is waiting, or a List object which contains all Thread objects waiting on this Channel.
Class Method Summary collapse
-
.allocate ⇒ Object
We must be sure a Channel is always created properly, so handle this the same as new.
-
.convert_to_channel(obj) ⇒ Object
Converts
obj
into a Channel using #to_channel. -
.new ⇒ Object
Creates a new Channel and registers it with the VM.
-
.receive(obj) ⇒ Object
Legacy API.
Instance Method Summary collapse
- #as_lock(val = nil) ⇒ Object
- #inspect ⇒ Object
-
#receive ⇒ Object
Removes and returns the first value from the Channel.
- #receive_timeout(duration) ⇒ Object
-
#send(obj) ⇒ Object
(also: #<<)
Puts
obj
in the Channel. -
#to_channel ⇒ Object
API compliance, returns self.
- #try_receive ⇒ Object
Instance Attribute Details
#value ⇒ Object (readonly)
Returns nil if there are no values, otherwise a List object containing all values the Channel contains.
28 29 30 |
# File 'lib/rubinius/kernel/bootstrap/channel.rb', line 28 def value @value end |
#waiting ⇒ Object (readonly)
Returns nil if nothing is waiting, or a List object which contains all Thread objects waiting on this Channel.
22 23 24 |
# File 'lib/rubinius/kernel/bootstrap/channel.rb', line 22 def waiting @waiting end |
Class Method Details
.allocate ⇒ Object
We must be sure a Channel is always created properly, so handle this the same as new.
40 41 42 43 |
# File 'lib/rubinius/kernel/bootstrap/channel.rb', line 40 def self.allocate Ruby.primitive :channel_new raise PrimitiveFailure, "Channel.new primitive failed" end |
.convert_to_channel(obj) ⇒ Object
Converts obj
into a Channel using #to_channel.
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rubinius/kernel/bootstrap/channel.rb', line 78 def self.convert_to_channel(obj) return obj if Channel === obj begin o2 = obj.to_channel unless Channel === o2 raise ArgumentError, "to_channel on #{obj.inspect} did not return a Channel" end return o2 rescue NoMethodError raise ArgumentError, "Unable to convert #{obj.inspect} into a channel" end end |
.new ⇒ Object
Creates a new Channel and registers it with the VM.
33 34 35 36 |
# File 'lib/rubinius/kernel/bootstrap/channel.rb', line 33 def self.new Ruby.primitive :channel_new raise PrimitiveFailure, "Channel.new primitive failed" end |
.receive(obj) ⇒ Object
Legacy API. To be removed.
94 95 96 |
# File 'lib/rubinius/kernel/bootstrap/channel.rb', line 94 def self.receive(obj) # :nodoc: return convert_to_channel(obj).receive end |
Instance Method Details
#as_lock(val = nil) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/rubinius/kernel/common/channel.rb', line 11 def as_lock(val=nil) receive begin yield ensure self << val end end |
#inspect ⇒ Object
7 8 9 |
# File 'lib/rubinius/kernel/common/channel.rb', line 7 def inspect "#<Rubinius::Channel>" end |
#receive ⇒ Object
Removes and returns the first value from the Channel. If the channel is empty, Thread.current is put to sleep until #send is called.
60 61 62 63 |
# File 'lib/rubinius/kernel/bootstrap/channel.rb', line 60 def receive Ruby.primitive :channel_receive raise PrimitiveFailure, "Channel#receive primitive failed" end |
#receive_timeout(duration) ⇒ Object
65 66 67 68 |
# File 'lib/rubinius/kernel/bootstrap/channel.rb', line 65 def receive_timeout(duration) Ruby.primitive :channel_receive_timeout raise PrimitiveFailure, "Channel#receive_timeout primitive failed" end |
#send(obj) ⇒ Object Also known as: <<
Puts obj
in the Channel. If there are waiting threads the first thread will be woken up and handed obj
.
49 50 51 52 |
# File 'lib/rubinius/kernel/bootstrap/channel.rb', line 49 def send(obj) Ruby.primitive :channel_send raise PrimitiveFailure, "Channel#send primitive failed" end |
#to_channel ⇒ Object
API compliance, returns self.
101 102 103 |
# File 'lib/rubinius/kernel/bootstrap/channel.rb', line 101 def to_channel self end |
#try_receive ⇒ Object
70 71 72 73 |
# File 'lib/rubinius/kernel/bootstrap/channel.rb', line 70 def try_receive Ruby.primitive :channel_try_receive raise PrimitiveFailure, "Channel#try_receive primitive failed" end |