sem.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Copyright (C) 2005-2022 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Offloading and Multi Processing Library
  4. (libgomp).
  5. Libgomp is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. /* This is the default POSIX 1003.1b implementation of a semaphore
  21. synchronization mechanism for libgomp. This type is private to
  22. the library.
  23. This is a bit heavy weight for what we need, in that we're not
  24. interested in sem_wait as a cancelation point, but it's not too
  25. bad for a default. */
  26. #include "libgomp.h"
  27. #ifdef HAVE_BROKEN_POSIX_SEMAPHORES
  28. #include <stdlib.h>
  29. void gomp_sem_init (gomp_sem_t *sem, int value)
  30. {
  31. int ret;
  32. ret = pthread_mutex_init (&sem->mutex, NULL);
  33. if (ret)
  34. return;
  35. ret = pthread_cond_init (&sem->cond, NULL);
  36. if (ret)
  37. return;
  38. sem->value = value;
  39. }
  40. void gomp_sem_wait (gomp_sem_t *sem)
  41. {
  42. int ret;
  43. ret = pthread_mutex_lock (&sem->mutex);
  44. if (ret)
  45. return;
  46. if (sem->value > 0)
  47. {
  48. sem->value--;
  49. ret = pthread_mutex_unlock (&sem->mutex);
  50. return;
  51. }
  52. while (sem->value <= 0)
  53. {
  54. ret = pthread_cond_wait (&sem->cond, &sem->mutex);
  55. if (ret)
  56. {
  57. pthread_mutex_unlock (&sem->mutex);
  58. return;
  59. }
  60. }
  61. sem->value--;
  62. ret = pthread_mutex_unlock (&sem->mutex);
  63. return;
  64. }
  65. void gomp_sem_post (gomp_sem_t *sem)
  66. {
  67. int ret;
  68. ret = pthread_mutex_lock (&sem->mutex);
  69. if (ret)
  70. return;
  71. sem->value++;
  72. ret = pthread_mutex_unlock (&sem->mutex);
  73. if (ret)
  74. return;
  75. ret = pthread_cond_signal (&sem->cond);
  76. return;
  77. }
  78. void gomp_sem_destroy (gomp_sem_t *sem)
  79. {
  80. int ret;
  81. ret = pthread_mutex_destroy (&sem->mutex);
  82. if (ret)
  83. return;
  84. ret = pthread_cond_destroy (&sem->cond);
  85. return;
  86. }
  87. int gomp_sem_getcount (gomp_sem_t *sem)
  88. {
  89. int ret, count;
  90. ret = pthread_mutex_lock (&sem->mutex);
  91. if (ret)
  92. return -1;
  93. count = sem->value;
  94. ret = pthread_mutex_unlock (&sem->mutex);
  95. if (ret)
  96. return -1;
  97. if (count < 0)
  98. return -1;
  99. return count;
  100. }
  101. #else /* HAVE_BROKEN_POSIX_SEMAPHORES */
  102. void
  103. gomp_sem_wait (gomp_sem_t *sem)
  104. {
  105. /* With POSIX, the wait can be canceled by signals. We don't want that.
  106. It is expected that the return value here is -1 and errno is EINTR. */
  107. while (sem_wait (sem) != 0)
  108. continue;
  109. }
  110. #endif