mutex.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* Linuxthreads - a simple clone()-based implementation of Posix */
  2. /* threads for Linux. */
  3. /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
  4. /* */
  5. /* This program is free software; you can redistribute it and/or */
  6. /* modify it under the terms of the GNU Library General Public License */
  7. /* as published by the Free Software Foundation; either version 2 */
  8. /* of the License, or (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU Library General Public License for more details. */
  14. /* changes for uClibc: remove strong_alias'es and define the real symbol */
  15. /* Mutexes */
  16. #include <errno.h>
  17. #include <sched.h>
  18. #include <stddef.h>
  19. #include "pthread.h"
  20. #include "internals.h"
  21. #include "spinlock.h"
  22. #include "queue.h"
  23. #include "restart.h"
  24. int pthread_mutex_init(pthread_mutex_t * mutex,
  25. const pthread_mutexattr_t * mutex_attr)
  26. {
  27. __pthread_init_lock(&mutex->__m_lock);
  28. mutex->__m_kind =
  29. mutex_attr == NULL ? PTHREAD_MUTEX_FAST_NP : mutex_attr->__mutexkind;
  30. mutex->__m_count = 0;
  31. mutex->__m_owner = NULL;
  32. return 0;
  33. }
  34. //strong_alias (__pthread_mutex_init, pthread_mutex_init)
  35. int pthread_mutex_destroy(pthread_mutex_t * mutex)
  36. {
  37. if (mutex->__m_lock.__status != 0) return EBUSY;
  38. return 0;
  39. }
  40. //strong_alias (__pthread_mutex_destroy, pthread_mutex_destroy)
  41. int pthread_mutex_trylock(pthread_mutex_t * mutex)
  42. {
  43. pthread_descr self;
  44. int retcode;
  45. switch(mutex->__m_kind) {
  46. case PTHREAD_MUTEX_FAST_NP:
  47. retcode = __pthread_trylock(&mutex->__m_lock);
  48. return retcode;
  49. case PTHREAD_MUTEX_RECURSIVE_NP:
  50. self = thread_self();
  51. if (mutex->__m_owner == self) {
  52. mutex->__m_count++;
  53. return 0;
  54. }
  55. retcode = __pthread_trylock(&mutex->__m_lock);
  56. if (retcode == 0) {
  57. mutex->__m_owner = self;
  58. mutex->__m_count = 0;
  59. }
  60. return retcode;
  61. case PTHREAD_MUTEX_ERRORCHECK_NP:
  62. retcode = __pthread_trylock(&mutex->__m_lock);
  63. if (retcode == 0) {
  64. mutex->__m_owner = thread_self();
  65. }
  66. return retcode;
  67. default:
  68. return EINVAL;
  69. }
  70. }
  71. //strong_alias (__pthread_mutex_trylock, pthread_mutex_trylock)
  72. int pthread_mutex_lock(pthread_mutex_t * mutex)
  73. {
  74. pthread_descr self;
  75. switch(mutex->__m_kind) {
  76. case PTHREAD_MUTEX_FAST_NP:
  77. __pthread_lock(&mutex->__m_lock, NULL);
  78. return 0;
  79. case PTHREAD_MUTEX_RECURSIVE_NP:
  80. self = thread_self();
  81. if (mutex->__m_owner == self) {
  82. mutex->__m_count++;
  83. return 0;
  84. }
  85. __pthread_lock(&mutex->__m_lock, self);
  86. mutex->__m_owner = self;
  87. mutex->__m_count = 0;
  88. return 0;
  89. case PTHREAD_MUTEX_ERRORCHECK_NP:
  90. self = thread_self();
  91. if (mutex->__m_owner == self) return EDEADLK;
  92. __pthread_lock(&mutex->__m_lock, self);
  93. mutex->__m_owner = self;
  94. return 0;
  95. default:
  96. return EINVAL;
  97. }
  98. }
  99. //strong_alias (__pthread_mutex_lock, pthread_mutex_lock)
  100. int pthread_mutex_unlock(pthread_mutex_t * mutex)
  101. {
  102. switch (mutex->__m_kind) {
  103. case PTHREAD_MUTEX_FAST_NP:
  104. __pthread_unlock(&mutex->__m_lock);
  105. return 0;
  106. case PTHREAD_MUTEX_RECURSIVE_NP:
  107. if (mutex->__m_count > 0) {
  108. mutex->__m_count--;
  109. return 0;
  110. }
  111. mutex->__m_owner = NULL;
  112. __pthread_unlock(&mutex->__m_lock);
  113. return 0;
  114. case PTHREAD_MUTEX_ERRORCHECK_NP:
  115. if (mutex->__m_owner != thread_self() || mutex->__m_lock.__status == 0)
  116. return EPERM;
  117. mutex->__m_owner = NULL;
  118. __pthread_unlock(&mutex->__m_lock);
  119. return 0;
  120. default:
  121. return EINVAL;
  122. }
  123. }
  124. //strong_alias (__pthread_mutex_unlock, pthread_mutex_unlock)
  125. int pthread_mutexattr_init(pthread_mutexattr_t *attr)
  126. {
  127. attr->__mutexkind = PTHREAD_MUTEX_FAST_NP;
  128. return 0;
  129. }
  130. //strong_alias (__pthread_mutexattr_init, pthread_mutexattr_init)
  131. int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
  132. {
  133. return 0;
  134. }
  135. //strong_alias (__pthread_mutexattr_destroy, pthread_mutexattr_destroy)
  136. int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
  137. {
  138. if (kind != PTHREAD_MUTEX_FAST_NP
  139. && kind != PTHREAD_MUTEX_RECURSIVE_NP
  140. && kind != PTHREAD_MUTEX_ERRORCHECK_NP)
  141. return EINVAL;
  142. attr->__mutexkind = kind;
  143. return 0;
  144. }
  145. weak_alias (__pthread_mutexattr_settype, pthread_mutexattr_settype)
  146. weak_alias ( __pthread_mutexattr_settype, __pthread_mutexattr_setkind_np)
  147. weak_alias (__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np)
  148. int __pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *kind)
  149. {
  150. *kind = attr->__mutexkind;
  151. return 0;
  152. }
  153. weak_alias (__pthread_mutexattr_gettype, pthread_mutexattr_gettype)
  154. weak_alias (__pthread_mutexattr_gettype, __pthread_mutexattr_getkind_np)
  155. weak_alias (__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np)
  156. /* Once-only execution */
  157. static pthread_mutex_t once_masterlock = PTHREAD_MUTEX_INITIALIZER;
  158. static pthread_cond_t once_finished = PTHREAD_COND_INITIALIZER;
  159. enum { NEVER = 0, IN_PROGRESS = 1, DONE = 2 };
  160. int pthread_once(pthread_once_t * once_control, void (*init_routine)(void))
  161. {
  162. /* Test without locking first for speed */
  163. if (*once_control == DONE) return 0;
  164. /* Lock and test again */
  165. pthread_mutex_lock(&once_masterlock);
  166. /* If init_routine is being called from another routine, wait until
  167. it completes. */
  168. while (*once_control == IN_PROGRESS) {
  169. pthread_cond_wait(&once_finished, &once_masterlock);
  170. }
  171. /* Here *once_control is stable and either NEVER or DONE. */
  172. if (*once_control == NEVER) {
  173. *once_control = IN_PROGRESS;
  174. pthread_mutex_unlock(&once_masterlock);
  175. init_routine();
  176. pthread_mutex_lock(&once_masterlock);
  177. *once_control = DONE;
  178. pthread_cond_broadcast(&once_finished);
  179. }
  180. pthread_mutex_unlock(&once_masterlock);
  181. return 0;
  182. }
  183. //strong_alias (__pthread_once, pthread_once)