Class: VirtualBox::COM::BaseInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/virtualbox/com/base_interface.rb

Direct Known Subclasses

FFIInterface, MSCOMInterface, NilInterface

Instance Method Summary collapse

Constructor Details

#initializeBaseInterface

Returns a new instance of BaseInterface.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/virtualbox/com/base_interface.rb', line 6

def initialize
  @task_queue = Queue.new

  @lib_thread = Thread.new(@task_queue) do |queue|
    while true
      task, result = queue.pop

      # Run the task, set the return value, and run the waiter
      # which will simply finish that thread
      result << task.call
    end
  end
end

Instance Method Details

#on_lib_thread(&task) ⇒ Object

This function takes a block and runs it on a thread which is guaranteed to be the same since the first time this is called. This is required by the MSCOM implementation and is a good idea in general so that multiple API calls aren’t firing at once.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/virtualbox/com/base_interface.rb', line 25

def on_lib_thread(&task)
  # If we're already on the lib thread, then just run it!
  return task.call if Thread.current == @lib_thread

  # Add the task to the queue
  result = Queue.new
  @task_queue << [task, result]

  # Pop the result off of the result queue
  result.pop
end