Class: Tribe::Mailbox
- Inherits:
-
Object
- Object
- Tribe::Mailbox
- Defined in:
- lib/tribe/mailbox.rb
Instance Method Summary collapse
- #alive? ⇒ Boolean
-
#initialize(pool) ⇒ Mailbox
constructor
A new instance of Mailbox.
- #kill ⇒ Object
- #obtain_and_shift ⇒ Object
- #push(event, &block) ⇒ Object
- #release(&block) ⇒ Object
Constructor Details
#initialize(pool) ⇒ Mailbox
Returns a new instance of Mailbox.
3 4 5 6 7 8 9 |
# File 'lib/tribe/mailbox.rb', line 3 def initialize(pool) @pool = pool @messages = [] @alive = true @lock = Mutex.new @owner_thread = nil end |
Instance Method Details
#alive? ⇒ Boolean
59 60 61 62 63 |
# File 'lib/tribe/mailbox.rb', line 59 def alive? @lock.synchronize do return @alive end end |
#kill ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/tribe/mailbox.rb', line 50 def kill @lock.synchronize do @alive = false @messages.clear end return nil end |
#obtain_and_shift ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/tribe/mailbox.rb', line 22 def obtain_and_shift @lock.synchronize do return nil unless @alive if @owner_thread if @owner_thread == Thread.current return @messages.shift else return nil end else @owner_thread = Thread.current return @messages.shift end end end |
#push(event, &block) ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/tribe/mailbox.rb', line 11 def push(event, &block) @lock.synchronize do return nil unless @alive @messages.push(event) @pool.perform { block.call } unless @owner_thread end return nil end |
#release(&block) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/tribe/mailbox.rb', line 39 def release(&block) @lock.synchronize do return nil unless @owner_thread == Thread.current @owner_thread = nil @pool.perform { block.call } if @alive && @messages.length > 0 end return nil end |