Module: RubyGo

Defined in:
lib/ruby_go.rb,
lib/ruby_go/version.rb

Defined Under Namespace

Classes: Timeout

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#chan(len = 1) ⇒ Object

chan creates a size channel with default size 1



13
14
15
# File 'lib/ruby_go.rb', line 13

def chan(len = 1)
  SizedQueue.new(len)
end

#go(&block) ⇒ Object

go starts a thread



8
9
10
# File 'lib/ruby_go.rb', line 8

def go(&block)
  Thread.new(&block)
end

#select(chans) ⇒ Object

select receives a { chan => action } map, and blocked til a chan to execute



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby_go.rb', line 18

def select(chans)
  chan_method = ->(_) {}
  value = nil

  selected = false
  loop do
    sleep 0.01 # to prevent busy loop

    chans.each do |c, cm|
      begin
        value = c.pop(true)
        chan_method = cm
        selected = true
        break
      rescue ThreadError
      end
    end

    break if selected
  end

  chan_method.call(value)
end