Class: Splib::Monitor
- Inherits:
-
Object
- Object
- Splib::Monitor
- Defined in:
- lib/splib/Monitor.rb
Instance Method Summary collapse
-
#broadcast ⇒ Object
Wake up all threads.
-
#initialize ⇒ Monitor
constructor
Create a new Monitor.
-
#lock ⇒ Object
Lock the monitor.
-
#locked?(cln = true) ⇒ Boolean
- cln
-
Clean dead threads Is monitor locked.
-
#signal ⇒ Object
Wake up earliest thread.
-
#synchronize ⇒ Object
Lock the monitor, execute block and unlock the monitor.
-
#try_lock ⇒ Object
Attempt to lock.
-
#unlock ⇒ Object
Unlock the monitor.
-
#wait(timeout = nil) ⇒ Object
- timeout
-
Wait for given amount of time Park a thread here.
-
#wait_until ⇒ Object
Park thread until block is true.
-
#wait_while ⇒ Object
Park thread while block is true.
-
#waiters ⇒ Object
Number of threads waiting.
Constructor Details
#initialize ⇒ Monitor
Create a new Monitor
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/splib/Monitor.rb', line 10 def initialize @threads = [] @locks = [] @lock_owner = nil @timers = {} @timer = start_timer @stop = false Kernel.at_exit do if(@timer) @stop = true @timer.raise Wakeup.new end end end |
Instance Method Details
#broadcast ⇒ Object
Wake up all threads
69 70 71 72 73 74 75 |
# File 'lib/splib/Monitor.rb', line 69 def broadcast synchronize do @threads.dup.each do |t| t.wakeup if t.alive? && t.stop? end end end |
#lock ⇒ Object
Lock the monitor
81 82 83 84 85 86 |
# File 'lib/splib/Monitor.rb', line 81 def lock Thread.exclusive{ do_lock } until(owner?(Thread.current)) do Thread.stop end end |
#locked?(cln = true) ⇒ Boolean
- cln
-
Clean dead threads
Is monitor locked
107 108 109 110 |
# File 'lib/splib/Monitor.rb', line 107 def locked?(cln=true) Thread.exclusive{clean} if cln @locks.size > 0 || @lock_owner end |
#signal ⇒ Object
Wake up earliest thread
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/splib/Monitor.rb', line 56 def signal synchronize do while(t = @threads.shift) if(t && t.alive? && t.stop?) t.wakeup break else next end end end end |
#synchronize ⇒ Object
Lock the monitor, execute block and unlock the monitor
112 113 114 115 116 117 118 |
# File 'lib/splib/Monitor.rb', line 112 def synchronize result = nil lock result = yield do_unlock result end |
#try_lock ⇒ Object
Attempt to lock. Returns true if lock is aquired and false if not.
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/splib/Monitor.rb', line 92 def try_lock locked = false Thread.exclusive do clean unless(locked?(false)) do_lock locked = true else locked = owner?(Thread.current) end end locked end |
#unlock ⇒ Object
Unlock the monitor
88 89 90 |
# File 'lib/splib/Monitor.rb', line 88 def unlock do_unlock end |
#wait(timeout = nil) ⇒ Object
- timeout
-
Wait for given amount of time
Park a thread here
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/splib/Monitor.rb', line 27 def wait(timeout=nil) raise 'This thread is already a registered sleeper' if @threads.include?(Thread.current) Thread.exclusive{ @threads << Thread.current } if(timeout) timeout = timeout.to_f Thread.exclusive{ @timers[Thread.current] = timeout } @timer.raise Wakeup.new end Thread.stop Thread.exclusive{ @threads.delete(Thread.current) } if(timeout && @timers.has_key?(Thread.current)) Thread.exclusive{ @timers.delete(Thread.current) } @timer.raise Wakeup.new end true end |
#wait_until ⇒ Object
Park thread until block is true
50 51 52 53 54 |
# File 'lib/splib/Monitor.rb', line 50 def wait_until until yield wait end end |
#wait_while ⇒ Object
Park thread while block is true
44 45 46 47 48 |
# File 'lib/splib/Monitor.rb', line 44 def wait_while while yield wait end end |
#waiters ⇒ Object
Number of threads waiting
77 78 79 |
# File 'lib/splib/Monitor.rb', line 77 def waiters @threads.size end |