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.



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

#reaperObject



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_sendObject

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