Class: ThreadSender
Instance Method Summary collapse
-
#initialize ⇒ ThreadSender
constructor
A new instance of ThreadSender.
- #reaper ⇒ Object
-
#thread_send ⇒ Object
If callbacks are invoked within a thread_send, we process them inside the same thread.
Constructor Details
#initialize ⇒ ThreadSender
Returns a new instance of ThreadSender.
2 3 4 5 |
# File 'lib/ffi-tk/thread_sender.rb', line 2 def initialize @queue = Queue.new @thread = Thread.new{ reaper } end |
Instance Method Details
#reaper ⇒ Object
7 8 9 10 11 12 |
# File 'lib/ffi-tk/thread_sender.rb', line 7 def reaper loop do block, response_queue = *@queue.pop response_queue.push(block.call) end end |
#thread_send ⇒ Object
If callbacks are invoked within a thread_send, we process them inside the same thread. Calling pop on the queue would cause deadlocks.
17 18 19 20 21 22 23 24 25 |
# File 'lib/ffi-tk/thread_sender.rb', line 17 def thread_send if @thread == Thread.current yield else response_queue = Queue.new @queue.push([Proc.new, response_queue]) response_queue.pop end end |