sem_timedwait.c 2.7 KB

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