Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread safety #69

Open
mgkuhn opened this issue Jul 9, 2020 · 0 comments
Open

Thread safety #69

mgkuhn opened this issue Jul 9, 2020 · 0 comments

Comments

@mgkuhn
Copy link
Collaborator

mgkuhn commented Jul 9, 2020

The libserialport documentation says under “Thread safety”:

“Certain combinations of calls can be made concurrently, as follows.

  • Calls using different ports may always be made concurrently, i.e. it is safe for separate threads to handle their own ports.
  • Calls using the same port may be made concurrently when one call is a read operation and one call is a write operation, i.e. >it is safe to use separate "reader" and "writer" threads for the same port. See below for which operations meet these >definitions.

Read operations: sp_blocking_read(), sp_blocking_read_next(), sp_nonblocking_read(), sp_input_waiting(), sp_flush() with SP_BUF_INPUT only, sp_wait() with SP_EVENT_RX_READY only.

Write operations: sp_blocking_write(), sp_nonblocking_write(), sp_output_waiting(), sp_drain(), sp_flush() with SP_BUF_OUTPUT only, sp_wait() with SP_EVENT_TX_READY only.

If two calls, on the same port, do not fit into one of these categories each, then they may not be made concurrently.”

Julia has claimed thread safety for its I/O operations since release 1.3, so I wonder if we shouldn't aim to ensure the same for LibSerialPort.

To enforce the constraints required above, e.g. for applications where multiple threads send data to the same serial port, we could add to the SerialPort type two new ReentrantLock members:

rlock::ReentrantLock
wlock::ReentrantLock

each to be initialized with ReentrantLock(). And then each call to one of the read operations above will have to be enclosed with corresponding lock(rlock) and unlock(rlock) methods, and equivalently with wlock for the write operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant