lowlevellock.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* low level locking for pthread library. Generic futex-using version.
  2. Copyright (C) 2003 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Paul Mackerras <paulus@au.ibm.com>, 2003.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <errno.h>
  18. #include <sysdep.h>
  19. #include <lowlevellock.h>
  20. #include <sys/time.h>
  21. void
  22. __lll_lock_wait (int *futex)
  23. {
  24. do
  25. {
  26. int oldval = atomic_compare_and_exchange_val_acq (futex, 2, 1);
  27. if (oldval != 0)
  28. lll_futex_wait (futex, 2);
  29. }
  30. while (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0);
  31. }
  32. int
  33. __lll_timedlock_wait (int *futex, const struct timespec *abstime)
  34. {
  35. /* Reject invalid timeouts. */
  36. if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
  37. return EINVAL;
  38. do
  39. {
  40. struct timeval tv;
  41. struct timespec rt;
  42. /* Get the current time. */
  43. (void) gettimeofday (&tv, NULL);
  44. /* Compute relative timeout. */
  45. rt.tv_sec = abstime->tv_sec - tv.tv_sec;
  46. rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
  47. if (rt.tv_nsec < 0)
  48. {
  49. rt.tv_nsec += 1000000000;
  50. --rt.tv_sec;
  51. }
  52. /* Already timed out? */
  53. if (rt.tv_sec < 0)
  54. return ETIMEDOUT;
  55. /* Wait. */
  56. int oldval = atomic_compare_and_exchange_val_acq (futex, 2, 1);
  57. if (oldval != 0)
  58. lll_futex_timed_wait (futex, 2, &rt);
  59. }
  60. while (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0);
  61. return 0;
  62. }
  63. /* These don't get included in libc.so */
  64. #ifdef IS_IN_libpthread
  65. int
  66. lll_unlock_wake_cb (int *futex)
  67. {
  68. int val = atomic_exchange_rel (futex, 0);
  69. if (__builtin_expect (val > 1, 0))
  70. lll_futex_wake (futex, 1);
  71. return 0;
  72. }
  73. int
  74. __lll_timedwait_tid (int *tidp, const struct timespec *abstime)
  75. {
  76. int tid;
  77. if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
  78. return EINVAL;
  79. /* Repeat until thread terminated. */
  80. while ((tid = *tidp) != 0)
  81. {
  82. struct timeval tv;
  83. struct timespec rt;
  84. /* Get the current time. */
  85. (void) gettimeofday (&tv, NULL);
  86. /* Compute relative timeout. */
  87. rt.tv_sec = abstime->tv_sec - tv.tv_sec;
  88. rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
  89. if (rt.tv_nsec < 0)
  90. {
  91. rt.tv_nsec += 1000000000;
  92. --rt.tv_sec;
  93. }
  94. /* Already timed out? */
  95. if (rt.tv_sec < 0)
  96. return ETIMEDOUT;
  97. /* Wait until thread terminates. */
  98. if (lll_futex_timed_wait (tidp, tid, &rt) == -ETIMEDOUT)
  99. return ETIMEDOUT;
  100. }
  101. return 0;
  102. }
  103. #endif