chmtx.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio.
  3. This file is part of ChibiOS.
  4. ChibiOS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. ChibiOS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /**
  16. * @file chmtx.h
  17. * @brief Mutexes macros and structures.
  18. *
  19. * @addtogroup mutexes
  20. * @{
  21. */
  22. #ifndef CHMTX_H
  23. #define CHMTX_H
  24. #if (CH_CFG_USE_MUTEXES == TRUE) || defined(__DOXYGEN__)
  25. /*===========================================================================*/
  26. /* Module constants. */
  27. /*===========================================================================*/
  28. /*===========================================================================*/
  29. /* Module pre-compile time settings. */
  30. /*===========================================================================*/
  31. /*===========================================================================*/
  32. /* Derived constants and error checks. */
  33. /*===========================================================================*/
  34. /*===========================================================================*/
  35. /* Module data structures and types. */
  36. /*===========================================================================*/
  37. /**
  38. * @brief Type of a mutex structure.
  39. */
  40. typedef struct ch_mutex mutex_t;
  41. /**
  42. * @brief Mutex structure.
  43. */
  44. struct ch_mutex {
  45. threads_queue_t queue; /**< @brief Queue of the threads sleeping
  46. on this mutex. */
  47. thread_t *owner; /**< @brief Owner @p thread_t pointer or
  48. @p NULL. */
  49. mutex_t *next; /**< @brief Next @p mutex_t into an
  50. owner-list or @p NULL. */
  51. #if (CH_CFG_USE_MUTEXES_RECURSIVE == TRUE) || defined(__DOXYGEN__)
  52. cnt_t cnt; /**< @brief Mutex recursion counter. */
  53. #endif
  54. };
  55. /*===========================================================================*/
  56. /* Module macros. */
  57. /*===========================================================================*/
  58. /**
  59. * @brief Data part of a static mutex initializer.
  60. * @details This macro should be used when statically initializing a mutex
  61. * that is part of a bigger structure.
  62. *
  63. * @param[in] name the name of the mutex variable
  64. */
  65. #if (CH_CFG_USE_MUTEXES_RECURSIVE == TRUE) || defined(__DOXYGEN__)
  66. #define _MUTEX_DATA(name) {_THREADS_QUEUE_DATA(name.queue), NULL, NULL, 0}
  67. #else
  68. #define _MUTEX_DATA(name) {_THREADS_QUEUE_DATA(name.queue), NULL, NULL}
  69. #endif
  70. /**
  71. * @brief Static mutex initializer.
  72. * @details Statically initialized mutexes require no explicit initialization
  73. * using @p chMtxInit().
  74. *
  75. * @param[in] name the name of the mutex variable
  76. */
  77. #define MUTEX_DECL(name) mutex_t name = _MUTEX_DATA(name)
  78. /*===========================================================================*/
  79. /* External declarations. */
  80. /*===========================================================================*/
  81. #ifdef __cplusplus
  82. extern "C" {
  83. #endif
  84. void chMtxObjectInit(mutex_t *mp);
  85. void chMtxLock(mutex_t *mp);
  86. void chMtxLockS(mutex_t *mp);
  87. bool chMtxTryLock(mutex_t *mp);
  88. bool chMtxTryLockS(mutex_t *mp);
  89. void chMtxUnlock(mutex_t *mp);
  90. void chMtxUnlockS(mutex_t *mp);
  91. void chMtxUnlockAll(void);
  92. void chMtxUnlockAllS(void);
  93. #ifdef __cplusplus
  94. }
  95. #endif
  96. /*===========================================================================*/
  97. /* Module inline functions. */
  98. /*===========================================================================*/
  99. /**
  100. * @brief Returns @p true if the mutex queue contains at least a waiting
  101. * thread.
  102. *
  103. * @param[out] mp pointer to a @p mutex_t structure
  104. * @return The mutex queue status.
  105. *
  106. * @sclass
  107. */
  108. static inline bool chMtxQueueNotEmptyS(mutex_t *mp) {
  109. chDbgCheckClassS();
  110. return queue_notempty(&mp->queue);
  111. }
  112. /**
  113. * @brief Returns the mutex owner thread.
  114. *
  115. * @param[out] mp pointer to a @p mutex_t structure
  116. * @return The owner thread.
  117. * @retval NULL if the mutex is not owned.
  118. *
  119. * @iclass
  120. */
  121. static inline thread_t *chMtxGetOwnerI(mutex_t *mp) {
  122. chDbgCheckClassI();
  123. return mp->owner;
  124. }
  125. /**
  126. * @brief Returns the next mutex in the mutexes stack of the current thread.
  127. *
  128. * @return A pointer to the next mutex in the stack.
  129. * @retval NULL if the stack is empty.
  130. *
  131. * @xclass
  132. */
  133. static inline mutex_t *chMtxGetNextMutexX(void) {
  134. return chThdGetSelfX()->mtxlist;
  135. }
  136. #endif /* CH_CFG_USE_MUTEXES == TRUE */
  137. #endif /* CHMTX_H */
  138. /** @} */