Blame README.multi-thread

Packit 9795e1
libusbredirparser and libusbredirhost are *not* 100% thread-safe. They allow
Packit 9795e1
usage from multiple threads, but with limitations.
Packit 9795e1
Packit 9795e1
The intended usage of the multi-threading support for libusbredirparser is to
Packit 9795e1
have one reader thread and allow writes / packet sends from multiple threads
Packit 9795e1
(including the reader thread). It is up to the app to deal with flushing
Packit 9795e1
writes by calling do_write itself. do_write may be called from multiple
Packit 9795e1
threads, libusbredirparser will serialize any calls to the write callback.
Packit 9795e1
Packit 9795e1
The intended usage of the multi-threading support for libusbredirhost is to
Packit 9795e1
have one reader thread, one thread calling libusb's handle_events function
Packit 9795e1
and optionally also a separate writer thread.
Packit 9795e1
Packit 9795e1
libusbredirhost offers setting a write flush callback, which it will call
Packit 9795e1
(if set) everytime it has queued some data to write. This can be used to
Packit 9795e1
wakeup a writer thread or it can call usbredirhost_write_guest_data, to
Packit 9795e1
directly write out the queued data from the context of its caller. Note that
Packit 9795e1
the flush callback may be called from both usbredirparser_do_read as well
Packit 9795e1
as from libusb_handle_events, so if those are done in separate threads,
Packit 9795e1
it may get called from multiple threads!!
Packit 9795e1
Packit 9795e1
Packit 9795e1
The above translates to some functions only allowing one caller at a time,
Packit 9795e1
while others allow multiple callers, see below for a detailed overview.
Packit 9795e1
Packit 9795e1
In order to enable the multi-thread support in libusbredir* the app
Packit 9795e1
must provide a number of locking callback functions, for libusbredirparser
Packit 9795e1
this is done by filling in the usbredirparser_*_lock funcs in the
Packit 9795e1
usbredirparser struct before calling usbredirparser_init(). For
Packit 9795e1
libusbredirhost the locking functions (and a write-flush callback) can
Packit 9795e1
be specified by using usbredirhost_open_full() instead of
Packit 9795e1
usbredirhost_open().
Packit 9795e1
Packit 9795e1
Note that the alloc_lock_func may not fail! If it returns NULL no locking
Packit 9795e1
will be done and usage from multiple threads will be unsafe.
Packit 9795e1
Packit 9795e1
Packit 9795e1
Overview of per function multi-thread safeness
Packit 9795e1
----------------------------------------------
Packit 9795e1
Packit 9795e1
usbredirparser:
Packit 9795e1
-Only one caller allowed at a time:
Packit 9795e1
 usbredirparser_create
Packit 9795e1
 usbredirparser_init
Packit 9795e1
 usbredirparser_destroy
Packit 9795e1
 usbredirparser_do_read
Packit 9795e1
Packit 9795e1
-Multiple callers allowed:
Packit 9795e1
 usbredirparser_get_peer_caps (1)
Packit 9795e1
 usbredirparser_peer_has_cap (1)
Packit 9795e1
 usbredirparser_has_data_to_write
Packit 9795e1
 usbredirparser_do_write
Packit 9795e1
 usbredirparser_free_write_buffer
Packit 9795e1
 usbredirparser_free_packet_data
Packit 9795e1
 usbredirparser_send_*
Packit 9795e1
Packit 9795e1
usbredirhost:
Packit 9795e1
-Only one caller allowed at a time:
Packit 9795e1
 usbredirhost_open
Packit 9795e1
 usbredirhost_open_full
Packit 9795e1
 usbredirhost_close
Packit 9795e1
 usbredirhost_read_guest_data
Packit 9795e1
 usbredirhost_set_device
Packit 9795e1
Packit 9795e1
-Multiple callers allowed:
Packit 9795e1
 usbredirhost_has_data_to_write
Packit 9795e1
 usbredirhost_write_guest_data
Packit 9795e1
 usbredirhost_free_write_buffer
Packit 9795e1
 libusb_handle_events (2)
Packit 9795e1
Packit 9795e1
(1) These only return the actual peer caps after the initial hello message
Packit 9795e1
    has been read, as indicated by the hello_func callback.
Packit 9795e1
Packit 9795e1
(2) libusb is thread safe itself, thus allowing multiple callers.