lowlevellock.c 3.3 KB

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