rwlock.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /* Read-write lock implementation.
  2. Copyright (C) 1998, 2000 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 Lesser General Public License as
  8. published by the Free Software Foundation; either version 2.1 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. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; see the file COPYING.LIB. If not,
  16. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. Boston, MA 02111-1307, USA. */
  18. #include <bits/libc-lock.h>
  19. #include <errno.h>
  20. #include <pthread.h>
  21. #include <stdlib.h>
  22. #include "internals.h"
  23. #include "queue.h"
  24. #include "spinlock.h"
  25. #include "restart.h"
  26. /* Function called by pthread_cancel to remove the thread from
  27. waiting inside pthread_rwlock_timedrdlock or pthread_rwlock_timedwrlock. */
  28. static int rwlock_rd_extricate_func(void *obj, pthread_descr th)
  29. {
  30. pthread_rwlock_t *rwlock = obj;
  31. int did_remove = 0;
  32. __pthread_lock(&rwlock->__rw_lock, NULL);
  33. did_remove = remove_from_queue(&rwlock->__rw_read_waiting, th);
  34. __pthread_unlock(&rwlock->__rw_lock);
  35. return did_remove;
  36. }
  37. static int rwlock_wr_extricate_func(void *obj, pthread_descr th)
  38. {
  39. pthread_rwlock_t *rwlock = obj;
  40. int did_remove = 0;
  41. __pthread_lock(&rwlock->__rw_lock, NULL);
  42. did_remove = remove_from_queue(&rwlock->__rw_write_waiting, th);
  43. __pthread_unlock(&rwlock->__rw_lock);
  44. return did_remove;
  45. }
  46. /*
  47. * Check whether the calling thread already owns one or more read locks on the
  48. * specified lock. If so, return a pointer to the read lock info structure
  49. * corresponding to that lock.
  50. */
  51. static pthread_readlock_info *
  52. rwlock_is_in_list(pthread_descr self, pthread_rwlock_t *rwlock)
  53. {
  54. pthread_readlock_info *info;
  55. for (info = THREAD_GETMEM (self, p_readlock_list); info != NULL;
  56. info = info->pr_next)
  57. {
  58. if (info->pr_lock == rwlock)
  59. return info;
  60. }
  61. return NULL;
  62. }
  63. /*
  64. * Add a new lock to the thread's list of locks for which it has a read lock.
  65. * A new info node must be allocated for this, which is taken from the thread's
  66. * free list, or by calling malloc. If malloc fails, a null pointer is
  67. * returned. Otherwise the lock info structure is initialized and pushed
  68. * onto the thread's list.
  69. */
  70. static pthread_readlock_info *
  71. rwlock_add_to_list(pthread_descr self, pthread_rwlock_t *rwlock)
  72. {
  73. pthread_readlock_info *info = THREAD_GETMEM (self, p_readlock_free);
  74. if (info != NULL)
  75. THREAD_SETMEM (self, p_readlock_free, info->pr_next);
  76. else
  77. info = malloc(sizeof *info);
  78. if (info == NULL)
  79. return NULL;
  80. info->pr_lock_count = 1;
  81. info->pr_lock = rwlock;
  82. info->pr_next = THREAD_GETMEM (self, p_readlock_list);
  83. THREAD_SETMEM (self, p_readlock_list, info);
  84. return info;
  85. }
  86. /*
  87. * If the thread owns a read lock over the given pthread_rwlock_t,
  88. * and this read lock is tracked in the thread's lock list,
  89. * this function returns a pointer to the info node in that list.
  90. * It also decrements the lock count within that node, and if
  91. * it reaches zero, it removes the node from the list.
  92. * If nothing is found, it returns a null pointer.
  93. */
  94. static pthread_readlock_info *
  95. rwlock_remove_from_list(pthread_descr self, pthread_rwlock_t *rwlock)
  96. {
  97. pthread_readlock_info **pinfo;
  98. for (pinfo = &self->p_readlock_list; *pinfo != NULL; pinfo = &(*pinfo)->pr_next)
  99. {
  100. if ((*pinfo)->pr_lock == rwlock)
  101. {
  102. pthread_readlock_info *info = *pinfo;
  103. if (--info->pr_lock_count == 0)
  104. *pinfo = info->pr_next;
  105. return info;
  106. }
  107. }
  108. return NULL;
  109. }
  110. /*
  111. * This function checks whether the conditions are right to place a read lock.
  112. * It returns 1 if so, otherwise zero. The rwlock's internal lock must be
  113. * locked upon entry.
  114. */
  115. static int
  116. rwlock_can_rdlock(pthread_rwlock_t *rwlock, int have_lock_already)
  117. {
  118. /* Can't readlock; it is write locked. */
  119. if (rwlock->__rw_writer != NULL)
  120. return 0;
  121. /* Lock prefers readers; get it. */
  122. if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP)
  123. return 1;
  124. /* Lock prefers writers, but none are waiting. */
  125. if (queue_is_empty(&rwlock->__rw_write_waiting))
  126. return 1;
  127. /* Writers are waiting, but this thread already has a read lock */
  128. if (have_lock_already)
  129. return 1;
  130. /* Writers are waiting, and this is a new lock */
  131. return 0;
  132. }
  133. /*
  134. * This function helps support brain-damaged recursive read locking
  135. * semantics required by Unix 98, while maintaining write priority.
  136. * This basically determines whether this thread already holds a read lock
  137. * already. It returns 1 if so, otherwise it returns 0.
  138. *
  139. * If the thread has any ``untracked read locks'' then it just assumes
  140. * that this lock is among them, just to be safe, and returns 1.
  141. *
  142. * Also, if it finds the thread's lock in the list, it sets the pointer
  143. * referenced by pexisting to refer to the list entry.
  144. *
  145. * If the thread has no untracked locks, and the lock is not found
  146. * in its list, then it is added to the list. If this fails,
  147. * then *pout_of_mem is set to 1.
  148. */
  149. static int
  150. rwlock_have_already(pthread_descr *pself, pthread_rwlock_t *rwlock,
  151. pthread_readlock_info **pexisting, int *pout_of_mem)
  152. {
  153. pthread_readlock_info *existing = NULL;
  154. int out_of_mem = 0, have_lock_already = 0;
  155. pthread_descr self = *pself;
  156. if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_WRITER_NP)
  157. {
  158. if (!self)
  159. *pself = self = thread_self();
  160. existing = rwlock_is_in_list(self, rwlock);
  161. if (existing != NULL
  162. || THREAD_GETMEM (self, p_untracked_readlock_count) > 0)
  163. have_lock_already = 1;
  164. else
  165. {
  166. existing = rwlock_add_to_list(self, rwlock);
  167. if (existing == NULL)
  168. out_of_mem = 1;
  169. }
  170. }
  171. *pout_of_mem = out_of_mem;
  172. *pexisting = existing;
  173. return have_lock_already;
  174. }
  175. int
  176. __pthread_rwlock_init (pthread_rwlock_t *rwlock,
  177. const pthread_rwlockattr_t *attr)
  178. {
  179. __pthread_init_lock(&rwlock->__rw_lock);
  180. rwlock->__rw_readers = 0;
  181. rwlock->__rw_writer = NULL;
  182. rwlock->__rw_read_waiting = NULL;
  183. rwlock->__rw_write_waiting = NULL;
  184. if (attr == NULL)
  185. {
  186. rwlock->__rw_kind = PTHREAD_RWLOCK_DEFAULT_NP;
  187. rwlock->__rw_pshared = PTHREAD_PROCESS_PRIVATE;
  188. }
  189. else
  190. {
  191. rwlock->__rw_kind = attr->__lockkind;
  192. rwlock->__rw_pshared = attr->__pshared;
  193. }
  194. return 0;
  195. }
  196. strong_alias (__pthread_rwlock_init, pthread_rwlock_init)
  197. int
  198. __pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
  199. {
  200. int readers;
  201. _pthread_descr writer;
  202. __pthread_lock (&rwlock->__rw_lock, NULL);
  203. readers = rwlock->__rw_readers;
  204. writer = rwlock->__rw_writer;
  205. __pthread_unlock (&rwlock->__rw_lock);
  206. if (readers > 0 || writer != NULL)
  207. return EBUSY;
  208. return 0;
  209. }
  210. strong_alias (__pthread_rwlock_destroy, pthread_rwlock_destroy)
  211. int
  212. __pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
  213. {
  214. pthread_descr self = NULL;
  215. pthread_readlock_info *existing;
  216. int out_of_mem, have_lock_already;
  217. have_lock_already = rwlock_have_already(&self, rwlock,
  218. &existing, &out_of_mem);
  219. if (self == NULL)
  220. self = thread_self ();
  221. for (;;)
  222. {
  223. __pthread_lock (&rwlock->__rw_lock, self);
  224. if (rwlock_can_rdlock(rwlock, have_lock_already))
  225. break;
  226. enqueue (&rwlock->__rw_read_waiting, self);
  227. __pthread_unlock (&rwlock->__rw_lock);
  228. suspend (self); /* This is not a cancellation point */
  229. }
  230. ++rwlock->__rw_readers;
  231. __pthread_unlock (&rwlock->__rw_lock);
  232. if (have_lock_already || out_of_mem)
  233. {
  234. if (existing != NULL)
  235. ++existing->pr_lock_count;
  236. else
  237. ++self->p_untracked_readlock_count;
  238. }
  239. return 0;
  240. }
  241. strong_alias (__pthread_rwlock_rdlock, pthread_rwlock_rdlock)
  242. int
  243. __pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock,
  244. const struct timespec *abstime)
  245. {
  246. pthread_descr self = NULL;
  247. pthread_readlock_info *existing;
  248. int out_of_mem, have_lock_already;
  249. pthread_extricate_if extr;
  250. if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
  251. return EINVAL;
  252. have_lock_already = rwlock_have_already(&self, rwlock,
  253. &existing, &out_of_mem);
  254. if (self == NULL)
  255. self = thread_self ();
  256. /* Set up extrication interface */
  257. extr.pu_object = rwlock;
  258. extr.pu_extricate_func = rwlock_rd_extricate_func;
  259. /* Register extrication interface */
  260. __pthread_set_own_extricate_if (self, &extr);
  261. for (;;)
  262. {
  263. __pthread_lock (&rwlock->__rw_lock, self);
  264. if (rwlock_can_rdlock(rwlock, have_lock_already))
  265. break;
  266. enqueue (&rwlock->__rw_read_waiting, self);
  267. __pthread_unlock (&rwlock->__rw_lock);
  268. /* This is not a cancellation point */
  269. if (timedsuspend (self, abstime) == 0)
  270. {
  271. int was_on_queue;
  272. __pthread_lock (&rwlock->__rw_lock, self);
  273. was_on_queue = remove_from_queue (&rwlock->__rw_read_waiting, self);
  274. __pthread_unlock (&rwlock->__rw_lock);
  275. if (was_on_queue)
  276. {
  277. __pthread_set_own_extricate_if (self, 0);
  278. return ETIMEDOUT;
  279. }
  280. /* Eat the outstanding restart() from the signaller */
  281. suspend (self);
  282. }
  283. }
  284. __pthread_set_own_extricate_if (self, 0);
  285. ++rwlock->__rw_readers;
  286. __pthread_unlock (&rwlock->__rw_lock);
  287. if (have_lock_already || out_of_mem)
  288. {
  289. if (existing != NULL)
  290. ++existing->pr_lock_count;
  291. else
  292. ++self->p_untracked_readlock_count;
  293. }
  294. return 0;
  295. }
  296. strong_alias (__pthread_rwlock_timedrdlock, pthread_rwlock_timedrdlock)
  297. int
  298. __pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
  299. {
  300. pthread_descr self = thread_self();
  301. pthread_readlock_info *existing;
  302. int out_of_mem, have_lock_already;
  303. int retval = EBUSY;
  304. have_lock_already = rwlock_have_already(&self, rwlock,
  305. &existing, &out_of_mem);
  306. __pthread_lock (&rwlock->__rw_lock, self);
  307. /* 0 is passed to here instead of have_lock_already.
  308. This is to meet Single Unix Spec requirements:
  309. if writers are waiting, pthread_rwlock_tryrdlock
  310. does not acquire a read lock, even if the caller has
  311. one or more read locks already. */
  312. if (rwlock_can_rdlock(rwlock, 0))
  313. {
  314. ++rwlock->__rw_readers;
  315. retval = 0;
  316. }
  317. __pthread_unlock (&rwlock->__rw_lock);
  318. if (retval == 0)
  319. {
  320. if (have_lock_already || out_of_mem)
  321. {
  322. if (existing != NULL)
  323. ++existing->pr_lock_count;
  324. else
  325. ++self->p_untracked_readlock_count;
  326. }
  327. }
  328. return retval;
  329. }
  330. strong_alias (__pthread_rwlock_tryrdlock, pthread_rwlock_tryrdlock)
  331. int
  332. __pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
  333. {
  334. pthread_descr self = thread_self ();
  335. while(1)
  336. {
  337. __pthread_lock (&rwlock->__rw_lock, self);
  338. if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
  339. {
  340. rwlock->__rw_writer = self;
  341. __pthread_unlock (&rwlock->__rw_lock);
  342. return 0;
  343. }
  344. /* Suspend ourselves, then try again */
  345. enqueue (&rwlock->__rw_write_waiting, self);
  346. __pthread_unlock (&rwlock->__rw_lock);
  347. suspend (self); /* This is not a cancellation point */
  348. }
  349. }
  350. strong_alias (__pthread_rwlock_wrlock, pthread_rwlock_wrlock)
  351. int
  352. __pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock,
  353. const struct timespec *abstime)
  354. {
  355. pthread_descr self;
  356. pthread_extricate_if extr;
  357. if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
  358. return EINVAL;
  359. self = thread_self ();
  360. /* Set up extrication interface */
  361. extr.pu_object = rwlock;
  362. extr.pu_extricate_func = rwlock_wr_extricate_func;
  363. /* Register extrication interface */
  364. __pthread_set_own_extricate_if (self, &extr);
  365. while(1)
  366. {
  367. __pthread_lock (&rwlock->__rw_lock, self);
  368. if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
  369. {
  370. rwlock->__rw_writer = self;
  371. __pthread_set_own_extricate_if (self, 0);
  372. __pthread_unlock (&rwlock->__rw_lock);
  373. return 0;
  374. }
  375. /* Suspend ourselves, then try again */
  376. enqueue (&rwlock->__rw_write_waiting, self);
  377. __pthread_unlock (&rwlock->__rw_lock);
  378. /* This is not a cancellation point */
  379. if (timedsuspend (self, abstime) == 0)
  380. {
  381. int was_on_queue;
  382. __pthread_lock (&rwlock->__rw_lock, self);
  383. was_on_queue = remove_from_queue (&rwlock->__rw_write_waiting, self);
  384. __pthread_unlock (&rwlock->__rw_lock);
  385. if (was_on_queue)
  386. {
  387. __pthread_set_own_extricate_if (self, 0);
  388. return ETIMEDOUT;
  389. }
  390. /* Eat the outstanding restart() from the signaller */
  391. suspend (self);
  392. }
  393. }
  394. }
  395. strong_alias (__pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock)
  396. int
  397. __pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
  398. {
  399. int result = EBUSY;
  400. __pthread_lock (&rwlock->__rw_lock, NULL);
  401. if (rwlock->__rw_readers == 0 && rwlock->__rw_writer == NULL)
  402. {
  403. rwlock->__rw_writer = thread_self ();
  404. result = 0;
  405. }
  406. __pthread_unlock (&rwlock->__rw_lock);
  407. return result;
  408. }
  409. strong_alias (__pthread_rwlock_trywrlock, pthread_rwlock_trywrlock)
  410. int
  411. __pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
  412. {
  413. pthread_descr torestart;
  414. pthread_descr th;
  415. __pthread_lock (&rwlock->__rw_lock, NULL);
  416. if (rwlock->__rw_writer != NULL)
  417. {
  418. /* Unlocking a write lock. */
  419. if (rwlock->__rw_writer != thread_self ())
  420. {
  421. __pthread_unlock (&rwlock->__rw_lock);
  422. return EPERM;
  423. }
  424. rwlock->__rw_writer = NULL;
  425. if ((rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP
  426. && !queue_is_empty(&rwlock->__rw_read_waiting))
  427. || (th = dequeue(&rwlock->__rw_write_waiting)) == NULL)
  428. {
  429. /* Restart all waiting readers. */
  430. torestart = rwlock->__rw_read_waiting;
  431. rwlock->__rw_read_waiting = NULL;
  432. __pthread_unlock (&rwlock->__rw_lock);
  433. while ((th = dequeue (&torestart)) != NULL)
  434. restart (th);
  435. }
  436. else
  437. {
  438. /* Restart one waiting writer. */
  439. __pthread_unlock (&rwlock->__rw_lock);
  440. restart (th);
  441. }
  442. }
  443. else
  444. {
  445. /* Unlocking a read lock. */
  446. if (rwlock->__rw_readers == 0)
  447. {
  448. __pthread_unlock (&rwlock->__rw_lock);
  449. return EPERM;
  450. }
  451. --rwlock->__rw_readers;
  452. if (rwlock->__rw_readers == 0)
  453. /* Restart one waiting writer, if any. */
  454. th = dequeue (&rwlock->__rw_write_waiting);
  455. else
  456. th = NULL;
  457. __pthread_unlock (&rwlock->__rw_lock);
  458. if (th != NULL)
  459. restart (th);
  460. /* Recursive lock fixup */
  461. if (rwlock->__rw_kind == PTHREAD_RWLOCK_PREFER_WRITER_NP)
  462. {
  463. pthread_descr self = thread_self();
  464. pthread_readlock_info *victim = rwlock_remove_from_list(self, rwlock);
  465. if (victim != NULL)
  466. {
  467. if (victim->pr_lock_count == 0)
  468. {
  469. victim->pr_next = THREAD_GETMEM (self, p_readlock_free);
  470. THREAD_SETMEM (self, p_readlock_free, victim);
  471. }
  472. }
  473. else
  474. {
  475. int val = THREAD_GETMEM (self, p_untracked_readlock_count);
  476. if (val > 0)
  477. THREAD_SETMEM (self, p_untracked_readlock_count, val - 1);
  478. }
  479. }
  480. }
  481. return 0;
  482. }
  483. strong_alias (__pthread_rwlock_unlock, pthread_rwlock_unlock)
  484. int
  485. pthread_rwlockattr_init (pthread_rwlockattr_t *attr)
  486. {
  487. attr->__lockkind = 0;
  488. attr->__pshared = PTHREAD_PROCESS_PRIVATE;
  489. return 0;
  490. }
  491. int
  492. __pthread_rwlockattr_destroy (pthread_rwlockattr_t *attr)
  493. {
  494. return 0;
  495. }
  496. strong_alias (__pthread_rwlockattr_destroy, pthread_rwlockattr_destroy)
  497. int
  498. pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *attr, int *pshared)
  499. {
  500. *pshared = attr->__pshared;
  501. return 0;
  502. }
  503. int
  504. pthread_rwlockattr_setpshared (pthread_rwlockattr_t *attr, int pshared)
  505. {
  506. if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
  507. return EINVAL;
  508. /* For now it is not possible to shared a conditional variable. */
  509. if (pshared != PTHREAD_PROCESS_PRIVATE)
  510. return ENOSYS;
  511. attr->__pshared = pshared;
  512. return 0;
  513. }
  514. int
  515. pthread_rwlockattr_getkind_np (const pthread_rwlockattr_t *attr, int *pref)
  516. {
  517. *pref = attr->__lockkind;
  518. return 0;
  519. }
  520. int
  521. pthread_rwlockattr_setkind_np (pthread_rwlockattr_t *attr, int pref)
  522. {
  523. if (pref != PTHREAD_RWLOCK_PREFER_READER_NP
  524. && pref != PTHREAD_RWLOCK_PREFER_WRITER_NP
  525. && pref != PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
  526. && pref != PTHREAD_RWLOCK_DEFAULT_NP)
  527. return EINVAL;
  528. attr->__lockkind = pref;
  529. return 0;
  530. }