Class: FiniteMachine::MessageQueue Private
- Inherits:
-
Object
- Object
- FiniteMachine::MessageQueue
- Defined in:
- lib/finite_machine/message_queue.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Allows for storage of asynchronous messages such as events and callbacks.
Used internally by Observer and StateMachine
Instance Method Summary collapse
-
#<<(event) ⇒ nil
Add asynchronous event to the event queue to process.
-
#alive? ⇒ Boolean
Check if the event queue is alive.
-
#empty? ⇒ Boolean
Check if there are any events to handle.
-
#initialize ⇒ MessageQueue
constructor
Initialize an event queue in separate thread.
- #inspect ⇒ Object private
-
#join(timeout = nil) ⇒ nil, Thread
Join the event queue from current thread.
- #running? ⇒ Boolean private
-
#shutdown ⇒ Boolean
Shut down this event queue and clean it up.
-
#size ⇒ Integer
Get number of events waiting for processing.
-
#spawn_thread ⇒ Object
private
Spawn new background thread.
-
#start ⇒ Object
private
Start a new thread with a queue of callback events to run.
-
#subscribe(*args, &block) ⇒ Object
Add listener to the queue to receive messages.
Constructor Details
#initialize ⇒ MessageQueue
Initialize an event queue in separate thread
20 21 22 23 24 25 26 27 |
# File 'lib/finite_machine/message_queue.rb', line 20 def initialize @not_empty = ConditionVariable.new @mutex = Mutex.new @queue = Queue.new @dead = false @listeners = [] @thread = nil end |
Instance Method Details
#<<(event) ⇒ nil
Add asynchronous event to the event queue to process
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/finite_machine/message_queue.rb', line 61 def <<(event) @mutex.synchronize do if @dead (event) else @queue << event @not_empty.signal end end end |
#alive? ⇒ Boolean
Check if the event queue is alive
101 102 103 |
# File 'lib/finite_machine/message_queue.rb', line 101 def alive? @mutex.synchronize { !@dead } end |
#empty? ⇒ Boolean
Check if there are any events to handle
89 90 91 |
# File 'lib/finite_machine/message_queue.rb', line 89 def empty? @mutex.synchronize { @queue.empty? } end |
#inspect ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
157 158 159 160 161 |
# File 'lib/finite_machine/message_queue.rb', line 157 def inspect @mutex.synchronize do "#<#{self.class}:#{object_id.to_s(16)} @size=#{size}, @dead=#{@dead}>" end end |
#join(timeout = nil) ⇒ nil, Thread
Join the event queue from current thread
115 116 117 118 |
# File 'lib/finite_machine/message_queue.rb', line 115 def join(timeout = nil) return unless @thread timeout.nil? ? @thread.join : @thread.join(timeout) end |
#running? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
47 48 49 |
# File 'lib/finite_machine/message_queue.rb', line 47 def running? !@thread.nil? && alive? end |
#shutdown ⇒ Boolean
Shut down this event queue and clean it up
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/finite_machine/message_queue.rb', line 128 def shutdown fail EventQueueDeadError, 'event queue already dead' if @dead queue = [] @mutex.synchronize do @dead = true @not_empty.broadcast queue = @queue @queue.clear end while !queue.empty? (queue.pop) end true end |
#size ⇒ Integer
Get number of events waiting for processing
153 154 155 |
# File 'lib/finite_machine/message_queue.rb', line 153 def size @mutex.synchronize { @queue.size } end |
#spawn_thread ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Spawn new background thread
40 41 42 43 44 45 |
# File 'lib/finite_machine/message_queue.rb', line 40 def spawn_thread @thread = Thread.new do Thread.current.abort_on_exception = true process_events end end |
#start ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Start a new thread with a queue of callback events to run
32 33 34 35 |
# File 'lib/finite_machine/message_queue.rb', line 32 def start return if running? @mutex.synchronize { spawn_thread } end |