sem.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #ifndef GOMP_SEM_H
  27. #define GOMP_SEM_H 1
  28. #ifdef HAVE_ATTRIBUTE_VISIBILITY
  29. # pragma GCC visibility push(default)
  30. #endif
  31. #include <semaphore.h>
  32. #ifdef HAVE_ATTRIBUTE_VISIBILITY
  33. # pragma GCC visibility pop
  34. #endif
  35. #ifdef HAVE_BROKEN_POSIX_SEMAPHORES
  36. #include <pthread.h>
  37. struct gomp_sem
  38. {
  39. pthread_mutex_t mutex;
  40. pthread_cond_t cond;
  41. int value;
  42. };
  43. typedef struct gomp_sem gomp_sem_t;
  44. extern void gomp_sem_init (gomp_sem_t *sem, int value);
  45. extern void gomp_sem_wait (gomp_sem_t *sem);
  46. extern void gomp_sem_post (gomp_sem_t *sem);
  47. extern void gomp_sem_destroy (gomp_sem_t *sem);
  48. extern int gomp_sem_getcount (gomp_sem_t *sem);
  49. #else /* HAVE_BROKEN_POSIX_SEMAPHORES */
  50. typedef sem_t gomp_sem_t;
  51. static inline void gomp_sem_init (gomp_sem_t *sem, int value)
  52. {
  53. sem_init (sem, 0, value);
  54. }
  55. extern void gomp_sem_wait (gomp_sem_t *sem);
  56. static inline void gomp_sem_post (gomp_sem_t *sem)
  57. {
  58. sem_post (sem);
  59. }
  60. static inline void gomp_sem_destroy (gomp_sem_t *sem)
  61. {
  62. sem_destroy (sem);
  63. }
  64. static inline int gomp_sem_getcount (gomp_sem_t *sem)
  65. {
  66. int val;
  67. if (sem_getvalue (sem, &val) < 0)
  68. return -1;
  69. return val;
  70. }
  71. #endif /* doesn't HAVE_BROKEN_POSIX_SEMAPHORES */
  72. #endif /* GOMP_SEM_H */