pthread_rwlock_timedwrlock.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 write lock for RWLOCK or return after specfied time. */
  21. int
  22. pthread_rwlock_timedwrlock (
  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 and no reader. */
  33. if (rwlock->__data.__writer == 0 && rwlock->__data.__nr_readers == 0)
  34. {
  35. /* Mark self as writer. */
  36. rwlock->__data.__writer = THREAD_GETMEM (THREAD_SELF, tid);
  37. break;
  38. }
  39. /* Make sure we are not holding the rwlock as a writer. This is
  40. a deadlock situation we recognize and report. */
  41. if (__builtin_expect (rwlock->__data.__writer
  42. == THREAD_GETMEM (THREAD_SELF, tid), 0))
  43. {
  44. result = EDEADLK;
  45. break;
  46. }
  47. /* Make sure the passed in timeout value is valid. Ideally this
  48. test would be executed once. But since it must not be
  49. performed if we would not block at all simply moving the test
  50. to the front is no option. Replicating all the code is
  51. costly while this test is not. */
  52. if (__builtin_expect (abstime->tv_nsec >= 1000000000
  53. || abstime->tv_nsec < 0, 0))
  54. {
  55. result = EINVAL;
  56. break;
  57. }
  58. /* Get the current time. So far we support only one clock. */
  59. struct timeval tv;
  60. (void) gettimeofday (&tv, NULL);
  61. /* Convert the absolute timeout value to a relative timeout. */
  62. struct timespec rt;
  63. rt.tv_sec = abstime->tv_sec - tv.tv_sec;
  64. rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
  65. if (rt.tv_nsec < 0)
  66. {
  67. rt.tv_nsec += 1000000000;
  68. --rt.tv_sec;
  69. }
  70. /* Did we already time out? */
  71. if (rt.tv_sec < 0)
  72. {
  73. result = ETIMEDOUT;
  74. break;
  75. }
  76. /* Remember that we are a writer. */
  77. if (++rwlock->__data.__nr_writers_queued == 0)
  78. {
  79. /* Overflow on number of queued writers. */
  80. --rwlock->__data.__nr_writers_queued;
  81. result = EAGAIN;
  82. break;
  83. }
  84. int waitval = rwlock->__data.__writer_wakeup;
  85. /* Free the lock. */
  86. lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
  87. /* Wait for the writer or reader(s) to finish. */
  88. err = lll_futex_timed_wait (&rwlock->__data.__writer_wakeup,
  89. waitval, &rt, rwlock->__data.__shared);
  90. /* Get the lock. */
  91. lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
  92. /* To start over again, remove the thread from the writer list. */
  93. --rwlock->__data.__nr_writers_queued;
  94. /* Did the futex call time out? */
  95. if (err == -ETIMEDOUT)
  96. {
  97. result = ETIMEDOUT;
  98. break;
  99. }
  100. }
  101. /* We are done, free the lock. */
  102. lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
  103. return result;
  104. }