mutex.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. hidden_strong_alias (pthread_mutex_init, __pthread_mutex_init)
  35. int 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. hidden_strong_alias (pthread_mutex_destroy, __pthread_mutex_destroy)
  53. int 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. hidden_strong_alias (pthread_mutex_trylock, __pthread_mutex_trylock)
  87. int 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. hidden_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 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. hidden_strong_alias (pthread_mutex_unlock, __pthread_mutex_unlock)
  188. libpthread_hidden_proto(pthread_mutexattr_init)
  189. int pthread_mutexattr_init(pthread_mutexattr_t *attr)
  190. {
  191. attr->__mutexkind = PTHREAD_MUTEX_TIMED_NP;
  192. return 0;
  193. }
  194. libpthread_hidden_def(pthread_mutexattr_init)
  195. libpthread_hidden_proto(pthread_mutexattr_destroy)
  196. int pthread_mutexattr_destroy(pthread_mutexattr_t *attr attribute_unused)
  197. {
  198. return 0;
  199. }
  200. libpthread_hidden_def(pthread_mutexattr_destroy)
  201. libpthread_hidden_proto(pthread_mutexattr_settype)
  202. int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
  203. {
  204. if (kind != PTHREAD_MUTEX_ADAPTIVE_NP
  205. && kind != PTHREAD_MUTEX_RECURSIVE_NP
  206. && kind != PTHREAD_MUTEX_ERRORCHECK_NP
  207. && kind != PTHREAD_MUTEX_TIMED_NP)
  208. return EINVAL;
  209. attr->__mutexkind = kind;
  210. return 0;
  211. }
  212. libpthread_hidden_def(pthread_mutexattr_settype)
  213. strong_alias (pthread_mutexattr_settype, __pthread_mutexattr_setkind_np)
  214. weak_alias (__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np)
  215. int __pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *kind)
  216. {
  217. *kind = attr->__mutexkind;
  218. return 0;
  219. }
  220. weak_alias (__pthread_mutexattr_gettype, pthread_mutexattr_gettype)
  221. strong_alias (__pthread_mutexattr_gettype, __pthread_mutexattr_getkind_np)
  222. weak_alias (__pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np)
  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)
  231. {
  232. if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
  233. return EINVAL;
  234. /* For now it is not possible to shared a conditional variable. */
  235. if (pshared != PTHREAD_PROCESS_PRIVATE)
  236. return ENOSYS;
  237. return 0;
  238. }
  239. weak_alias (__pthread_mutexattr_setpshared, pthread_mutexattr_setpshared)
  240. /* Once-only execution */
  241. static pthread_mutex_t once_masterlock = PTHREAD_MUTEX_INITIALIZER;
  242. static pthread_cond_t once_finished = PTHREAD_COND_INITIALIZER;
  243. static int fork_generation = 0; /* Child process increments this after fork. */
  244. enum { NEVER = 0, IN_PROGRESS = 1, DONE = 2 };
  245. /* If a thread is canceled while calling the init_routine out of
  246. pthread once, this handler will reset the once_control variable
  247. to the NEVER state. */
  248. static void pthread_once_cancelhandler(void *arg)
  249. {
  250. pthread_once_t *once_control = arg;
  251. __pthread_mutex_lock(&once_masterlock);
  252. *once_control = NEVER;
  253. __pthread_mutex_unlock(&once_masterlock);
  254. pthread_cond_broadcast(&once_finished);
  255. }
  256. int __pthread_once(pthread_once_t * once_control, void (*init_routine)(void))
  257. {
  258. /* flag for doing the condition broadcast outside of mutex */
  259. int state_changed;
  260. /* Test without locking first for speed */
  261. if (*once_control == DONE) {
  262. READ_MEMORY_BARRIER();
  263. return 0;
  264. }
  265. /* Lock and test again */
  266. state_changed = 0;
  267. __pthread_mutex_lock(&once_masterlock);
  268. /* If this object was left in an IN_PROGRESS state in a parent
  269. process (indicated by stale generation field), reset it to NEVER. */
  270. if ((*once_control & 3) == IN_PROGRESS && (*once_control & ~3) != fork_generation)
  271. *once_control = NEVER;
  272. /* If init_routine is being called from another routine, wait until
  273. it completes. */
  274. while ((*once_control & 3) == IN_PROGRESS) {
  275. pthread_cond_wait(&once_finished, &once_masterlock);
  276. }
  277. /* Here *once_control is stable and either NEVER or DONE. */
  278. if (*once_control == NEVER) {
  279. *once_control = IN_PROGRESS | fork_generation;
  280. __pthread_mutex_unlock(&once_masterlock);
  281. pthread_cleanup_push(pthread_once_cancelhandler, once_control);
  282. init_routine();
  283. pthread_cleanup_pop(0);
  284. __pthread_mutex_lock(&once_masterlock);
  285. WRITE_MEMORY_BARRIER();
  286. *once_control = DONE;
  287. state_changed = 1;
  288. }
  289. __pthread_mutex_unlock(&once_masterlock);
  290. if (state_changed)
  291. pthread_cond_broadcast(&once_finished);
  292. return 0;
  293. }
  294. strong_alias (__pthread_once, pthread_once)
  295. /*
  296. * Handle the state of the pthread_once mechanism across forks. The
  297. * once_masterlock is acquired in the parent process prior to a fork to ensure
  298. * that no thread is in the critical region protected by the lock. After the
  299. * fork, the lock is released. In the child, the lock and the condition
  300. * variable are simply reset. The child also increments its generation
  301. * counter which lets pthread_once calls detect stale IN_PROGRESS states
  302. * and reset them back to NEVER.
  303. */
  304. void __pthread_once_fork_prepare(void)
  305. {
  306. __pthread_mutex_lock(&once_masterlock);
  307. }
  308. void __pthread_once_fork_parent(void)
  309. {
  310. __pthread_mutex_unlock(&once_masterlock);
  311. }
  312. void __pthread_once_fork_child(void)
  313. {
  314. __pthread_mutex_init(&once_masterlock, NULL);
  315. pthread_cond_init(&once_finished, NULL);
  316. if (fork_generation <= INT_MAX - 4)
  317. fork_generation += 4; /* leave least significant two bits zero */
  318. else
  319. fork_generation = 0;
  320. }