logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

synchronization_util.h (2529B)


  1. // Copyright 2022 Stefan Kerkmann
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #if defined(PLATFORM_SUPPORTS_SYNCHRONIZATION)
  5. # if defined(SPLIT_KEYBOARD)
  6. void split_shared_memory_lock(void);
  7. void split_shared_memory_unlock(void);
  8. # endif
  9. #else
  10. # if defined(SPLIT_KEYBOARD)
  11. inline void split_shared_memory_lock(void){};
  12. inline void split_shared_memory_unlock(void){};
  13. # endif
  14. #endif
  15. /* GCCs cleanup attribute expects a function with one parameter, which is a
  16. * pointer to a type compatible with the variable. As we don't want to expose
  17. * the platforms internal mutex type this workaround with auto generated adapter
  18. * function is defined */
  19. #define QMK_DECLARE_AUTOUNLOCK_HELPERS(prefix) \
  20. inline unsigned prefix##_autounlock_lock_helper(void) { \
  21. prefix##_lock(); \
  22. return 0; \
  23. } \
  24. \
  25. inline void prefix##_autounlock_unlock_helper(unsigned* unused_guard) { \
  26. prefix##_unlock(); \
  27. }
  28. /* Generate an out-of-line implementation in case the inline functions defined
  29. * by the above macro don't actually get inlined. */
  30. #define QMK_IMPLEMENT_AUTOUNLOCK_HELPERS(prefix) \
  31. extern inline unsigned prefix##_autounlock_lock_helper(void); \
  32. extern inline void prefix##_autounlock_unlock_helper(unsigned* unused_guard);
  33. /* Convinience macro the automatically generate the correct RAII-style
  34. * lock_autounlock function macro */
  35. #define QMK_DECLARE_AUTOUNLOCK_CALL(prefix) unsigned prefix##_guard __attribute__((unused, cleanup(prefix##_autounlock_unlock_helper))) = prefix##_autounlock_lock_helper
  36. #if defined(SPLIT_KEYBOARD)
  37. QMK_DECLARE_AUTOUNLOCK_HELPERS(split_shared_memory)
  38. /**
  39. * @brief Acquire exclusive access to the split keyboard shared memory, by
  40. * calling the platforms `split_shared_memory_lock()` function. The lock is
  41. * automatically released by calling the platforms `split_shared_memory_unlock()`
  42. * function. This happens when the block where
  43. * `split_shared_memory_lock_autounlock()` is called in goes out of scope i.e.
  44. * when the enclosing function returns.
  45. */
  46. # define split_shared_memory_lock_autounlock QMK_DECLARE_AUTOUNLOCK_CALL(split_shared_memory)
  47. #endif