mutex.c 11 KB

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