Class: Rubinius::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/rubinius/kernel/common/channel.rb,
lib/rubinius/kernel/bootstrap/channel.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#valueObject (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

#waitingObject (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

.allocateObject

We must be sure a Channel is always created properly, so handle this the same as new.

Raises:

  • (PrimitiveFailure)


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

.newObject

Creates a new Channel and registers it with the VM.

Raises:

  • (PrimitiveFailure)


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

#inspectObject



7
8
9
# File 'lib/rubinius/kernel/common/channel.rb', line 7

def inspect
  "#<Rubinius::Channel>"
end

#receiveObject

Removes and returns the first value from the Channel. If the channel is empty, Thread.current is put to sleep until #send is called.

Raises:

  • (PrimitiveFailure)


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

Raises:

  • (PrimitiveFailure)


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.

Raises:

  • (PrimitiveFailure)


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_channelObject

API compliance, returns self.



101
102
103
# File 'lib/rubinius/kernel/bootstrap/channel.rb', line 101

def to_channel
  self
end

#try_receiveObject

Raises:

  • (PrimitiveFailure)


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