Class: ThreadSender

Inherits:
Object show all
Defined in:
lib/ffi-tk/thread_sender.rb

Instance Method Summary collapse

Constructor Details

#initializeThreadSender

Returns a new instance of ThreadSender.



3
4
5
6
# File 'lib/ffi-tk/thread_sender.rb', line 3

def initialize
  @queue = Queue.new
  @thread = Thread.new{ reaper }
end

Instance Method Details

#reaperObject



8
9
10
11
12
13
# File 'lib/ffi-tk/thread_sender.rb', line 8

def reaper
  loop do
    block, response_queue = *@queue.pop
    response_queue.push(block.call)
  end
end

#thread_sendObject

If callbacks are invoked within a thread_send, we process them inside the same thread. Calling pop on the queue would cause deadlocks.



18
19
20
21
22
23
24
25
26
# File 'lib/ffi-tk/thread_sender.rb', line 18

def thread_send
  if @thread == Thread.current
    yield
  else
    response_queue = Queue.new
    @queue.push([Proc.new, response_queue])
    response_queue.pop
  end
end