mutex.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /* mutex.c - a nut/os implementation of recursive mutex functions
  34. *
  35. * 2004.05.06 Matthias Ringwald <matthias.ringwald@inf.ethz.ch>
  36. *
  37. */
  38. /*
  39. * $Log$
  40. * Revision 1.6 2009/01/17 15:37:52 haraldkipp
  41. * Added some NUTASSERT macros to check function parameters.
  42. *
  43. * Revision 1.5 2006/10/08 16:48:22 haraldkipp
  44. * Documentation fixed
  45. *
  46. * Revision 1.4 2005/08/02 17:47:04 haraldkipp
  47. * Major API documentation update.
  48. *
  49. * Revision 1.3 2004/07/19 16:24:23 freckle
  50. * Code contained same bug as os/semaphore.c.
  51. * Unfortunately I didn't understand the other fix, but anyway made
  52. * NutMutexLock more robust.
  53. * Fixed wrong indention of all functions
  54. *
  55. * Revision 1.2 2004/05/18 18:38:42 drsung
  56. * Added $Log keyword for CVS.
  57. *
  58. */
  59. /*!
  60. * \addtogroup xgMutex
  61. */
  62. /*@{*/
  63. #ifdef __cplusplus
  64. extern "C" {
  65. #endif
  66. #include <sys/nutdebug.h>
  67. #include <sys/mutex.h>
  68. #include <sys/event.h>
  69. /*!
  70. * \brief Create a mutex
  71. *
  72. * The type for the mutex is recursive
  73. */
  74. void NutMutexInit(MUTEX * mutex) {
  75. NUTASSERT(mutex != NULL);
  76. mutex->thread = 0;
  77. mutex->count = 0;
  78. mutex->qhp = 0;
  79. }
  80. /*!
  81. * \brief Lock a mutex
  82. *
  83. * If the mutex is already locked by another thread,
  84. * the thread will block until the mutex becomes available
  85. *
  86. * \note: Should not be called from interrupt context
  87. */
  88. void NutMutexLock(MUTEX * mutex) {
  89. NUTASSERT(mutex != NULL);
  90. if (mutex->thread != runningThread) {
  91. while( mutex->count != 0)
  92. NutEventWaitNext(&mutex->qhp, NUT_WAIT_INFINITE);
  93. }
  94. mutex->thread = runningThread;
  95. mutex->count++;
  96. }
  97. /*!
  98. * \brief Attempt to lock a mutex without blocking
  99. *
  100. * Return zero, if successful, otherwise the mutex is already locked
  101. * by another thread
  102. * \note: Should not be called from interrupt context
  103. */
  104. int NutMutexTrylock(MUTEX * mutex) {
  105. NUTASSERT(mutex != NULL);
  106. if ((mutex->count != 0) && (mutex->thread != runningThread))
  107. return -1;
  108. NutMutexLock(mutex);
  109. return 0;
  110. }
  111. /*!
  112. * \brief Unlock a mutex.
  113. *
  114. * Return zero, if successful, otherwise the current thread does not
  115. * hold a lock on mutex.
  116. * \note: Should not be called from interrupt context
  117. */
  118. int NutMutexUnlock(MUTEX * mutex) {
  119. NUTASSERT(mutex != NULL);
  120. if (mutex->thread != runningThread)
  121. return -1;
  122. if (--mutex->count == 0) {
  123. NutEventPost(&mutex->qhp);
  124. }
  125. return 0;
  126. }
  127. /*!
  128. * \brief Free resources allocated for a mutex
  129. *
  130. * Return zero, if successful, otherwise the mutex is locked
  131. * by another thread
  132. */
  133. int NutMutexDestroy(MUTEX * mutex) {
  134. NUTASSERT(mutex != NULL);
  135. if (mutex->count == 0)
  136. return 0;
  137. if (mutex->thread == runningThread)
  138. return 0;
  139. return -1;
  140. }
  141. #ifdef __cplusplus
  142. }
  143. #endif
  144. /*@}*/