lowlevellock.c 3.4 KB

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