mutex.c 11 KB

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