sem_wait.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* sem_wait -- wait on a semaphore. Generic futex-using version.
  2. Copyright (C) 2003-2019 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 <lowlevellock.h> /* lll_futex* used by the old code. */
  17. #include "sem_waitcommon.c"
  18. int
  19. __new_sem_wait (sem_t *sem)
  20. {
  21. /* We need to check whether we need to act upon a cancellation request here
  22. because POSIX specifies that cancellation points "shall occur" in
  23. sem_wait and sem_timedwait, which also means that they need to check
  24. this regardless whether they block or not (unlike "may occur"
  25. functions). See the POSIX Rationale for this requirement: Section
  26. "Thread Cancellation Overview" [1] and austin group issue #1076 [2]
  27. for thoughs on why this may be a suboptimal design.
  28. [1] http://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xsh_chap02.html
  29. [2] http://austingroupbugs.net/view.php?id=1076 for thoughts on why this
  30. */
  31. __pthread_testcancel ();
  32. if (__new_sem_wait_fast ((struct new_sem *) sem, 0) == 0)
  33. return 0;
  34. else
  35. return __new_sem_wait_slow((struct new_sem *) sem, NULL);
  36. }
  37. versioned_symbol (libpthread, __new_sem_wait, sem_wait, GLIBC_2_1);
  38. #if SHLIB_COMPAT (libpthread, GLIBC_2_0, GLIBC_2_1)
  39. int
  40. attribute_compat_text_section
  41. __old_sem_wait (sem_t *sem)
  42. {
  43. int *futex = (int *) sem;
  44. int err;
  45. do
  46. {
  47. if (atomic_decrement_if_positive (futex) > 0)
  48. return 0;
  49. /* Always assume the semaphore is shared. */
  50. err = lll_futex_wait_cancel (futex, 0, LLL_SHARED);
  51. }
  52. while (err == 0 || err == -EWOULDBLOCK);
  53. __set_errno (-err);
  54. return -1;
  55. }
  56. compat_symbol (libpthread, __old_sem_wait, sem_wait, GLIBC_2_0);
  57. #endif
  58. int
  59. __new_sem_trywait (sem_t *sem)
  60. {
  61. /* We must not fail spuriously, so require a definitive result even if this
  62. may lead to a long execution time. */
  63. if (__new_sem_wait_fast ((struct new_sem *) sem, 1) == 0)
  64. return 0;
  65. __set_errno (EAGAIN);
  66. return -1;
  67. }
  68. versioned_symbol (libpthread, __new_sem_trywait, sem_trywait, GLIBC_2_1);
  69. #if SHLIB_COMPAT (libpthread, GLIBC_2_0, GLIBC_2_1)
  70. int
  71. __old_sem_trywait (sem_t *sem)
  72. {
  73. int *futex = (int *) sem;
  74. int val;
  75. if (*futex > 0)
  76. {
  77. val = atomic_decrement_if_positive (futex);
  78. if (val > 0)
  79. return 0;
  80. }
  81. __set_errno (EAGAIN);
  82. return -1;
  83. }
  84. compat_symbol (libpthread, __old_sem_trywait, sem_trywait, GLIBC_2_0);
  85. #endif