lowlevellock.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* low level locking for pthread library. Generic futex-using version.
  2. Copyright (C) 2003, 2005 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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <errno.h>
  17. #include <sysdep.h>
  18. #include <lowlevellock.h>
  19. #include <sys/time.h>
  20. int
  21. __lll_timedlock_wait (int *futex, const struct timespec *abstime)
  22. {
  23. struct timespec rt;
  24. /* Reject invalid timeouts. */
  25. if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
  26. return EINVAL;
  27. /* Upgrade the lock. */
  28. if (atomic_exchange_acq (futex, 2) == 0)
  29. return 0;
  30. do
  31. {
  32. struct timeval tv;
  33. /* Get the current time. */
  34. (void) gettimeofday (&tv, NULL);
  35. /* Compute relative timeout. */
  36. rt.tv_sec = abstime->tv_sec - tv.tv_sec;
  37. rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
  38. if (rt.tv_nsec < 0)
  39. {
  40. rt.tv_nsec += 1000000000;
  41. --rt.tv_sec;
  42. }
  43. /* Already timed out? */
  44. if (rt.tv_sec < 0)
  45. return ETIMEDOUT;
  46. lll_futex_timed_wait (futex, 2, &rt);
  47. }
  48. while (atomic_exchange_acq (futex, 2) != 0);
  49. return 0;
  50. }
  51. /* These don't get included in libc.so */
  52. #ifdef IS_IN_libpthread
  53. int
  54. lll_unlock_wake_cb (int *futex)
  55. {
  56. int val = atomic_exchange_rel (futex, 0);
  57. if (__builtin_expect (val > 1, 0))
  58. lll_futex_wake (futex, 1);
  59. return 0;
  60. }
  61. int
  62. __lll_timedwait_tid (int *tidp, const struct timespec *abstime)
  63. {
  64. int tid;
  65. if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
  66. return EINVAL;
  67. /* Repeat until thread terminated. */
  68. while ((tid = *tidp) != 0)
  69. {
  70. struct timeval tv;
  71. struct timespec rt;
  72. /* Get the current time. */
  73. (void) gettimeofday (&tv, NULL);
  74. /* Compute relative timeout. */
  75. rt.tv_sec = abstime->tv_sec - tv.tv_sec;
  76. rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
  77. if (rt.tv_nsec < 0)
  78. {
  79. rt.tv_nsec += 1000000000;
  80. --rt.tv_sec;
  81. }
  82. /* Already timed out? */
  83. if (rt.tv_sec < 0)
  84. return ETIMEDOUT;
  85. /* Wait until thread terminates. */
  86. if (lll_futex_timed_wait (tidp, tid, &rt) == -ETIMEDOUT)
  87. return ETIMEDOUT;
  88. }
  89. return 0;
  90. }
  91. #endif