lowlevellock.c 3.2 KB

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