rwlock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /* Read-write lock implementation.
  2. Copyright (C) 1998 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Xavier Leroy <Xavier.Leroy@inria.fr>
  5. and Ulrich Drepper <drepper@cygnus.com>, 1998.
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public License as
  8. published by the Free Software Foundation; either version 2 of the
  9. License, or (at your option) any later version.
  10. The GNU C Library 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 GNU
  13. Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB. If not,
  16. see <http://www.gnu.org/licenses/>. */
  17. #include <errno.h>
  18. #include <pthread.h>
  19. #include <stdlib.h>
  20. #include "internals.h"
  21. #include "queue.h"
  22. #include "spinlock.h"
  23. #include "restart.h"
  24. /*
  25. * Check whether the calling thread already owns one or more read locks on the
  26. * specified lock. If so, return a pointer to the read lock info structure
  27. * corresponding to that lock.
  28. */
  29. static pthread_readlock_info *
  30. rwlock_is_in_list(pthread_descr self, pthread_rwlock_t *rwlock)
  31. {
  32. pthread_readlock_info *info;
  33. for (info = self->p_readlock_list; info != NULL; info = info->pr_next)
  34. {
  35. if (info->pr_lock == rwlock)
  36. return info;
  37. }
  38. return NULL;
  39. }
  40. /*
  41. * Add a new lock to the thread's list of locks for which it has a read lock.
  42. * A new info node must be allocated for this, which is taken from the thread's
  43. * free list, or by calling malloc. If malloc fails, a null pointer is
  44. * returned. Otherwise the lock info structure is initialized and pushed
  45. * onto the thread's list.
  46. */
  47. static pthread_readlock_info *
  48. rwlock_add_to_list(pthread_descr self, pthread_rwlock_t *rwlock)
  49. {
  50. pthread_readlock_info *info = self->p_readlock_free;
  51. if (info != NULL)
  52. self->p_readlock_free = info->pr_next;
  53. else
  54. info = malloc(sizeof *info);
  55. if (info == NULL)
  56. return NULL;
  57. info->pr_lock_count = 1;
  58. info->pr_lock = rwlock;
  59. info->pr_next = self->p_readlock_list;
  60. self->p_readlock_list = info;
  61. return info;
  62. }
  63. /*
  64. * If the thread owns a read lock over the given pthread_rwlock_t,
  65. * and this read lock is tracked in the thread's lock list,
  66. * this function returns a pointer to the info node in that list.
  67. * It also decrements the lock count within that node, and if
  68. * it reaches zero, it removes the node from the list.
  69. * If nothing is found, it returns a null pointer.
  70. */
  71. static pthread_readlock_info *
  72. rwlock_remove_from_list(pthread_descr self, pthread_rwlock_t *rwlock)
  73. {
  74. pthread_readlock_info **pinfo;
  75. for (pinfo = &self->p_readlock_list; *pinfo != NULL; pinfo = &(*pinfo)->pr_next)
  76. {
  77. if ((*pinfo)->pr_lock == rwlock)
  78. {
  79. pthread_readlock_info *info = *pinfo;
  80. if (--info->pr_lock_count == 0)
  81. *pinfo = info->pr_next;
  82. return info;
  83. }
  84. }
  85. return NULL;
  86. }
  87. /*
  88. * This function checks whether the conditions are right to place a read lock.
  89. * It returns 1 if so, otherwise zero. The rwlock's internal lock must be
  90. * locked upon entry.
  91. */
  92. static int
  93. rwlock_can_rdlock(pthread_rwlock_t *rwlock, int have_lock_already)
  94. {
  95. /* Can't readlock; it is write locked. */
  96. if (rwlock->__rw_writer != NULL)
  97. return 0;
  98. /* Lock prefers readers; get it. */
  99. if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP)
  100. return 1;
  101. /* Lock prefers writers, but none are waiting. */
  102. if (queue_is_empty(&rwlock->__rw_write_waiting))
  103. return 1;
  104. /* Writers are waiting, but this thread already has a read lock */
  105. if (have_lock_already)
  106. return 1;
  107. /* Writers are waiting, and this is a new lock */
  108. return 0;
  109. }
  110. /*
  111. * This function helps support brain-damaged recursive read locking
  112. * semantics required by Unix 98, while maintaining write priority.
  113. * This basically determines whether this thread already holds a read lock
  114. * already. It returns 1 if so, otherwise it returns 0.
  115. *
  116. * If the thread has any ``untracked read locks'' then it just assumes
  117. * that this lock is among them, just to be safe, and returns 1.
  118. *
  119. * Also, if it finds the thread's lock in the list, it sets the pointer
  120. * referenced by pexisting to refer to the list entry.
  121. *
  122. * If the thread has no untracked locks, and the lock is not found
  123. * in its list, then it is added to the list. If this fails,
  124. * then *pout_of_mem is set to 1.
  125. */
  126. static int
  127. rwlock_have_already(pthread_descr *pself, pthread_rwlock_t *rwlock,
  128. pthread_readlock_info **pexisting, int *pout_of_mem)
  129. {
  130. pthread_readlock_info *existing = NULL;
  131. int out_of_mem = 0, have_lock_already = 0;
  132. pthread_descr self = *pself;
  133. if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_WRITER_NP)
  134. {
  135. if (!self)
  136. self = thread_self();
  137. existing = rwlock_is_in_list(self, rwlock);
  138. if (existing != NULL || self->p_untracked_readlock_count > 0)
  139. have_lock_already = 1;
  140. else
  141. {
  142. existing = rwlock_add_to_list(self, rwlock);
  143. if (existing == NULL)
  144. out_of_mem = 1;
  145. }
  146. }
  147. *pout_of_mem = out_of_mem;
  148. *pexisting = existing;
  149. *pself = self;
  150. return have_lock_already;
  151. }
  152. int
  153. pthread_rwlock_init (pthread_rwlock_t *rwlock,
  154. const pthread_rwlockattr_t *attr)
  155. {
  156. __pthread_init_lock(&rwlock->__rw_lock);
  157. rwlock->__rw_readers = 0;
  158. rwlock->__rw_writer = NULL;
  159. rwlock->__rw_read_waiting = NULL;
  160. rwlock->__rw_write_waiting = NULL;
  161. if (attr == NULL)
  162. {
  163. rwlock->__rw_kind = PTHREAD_RWLOCK_DEFAULT_NP;
  164. rwlock->__rw_pshared = PTHREAD_PROCESS_PRIVATE;
  165. }
  166. else
  167. {
  168. rwlock->__rw_kind = attr->__lockkind;
  169. rwlock->__rw_pshared = attr->__pshared;
  170. }
  171. return 0;
  172. }
  173. int
  174. pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
  175. {
  176. int readers;
  177. _pthread_descr writer;
  178. __pthread_lock (&rwlock->__rw_lock, NULL);
  179. readers = rwlock->__rw_readers;
  180. writer = rwlock->__rw_writer;
  181. __pthread_unlock (&rwlock->__rw_lock);
  182. if (readers > 0 || writer != NULL)
  183. return EBUSY;
  184. return 0;
  185. }
  186. int
  187. pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
  188. {
  189. pthread_descr self = NULL;
  190. pthread_readlock_info *existing;
  191. int out_of_mem, have_lock_already;
  192. have_lock_already = rwlock_have_already(&self, rwlock,
  193. &existing, &out_of_mem);
  194. for (;;)
  195. {
  196. if (self == NULL)
  197. self = thread_self ();
  198. __pthread_lock (&rwlock->__rw_lock, self);
  199. if (rwlock_can_rdlock(rwlock, have_lock_already))
  200. break;
  201. enqueue (&rwlock->__rw_read_waiting, self);
  202. __pthread_unlock (&rwlock->__rw_lock);
  203. suspend (self); /* This is not a cancellation point */
  204. }
  205. ++rwlock->__rw_readers;
  206. __pthread_unlock (&rwlock->__rw_lock);
  207. if (have_lock_already || out_of_mem)
  208. {
  209. if (existing != NULL)
  210. existing->pr_lock_count++;
  211. else
  212. self->p_untracked_readlock_count++;
  213. }
  214. return 0;
  215. }
  216. int
  217. pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
  218. {
  219. pthread_descr self = thread_self();
  220. pthread_readlock_info *existing;
  221. int out_of_mem, have_lock_already;
  222. int retval = EBUSY;
  223. have_lock_already = rwlock_have_already(&self, rwlock,
  224. &existing, &out_of_mem);
  225. __pthread_lock (&rwlock->__rw_lock, self);
  226. /* 0 is passed to here instead of have_lock_already.
  227. This is to meet Single Unix Spec requirements:
  228. if writers are waiting, pthread_rwlock_tryrdlock
  229. does not acquire a read lock, even if the caller has
  230. one or more read locks already. */
  231. if (rwlock_can_rdlock(rwlock, 0))
  232. {
  233. ++rwlock->__rw_readers;
  234. retval = 0;
  235. }
  236. __pthread_unlock (&rwlock->__rw_lock);
  237. if (retval == 0)
  238. {
  239. if (have_lock_already || out_of_mem)
  240. {
  241. if (existing != NULL)
  242. existing->pr_lock_count++;
  243. else
  244. self->p_untracked_readlock_count++;
  245. }
  246. }
  247. return retval;
  248. }
  249. int
  250. pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
  251. {
  252. pthread_descr self = thread_self ();
  253. while(1)
  254. {
  255. __pthread_lock (&rwlock->__rw_lock, self);
  256. if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
  257. {
  258. rwlock->__rw_writer = self;
  259. __pthread_unlock (&rwlock->__rw_lock);
  260. return 0;
  261. }
  262. /* Suspend ourselves, then try again */
  263. enqueue (&rwlock->__rw_write_waiting, self);
  264. __pthread_unlock (&rwlock->__rw_lock);
  265. suspend (self); /* This is not a cancellation point */
  266. }
  267. }
  268. int
  269. pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
  270. {
  271. int result = EBUSY;
  272. __pthread_lock (&rwlock->__rw_lock, NULL);
  273. if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
  274. {
  275. rwlock->__rw_writer = thread_self ();
  276. result = 0;
  277. }
  278. __pthread_unlock (&rwlock->__rw_lock);
  279. return result;
  280. }
  281. int
  282. pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
  283. {
  284. pthread_descr torestart;
  285. pthread_descr th;
  286. __pthread_lock (&rwlock->__rw_lock, NULL);
  287. if (rwlock->__rw_writer != NULL)
  288. {
  289. /* Unlocking a write lock. */
  290. if (rwlock->__rw_writer != thread_self ())
  291. {
  292. __pthread_unlock (&rwlock->__rw_lock);
  293. return EPERM;
  294. }
  295. rwlock->__rw_writer = NULL;
  296. if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP
  297. || (th = dequeue (&rwlock->__rw_write_waiting)) == NULL)
  298. {
  299. /* Restart all waiting readers. */
  300. torestart = rwlock->__rw_read_waiting;
  301. rwlock->__rw_read_waiting = NULL;
  302. __pthread_unlock (&rwlock->__rw_lock);
  303. while ((th = dequeue (&torestart)) != NULL)
  304. restart (th);
  305. }
  306. else
  307. {
  308. /* Restart one waiting writer. */
  309. __pthread_unlock (&rwlock->__rw_lock);
  310. restart (th);
  311. }
  312. }
  313. else
  314. {
  315. /* Unlocking a read lock. */
  316. if (rwlock->__rw_readers == 0)
  317. {
  318. __pthread_unlock (&rwlock->__rw_lock);
  319. return EPERM;
  320. }
  321. --rwlock->__rw_readers;
  322. if (rwlock->__rw_readers == 0)
  323. /* Restart one waiting writer, if any. */
  324. th = dequeue (&rwlock->__rw_write_waiting);
  325. else
  326. th = NULL;
  327. __pthread_unlock (&rwlock->__rw_lock);
  328. if (th != NULL)
  329. restart (th);
  330. /* Recursive lock fixup */
  331. if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_WRITER_NP)
  332. {
  333. pthread_descr self = thread_self();
  334. pthread_readlock_info *victim = rwlock_remove_from_list(self, rwlock);
  335. if (victim != NULL)
  336. {
  337. if (victim->pr_lock_count == 0)
  338. {
  339. victim->pr_next = self->p_readlock_free;
  340. self->p_readlock_free = victim;
  341. }
  342. }
  343. else
  344. {
  345. if (self->p_untracked_readlock_count > 0)
  346. self->p_untracked_readlock_count--;
  347. }
  348. }
  349. }
  350. return 0;
  351. }
  352. int
  353. pthread_rwlockattr_init (pthread_rwlockattr_t *attr)
  354. {
  355. attr->__lockkind = 0;
  356. attr->__pshared = 0;
  357. return 0;
  358. }
  359. int
  360. pthread_rwlockattr_destroy (pthread_rwlockattr_t *attr attribute_unused)
  361. {
  362. return 0;
  363. }
  364. int
  365. pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *attr, int *pshared)
  366. {
  367. *pshared = attr->__pshared;
  368. return 0;
  369. }
  370. int
  371. pthread_rwlockattr_setpshared (pthread_rwlockattr_t *attr, int pshared)
  372. {
  373. if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
  374. return EINVAL;
  375. attr->__pshared = pshared;
  376. return 0;
  377. }
  378. int
  379. pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t *attr, int *pref)
  380. {
  381. *pref = attr->__lockkind;
  382. return 0;
  383. }
  384. int
  385. pthread_rwlockattr_setkind_np (pthread_rwlockattr_t *attr, int pref)
  386. {
  387. if (pref != PTHREAD_RWLOCK_PREFER_READER_NP
  388. && pref != PTHREAD_RWLOCK_PREFER_WRITER_NP
  389. && pref != PTHREAD_RWLOCK_DEFAULT_NP)
  390. return EINVAL;
  391. attr->__lockkind = pref;
  392. return 0;
  393. }