lowlevellock.c 3.1 KB

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