lowlevelrobustlock.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Copyright (C) 2006, 2007 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Jakub Jelinek <jakub@redhat.com>, 2006.
  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 <sys/time.h>
  19. #include <pthreadP.h>
  20. int
  21. __lll_robust_lock_wait (int *futex, int private)
  22. {
  23. int oldval = *futex;
  24. int tid = THREAD_GETMEM (THREAD_SELF, tid);
  25. /* If the futex changed meanwhile try locking again. */
  26. if (oldval == 0)
  27. goto try;
  28. do
  29. {
  30. if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
  31. return oldval;
  32. int newval = oldval | FUTEX_WAITERS;
  33. if (oldval != newval
  34. && atomic_compare_and_exchange_bool_acq (futex, newval, oldval))
  35. continue;
  36. lll_futex_wait (futex, newval, private);
  37. try:
  38. ;
  39. }
  40. while ((oldval = atomic_compare_and_exchange_val_acq (futex,
  41. tid | FUTEX_WAITERS,
  42. 0)) != 0);
  43. return 0;
  44. }
  45. int
  46. __lll_robust_timedlock_wait (int *futex, const struct timespec *abstime,
  47. int private)
  48. {
  49. /* Reject invalid timeouts. */
  50. if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
  51. return EINVAL;
  52. int tid = THREAD_GETMEM (THREAD_SELF, tid);
  53. int oldval = *futex;
  54. /* If the futex changed meanwhile try locking again. */
  55. if (oldval == 0)
  56. goto try;
  57. do
  58. {
  59. struct timeval tv;
  60. struct timespec rt;
  61. /* Get the current time. */
  62. (void) __gettimeofday (&tv, NULL);
  63. /* Compute relative timeout. */
  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. /* Already timed out? */
  72. if (rt.tv_sec < 0)
  73. return ETIMEDOUT;
  74. /* Wait. */
  75. if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
  76. return oldval;
  77. int newval = oldval | FUTEX_WAITERS;
  78. if (oldval != newval
  79. && atomic_compare_and_exchange_bool_acq (futex, newval, oldval))
  80. continue;
  81. lll_futex_timed_wait (futex, newval, &rt, private);
  82. try:
  83. ;
  84. }
  85. while ((oldval = atomic_compare_and_exchange_val_acq (futex,
  86. tid | FUTEX_WAITERS,
  87. 0)) != 0);
  88. return 0;
  89. }