semaphore.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (C) 2000-2004 by ETH Zurich
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY ETH ZURICH AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ETH ZURICH
  21. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /* semaphore.c - a nut/os implementation of semaphore functions
  34. *
  35. * 2004.05.06 Matthias Ringwald <matthias.ringwald@inf.ethz.ch>
  36. *
  37. */
  38. /*
  39. * $Log$
  40. * Revision 1.9 2009/01/17 15:37:52 haraldkipp
  41. * Added some NUTASSERT macros to check function parameters.
  42. *
  43. * Revision 1.8 2008/06/16 15:12:07 freckle
  44. * fix bug in NutSemTryWait of os/semaphore.c
  45. *
  46. * Revision 1.7 2006/10/08 16:48:22 haraldkipp
  47. * Documentation fixed
  48. *
  49. * Revision 1.6 2005/08/02 17:47:04 haraldkipp
  50. * Major API documentation update.
  51. *
  52. * Revision 1.5 2004/06/03 08:44:50 olereinhardt
  53. * According to a hint from oliver I changed NutEventWait to NutEventWaitNext
  54. *
  55. * Revision 1.4 2004/06/03 08:24:21 olereinhardt
  56. * Changed semaphore behavior in NutSemTryWait too.
  57. *
  58. * Revision 1.3 2004/06/02 16:42:53 olereinhardt
  59. * fixed bug (integer overflow) in semaphore implementation.
  60. *
  61. * Revision 1.2 2004/05/18 18:38:42 drsung
  62. * Added $Log keyword for CVS.
  63. *
  64. */
  65. /*!
  66. * \addtogroup xgSemaphore
  67. */
  68. /*@{*/
  69. #ifdef __cplusplus
  70. extern "C" {
  71. #endif
  72. #include <sys/nutdebug.h>
  73. #include <sys/semaphore.h>
  74. #include <sys/event.h>
  75. /*!
  76. * \brief Initialize an unnamed semaphore to value
  77. */
  78. void NutSemInit(SEM * sem, short value) {
  79. NUTASSERT(sem != NULL);
  80. sem->qhp = 0;
  81. sem->value = value;
  82. }
  83. /*!
  84. * \brief Lock a semaphore
  85. *
  86. * If the semaphore value is currently zero, then the calling thread will not
  87. * return from the call to sem_wait() the semaphore becomes available
  88. *
  89. * \note: Should not be called from interrupt context
  90. */ void NutSemWait(SEM * sem) {
  91. NUTASSERT(sem != NULL);
  92. sem->value--;
  93. if (sem->value < 0)
  94. {
  95. NutEventWaitNext(&sem->qhp, NUT_WAIT_INFINITE);
  96. }
  97. }
  98. /*!
  99. * \brief Unlock a sempahore.
  100. *
  101. * \note: Should not be called from interrupt context
  102. */
  103. void NutSemPost(SEM * sem) {
  104. NUTASSERT(sem != NULL);
  105. sem->value++;
  106. if (sem->value <= 0)
  107. {
  108. NutEventPost(&sem->qhp);
  109. }
  110. }
  111. /*!
  112. * \brief Attempt to lock a semaphore without blocking
  113. *
  114. * Return zero, if successful, otherwise the sempahore is already locked
  115. * \note: Should not be called from interrupt context
  116. */
  117. int NutSemTryWait(SEM * sem) {
  118. NUTASSERT(sem != NULL);
  119. if (sem->value <= 0)
  120. return -1;
  121. else
  122. NutSemWait(sem);
  123. return 0;
  124. }
  125. /*!
  126. * \brief Free resources allocated for a semaphore
  127. *
  128. * Return zero, if successful, otherwise there are threads blocked on the
  129. * sempahore
  130. */
  131. int NutSemDestroy(SEM * sem) {
  132. NUTASSERT(sem != NULL);
  133. if (sem->qhp == SIGNALED)
  134. return 0;
  135. if (sem->qhp == 0)
  136. return 0;
  137. return -1;
  138. }
  139. #ifdef __cplusplus
  140. }
  141. #endif
  142. /*@}*/