sem_wait.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* sem_wait -- 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. void
  23. attribute_hidden
  24. __sem_wait_cleanup (void *arg)
  25. {
  26. struct sparc_new_sem *isem = (struct sparc_new_sem *) arg;
  27. if (__atomic_is_v9)
  28. atomic_decrement (&isem->nwaiters);
  29. else
  30. {
  31. __sparc32_atomic_do_lock24 (&isem->lock);
  32. isem->nwaiters--;
  33. __sparc32_atomic_do_unlock24 (&isem->lock);
  34. }
  35. }
  36. int
  37. sem_wait (sem_t *sem)
  38. {
  39. struct sparc_new_sem *isem = (struct sparc_new_sem *) sem;
  40. int err;
  41. int val;
  42. if (__atomic_is_v9)
  43. val = atomic_decrement_if_positive (&isem->value);
  44. else
  45. {
  46. __sparc32_atomic_do_lock24 (&isem->lock);
  47. val = isem->value;
  48. if (val > 0)
  49. isem->value = val - 1;
  50. else
  51. isem->nwaiters++;
  52. __sparc32_atomic_do_unlock24 (&isem->lock);
  53. }
  54. if (val > 0)
  55. return 0;
  56. if (__atomic_is_v9)
  57. atomic_increment (&isem->nwaiters);
  58. else
  59. /* Already done above while still holding isem->lock. */;
  60. pthread_cleanup_push (__sem_wait_cleanup, isem);
  61. while (1)
  62. {
  63. /* Enable asynchronous cancellation. Required by the standard. */
  64. int oldtype = __pthread_enable_asynccancel ();
  65. err = lll_futex_wait (&isem->value, 0,
  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_is_v9)
  76. val = atomic_decrement_if_positive (&isem->value);
  77. else
  78. {
  79. __sparc32_atomic_do_lock24 (&isem->lock);
  80. val = isem->value;
  81. if (val > 0)
  82. isem->value = val - 1;
  83. __sparc32_atomic_do_unlock24 (&isem->lock);
  84. }
  85. if (val > 0)
  86. {
  87. err = 0;
  88. break;
  89. }
  90. }
  91. pthread_cleanup_pop (0);
  92. if (__atomic_is_v9)
  93. atomic_decrement (&isem->nwaiters);
  94. else
  95. {
  96. __sparc32_atomic_do_lock24 (&isem->lock);
  97. isem->nwaiters--;
  98. __sparc32_atomic_do_unlock24 (&isem->lock);
  99. }
  100. return err;
  101. }