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