Module: CRuby::Coroutine

Defined in:
lib/cruby/coroutine.rb

Constant Summary collapse

@@rdyQ =
CRuby::Queue.new
@@flag =

割り込み可能フラグ

false
@@mutex =
Mutex.new
@@condv =
ConditionVariable.new

Class Method Summary collapse

Class Method Details

.dispatchObject



65
66
67
68
69
70
71
72
# File 'lib/cruby/coroutine.rb', line 65

def self.dispatch
  k = @@rdyQ.dequeue
  if k != nil then
    k.call
    assert false
  end
  IOT.exec
end

.empty?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/cruby/coroutine.rb', line 61

def self.empty?
  @@rdyQ.empty?
end

.enqueue(k) ⇒ Object



82
83
84
# File 'lib/cruby/coroutine.rb', line 82

def self.enqueue k
  @@rdyQ.enqueue(k)
end

.interruptObject

メインスレッドに割り込みをかける



42
43
44
45
46
47
48
49
# File 'lib/cruby/coroutine.rb', line 42

def self.interrupt
  # 割り込み可能になるまで待つ
  @@mutex.synchronize {
    while not @@flag
      @@condv.wait(@@mutex)
    end
  }
end

.interruptible(flag) ⇒ Object

割り込み可能フラグをセットする



52
53
54
55
56
57
58
59
# File 'lib/cruby/coroutine.rb', line 52

def self.interruptible flag
  @@mutex.synchronize {
    if flag
      @@condv.signal
    end
    @@flag = flag
  }
end

.rdyqObject



37
38
39
# File 'lib/cruby/coroutine.rb', line 37

def self.rdyq
  p @@rdyQ
end

.spawn(&f) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/cruby/coroutine.rb', line 74

def self.spawn &f
  callcc {|parentK|
    @@rdyQ.enqueue(parentK)
    f.call         # 例外捕捉する必要あり
    CRuby::Coroutine.dispatch
  }
end