lowlevellock.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* low level locking for pthread library. Generic futex-using version.
  2. Copyright (C) 2003, 2007 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, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <errno.h>
  17. #include <sysdep.h>
  18. #include <lowlevellock.h>
  19. #include <sys/time.h>
  20. #include <tls.h>
  21. #include <tcb-offsets.h>
  22. void
  23. #ifndef IS_IN_libpthread
  24. weak_function
  25. #endif
  26. __lll_lock_wait_private (int *futex)
  27. {
  28. if (*futex == 2)
  29. lll_futex_wait (futex, 2, LLL_PRIVATE);
  30. while (atomic_exchange_acq (futex, 2) != 0)
  31. lll_futex_wait (futex, 2, LLL_PRIVATE);
  32. }
  33. /* These functions don't get included in libc.so */
  34. #ifdef IS_IN_libpthread
  35. void
  36. __lll_lock_wait (int *futex, int private)
  37. {
  38. if (*futex == 2)
  39. lll_futex_wait (futex, 2, private);
  40. while (atomic_exchange_acq (futex, 2) != 0)
  41. lll_futex_wait (futex, 2, private);
  42. }
  43. int
  44. __lll_timedlock_wait (int *futex, const struct timespec *abstime, int private)
  45. {
  46. /* Reject invalid timeouts. */
  47. if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
  48. return EINVAL;
  49. /* Try locking. */
  50. while (atomic_exchange_acq (futex, 2) != 0)
  51. {
  52. struct timeval tv;
  53. /* Get the current time. */
  54. (void) gettimeofday (&tv, NULL);
  55. /* Compute relative timeout. */
  56. struct timespec rt;
  57. rt.tv_sec = abstime->tv_sec - tv.tv_sec;
  58. rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
  59. if (rt.tv_nsec < 0)
  60. {
  61. rt.tv_nsec += 1000000000;
  62. --rt.tv_sec;
  63. }
  64. if (rt.tv_sec < 0)
  65. return ETIMEDOUT;
  66. /* Wait. */
  67. lll_futex_timed_wait (futex, 2, &rt, private);
  68. }
  69. return 0;
  70. }
  71. int
  72. __lll_timedwait_tid (int *tidp, const struct timespec *abstime)
  73. {
  74. int tid;
  75. if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
  76. return EINVAL;
  77. /* Repeat until thread terminated. */
  78. while ((tid = *tidp) != 0)
  79. {
  80. struct timeval tv;
  81. struct timespec rt;
  82. /* Get the current time. */
  83. (void) __gettimeofday (&tv, NULL);
  84. /* Compute relative timeout. */
  85. rt.tv_sec = abstime->tv_sec - tv.tv_sec;
  86. rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
  87. if (rt.tv_nsec < 0)
  88. {
  89. rt.tv_nsec += 1000000000;
  90. --rt.tv_sec;
  91. }
  92. /* Already timed out? */
  93. if (rt.tv_sec < 0)
  94. return ETIMEDOUT;
  95. /* Wait until thread terminates. The kernel so far does not use
  96. the private futex operations for this. */
  97. if (lll_futex_timed_wait (tidp, tid, &rt, LLL_SHARED) == -ETIMEDOUT)
  98. return ETIMEDOUT;
  99. }
  100. return 0;
  101. }
  102. #endif