thread-safety doc + Rust modeling
cover thread-safety doc + Rust modeling
This updates the docs with what was discussed in the linked thread. Then, the Rust bindings are adjusted to match in behaviour.
To: Linux-GPIO <[email protected]>
Cc: Bartosz Golaszewski <[email protected]>
Cc: Viresh Kumar <[email protected]>
---
Erik Schilling (3):
doc: drop unneeded <p> tags
doc: document thread safety guarantees
bindings: rust: mark all owning types as Send
bindings/rust/libgpiod/src/edge_event.rs | 4 ++++ bindings/rust/libgpiod/src/event_buffer.rs | 8 ++++++++ bindings/rust/libgpiod/src/info_event.rs | 4 ++++ bindings/rust/libgpiod/src/line_config.rs | 4 ++++ bindings/rust/libgpiod/src/line_request.rs | 4 ++++ bindings/rust/libgpiod/src/line_settings.rs | 4 ++++ bindings/rust/libgpiod/src/request_config.rs | 4 ++++ include/gpiod.h | 26 +++++++++++++++++++++++--- 8 files changed, 55 insertions(+), 3 deletions(-)
---
base-commit: ced90e79217793957b11414f47f8aa8a77c7a2d5
change-id: 20230928-rust-send-trait-316a8f31bb97
Best regards,
Signature · 1 line
Erik Schilling <[email protected]>
PATCH 1/3 doc: drop unneeded <p> tags
Even before Doxygen gained Markdown support, empty lines were considered as paragraphs. Changelogs indicate that this was the case since at least the doxygen 1.2 series (where I found a mentiond that something around this behaviour was fixed). So at least works in Doxygen versions released after 2001 [1].
[1] https://sourceforge.net/p/doxygen/mailman/message/172899/
--- include/gpiod.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
include/gpiod.h +3 -3
@@ -22,11 +22,11 @@ extern "C" {* This is the complete documentation of the public API made available to * users of libgpiod. * -* <p>The API is logically split into several sections. For each opaque data +* The API is logically split into several sections. For each opaque data * class, there's a set of functions for manipulating it. Together they can be * thought of as objects and their methods in OOP parlance. * -* <p>General note on error handling: all functions exported by libgpiod that +* General note on error handling: all functions exported by libgpiod that * can fail, set errno to one of the error values defined in errno.h upon * failure. The way of notifying the caller that an error occurred varies * between functions, but in general a function that returns an int, returns -1
@@ -35,7 +35,7 @@ extern "C" {* codes for every function as they propagate errors from the underlying libc * functions. * -* <p>In general libgpiod functions are NULL-aware. For functions that are +* In general libgpiod functions are NULL-aware. For functions that are * logically methods of data classes - ones that take a pointer to the object * of that class as the first argument - passing a NULL pointer will result in * the program aborting the execution. For non-methods, init functions and
Signature · 1 line
2.41.0
PATCH 2/3 doc: document thread safety guarantees
In reply to Erik SchillingThis mostly adds the information from the linked thread to the doxygen documentation.
Summarized:
- libgpiod object's require synchronization
- individual objects may be used concurrently by different threds
--- include/gpiod.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
include/gpiod.h +20 -0
@@ -42,6 +42,14 @@ extern "C" {* methods that take a pointer as any of the subsequent arguments, the handling * of a NULL-pointer depends on the implementation and may range from gracefully * handling it, ignoring it or returning an error. +* +* libgpiod is thread-aware but does not provide any further thread-safety +* guarantees. This requires the user to ensure that at most one thread may +* work with an object at any time. Sharing objects across threads is allowed +* if a suitable synchronization mechanism serializes the access. Different, +* standalone objects can safely be used concurrently. Most libgpiod objects +* are standalone. Exceptions - such as events allocated in buffers - exist and +* are noted in the documentation. */ /**
@@ -624,6 +632,12 @@ uint64_t gpiod_info_event_get_timestamp_ns(struct gpiod_info_event *event);* @return Returns a pointer to the line-info object associated with the event. * The object lifetime is tied to the event object, so the pointer must * be not be freed by the caller. +* @warning Thread-safety: +* Since the line-info object is tied to the event, different threads +* may not operate on the event and line-info at the same time. The +* line-info can be copied using ::gpiod_line_info_copy in order to +* create a standalone object - which then may safely be used from a +* different thread concurrently. */ struct gpiod_line_info *gpiod_info_event_get_line_info (struct gpiod_info_event *event);
@@ -1297,6 +1311,12 @@ void gpiod_edge_event_buffer_free(struct gpiod_edge_event_buffer *buffer);* @return Pointer to an event stored in the buffer. The lifetime of the * event is tied to the buffer object. Users must not free the event * returned by this function. +* @warning Thread-safety: +* Since events are tied to the buffer instance, different threads +* may not operate on the buffer and any associated events at the same +* time. Events can be copied using ::gpiod_edge_event_copy in order +* to create a standalone objects - which each may safely be used from +* a different thread concurrently. */ struct gpiod_edge_event * gpiod_edge_event_buffer_get_event(struct gpiod_edge_event_buffer *buffer,
Signature · 1 line
2.41.0
PATCH 3/3 bindings: rust: mark all owning types as `Send`
In reply to Erik SchillingThe thread-safety rules of libgpiod allow individual object instances to be used from different threads. So far, this was not actually possible with the Rust bindings. Not being Send disallowed the user to transfer the ownership to different threads.
Rust also has a Sync marker. That one would even allow sending references of objects to other threads. Since we wrap a lot of C functions with fn foo(&self) signatures, that would not be safe. libgpiod does not allow concurrent API calls to the same object instance
- which Rust would allow for read-only references. Thus, we do not define that one.
Chip was already modeled correctly.
line::Info is not marked as Send since it may either be owning or non- owning. That problem is fixed as part of a separate pull request [1].
[1] https://lore.kernel.org/r/[email protected]
--- bindings/rust/libgpiod/src/edge_event.rs | 4 ++++ bindings/rust/libgpiod/src/event_buffer.rs | 8 ++++++++ bindings/rust/libgpiod/src/info_event.rs | 4 ++++ bindings/rust/libgpiod/src/line_config.rs | 4 ++++ bindings/rust/libgpiod/src/line_request.rs | 4 ++++ bindings/rust/libgpiod/src/line_settings.rs | 4 ++++ bindings/rust/libgpiod/src/request_config.rs | 4 ++++ 7 files changed, 32 insertions(+)
bindings/rust/libgpiod/src/edge_event.rs +4 -0
@@ -24,6 +24,10 @@ use super::{#[derive( Debug , Eq , PartialEq )] pub struct Event (*mut gpiod::gpiod_edge_event ); +// SAFETY: Event models a wrapper around an owned gpiod_edge_event and may +// be safely sent to other threads. +unsafe impl Send for Event {} +impl Event {pub fn event_clone (event: &Event ) ->Result <Event > {// SAFETY: `gpiod_edge_event` is guaranteed to be valid here.
bindings/rust/libgpiod/src/event_buffer.rs +8 -0
@@ -68,6 +68,14 @@ pub struct Buffer { events: Vec<*mut gpiod::gpiod_edge_event>, } +// SAFETY: Buffer models an owned gpiod_edge_event_buffer. However, there may +// be events tied to it. Concurrent access from multiple threads to a buffer +// and its associated events is not allowed by the C lib. +// In Rust, those events will always be borrowed from a buffer instance. Thus, +// either Rust prevents the user to move the Buffer while there are still +// borrowed events, or we can safely send the the Buffer. +unsafe impl Send for Buffer {} +impl Buffer {/// Create a new edge event buffer. ///
bindings/rust/libgpiod/src/info_event.rs +4 -0
@@ -25,6 +25,10 @@ pub struct Event { pub(crate) event: *mut gpiod::gpiod_info_event, } +// SAFETY: Event models a wrapper around an owned gpiod_info_event and may be +// safely sent to other threads. +unsafe impl Send for Event {} +impl Event {/// Get a single chip's line's status change event. pub (crate )fn new (event: *mut gpiod::gpiod_info_event ) ->Self {
bindings/rust/libgpiod/src/line_config.rs +4 -0
@@ -28,6 +28,10 @@ pub struct Config { pub(crate) config: *mut gpiod::gpiod_line_config, } +// SAFETY: Config models a wrapper around an owned gpiod_line_config and may be +// safely sent to other threads. +unsafe impl Send for Config {} +impl Config {/// Create a new line config object. pub fn new () ->Result <Self > {
bindings/rust/libgpiod/src/line_request.rs +4 -0
@@ -20,6 +20,10 @@ pub struct Request { pub(crate) request: *mut gpiod::gpiod_line_request, } +// SAFETY: Request models a wrapper around an owned gpiod_line_request and may +// be safely sent to other threads. +unsafe impl Send for Request {} +impl Request {/// Request a set of lines for exclusive usage. pub (crate )fn new (request: *mut gpiod::gpiod_line_request ) ->Result <Self > {
bindings/rust/libgpiod/src/line_settings.rs +4 -0
@@ -25,6 +25,10 @@ pub struct Settings { pub(crate) settings: *mut gpiod::gpiod_line_settings, } +// SAFETY: Settings models a wrapper around an owned gpiod_line_settings and may +// be safely sent to other threads. +unsafe impl Send for Settings {} +impl Settings {/// Create a new line settings object. pub fn new () ->Result <Self > {
bindings/rust/libgpiod/src/request_config.rs +4 -0
@@ -20,6 +20,10 @@ pub struct Config { pub(crate) config: *mut gpiod::gpiod_request_config, } +// SAFETY: Config models a wrapper around an owned gpiod_request_config and may +// be safely sent to other threads. +unsafe impl Send for Config {} +impl Config {/// Create a new request config object. pub fn new () ->Result <Self > {
Signature · 1 line
2.41.0
Re: [libgpiod][PATCH 3/3] bindings: rust: mark all owning types as `Send`
Quoted 21 lines · Erik Schilling
The thread-safety rules of libgpiod allow individual object instances to be used from different threads. So far, this was not actually possible with the Rust bindings. Not being
Senddisallowed the user to transfer the ownership to different threads.Rust also has a
Syncmarker. That one would even allow sending references of objects to other threads. Since we wrap a lot of C functions withfn foo(&self)signatures, that would not be safe. libgpiod does not allow concurrent API calls to the same object instance
- which Rust would allow for read-only references. Thus, we do not define that one.Chip was already modeled correctly.
line::Infois not marked as Send since it may either be owning or non- owning. That problem is fixed as part of a separate pull request [1].
Quoted 9 lines
--- bindings/rust/libgpiod/src/edge_event.rs | 4 ++++ bindings/rust/libgpiod/src/event_buffer.rs | 8 ++++++++ bindings/rust/libgpiod/src/info_event.rs | 4 ++++ bindings/rust/libgpiod/src/line_config.rs | 4 ++++ bindings/rust/libgpiod/src/line_request.rs | 4 ++++ bindings/rust/libgpiod/src/line_settings.rs | 4 ++++ bindings/rust/libgpiod/src/request_config.rs | 4 ++++ 7 files changed, 32 insertions(+)
Signature · 1 line
viresh
Re: [libgpiod][PATCH 3/3] bindings: rust: mark all owning types as `Send`
Quoted 33 lines · Viresh Kumar
Quoted 21 lines · Erik Schilling
The thread-safety rules of libgpiod allow individual object instances to be used from different threads. So far, this was not actually possible with the Rust bindings. Not being
Senddisallowed the user to transfer the ownership to different threads.Rust also has a
Syncmarker. That one would even allow sending references of objects to other threads. Since we wrap a lot of C functions withfn foo(&self)signatures, that would not be safe. libgpiod does not allow concurrent API calls to the same object instance
- which Rust would allow for read-only references. Thus, we do not define that one.Chip was already modeled correctly.
line::Infois not marked as Send since it may either be owning or non- owning. That problem is fixed as part of a separate pull request [1].Quoted 9 lines
--- bindings/rust/libgpiod/src/edge_event.rs | 4 ++++ bindings/rust/libgpiod/src/event_buffer.rs | 8 ++++++++ bindings/rust/libgpiod/src/info_event.rs | 4 ++++ bindings/rust/libgpiod/src/line_config.rs | 4 ++++ bindings/rust/libgpiod/src/line_request.rs | 4 ++++ bindings/rust/libgpiod/src/line_settings.rs | 4 ++++ bindings/rust/libgpiod/src/request_config.rs | 4 ++++ 7 files changed, 32 insertions(+)
Quoted 2 lines
Acked-by Viresh Kumar <[email protected]>
Thanks, do you have any comments about the other patches in this series?
Bart
Re: [libgpiod][PATCH 3/3] bindings: rust: mark all owning types as `Send`
Quoted 34 lines · Bartosz Golaszewski
Quoted 33 lines · Viresh Kumar
Quoted 30 lines · Erik Schilling
The thread-safety rules of libgpiod allow individual object instances to be used from different threads. So far, this was not actually possible with the Rust bindings. Not being
Senddisallowed the user to transfer the ownership to different threads.Rust also has a
Syncmarker. That one would even allow sending references of objects to other threads. Since we wrap a lot of C functions withfn foo(&self)signatures, that would not be safe. libgpiod does not allow concurrent API calls to the same object instance
- which Rust would allow for read-only references. Thus, we do not define that one.Chip was already modeled correctly.
line::Infois not marked as Send since it may either be owning or non- owning. That problem is fixed as part of a separate pull request [1].[1] https://lore.kernel.org/r/[email protected]
Signed-off-by Erik Schilling <[email protected]>--- bindings/rust/libgpiod/src/edge_event.rs | 4 ++++ bindings/rust/libgpiod/src/event_buffer.rs | 8 ++++++++ bindings/rust/libgpiod/src/info_event.rs | 4 ++++ bindings/rust/libgpiod/src/line_config.rs | 4 ++++ bindings/rust/libgpiod/src/line_request.rs | 4 ++++ bindings/rust/libgpiod/src/line_settings.rs | 4 ++++ bindings/rust/libgpiod/src/request_config.rs | 4 ++++ 7 files changed, 32 insertions(+)
Quoted 4 lines
Quoted 2 lines
Acked-by Viresh Kumar <[email protected]>Thanks, do you have any comments about the other patches in this series?
Hm. The others are not Rust-related and mostly try to document our discussion about thread-safety of the C lib. It would probably be good if you could double-check that I summarized everything correctly.
- Erik
Re: [libgpiod][PATCH 3/3] bindings: rust: mark all owning types as `Send`
Quoted 39 lines · Erik Schilling
Quoted 37 lines · Bartosz Golaszewski
Quoted 35 lines · Viresh Kumar
Quoted 21 lines · Erik Schilling
The thread-safety rules of libgpiod allow individual object instances to be used from different threads. So far, this was not actually possible with the Rust bindings. Not being
Senddisallowed the user to transfer the ownership to different threads.Rust also has a
Syncmarker. That one would even allow sending references of objects to other threads. Since we wrap a lot of C functions withfn foo(&self)signatures, that would not be safe. libgpiod does not allow concurrent API calls to the same object instance
- which Rust would allow for read-only references. Thus, we do not define that one.Chip was already modeled correctly.
line::Infois not marked as Send since it may either be owning or non- owning. That problem is fixed as part of a separate pull request [1].Quoted 9 lines
--- bindings/rust/libgpiod/src/edge_event.rs | 4 ++++ bindings/rust/libgpiod/src/event_buffer.rs | 8 ++++++++ bindings/rust/libgpiod/src/info_event.rs | 4 ++++ bindings/rust/libgpiod/src/line_config.rs | 4 ++++ bindings/rust/libgpiod/src/line_request.rs | 4 ++++ bindings/rust/libgpiod/src/line_settings.rs | 4 ++++ bindings/rust/libgpiod/src/request_config.rs | 4 ++++ 7 files changed, 32 insertions(+)Acked-by Viresh Kumar <[email protected]>
Quoted 8 lines
Quoted 1 line
Thanks, do you have any comments about the other patches in this series?
Hm. The others are not Rust-related and mostly try to document our discussion about thread-safety of the C lib. It would probably be good if you could double-check that I summarized everything correctly.
- Erik
Of course they aren't and I should have read them before assuming the entire series is for rust bindings. :)
Thanks.
Bart
Quoted 25 lines · Erik Schilling
This updates the docs with what was discussed in the linked thread. Then, the Rust bindings are adjusted to match in behaviour.
To: Linux-GPIO <[email protected]>
Cc: Bartosz Golaszewski <[email protected]>
Cc: Viresh Kumar <[email protected]>Signed-off-by Erik Schilling <[email protected]>---
Erik Schilling (3):
doc: drop unneeded <p> tags
doc: document thread safety guarantees
bindings: rust: mark all owning types asSendbindings/rust/libgpiod/src/edge_event.rs | 4 ++++ bindings/rust/libgpiod/src/event_buffer.rs | 8 ++++++++ bindings/rust/libgpiod/src/info_event.rs | 4 ++++ bindings/rust/libgpiod/src/line_config.rs | 4 ++++ bindings/rust/libgpiod/src/line_request.rs | 4 ++++ bindings/rust/libgpiod/src/line_settings.rs | 4 ++++ bindings/rust/libgpiod/src/request_config.rs | 4 ++++ include/gpiod.h | 26 +++++++++++++++++++++++--- 8 files changed, 55 insertions(+), 3 deletions(-)
Quoted 8 lines
---
base-commit:ced90e79217793957b11414f47f8aa8a77c7a2d5
change-id: 20230928-rust-send-trait-316a8f31bb97Best regards,
--
Erik Schilling <[email protected]>
Series applied, thanks!
Bart
Quoted 31 lines · Bartosz Golaszewski
Quoted 29 lines · Erik Schilling
This updates the docs with what was discussed in the linked thread. Then, the Rust bindings are adjusted to match in behaviour.
To: Linux-GPIO <[email protected]>
Cc: Bartosz Golaszewski <[email protected]>
Cc: Viresh Kumar <[email protected]>Signed-off-by Erik Schilling <[email protected]>---
Erik Schilling (3):
doc: drop unneeded <p> tags
doc: document thread safety guarantees
bindings: rust: mark all owning types asSendbindings/rust/libgpiod/src/edge_event.rs | 4 ++++ bindings/rust/libgpiod/src/event_buffer.rs | 8 ++++++++ bindings/rust/libgpiod/src/info_event.rs | 4 ++++ bindings/rust/libgpiod/src/line_config.rs | 4 ++++ bindings/rust/libgpiod/src/line_request.rs | 4 ++++ bindings/rust/libgpiod/src/line_settings.rs | 4 ++++ bindings/rust/libgpiod/src/request_config.rs | 4 ++++ include/gpiod.h | 26 +++++++++++++++++++++++--- 8 files changed, 55 insertions(+), 3 deletions(-)---
base-commit:ced90e79217793957b11414f47f8aa8a77c7a2d5
change-id: 20230928-rust-send-trait-316a8f31bb97
Quoted 6 lines
Quoted 4 lines
Best regards,
--
Erik Schilling <[email protected]>Series applied, thanks!
Thanks for merging and the clarifications!
- Erik