You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
2.1 KiB

  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. /* Convinience macro the automatically generate the correct RAII-style
  29. * lock_autounlock function macro */
  30. #define QMK_DECLARE_AUTOUNLOCK_CALL(prefix) unsigned prefix##_guard __attribute__((unused, cleanup(prefix##_autounlock_unlock_helper))) = prefix##_autounlock_lock_helper
  31. #if defined(SPLIT_KEYBOARD)
  32. QMK_DECLARE_AUTOUNLOCK_HELPERS(split_shared_memory)
  33. /**
  34. * @brief Acquire exclusive access to the split keyboard shared memory, by
  35. * calling the platforms `split_shared_memory_lock()` function. The lock is
  36. * automatically released by calling the platforms `split_shared_memory_unlock()`
  37. * function. This happens when the block where
  38. * `split_shared_memory_lock_autounlock()` is called in goes out of scope i.e.
  39. * when the enclosing function returns.
  40. */
  41. # define split_shared_memory_lock_autounlock QMK_DECLARE_AUTOUNLOCK_CALL(split_shared_memory)
  42. #endif