sem_timedwait.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* sem_timedwait -- wait on a semaphore. Generic futex-using version.
  2. Copyright (C) 2003, 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 new_sem *isem = (struct new_sem *) sem;
  27. int err;
  28. if (atomic_decrement_if_positive (&isem->value) > 0)
  29. return 0;
  30. if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
  31. {
  32. __set_errno (EINVAL);
  33. return -1;
  34. }
  35. atomic_increment (&isem->nwaiters);
  36. pthread_cleanup_push (__sem_wait_cleanup, isem);
  37. while (1)
  38. {
  39. struct timeval tv;
  40. struct timespec rt;
  41. int sec, nsec;
  42. /* Get the current time. */
  43. __gettimeofday (&tv, NULL);
  44. /* Compute relative timeout. */
  45. sec = abstime->tv_sec - tv.tv_sec;
  46. nsec = abstime->tv_nsec - tv.tv_usec * 1000;
  47. if (nsec < 0)
  48. {
  49. nsec += 1000000000;
  50. --sec;
  51. }
  52. /* Already timed out? */
  53. err = -ETIMEDOUT;
  54. if (sec < 0)
  55. {
  56. __set_errno (ETIMEDOUT);
  57. err = -1;
  58. break;
  59. }
  60. /* Do wait. */
  61. rt.tv_sec = sec;
  62. rt.tv_nsec = nsec;
  63. /* Enable asynchronous cancellation. Required by the standard. */
  64. int oldtype = __pthread_enable_asynccancel ();
  65. err = lll_futex_timed_wait (&isem->value, 0, &rt,
  66. isem->private ^ FUTEX_PRIVATE_FLAG);
  67. /* Disable asynchronous cancellation. */
  68. __pthread_disable_asynccancel (oldtype);
  69. if (err != 0 && err != -EWOULDBLOCK)
  70. {
  71. __set_errno (-err);
  72. err = -1;
  73. break;
  74. }
  75. if (atomic_decrement_if_positive (&isem->value) > 0)
  76. {
  77. err = 0;
  78. break;
  79. }
  80. }
  81. pthread_cleanup_pop (0);
  82. atomic_decrement (&isem->nwaiters);
  83. return err;
  84. }