Method: LIBUSB::Context#handle_events

Defined in:
lib/libusb/context.rb

#handle_events(timeout = nil, completion_flag = nil) ⇒ Object

Handle any pending events in blocking mode.

This method must be called when libusb is running asynchronous transfers. This gives libusb the opportunity to reap pending transfers, invoke callbacks, etc.

If a zero timeout is passed, this function will handle any already-pending events and then immediately return in non-blocking style.

If a non-zero timeout is passed and no events are currently pending, this method will block waiting for events to handle up until the specified timeout. If an event arrives or a signal is raised, this method will return early.

If the parameter completion_flag is used, then after obtaining the event handling lock this function will return immediately if the flag is set to completed. This allows for race free waiting for the completion of a specific transfer. See source of Transfer#submit_and_wait for a use case of completion_flag.

Parameters:

  • timeout (Integer, nil) (defaults to: nil)

    the maximum time (in millseconds) to block waiting for events, or 0 for non-blocking mode

  • completion_flag (Context::CompletionFlag, nil) (defaults to: nil)

    CompletionFlag to check

See Also:



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/libusb/context.rb', line 331

def handle_events(timeout=nil, completion_flag=nil)
  if completion_flag && !completion_flag.is_a?(Context::CompletionFlag)
    raise ArgumentError, "completion_flag is not a CompletionFlag"
  end
  if timeout
    timeval = Call::Timeval.new
    timeval.in_ms = timeout
    res = if Call.respond_to?(:libusb_handle_events_timeout_completed)
      Call.libusb_handle_events_timeout_completed(@ctx, timeval, completion_flag)
    else
      Call.libusb_handle_events_timeout(@ctx, timeval)
    end
  else
    res = if Call.respond_to?(:libusb_handle_events_completed)
      Call.libusb_handle_events_completed(@ctx, completion_flag )
    else
      Call.libusb_handle_events(@ctx)
    end
  end
  LIBUSB.raise_error res, "in libusb_handle_events" if res<0
end