Class: Teek::BackgroundThread::BackgroundWork::TaskContext

Inherits:
Object
  • Object
show all
Defined in:
lib/teek/background_thread.rb

Overview

Context passed to the work block

Instance Method Summary collapse

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, message_queue)
  @output_queue = output_queue
  @message_queue = message_queue
  @paused = false
end

Instance Method Details

#check_messageObject

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 check_message
  return nil if @message_queue.empty?
  msg = @message_queue.pop(true)
  handle_control_message(msg)
  msg
rescue ThreadError
  nil
end

#check_pauseObject

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 @message_queue.empty?
    msg = @message_queue.pop(true)
    handle_control_message(msg)
  end

  # Then block while paused
  while @paused
    msg = @message_queue.pop  # Blocking wait
    handle_control_message(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 send_message(msg)
  @output_queue << [:message, msg]
end

#wait_messageObject

Blocking wait for next message



231
232
233
234
235
# File 'lib/teek/background_thread.rb', line 231

def wait_message
  msg = @message_queue.pop
  handle_control_message(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