sem_timedwait.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* sem_timedwait -- wait on a semaphore. 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 <internaltypes.h>
  20. #include <semaphore.h>
  21. #include <pthreadP.h>
  22. extern void __sem_wait_cleanup (void *arg) attribute_hidden;
  23. int
  24. sem_timedwait (sem_t *sem, const struct timespec *abstime)
  25. {
  26. struct sparc_new_sem *isem = (struct sparc_new_sem *) sem;
  27. int err;
  28. int val;
  29. if (__atomic_is_v9)
  30. val = atomic_decrement_if_positive (&isem->value);
  31. else
  32. {
  33. __sparc32_atomic_do_lock24 (&isem->lock);
  34. val = isem->value;
  35. if (val > 0)
  36. isem->value = val - 1;
  37. __sparc32_atomic_do_unlock24 (&isem->lock);
  38. }
  39. if (val > 0)
  40. return 0;
  41. if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
  42. {
  43. __set_errno (EINVAL);
  44. return -1;
  45. }
  46. if (__atomic_is_v9)
  47. atomic_increment (&isem->nwaiters);
  48. else
  49. {
  50. __sparc32_atomic_do_lock24 (&isem->lock);
  51. isem->nwaiters++;
  52. __sparc32_atomic_do_unlock24 (&isem->lock);
  53. }
  54. pthread_cleanup_push (__sem_wait_cleanup, isem);
  55. while (1)
  56. {
  57. struct timeval tv;
  58. struct timespec rt;
  59. int sec, nsec;
  60. /* Get the current time. */
  61. __gettimeofday (&tv, NULL);
  62. /* Compute relative timeout. */
  63. sec = abstime->tv_sec - tv.tv_sec;
  64. nsec = abstime->tv_nsec - tv.tv_usec * 1000;
  65. if (nsec < 0)
  66. {
  67. nsec += 1000000000;
  68. --sec;
  69. }
  70. /* Already timed out? */
  71. err = -ETIMEDOUT;
  72. if (sec < 0)
  73. {
  74. __set_errno (ETIMEDOUT);
  75. err = -1;
  76. break;
  77. }
  78. /* Do wait. */
  79. rt.tv_sec = sec;
  80. rt.tv_nsec = nsec;
  81. /* Enable asynchronous cancellation. Required by the standard. */
  82. int oldtype = __pthread_enable_asynccancel ();
  83. err = lll_futex_timed_wait (&isem->value, 0, &rt,
  84. isem->private ^ FUTEX_PRIVATE_FLAG);
  85. /* Disable asynchronous cancellation. */
  86. __pthread_disable_asynccancel (oldtype);
  87. if (err != 0 && err != -EWOULDBLOCK)
  88. {
  89. __set_errno (-err);
  90. err = -1;
  91. break;
  92. }
  93. if (__atomic_is_v9)
  94. val = atomic_decrement_if_positive (&isem->value);
  95. else
  96. {
  97. __sparc32_atomic_do_lock24 (&isem->lock);
  98. val = isem->value;
  99. if (val > 0)
  100. isem->value = val - 1;
  101. __sparc32_atomic_do_unlock24 (&isem->lock);
  102. }
  103. if (val > 0)
  104. {
  105. err = 0;
  106. break;
  107. }
  108. }
  109. pthread_cleanup_pop (0);
  110. if (__atomic_is_v9)
  111. atomic_decrement (&isem->nwaiters);
  112. else
  113. {
  114. __sparc32_atomic_do_lock24 (&isem->lock);
  115. isem->nwaiters--;
  116. __sparc32_atomic_do_unlock24 (&isem->lock);
  117. }
  118. return err;
  119. }