raspberrypi / utils

A collection of scripts and simple applications
BSD 3-Clause "New" or "Revised" License
134 stars 41 forks source link

thread concurrency for gpio_get_level() #66

Closed rafael2k closed 5 months ago

rafael2k commented 5 months ago

Considering concurrency, I'm already wrapping all calls that change gpio states with mutex.

But considering gpio_get_level(), could it be called concurrently in a multi-thread software or better I just make sure I don't call API concurrently?

Reading the datasheet it seems to me that DMA memory writes to gpio registers on memory are atomic.

pelwell commented 5 months ago

Reading the datasheet it seems to me that DMA memory writes to gpio registers on memory are atomic.

There's no DMA involved here, but apart from the possibility that another thread has changed the direction or FSEL of your pin there should be no atomicity problems for gpio_get_level and gpio_set_drive without an external mutex.

rafael2k commented 5 months ago

Thanks @pelwell . Indeed, I did not see any problem yet. Just in case, I added wrapper functions with mutexes: https://github.com/Rhizomatica/hermes-net/commit/f168802f642d3ae0f39b60bc7195929721b27755

rafael2k commented 5 months ago

Btw, how distributions should package Raspberry Pi's gpiolib? Do you think a system-wide shared object is a good idea?

GPIOLIB is just great, perfect for people moving away from WiringPi (like me). : )

pelwell commented 5 months ago

Btw, how distributions should package Raspberry Pi's gpiolib?

It wasn't really intended to be a WiringPi replacement - we'd prefer that to be libgpiod - but I thought it may have other uses. If it is going to be used by more than one binary then a shared library seems sensible, but static builds are so much easier to maintain.

rafael2k commented 5 months ago

Tbh, I found your library easier to use than libgpiod, especially when the target hardware is Rasperry Pi computer line. : ) Anyway, when I create a "debian/" folder for package creation I let you know, in case you have any concern. Thanks a lot!

I'll close this one.