pthread_rwlock_timedrdlock.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Copyright (C) 2003, 2004, 2007 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Martin Schwidefsky <schwidefsky@de.ibm.com>, 2003.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <sysdep.h>
  17. #include <lowlevellock.h>
  18. #include <pthread.h>
  19. #include <pthreadP.h>
  20. /* Try to acquire read lock for RWLOCK or return after specfied time. */
  21. int
  22. pthread_rwlock_timedrdlock (
  23. pthread_rwlock_t *rwlock,
  24. const struct timespec *abstime)
  25. {
  26. int result = 0;
  27. /* Make sure we are along. */
  28. lll_lock(rwlock->__data.__lock, rwlock->__data.__shared);
  29. while (1)
  30. {
  31. int err;
  32. /* Get the rwlock if there is no writer... */
  33. if (rwlock->__data.__writer == 0
  34. /* ...and if either no writer is waiting or we prefer readers. */
  35. && (!rwlock->__data.__nr_writers_queued
  36. || PTHREAD_RWLOCK_PREFER_READER_P (rwlock)))
  37. {
  38. /* Increment the reader counter. Avoid overflow. */
  39. if (++rwlock->__data.__nr_readers == 0)
  40. {
  41. /* Overflow on number of readers. */
  42. --rwlock->__data.__nr_readers;
  43. result = EAGAIN;
  44. }
  45. break;
  46. }
  47. /* Make sure we are not holding the rwlock as a writer. This is
  48. a deadlock situation we recognize and report. */
  49. if (__builtin_expect (rwlock->__data.__writer
  50. == THREAD_GETMEM (THREAD_SELF, tid), 0))
  51. {
  52. result = EDEADLK;
  53. break;
  54. }
  55. /* Make sure the passed in timeout value is valid. Ideally this
  56. test would be executed once. But since it must not be
  57. performed if we would not block at all simply moving the test
  58. to the front is no option. Replicating all the code is
  59. costly while this test is not. */
  60. if (__builtin_expect (abstime->tv_nsec >= 1000000000
  61. || abstime->tv_nsec < 0, 0))
  62. {
  63. result = EINVAL;
  64. break;
  65. }
  66. /* Get the current time. So far we support only one clock. */
  67. struct timeval tv;
  68. (void) gettimeofday (&tv, NULL);
  69. /* Convert the absolute timeout value to a relative timeout. */
  70. struct timespec rt;
  71. rt.tv_sec = abstime->tv_sec - tv.tv_sec;
  72. rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
  73. if (rt.tv_nsec < 0)
  74. {
  75. rt.tv_nsec += 1000000000;
  76. --rt.tv_sec;
  77. }
  78. /* Did we already time out? */
  79. if (rt.tv_sec < 0)
  80. {
  81. /* Yep, return with an appropriate error. */
  82. result = ETIMEDOUT;
  83. break;
  84. }
  85. /* Remember that we are a reader. */
  86. if (++rwlock->__data.__nr_readers_queued == 0)
  87. {
  88. /* Overflow on number of queued readers. */
  89. --rwlock->__data.__nr_readers_queued;
  90. result = EAGAIN;
  91. break;
  92. }
  93. int waitval = rwlock->__data.__readers_wakeup;
  94. /* Free the lock. */
  95. lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
  96. /* Wait for the writer to finish. */
  97. err = lll_futex_timed_wait (&rwlock->__data.__readers_wakeup,
  98. waitval, &rt, rwlock->__data.__shared);
  99. /* Get the lock. */
  100. lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
  101. --rwlock->__data.__nr_readers_queued;
  102. /* Did the futex call time out? */
  103. if (err == -ETIMEDOUT)
  104. {
  105. /* Yep, report it. */
  106. result = ETIMEDOUT;
  107. break;
  108. }
  109. }
  110. /* We are done, free the lock. */
  111. lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
  112. return result;
  113. }