mutex.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 <features.h>
  17. #define __USE_GNU
  18. #include <errno.h>
  19. #include <sched.h>
  20. #include <stddef.h>
  21. #include "pthread.h"
  22. #include "internals.h"
  23. #include "spinlock.h"
  24. #include "queue.h"
  25. #include "restart.h"
  26. int pthread_mutex_init(pthread_mutex_t * mutex,
  27. const pthread_mutexattr_t * mutex_attr)
  28. {
  29. __pthread_init_lock(&mutex->__m_lock);
  30. mutex->__m_kind =
  31. mutex_attr == NULL ? PTHREAD_MUTEX_ADAPTIVE_NP : mutex_attr->__mutexkind;
  32. mutex->__m_count = 0;
  33. mutex->__m_owner = NULL;
  34. return 0;
  35. }
  36. //strong_alias (__pthread_mutex_init, pthread_mutex_init)
  37. int pthread_mutex_destroy(pthread_mutex_t * mutex)
  38. {
  39. if (mutex->__m_lock.__status != 0) return EBUSY;
  40. return 0;
  41. }
  42. //strong_alias (__pthread_mutex_destroy, pthread_mutex_destroy)
  43. int pthread_mutex_trylock(pthread_mutex_t * mutex)
  44. {
  45. pthread_descr self;
  46. int retcode;
  47. switch(mutex->__m_kind) {
  48. case PTHREAD_MUTEX_ADAPTIVE_NP:
  49. retcode = __pthread_trylock(&mutex->__m_lock);
  50. return retcode;
  51. case PTHREAD_MUTEX_RECURSIVE_NP:
  52. self = thread_self();
  53. if (mutex->__m_owner == self) {
  54. mutex->__m_count++;
  55. return 0;
  56. }
  57. retcode = __pthread_trylock(&mutex->__m_lock);
  58. if (retcode == 0) {
  59. mutex->__m_owner = self;
  60. mutex->__m_count = 0;
  61. }
  62. return retcode;
  63. case PTHREAD_MUTEX_ERRORCHECK_NP:
  64. retcode = __pthread_trylock(&mutex->__m_lock);
  65. if (retcode == 0) {
  66. mutex->__m_owner = thread_self();
  67. }
  68. return retcode;
  69. default:
  70. return EINVAL;
  71. }
  72. }
  73. //strong_alias (__pthread_mutex_trylock, pthread_mutex_trylock)
  74. int pthread_mutex_lock(pthread_mutex_t * mutex)
  75. {
  76. pthread_descr self;
  77. switch(mutex->__m_kind) {
  78. case PTHREAD_MUTEX_ADAPTIVE_NP:
  79. __pthread_lock(&mutex->__m_lock, NULL);
  80. return 0;
  81. case PTHREAD_MUTEX_RECURSIVE_NP:
  82. self = thread_self();
  83. if (mutex->__m_owner == self) {
  84. mutex->__m_count++;
  85. return 0;
  86. }
  87. __pthread_lock(&mutex->__m_lock, self);
  88. mutex->__m_owner = self;
  89. mutex->__m_count = 0;
  90. return 0;
  91. case PTHREAD_MUTEX_ERRORCHECK_NP:
  92. self = thread_self();
  93. if (mutex->__m_owner == self) return EDEADLK;
  94. __pthread_lock(&mutex->__m_lock, self);
  95. mutex->__m_owner = self;
  96. return 0;
  97. default:
  98. return EINVAL;
  99. }
  100. }
  101. //strong_alias (__pthread_mutex_lock, pthread_mutex_lock)
  102. int pthread_mutex_unlock(pthread_mutex_t * mutex)
  103. {
  104. switch (mutex->__m_kind) {
  105. case PTHREAD_MUTEX_ADAPTIVE_NP:
  106. __pthread_unlock(&mutex->__m_lock);
  107. return 0;
  108. case PTHREAD_MUTEX_RECURSIVE_NP:
  109. if (mutex->__m_count > 0) {
  110. mutex->__m_count--;
  111. return 0;
  112. }
  113. mutex->__m_owner = NULL;
  114. __pthread_unlock(&mutex->__m_lock);
  115. return 0;
  116. case PTHREAD_MUTEX_ERRORCHECK_NP:
  117. if (mutex->__m_owner != thread_self() || mutex->__m_lock.__status == 0)
  118. return EPERM;
  119. mutex->__m_owner = NULL;
  120. __pthread_unlock(&mutex->__m_lock);
  121. return 0;
  122. default:
  123. return EINVAL;
  124. }
  125. }
  126. //strong_alias (__pthread_mutex_unlock, pthread_mutex_unlock)
  127. int pthread_mutexattr_init(pthread_mutexattr_t *attr)
  128. {
  129. attr->__mutexkind = PTHREAD_MUTEX_ADAPTIVE_NP;
  130. return 0;
  131. }
  132. //strong_alias (__pthread_mutexattr_init, pthread_mutexattr_init)
  133. int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
  134. {
  135. return 0;
  136. }
  137. //strong_alias (__pthread_mutexattr_destroy, pthread_mutexattr_destroy)
  138. int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
  139. {
  140. if (kind != PTHREAD_MUTEX_ADAPTIVE_NP
  141. && kind != PTHREAD_MUTEX_RECURSIVE_NP
  142. && kind != PTHREAD_MUTEX_ERRORCHECK_NP)
  143. return EINVAL;
  144. attr->__mutexkind = kind;
  145. return 0;
  146. }
  147. weak_alias (__pthread_mutexattr_settype, pthread_mutexattr_settype)
  148. weak_alias ( __pthread_mutexattr_settype, __pthread_mutexattr_setkind_np)
  149. weak_alias (__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np)
  150. int __pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *kind)
  151. {
  152. *kind = attr->__mutexkind;
  153. return 0;
  154. }
  155. weak_alias (__pthread_mutexattr_gettype, pthread_mutexattr_gettype)
  156. weak_alias (__pthread_mutexattr_gettype, __pthread_mutexattr_getkind_np)
  157. weak_alias (__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np)
  158. int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *attr, int *pshared)
  159. {
  160. *pshared = PTHREAD_PROCESS_PRIVATE;
  161. return 0;
  162. }
  163. weak_alias (__pthread_mutexattr_getpshared, pthread_mutexattr_getpshared)
  164. int __pthread_mutexattr_setpshared (pthread_mutexattr_t *attr, int pshared)
  165. {
  166. if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
  167. return EINVAL;
  168. /* For now it is not possible to shared a conditional variable. */
  169. if (pshared != PTHREAD_PROCESS_PRIVATE)
  170. return ENOSYS;
  171. return 0;
  172. }
  173. weak_alias (__pthread_mutexattr_setpshared, pthread_mutexattr_setpshared)
  174. /* Once-only execution */
  175. static pthread_mutex_t once_masterlock = PTHREAD_MUTEX_INITIALIZER;
  176. static pthread_cond_t once_finished = PTHREAD_COND_INITIALIZER;
  177. enum { NEVER = 0, IN_PROGRESS = 1, DONE = 2 };
  178. int pthread_once(pthread_once_t * once_control, void (*init_routine)(void))
  179. {
  180. /* Test without locking first for speed */
  181. if (*once_control == DONE) return 0;
  182. /* Lock and test again */
  183. pthread_mutex_lock(&once_masterlock);
  184. /* If init_routine is being called from another routine, wait until
  185. it completes. */
  186. while (*once_control == IN_PROGRESS) {
  187. pthread_cond_wait(&once_finished, &once_masterlock);
  188. }
  189. /* Here *once_control is stable and either NEVER or DONE. */
  190. if (*once_control == NEVER) {
  191. *once_control = IN_PROGRESS;
  192. pthread_mutex_unlock(&once_masterlock);
  193. init_routine();
  194. pthread_mutex_lock(&once_masterlock);
  195. *once_control = DONE;
  196. pthread_cond_broadcast(&once_finished);
  197. }
  198. pthread_mutex_unlock(&once_masterlock);
  199. return 0;
  200. }
  201. //strong_alias (__pthread_once, pthread_once)