sem_wait.c 3.0 KB

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