Class: CRuby::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/cruby/channel.rb

Direct Known Subclasses

IoChannel, TimerChannel

Instance Method Summary collapse

Constructor Details

#initializeChannel

Returns a new instance of Channel.



3
4
5
6
# File 'lib/cruby/channel.rb', line 3

def initialize
  @sendQ = CRuby::Queue.new()
  @recvQ = CRuby::Queue.new()
end

Instance Method Details

#recvObject



12
13
14
# File 'lib/cruby/channel.rb', line 12

def recv
  recvEvt.sync
end

#recvEvtObject

受信イベントを生成する



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cruby/channel.rb', line 32

def recvEvt
  pollFn = Proc.new { @sendQ.poll }
  doFn = Proc.new {
    item = @sendQ.dequeue
    item['flg'][0] = true
    CRuby::Coroutine.enqueue(item['cont'])
    item['msg']
  }
  blockFn = Proc.new {|flg,k| @recvQ.enqueue({'flg'=>flg,'cont'=>k}) }
  CRuby::Event.new([CRuby::Event::BEvt.new(pollFn, doFn, blockFn)])
end

#send(msg) ⇒ Object



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

def send msg
  sendEvt(msg).sync
end

#sendEvt(msg) ⇒ Object

送信イベントを生成する



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cruby/channel.rb', line 17

def sendEvt msg
  pollFn = Proc.new { @recvQ.poll }
  doFn = Proc.new {
    callcc {|k|
      item = @recvQ.dequeue
      item['flg'][0] = true
      CRuby::Coroutine.enqueue(k)
      item['cont'].call(msg)
    }
  }
  blockFn = Proc.new {|flg,k| @sendQ.enqueue({'flg'=>flg,'msg'=>msg,'cont'=>k}) }
  CRuby::Event.new([CRuby::Event::BEvt.new(pollFn, doFn, blockFn)])
end