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

Add basic mutex (lock) and spin lock implementations in C11, C++11, AVR, and STM32 #12

Open
ElectricRCAircraftGuy opened this issue Sep 22, 2022 · 0 comments

Comments

@ElectricRCAircraftGuy
Copy link
Owner

ElectricRCAircraftGuy commented Sep 22, 2022

Add basic mutex (lock) and spin lock implementations in C11, C++11, AVR, and STM32

Keep in mind that in AVR I may need to use atomic access guards (turn off interrupts, critical section, restore interrupts), and in both AVR and STM32 (any single-core system) I need to add safety anti-deadlock mechanisms such as automatic deferral, timeout, and retry.

References and research to get me started:

  1. My answer on "Basic spin lock implementation in C11 using _Atomic types": https://stackoverflow.com/a/73819087/4561887
  2. https://stackoverflow.com/questions/71626597/what-are-the-various-ways-to-disable-and-re-enable-interrupts-in-stm32-microcont/71626598#71626598
    1. Also the answer by Tom V about LDREX and STREX (exclusive load and store instructions): https://stackoverflow.com/a/71626777/4561887
    2. Example from mbedOS: https://github.com/ARMmbed/mbed-os/blob/master/rtos/source/Semaphore.cpp
  3. https://en.wikipedia.org/wiki/Test-and-set#Pseudo-C_implementation_of_a_spin_lock
  4. Google search for "STREX stm32"
  5. YouTube: Spinlocks - Part 1 - A Basic Spinlock
  6. http://blog.bachi.net/?p=11934
  7. https://www.mikrocontroller.net/topic/358456 - mentions possible STM32 __LDREX() type C functions to mimic the assembly LDREX and STREX family of instructions which allow making mutexes
  8. https://www.manualslib.com/manual/1375966/St-Stm32f4-Series.html?page=78
  9. https://community.st.com/s/project/a8g3W000000fxUvQAI/strexldrex-is-not-an-intercore-synchronization-method
  10. https://developer.arm.com/documentation/dui0553/b/?lang=en - download the "Cortex -M4 Devices Generic User Guide
    Generic User Guide" here!
  11. https://en.cppreference.com/w/cpp/atomic/atomic
  12. https://en.cppreference.com/w/c/language/atomic
  13. https://en.cppreference.com/w/cpp/atomic/atomic/exchange
  14. https://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange
  15. https://cplusplus.com/reference/atomic/atomic/compare_exchange_strong/
  16. https://en.cppreference.com/w/c/language/atomic
  17. https://en.cppreference.com/w/c/thread
  18. Google search for "C atomic exchange"
  19. Google search for __c11_atomic_exchange
  20. https://www.gnu.org/software/libc/manual/html_mono/libc.html
  21. Google search for "atomic_compare_exchange_weak"
  22. https://en.cppreference.com/w/c/atomic/atomic_compare_exchange
  23. https://en.cppreference.com/w/c/atomic/atomic_exchange
  24. https://en.cppreference.com/w/c/atomic/atomic_compare_exchange
  25. https://en.cppreference.com/w/c/thread
  26. https://en.cppreference.com/w/c/atomic/ATOMIC_LOCK_FREE_consts - macros to see which atomic types in C are lock-free - THIS IS IMPORTANT TO ALLOW YOU TO assert in a library to ensure you're using truly lock-free types in any implementations you do for a spin lock!
  27. https://en.cppreference.com/w/c/language/atomic
  28. https://cplusplus.com/reference/atomic/atomic_exchange/
  29. Google search for atomic spin lock in c
  30. https://en.cppreference.com/w/cpp/atomic/atomic_flag - USE THIS in a library implementation I make for a spin lock
    22. https://en.cppreference.com/w/cpp/atomic/atomic_flag_test_and_set
  31. https://en.cppreference.com/w/c/atomic/atomic_flag - no, use this one--this is the C documentation
    23. https://en.cppreference.com/w/c/atomic/atomic_flag_test_and_set
  32. https://en.cppreference.com/w/c/thread#Atomic_operations

Important sections to look at in the [Cortex -M4 Devices Generic User Guide

Generic User Guide](https://developer.arm.com/documentation/dui0553/b/?lang=en):

p83-84:
image
image

p32-33:
image

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