linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: [libgpiod] Question regarding locks / race conditions
       [not found] <CABrMOxZY6Q3wiYsJUaXZiSET_gjMJ3GWaMrDCXwXC6nf7AX2_g@mail.gmail.com>
@ 2023-04-17  7:19 ` Bartosz Golaszewski
  2023-04-17 12:29   ` Kent Gibson
  0 siblings, 1 reply; 3+ messages in thread
From: Bartosz Golaszewski @ 2023-04-17  7:19 UTC (permalink / raw)
  To: Sameh Mohamed; +Cc: linux-gpio

On Sun, 16 Apr 2023 at 18:20, Sameh Mohamed <sameh4@gmx.com> wrote:
>
> Hello,
>
> Many thanks for your excellent work and contributions.  I've been learning about libgpiod in the last few days.
>
> Studying the cxx bindings examples, particularly around line request, I looked up the C code for gpiod_line_request_get_values_subset  and saw that it does not deal with any potential locks.
>
> I searched a bit on stackoverflow regarding Linux device drivers, and saw that it's basically up to the application to manage locks.
>
> As an inexperienced programmer in the Linux user-space; I have worked primarily in python and C++ client applications at a higher level, I wanted to confirm my understanding.  If I am writing some library to run the RaspberryPi that will make use of libgpiod-2.0.1, it will be up to me to avoid locks when making line requests, etc.
>
> Thanks,
> Sameh

Yes! It's a design approach low-level C libraries usually take and
libgpiod is no exception.

Bart

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [libgpiod] Question regarding locks / race conditions
  2023-04-17  7:19 ` [libgpiod] Question regarding locks / race conditions Bartosz Golaszewski
@ 2023-04-17 12:29   ` Kent Gibson
       [not found]     ` <CABrMOxbWa8mxA=ckAu6QVKnej5o7UGrWXdeDMaAqScGVVk3=wA@mail.gmail.com>
  0 siblings, 1 reply; 3+ messages in thread
From: Kent Gibson @ 2023-04-17 12:29 UTC (permalink / raw)
  To: Sameh Mohamed; +Cc: Bartosz Golaszewski, linux-gpio

On Mon, Apr 17, 2023 at 09:19:31AM +0200, Bartosz Golaszewski wrote:
> On Sun, 16 Apr 2023 at 18:20, Sameh Mohamed <sameh4@gmx.com> wrote:
> >
> > Hello,
> >
> > Many thanks for your excellent work and contributions.  I've been learning about libgpiod in the last few days.
> >
> > Studying the cxx bindings examples, particularly around line request, I looked up the C code for gpiod_line_request_get_values_subset  and saw that it does not deal with any potential locks.
> >
> > I searched a bit on stackoverflow regarding Linux device drivers, and saw that it's basically up to the application to manage locks.
> >
> > As an inexperienced programmer in the Linux user-space; I have worked primarily in python and C++ client applications at a higher level, I wanted to confirm my understanding.  If I am writing some library to run the RaspberryPi that will make use of libgpiod-2.0.1, it will be up to me to avoid locks when making line requests, etc.
> >
> > Thanks,
> > Sameh
> 
> Yes! It's a design approach low-level C libraries usually take and
> libgpiod is no exception.
> 

Just to add my 2c...

It isn't clear to me where you expect that locks need to be applied.

Single threaded apps, such as the examples, do not require any locks.
Similarly apps that restrict a given line request to a single thread.
Any shared resources within the kernel, including the relevant device
drivers, are handled by the kernel.

The GPIO uAPI provided by the kernel, and that libgpiod wraps, is MT-safe
(though I can't think of a use case where making calls on a given line request
concurrently from multiple threads is a good idea).

However, data structures returned by libgpiod are in userspace and are not
MT-safe.  So if you want to share those structures between threads then
you need to add locking to prevent potential race conditions.
As Bart alluded to above, the best way to do that is dependent on the
application, so low-level libraries like libgpiod usually leave it to the
app.

And again, you probably don't want to share a line request between
threads anyway - even if you think you do.

Cheers,
Kent.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [libgpiod] Question regarding locks / race conditions
       [not found]     ` <CABrMOxbWa8mxA=ckAu6QVKnej5o7UGrWXdeDMaAqScGVVk3=wA@mail.gmail.com>
@ 2023-04-17 14:17       ` Kent Gibson
  0 siblings, 0 replies; 3+ messages in thread
From: Kent Gibson @ 2023-04-17 14:17 UTC (permalink / raw)
  To: Sameh Mohamed; +Cc: Bartosz Golaszewski, linux-gpio

On Mon, Apr 17, 2023 at 10:13:18AM -0400, Sameh Mohamed wrote:
> Thanks Kent and Bartosz,
> 
> @Kent: you are correct, I don't intend to share any line requests on the
> same line, rather I am curious what will happen if two threads create two
> requests for two lines at the same time.  For instance two threads setting
> values on different GPIO pins but at the same time.
> 

No locks necessary - the kernel will deal with any resource contention
internally.

Cheers,
Kent.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-04-17 14:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <CABrMOxZY6Q3wiYsJUaXZiSET_gjMJ3GWaMrDCXwXC6nf7AX2_g@mail.gmail.com>
2023-04-17  7:19 ` [libgpiod] Question regarding locks / race conditions Bartosz Golaszewski
2023-04-17 12:29   ` Kent Gibson
     [not found]     ` <CABrMOxbWa8mxA=ckAu6QVKnej5o7UGrWXdeDMaAqScGVVk3=wA@mail.gmail.com>
2023-04-17 14:17       ` Kent Gibson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).