mutex.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. /* Mutexes */
  15. #include <bits/libc-lock.h>
  16. #include <errno.h>
  17. #include <sched.h>
  18. #include <stddef.h>
  19. #include <limits.h>
  20. #include "pthread.h"
  21. #include "internals.h"
  22. #include "spinlock.h"
  23. #include "queue.h"
  24. #include "restart.h"
  25. int __pthread_mutex_init(pthread_mutex_t * mutex,
  26. const pthread_mutexattr_t * mutex_attr)
  27. {
  28. __pthread_init_lock(&mutex->__m_lock);
  29. mutex->__m_kind =
  30. mutex_attr == NULL ? PTHREAD_MUTEX_TIMED_NP : mutex_attr->__mutexkind;
  31. mutex->__m_count = 0;
  32. mutex->__m_owner = NULL;
  33. return 0;
  34. }
  35. strong_alias (__pthread_mutex_init, pthread_mutex_init)
  36. int __pthread_mutex_destroy(pthread_mutex_t * mutex)
  37. {
  38. switch (mutex->__m_kind) {
  39. case PTHREAD_MUTEX_ADAPTIVE_NP:
  40. case PTHREAD_MUTEX_RECURSIVE_NP:
  41. if ((mutex->__m_lock.__status & 1) != 0)
  42. return EBUSY;
  43. return 0;
  44. case PTHREAD_MUTEX_ERRORCHECK_NP:
  45. case PTHREAD_MUTEX_TIMED_NP:
  46. if (mutex->__m_lock.__status != 0)
  47. return EBUSY;
  48. return 0;
  49. default:
  50. return EINVAL;
  51. }
  52. }
  53. strong_alias (__pthread_mutex_destroy, pthread_mutex_destroy)
  54. int __pthread_mutex_trylock(pthread_mutex_t * mutex)
  55. {
  56. pthread_descr self;
  57. int retcode;
  58. switch(mutex->__m_kind) {
  59. case PTHREAD_MUTEX_ADAPTIVE_NP:
  60. retcode = __pthread_trylock(&mutex->__m_lock);
  61. return retcode;
  62. case PTHREAD_MUTEX_RECURSIVE_NP:
  63. self = thread_self();
  64. if (mutex->__m_owner == self) {
  65. mutex->__m_count++;
  66. return 0;
  67. }
  68. retcode = __pthread_trylock(&mutex->__m_lock);
  69. if (retcode == 0) {
  70. mutex->__m_owner = self;
  71. mutex->__m_count = 0;
  72. }
  73. return retcode;
  74. case PTHREAD_MUTEX_ERRORCHECK_NP:
  75. retcode = __pthread_alt_trylock(&mutex->__m_lock);
  76. if (retcode == 0) {
  77. mutex->__m_owner = thread_self();
  78. }
  79. return retcode;
  80. case PTHREAD_MUTEX_TIMED_NP:
  81. retcode = __pthread_alt_trylock(&mutex->__m_lock);
  82. return retcode;
  83. default:
  84. return EINVAL;
  85. }
  86. }
  87. strong_alias (__pthread_mutex_trylock, pthread_mutex_trylock)
  88. int __pthread_mutex_lock(pthread_mutex_t * mutex)
  89. {
  90. pthread_descr self;
  91. switch(mutex->__m_kind) {
  92. case PTHREAD_MUTEX_ADAPTIVE_NP:
  93. __pthread_lock(&mutex->__m_lock, NULL);
  94. return 0;
  95. case PTHREAD_MUTEX_RECURSIVE_NP:
  96. self = thread_self();
  97. if (mutex->__m_owner == self) {
  98. mutex->__m_count++;
  99. return 0;
  100. }
  101. __pthread_lock(&mutex->__m_lock, self);
  102. mutex->__m_owner = self;
  103. mutex->__m_count = 0;
  104. return 0;
  105. case PTHREAD_MUTEX_ERRORCHECK_NP:
  106. self = thread_self();
  107. if (mutex->__m_owner == self) return EDEADLK;
  108. __pthread_alt_lock(&mutex->__m_lock, self);
  109. mutex->__m_owner = self;
  110. return 0;
  111. case PTHREAD_MUTEX_TIMED_NP:
  112. __pthread_alt_lock(&mutex->__m_lock, NULL);
  113. return 0;
  114. default:
  115. return EINVAL;
  116. }
  117. }
  118. strong_alias (__pthread_mutex_lock, pthread_mutex_lock)
  119. int __pthread_mutex_timedlock (pthread_mutex_t *mutex,
  120. const struct timespec *abstime)
  121. {
  122. pthread_descr self;
  123. int res;
  124. if (__builtin_expect (abstime->tv_nsec, 0) < 0
  125. || __builtin_expect (abstime->tv_nsec, 0) >= 1000000000)
  126. return EINVAL;
  127. switch(mutex->__m_kind) {
  128. case PTHREAD_MUTEX_ADAPTIVE_NP:
  129. __pthread_lock(&mutex->__m_lock, NULL);
  130. return 0;
  131. case PTHREAD_MUTEX_RECURSIVE_NP:
  132. self = thread_self();
  133. if (mutex->__m_owner == self) {
  134. mutex->__m_count++;
  135. return 0;
  136. }
  137. __pthread_lock(&mutex->__m_lock, self);
  138. mutex->__m_owner = self;
  139. mutex->__m_count = 0;
  140. return 0;
  141. case PTHREAD_MUTEX_ERRORCHECK_NP:
  142. self = thread_self();
  143. if (mutex->__m_owner == self) return EDEADLK;
  144. res = __pthread_alt_timedlock(&mutex->__m_lock, self, abstime);
  145. if (res != 0)
  146. {
  147. mutex->__m_owner = self;
  148. return 0;
  149. }
  150. return ETIMEDOUT;
  151. case PTHREAD_MUTEX_TIMED_NP:
  152. /* Only this type supports timed out lock. */
  153. return (__pthread_alt_timedlock(&mutex->__m_lock, NULL, abstime)
  154. ? 0 : ETIMEDOUT);
  155. default:
  156. return EINVAL;
  157. }
  158. }
  159. strong_alias (__pthread_mutex_timedlock, pthread_mutex_timedlock)
  160. int __pthread_mutex_unlock(pthread_mutex_t * mutex)
  161. {
  162. switch (mutex->__m_kind) {
  163. case PTHREAD_MUTEX_ADAPTIVE_NP:
  164. __pthread_unlock(&mutex->__m_lock);
  165. return 0;
  166. case PTHREAD_MUTEX_RECURSIVE_NP:
  167. if (mutex->__m_owner != thread_self())
  168. return EPERM;
  169. if (mutex->__m_count > 0) {
  170. mutex->__m_count--;
  171. return 0;
  172. }
  173. mutex->__m_owner = NULL;
  174. __pthread_unlock(&mutex->__m_lock);
  175. return 0;
  176. case PTHREAD_MUTEX_ERRORCHECK_NP:
  177. if (mutex->__m_owner != thread_self() || mutex->__m_lock.__status == 0)
  178. return EPERM;
  179. mutex->__m_owner = NULL;
  180. __pthread_alt_unlock(&mutex->__m_lock);
  181. return 0;
  182. case PTHREAD_MUTEX_TIMED_NP:
  183. __pthread_alt_unlock(&mutex->__m_lock);
  184. return 0;
  185. default:
  186. return EINVAL;
  187. }
  188. }
  189. strong_alias (__pthread_mutex_unlock, pthread_mutex_unlock)
  190. int __pthread_mutexattr_init(pthread_mutexattr_t *attr)
  191. {
  192. attr->__mutexkind = PTHREAD_MUTEX_TIMED_NP;
  193. return 0;
  194. }
  195. strong_alias (__pthread_mutexattr_init, pthread_mutexattr_init)
  196. int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
  197. {
  198. return 0;
  199. }
  200. strong_alias (__pthread_mutexattr_destroy, pthread_mutexattr_destroy)
  201. int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
  202. {
  203. if (kind != PTHREAD_MUTEX_ADAPTIVE_NP
  204. && kind != PTHREAD_MUTEX_RECURSIVE_NP
  205. && kind != PTHREAD_MUTEX_ERRORCHECK_NP
  206. && kind != PTHREAD_MUTEX_TIMED_NP)
  207. return EINVAL;
  208. attr->__mutexkind = kind;
  209. return 0;
  210. }
  211. weak_alias (__pthread_mutexattr_settype, pthread_mutexattr_settype)
  212. strong_alias ( __pthread_mutexattr_settype, __pthread_mutexattr_setkind_np)
  213. weak_alias (__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np)
  214. int __pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *kind)
  215. {
  216. *kind = attr->__mutexkind;
  217. return 0;
  218. }
  219. weak_alias (__pthread_mutexattr_gettype, pthread_mutexattr_gettype)
  220. strong_alias (__pthread_mutexattr_gettype, __pthread_mutexattr_getkind_np)
  221. weak_alias (__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np)
  222. int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *attr,
  223. int *pshared)
  224. {
  225. *pshared = PTHREAD_PROCESS_PRIVATE;
  226. return 0;
  227. }
  228. weak_alias (__pthread_mutexattr_getpshared, pthread_mutexattr_getpshared)
  229. int __pthread_mutexattr_setpshared (pthread_mutexattr_t *attr, int pshared)
  230. {
  231. if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
  232. return EINVAL;
  233. /* For now it is not possible to shared a conditional variable. */
  234. if (pshared != PTHREAD_PROCESS_PRIVATE)
  235. return ENOSYS;
  236. return 0;
  237. }
  238. weak_alias (__pthread_mutexattr_setpshared, pthread_mutexattr_setpshared)
  239. /* Once-only execution */
  240. static pthread_mutex_t once_masterlock = PTHREAD_MUTEX_INITIALIZER;
  241. static pthread_cond_t once_finished = PTHREAD_COND_INITIALIZER;
  242. static int fork_generation = 0; /* Child process increments this after fork. */
  243. enum { NEVER = 0, IN_PROGRESS = 1, DONE = 2 };
  244. /* If a thread is canceled while calling the init_routine out of
  245. pthread once, this handler will reset the once_control variable
  246. to the NEVER state. */
  247. static void pthread_once_cancelhandler(void *arg)
  248. {
  249. pthread_once_t *once_control = arg;
  250. pthread_mutex_lock(&once_masterlock);
  251. *once_control = NEVER;
  252. pthread_mutex_unlock(&once_masterlock);
  253. pthread_cond_broadcast(&once_finished);
  254. }
  255. int __pthread_once(pthread_once_t * once_control, void (*init_routine)(void))
  256. {
  257. /* flag for doing the condition broadcast outside of mutex */
  258. int state_changed;
  259. /* Test without locking first for speed */
  260. if (*once_control == DONE) {
  261. READ_MEMORY_BARRIER();
  262. return 0;
  263. }
  264. /* Lock and test again */
  265. state_changed = 0;
  266. pthread_mutex_lock(&once_masterlock);
  267. /* If this object was left in an IN_PROGRESS state in a parent
  268. process (indicated by stale generation field), reset it to NEVER. */
  269. if ((*once_control & 3) == IN_PROGRESS && (*once_control & ~3) != fork_generation)
  270. *once_control = NEVER;
  271. /* If init_routine is being called from another routine, wait until
  272. it completes. */
  273. while ((*once_control & 3) == IN_PROGRESS) {
  274. pthread_cond_wait(&once_finished, &once_masterlock);
  275. }
  276. /* Here *once_control is stable and either NEVER or DONE. */
  277. if (*once_control == NEVER) {
  278. *once_control = IN_PROGRESS | fork_generation;
  279. pthread_mutex_unlock(&once_masterlock);
  280. pthread_cleanup_push(pthread_once_cancelhandler, once_control);
  281. init_routine();
  282. pthread_cleanup_pop(0);
  283. pthread_mutex_lock(&once_masterlock);
  284. WRITE_MEMORY_BARRIER();
  285. *once_control = DONE;
  286. state_changed = 1;
  287. }
  288. pthread_mutex_unlock(&once_masterlock);
  289. if (state_changed)
  290. pthread_cond_broadcast(&once_finished);
  291. return 0;
  292. }
  293. strong_alias (__pthread_once, pthread_once)
  294. /*
  295. * Handle the state of the pthread_once mechanism across forks. The
  296. * once_masterlock is acquired in the parent process prior to a fork to ensure
  297. * that no thread is in the critical region protected by the lock. After the
  298. * fork, the lock is released. In the child, the lock and the condition
  299. * variable are simply reset. The child also increments its generation
  300. * counter which lets pthread_once calls detect stale IN_PROGRESS states
  301. * and reset them back to NEVER.
  302. */
  303. void __pthread_once_fork_prepare(void)
  304. {
  305. pthread_mutex_lock(&once_masterlock);
  306. }
  307. void __pthread_once_fork_parent(void)
  308. {
  309. pthread_mutex_unlock(&once_masterlock);
  310. }
  311. void __pthread_once_fork_child(void)
  312. {
  313. pthread_mutex_init(&once_masterlock, NULL);
  314. pthread_cond_init(&once_finished, NULL);
  315. if (fork_generation <= INT_MAX - 4)
  316. fork_generation += 4; /* leave least significant two bits zero */
  317. else
  318. fork_generation = 0;
  319. }