lowlevellock.c 3.4 KB

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