lowlevellock.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* low level locking for pthread library. Generic futex-using version.
  2. Copyright (C) 2003-2013 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library. If not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <sysdep.h>
  17. #include <lowlevellock.h>
  18. #include <sys/time.h>
  19. void
  20. __lll_lock_wait_private (int *futex)
  21. {
  22. do
  23. {
  24. int oldval = atomic_compare_and_exchange_val_acq (futex, 2, 1);
  25. if (oldval != 0)
  26. lll_futex_wait (futex, 2, LLL_PRIVATE);
  27. }
  28. while (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0);
  29. }
  30. /* These functions don't get included in libc.so */
  31. #ifdef IS_IN_libpthread
  32. void
  33. __lll_lock_wait (int *futex, int private)
  34. {
  35. do
  36. {
  37. int oldval = atomic_compare_and_exchange_val_acq (futex, 2, 1);
  38. if (oldval != 0)
  39. lll_futex_wait (futex, 2, private);
  40. }
  41. while (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0);
  42. }
  43. int
  44. __lll_timedlock_wait (int *futex, const struct timespec *abstime, int private)
  45. {
  46. struct timespec rt;
  47. /* Reject invalid timeouts. */
  48. if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
  49. return EINVAL;
  50. /* Upgrade the lock. */
  51. if (atomic_exchange_acq (futex, 2) == 0)
  52. return 0;
  53. do
  54. {
  55. struct timeval tv;
  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. // XYZ: Lost the lock to check whether it was private.
  70. lll_futex_timed_wait (futex, 2, &rt, private);
  71. }
  72. while (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0);
  73. return 0;
  74. }
  75. int
  76. __lll_timedwait_tid (int *tidp, const struct timespec *abstime)
  77. {
  78. int tid;
  79. if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
  80. return EINVAL;
  81. /* Repeat until thread terminated. */
  82. while ((tid = *tidp) != 0)
  83. {
  84. struct timeval tv;
  85. struct timespec rt;
  86. /* Get the current time. */
  87. (void) __gettimeofday (&tv, NULL);
  88. /* Compute relative timeout. */
  89. rt.tv_sec = abstime->tv_sec - tv.tv_sec;
  90. rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
  91. if (rt.tv_nsec < 0)
  92. {
  93. rt.tv_nsec += 1000000000;
  94. --rt.tv_sec;
  95. }
  96. /* Already timed out? */
  97. if (rt.tv_sec < 0)
  98. return ETIMEDOUT;
  99. /* Wait until thread terminates. */
  100. // XYZ: Lost the lock to check whether it was private.
  101. if (lll_futex_timed_wait (tidp, tid, &rt, LLL_SHARED) == -ETIMEDOUT)
  102. return ETIMEDOUT;
  103. }
  104. return 0;
  105. }
  106. #endif