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
3 4 5 6 7 8 |
# File 'lib/tribe/mailbox.rb', line 3 def initialize(pool) @pool = pool = [] @alive = true @mutex = Mutex.new end |
Instance Method Details
#alive? ⇒ Boolean
58 59 60 61 62 |
# File 'lib/tribe/mailbox.rb', line 58 def alive? @mutex.synchronize do return @alive end end |
#kill ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/tribe/mailbox.rb', line 49 def kill @mutex.synchronize do @alive = false .clear end return nil end |
#obtain_and_shift ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/tribe/mailbox.rb', line 21 def obtain_and_shift @mutex.synchronize do return nil unless @alive if @owner_thread if @owner_thread == Thread.current return .shift else return nil end else @owner_thread = Thread.current return .shift end end end |
#push(event, &block) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/tribe/mailbox.rb', line 10 def push(event, &block) @mutex.synchronize do return nil unless @alive .push(event) @pool.perform { block.call } unless @owner_thread end return nil end |
#release(&block) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/tribe/mailbox.rb', line 38 def release(&block) @mutex.synchronize do return nil unless @owner_thread == Thread.current @owner_thread = nil @pool.perform { block.call } if @alive && .length > 0 end return nil end |