sem_timedwait.c 3.5 KB

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