Module: Proco::MT::Base

Included in:
Future, Threaded, Queue::Base
Defined in:
lib/proco/mt/base.rb

Instance Method Summary collapse

Instance Method Details

#broadcast(&block) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/proco/mt/base.rb', line 54

def broadcast &block
  @mtx.synchronize do
    begin
      block.call if block
    ensure
      @cv.broadcast
    end
  end
end

#do_when(cond, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/proco/mt/base.rb', line 21

def do_when cond, &block
  @mtx.lock
  while !cond.call
    @cv.wait @mtx
  end
  block.call
ensure
  # A good discussion on the use of broadcast instead of signal
  # http://stackoverflow.com/questions/37026/java-notify-vs-notifyall-all-over-again
  @cv.broadcast
  @mtx.unlock
end

#initializeObject



5
6
7
8
# File 'lib/proco/mt/base.rb', line 5

def initialize
  @mtx = Mutex.new
  @cv  = ConditionVariable.new
end

#signal(&block) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/proco/mt/base.rb', line 44

def signal &block
  @mtx.synchronize do
    begin
      block.call if block
    ensure
      @cv.signal
    end
  end
end

#synchronizeObject



34
35
36
37
38
# File 'lib/proco/mt/base.rb', line 34

def synchronize
  @mtx.synchronize do
    yield
  end
end

#try_when(cond, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/proco/mt/base.rb', line 10

def try_when cond, &block
  return unless @mtx.try_lock

  begin
    block.call if cond.call
  ensure
    @cv.broadcast
    @mtx.unlock
  end
end

#wait_until(&cond) ⇒ Object



40
41
42
# File 'lib/proco/mt/base.rb', line 40

def wait_until &cond
  do_when(cond) {}
end