Class: Ori::Channel

Inherits:
Object
  • Object
show all
Includes:
Selectable
Defined in:
lib/ori/channel.rb

Overview

: [E]

Constant Summary collapse

EMPTY =
"empty"

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Channel

: (Integer size) -> void



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ori/channel.rb', line 11

def initialize(size)
  @size = size
  if size.zero?
    # Zero-sized channel state
    @taker_waiting = false
    @sender_waiting = false
    @value = EMPTY
  else
    # Buffered channel state
    @queue = UnboundedQueue.new
  end
end

Instance Method Details

#awaitObject

: () -> Channel



62
63
64
65
# File 'lib/ori/channel.rb', line 62

def await
  peek
  self
end

#deconstructObject

: () -> Array



68
69
70
71
# File 'lib/ori/channel.rb', line 68

def deconstruct
  Ori.sync { peek }
  [take]
end

#peekObject

: () -> E



44
45
46
47
48
49
50
# File 'lib/ori/channel.rb', line 44

def peek
  if @size.zero?
    peek_zero_sized
  else
    peek_buffered
  end
end

#put(item) ⇒ Object Also known as: <<

: (E item) -> void



25
26
27
28
29
30
31
# File 'lib/ori/channel.rb', line 25

def put(item)
  if @size.zero?
    put_zero_sized(item)
  else
    put_buffered(item)
  end
end

#takeObject

: () -> E



35
36
37
38
39
40
41
# File 'lib/ori/channel.rb', line 35

def take
  if @size.zero?
    take_zero_sized
  else
    take_buffered
  end
end

#value?Boolean

: () -> bool

Returns:

  • (Boolean)


53
54
55
56
57
58
59
# File 'lib/ori/channel.rb', line 53

def value?
  if @size.zero?
    @value != EMPTY
  else
    @queue.peek != UnboundedQueue::EMPTY
  end
end