module RubyGo

Constants

VERSION

Public Instance Methods

chan(len = 1) click to toggle source

chan creates a size channel with default size 1

# File lib/ruby_go.rb, line 13
def chan(len = 1)
  SizedQueue.new(len)
end
go(&block) click to toggle source

go starts a thread

# File lib/ruby_go.rb, line 8
def go(&block)
  Thread.new(&block)
end
select(chans) click to toggle source

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

# 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