Class: Teek::BackgroundThread::BackgroundWork::TaskContext
- Inherits:
-
Object
- Object
- Teek::BackgroundThread::BackgroundWork::TaskContext
- Defined in:
- lib/teek/background_thread.rb
Overview
Context passed to the work block
Instance Method Summary collapse
-
#check_message ⇒ Object
Non-blocking check for messages from main thread.
-
#check_pause ⇒ Object
Check pause state, blocking if paused.
-
#initialize(output_queue, message_queue) ⇒ TaskContext
constructor
A new instance of TaskContext.
-
#send_message(msg) ⇒ Object
Send a message back to main thread (not a result).
-
#wait_message ⇒ Object
Blocking wait for next message.
-
#yield(value) ⇒ Object
Yield a result to the main thread.
Constructor Details
#initialize(output_queue, message_queue) ⇒ TaskContext
Returns a new instance of TaskContext.
206 207 208 209 210 |
# File 'lib/teek/background_thread.rb', line 206 def initialize(output_queue, ) @output_queue = output_queue = @paused = false end |
Instance Method Details
#check_message ⇒ Object
Non-blocking check for messages from main thread. Returns the message or nil if none.
221 222 223 224 225 226 227 228 |
# File 'lib/teek/background_thread.rb', line 221 def return nil if .empty? msg = .pop(true) (msg) msg rescue ThreadError nil end |
#check_pause ⇒ Object
Check pause state, blocking if paused
243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/teek/background_thread.rb', line 243 def check_pause # First drain any pending messages (non-blocking) until .empty? msg = .pop(true) (msg) end # Then block while paused while @paused msg = .pop # Blocking wait (msg) end end |
#send_message(msg) ⇒ Object
Send a message back to main thread (not a result)
238 239 240 |
# File 'lib/teek/background_thread.rb', line 238 def (msg) @output_queue << [:message, msg] end |
#wait_message ⇒ Object
Blocking wait for next message
231 232 233 234 235 |
# File 'lib/teek/background_thread.rb', line 231 def msg = .pop (msg) msg end |
#yield(value) ⇒ Object
Yield a result to the main thread. Calls Thread.pass to give main thread a chance to process events.
214 215 216 217 |
# File 'lib/teek/background_thread.rb', line 214 def yield(value) @output_queue << [:result, value] Thread.pass end |