mutex.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. 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. 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. 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. 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. strong_alias (__pthread_mutex_timedlock, pthread_mutex_timedlock)
  159. int __pthread_mutex_unlock(pthread_mutex_t * mutex)
  160. {
  161. switch (mutex->__m_kind) {
  162. case PTHREAD_MUTEX_ADAPTIVE_NP:
  163. __pthread_unlock(&mutex->__m_lock);
  164. return 0;
  165. case PTHREAD_MUTEX_RECURSIVE_NP:
  166. if (mutex->__m_owner != thread_self())
  167. return EPERM;
  168. if (mutex->__m_count > 0) {
  169. mutex->__m_count--;
  170. return 0;
  171. }
  172. mutex->__m_owner = NULL;
  173. __pthread_unlock(&mutex->__m_lock);
  174. return 0;
  175. case PTHREAD_MUTEX_ERRORCHECK_NP:
  176. if (mutex->__m_owner != thread_self() || mutex->__m_lock.__status == 0)
  177. return EPERM;
  178. mutex->__m_owner = NULL;
  179. __pthread_alt_unlock(&mutex->__m_lock);
  180. return 0;
  181. case PTHREAD_MUTEX_TIMED_NP:
  182. __pthread_alt_unlock(&mutex->__m_lock);
  183. return 0;
  184. default:
  185. return EINVAL;
  186. }
  187. }
  188. strong_alias (__pthread_mutex_unlock, pthread_mutex_unlock)
  189. int __pthread_mutexattr_init(pthread_mutexattr_t *attr)
  190. {
  191. attr->__mutexkind = PTHREAD_MUTEX_TIMED_NP;
  192. return 0;
  193. }
  194. strong_alias (__pthread_mutexattr_init, pthread_mutexattr_init)
  195. int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
  196. {
  197. return 0;
  198. }
  199. strong_alias (__pthread_mutexattr_destroy, pthread_mutexattr_destroy)
  200. int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int kind)
  201. {
  202. if (kind != PTHREAD_MUTEX_ADAPTIVE_NP
  203. && kind != PTHREAD_MUTEX_RECURSIVE_NP
  204. && kind != PTHREAD_MUTEX_ERRORCHECK_NP
  205. && kind != PTHREAD_MUTEX_TIMED_NP)
  206. return EINVAL;
  207. attr->__mutexkind = kind;
  208. return 0;
  209. }
  210. weak_alias (__pthread_mutexattr_settype, pthread_mutexattr_settype)
  211. strong_alias ( __pthread_mutexattr_settype, __pthread_mutexattr_setkind_np)
  212. weak_alias (__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np)
  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,
  222. int *pshared)
  223. {
  224. *pshared = PTHREAD_PROCESS_PRIVATE;
  225. return 0;
  226. }
  227. weak_alias (__pthread_mutexattr_getpshared, pthread_mutexattr_getpshared)
  228. int __pthread_mutexattr_setpshared (pthread_mutexattr_t *attr, int pshared)
  229. {
  230. if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
  231. return EINVAL;
  232. /* For now it is not possible to shared a conditional variable. */
  233. if (pshared != PTHREAD_PROCESS_PRIVATE)
  234. return ENOSYS;
  235. return 0;
  236. }
  237. weak_alias (__pthread_mutexattr_setpshared, pthread_mutexattr_setpshared)
  238. /* Once-only execution */
  239. static pthread_mutex_t once_masterlock = PTHREAD_MUTEX_INITIALIZER;
  240. static pthread_cond_t once_finished = PTHREAD_COND_INITIALIZER;
  241. static int fork_generation = 0; /* Child process increments this after fork. */
  242. enum { NEVER = 0, IN_PROGRESS = 1, DONE = 2 };
  243. /* If a thread is canceled while calling the init_routine out of
  244. pthread once, this handler will reset the once_control variable
  245. to the NEVER state. */
  246. static void pthread_once_cancelhandler(void *arg)
  247. {
  248. pthread_once_t *once_control = arg;
  249. pthread_mutex_lock(&once_masterlock);
  250. *once_control = NEVER;
  251. pthread_mutex_unlock(&once_masterlock);
  252. pthread_cond_broadcast(&once_finished);
  253. }
  254. int __pthread_once(pthread_once_t * once_control, void (*init_routine)(void))
  255. {
  256. /* flag for doing the condition broadcast outside of mutex */
  257. int state_changed;
  258. /* Test without locking first for speed */
  259. if (*once_control == DONE) {
  260. READ_MEMORY_BARRIER();
  261. return 0;
  262. }
  263. /* Lock and test again */
  264. state_changed = 0;
  265. pthread_mutex_lock(&once_masterlock);
  266. /* If this object was left in an IN_PROGRESS state in a parent
  267. process (indicated by stale generation field), reset it to NEVER. */
  268. if ((*once_control & 3) == IN_PROGRESS && (*once_control & ~3) != fork_generation)
  269. *once_control = NEVER;
  270. /* If init_routine is being called from another routine, wait until
  271. it completes. */
  272. while ((*once_control & 3) == IN_PROGRESS) {
  273. pthread_cond_wait(&once_finished, &once_masterlock);
  274. }
  275. /* Here *once_control is stable and either NEVER or DONE. */
  276. if (*once_control == NEVER) {
  277. *once_control = IN_PROGRESS | fork_generation;
  278. pthread_mutex_unlock(&once_masterlock);
  279. pthread_cleanup_push(pthread_once_cancelhandler, once_control);
  280. init_routine();
  281. pthread_cleanup_pop(0);
  282. pthread_mutex_lock(&once_masterlock);
  283. WRITE_MEMORY_BARRIER();
  284. *once_control = DONE;
  285. state_changed = 1;
  286. }
  287. pthread_mutex_unlock(&once_masterlock);
  288. if (state_changed)
  289. pthread_cond_broadcast(&once_finished);
  290. return 0;
  291. }
  292. strong_alias (__pthread_once, pthread_once)
  293. /*
  294. * Handle the state of the pthread_once mechanism across forks. The
  295. * once_masterlock is acquired in the parent process prior to a fork to ensure
  296. * that no thread is in the critical region protected by the lock. After the
  297. * fork, the lock is released. In the child, the lock and the condition
  298. * variable are simply reset. The child also increments its generation
  299. * counter which lets pthread_once calls detect stale IN_PROGRESS states
  300. * and reset them back to NEVER.
  301. */
  302. void __pthread_once_fork_prepare(void)
  303. {
  304. pthread_mutex_lock(&once_masterlock);
  305. }
  306. void __pthread_once_fork_parent(void)
  307. {
  308. pthread_mutex_unlock(&once_masterlock);
  309. }
  310. void __pthread_once_fork_child(void)
  311. {
  312. pthread_mutex_init(&once_masterlock, NULL);
  313. pthread_cond_init(&once_finished, NULL);
  314. if (fork_generation <= INT_MAX - 4)
  315. fork_generation += 4; /* leave least significant two bits zero */
  316. else
  317. fork_generation = 0;
  318. }