chsem.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 chsem.h
  17. * @brief Semaphores macros and structures.
  18. *
  19. * @addtogroup semaphores
  20. * @{
  21. */
  22. #ifndef CHSEM_H
  23. #define CHSEM_H
  24. #if (CH_CFG_USE_SEMAPHORES == 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 Semaphore structure.
  39. */
  40. typedef struct ch_semaphore {
  41. threads_queue_t queue; /**< @brief Queue of the threads sleeping
  42. on this semaphore. */
  43. cnt_t cnt; /**< @brief The semaphore counter. */
  44. } semaphore_t;
  45. /*===========================================================================*/
  46. /* Module macros. */
  47. /*===========================================================================*/
  48. /**
  49. * @brief Data part of a static semaphore initializer.
  50. * @details This macro should be used when statically initializing a semaphore
  51. * that is part of a bigger structure.
  52. *
  53. * @param[in] name the name of the semaphore variable
  54. * @param[in] n the counter initial value, this value must be
  55. * non-negative
  56. */
  57. #define _SEMAPHORE_DATA(name, n) {_THREADS_QUEUE_DATA(name.queue), n}
  58. /**
  59. * @brief Static semaphore initializer.
  60. * @details Statically initialized semaphores require no explicit
  61. * initialization using @p chSemInit().
  62. *
  63. * @param[in] name the name of the semaphore variable
  64. * @param[in] n the counter initial value, this value must be
  65. * non-negative
  66. */
  67. #define SEMAPHORE_DECL(name, n) semaphore_t name = _SEMAPHORE_DATA(name, n)
  68. /*===========================================================================*/
  69. /* External declarations. */
  70. /*===========================================================================*/
  71. #ifdef __cplusplus
  72. extern "C" {
  73. #endif
  74. void chSemObjectInit(semaphore_t *sp, cnt_t n);
  75. void chSemReset(semaphore_t *sp, cnt_t n);
  76. void chSemResetI(semaphore_t *sp, cnt_t n);
  77. msg_t chSemWait(semaphore_t *sp);
  78. msg_t chSemWaitS(semaphore_t *sp);
  79. msg_t chSemWaitTimeout(semaphore_t *sp, sysinterval_t timeout);
  80. msg_t chSemWaitTimeoutS(semaphore_t *sp, sysinterval_t timeout);
  81. void chSemSignal(semaphore_t *sp);
  82. void chSemSignalI(semaphore_t *sp);
  83. void chSemAddCounterI(semaphore_t *sp, cnt_t n);
  84. msg_t chSemSignalWait(semaphore_t *sps, semaphore_t *spw);
  85. #ifdef __cplusplus
  86. }
  87. #endif
  88. /*===========================================================================*/
  89. /* Module inline functions. */
  90. /*===========================================================================*/
  91. /**
  92. * @brief Decreases the semaphore counter.
  93. * @details This macro can be used when the counter is known to be positive.
  94. *
  95. * @param[in] sp pointer to a @p semaphore_t structure
  96. *
  97. * @iclass
  98. */
  99. static inline void chSemFastWaitI(semaphore_t *sp) {
  100. chDbgCheckClassI();
  101. sp->cnt--;
  102. }
  103. /**
  104. * @brief Increases the semaphore counter.
  105. * @details This macro can be used when the counter is known to be not
  106. * negative.
  107. *
  108. * @param[in] sp pointer to a @p semaphore_t structure
  109. *
  110. * @iclass
  111. */
  112. static inline void chSemFastSignalI(semaphore_t *sp) {
  113. chDbgCheckClassI();
  114. sp->cnt++;
  115. }
  116. /**
  117. * @brief Returns the semaphore counter current value.
  118. *
  119. * @param[in] sp pointer to a @p semaphore_t structure
  120. * @return The semaphore counter value.
  121. *
  122. * @iclass
  123. */
  124. static inline cnt_t chSemGetCounterI(const semaphore_t *sp) {
  125. chDbgCheckClassI();
  126. return sp->cnt;
  127. }
  128. #endif /* CH_CFG_USE_SEMAPHORES == TRUE */
  129. #endif /* CHSEM_H */
  130. /** @} */