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 __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. hidden_def (__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. hidden_def (__pthread_mutex_destroy)
  55. int __pthread_mutex_trylock(pthread_mutex_t * mutex)
  56. {
  57. pthread_descr self;
  58. int retcode;
  59. switch(mutex->__m_kind) {
  60. case PTHREAD_MUTEX_ADAPTIVE_NP:
  61. retcode = __pthread_trylock(&mutex->__m_lock);
  62. return retcode;
  63. case PTHREAD_MUTEX_RECURSIVE_NP:
  64. self = thread_self();
  65. if (mutex->__m_owner == self) {
  66. mutex->__m_count++;
  67. return 0;
  68. }
  69. retcode = __pthread_trylock(&mutex->__m_lock);
  70. if (retcode == 0) {
  71. mutex->__m_owner = self;
  72. mutex->__m_count = 0;
  73. }
  74. return retcode;
  75. case PTHREAD_MUTEX_ERRORCHECK_NP:
  76. retcode = __pthread_alt_trylock(&mutex->__m_lock);
  77. if (retcode == 0) {
  78. mutex->__m_owner = thread_self();
  79. }
  80. return retcode;
  81. case PTHREAD_MUTEX_TIMED_NP:
  82. retcode = __pthread_alt_trylock(&mutex->__m_lock);
  83. return retcode;
  84. default:
  85. return EINVAL;
  86. }
  87. }
  88. strong_alias (__pthread_mutex_trylock, pthread_mutex_trylock)
  89. hidden_def (__pthread_mutex_trylock)
  90. int __pthread_mutex_lock(pthread_mutex_t * mutex)
  91. {
  92. pthread_descr self;
  93. switch(mutex->__m_kind) {
  94. case PTHREAD_MUTEX_ADAPTIVE_NP:
  95. __pthread_lock(&mutex->__m_lock, NULL);
  96. return 0;
  97. case PTHREAD_MUTEX_RECURSIVE_NP:
  98. self = thread_self();
  99. if (mutex->__m_owner == self) {
  100. mutex->__m_count++;
  101. return 0;
  102. }
  103. __pthread_lock(&mutex->__m_lock, self);
  104. mutex->__m_owner = self;
  105. mutex->__m_count = 0;
  106. return 0;
  107. case PTHREAD_MUTEX_ERRORCHECK_NP:
  108. self = thread_self();
  109. if (mutex->__m_owner == self) return EDEADLK;
  110. __pthread_alt_lock(&mutex->__m_lock, self);
  111. mutex->__m_owner = self;
  112. return 0;
  113. case PTHREAD_MUTEX_TIMED_NP:
  114. __pthread_alt_lock(&mutex->__m_lock, NULL);
  115. return 0;
  116. default:
  117. return EINVAL;
  118. }
  119. }
  120. strong_alias (__pthread_mutex_lock, pthread_mutex_lock)
  121. hidden_def (__pthread_mutex_lock)
  122. int __pthread_mutex_timedlock (pthread_mutex_t *mutex,
  123. const struct timespec *abstime)
  124. {
  125. pthread_descr self;
  126. int res;
  127. if (__builtin_expect (abstime->tv_nsec, 0) < 0
  128. || __builtin_expect (abstime->tv_nsec, 0) >= 1000000000)
  129. return EINVAL;
  130. switch(mutex->__m_kind) {
  131. case PTHREAD_MUTEX_ADAPTIVE_NP:
  132. __pthread_lock(&mutex->__m_lock, NULL);
  133. return 0;
  134. case PTHREAD_MUTEX_RECURSIVE_NP:
  135. self = thread_self();
  136. if (mutex->__m_owner == self) {
  137. mutex->__m_count++;
  138. return 0;
  139. }
  140. __pthread_lock(&mutex->__m_lock, self);
  141. mutex->__m_owner = self;
  142. mutex->__m_count = 0;
  143. return 0;
  144. case PTHREAD_MUTEX_ERRORCHECK_NP:
  145. self = thread_self();
  146. if (mutex->__m_owner == self) return EDEADLK;
  147. res = __pthread_alt_timedlock(&mutex->__m_lock, self, abstime);
  148. if (res != 0)
  149. {
  150. mutex->__m_owner = self;
  151. return 0;
  152. }
  153. return ETIMEDOUT;
  154. case PTHREAD_MUTEX_TIMED_NP:
  155. /* Only this type supports timed out lock. */
  156. return (__pthread_alt_timedlock(&mutex->__m_lock, NULL, abstime)
  157. ? 0 : ETIMEDOUT);
  158. default:
  159. return EINVAL;
  160. }
  161. }
  162. strong_alias (__pthread_mutex_timedlock, pthread_mutex_timedlock)
  163. hidden_def (__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. 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. }