zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
10.89k stars 6.63k forks source link

RFC: standardize on a synchronization mechanism for device access #26073

Open pabigot opened 4 years ago

pabigot commented 4 years ago

There are three types of synchronization necessary for devices, several of which are considered in #22941 and its draft PRs particularly #24511:

The topic of this issue is access control, which historically has been implemented using k_sem. As originally raised in this comment I'd like to propose that was use k_mutex instead, as this offers:

The first question is whether this the right move. A follow-up question is whether the generic device API should provide support for any or all of the above synchronizations.

If we use k_mutex for access control we would need #25678 to be worked so its operations can be initiated from interrupt context, as is already allowed for k_sem.

andrewboie commented 4 years ago

While we're thinking about this topic, it looks the the PREEMPT_RT patch to Linux goes even further and replaces some usages of spinlocks with RT-mutexes. There are caveats and I'm not sure how applicable this is to Zephyr, but interesting nonetheless https://lwn.net/Articles/146861/

carlescufi commented 2 years ago

API meeting

jfischer-no commented 2 years ago
  • Synchronization shall be disabled when CONFIG_MULTITHREADING is n

I am against forcing it for all drivers APIs.

gregshue commented 2 years ago

When we need exclusive access to resources shared with interrupt context we need to lock interrupts to the level of the ISR(s) accessing it (or we need to disable the ISRs that access it - causing priority inversion). This cannot be done by a k_mutex, so we will need at least two mechanisms. Which one depends on whether the project configures the interrupt service routine to run in interrupt context or meta-irq context.

Also, don't forget about access in an SMP environment and any issues with access originating from User threads.