pthread_rwlock_timedwrlock.c 3.7 KB

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