lowlevelrobustlock.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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, 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 <sys/time.h>
  20. #include <pthreadP.h>
  21. int
  22. __lll_robust_lock_wait (int *futex, int private)
  23. {
  24. int oldval = *futex;
  25. int tid = THREAD_GETMEM (THREAD_SELF, tid);
  26. /* If the futex changed meanwhile try locking again. */
  27. if (oldval == 0)
  28. goto try;
  29. do
  30. {
  31. if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
  32. return oldval;
  33. int newval = oldval | FUTEX_WAITERS;
  34. if (oldval != newval
  35. && atomic_compare_and_exchange_bool_acq (futex, newval, oldval))
  36. continue;
  37. lll_futex_wait (futex, newval, private);
  38. try:
  39. ;
  40. }
  41. while ((oldval = atomic_compare_and_exchange_val_acq (futex,
  42. tid | FUTEX_WAITERS,
  43. 0)) != 0);
  44. return 0;
  45. }
  46. int
  47. __lll_robust_timedlock_wait (int *futex, const struct timespec *abstime,
  48. int private)
  49. {
  50. /* Reject invalid timeouts. */
  51. if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
  52. return EINVAL;
  53. int tid = THREAD_GETMEM (THREAD_SELF, tid);
  54. int oldval = *futex;
  55. /* If the futex changed meanwhile try locking again. */
  56. if (oldval == 0)
  57. goto try;
  58. do
  59. {
  60. struct timeval tv;
  61. struct timespec rt;
  62. /* Get the current time. */
  63. (void) __gettimeofday (&tv, NULL);
  64. /* Compute relative timeout. */
  65. rt.tv_sec = abstime->tv_sec - tv.tv_sec;
  66. rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
  67. if (rt.tv_nsec < 0)
  68. {
  69. rt.tv_nsec += 1000000000;
  70. --rt.tv_sec;
  71. }
  72. /* Already timed out? */
  73. if (rt.tv_sec < 0)
  74. return ETIMEDOUT;
  75. /* Wait. */
  76. if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
  77. return oldval;
  78. int newval = oldval | FUTEX_WAITERS;
  79. if (oldval != newval
  80. && atomic_compare_and_exchange_bool_acq (futex, newval, oldval))
  81. continue;
  82. lll_futex_timed_wait (futex, newval, &rt, private);
  83. try:
  84. ;
  85. }
  86. while ((oldval = atomic_compare_and_exchange_val_acq (futex,
  87. tid | FUTEX_WAITERS,
  88. 0)) != 0);
  89. return 0;
  90. }