sem.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* Copyright (C) 2015-2022 Free Software Foundation, Inc.
  2. Contributed by Alexander Monakov <amonakov@ispras.ru>
  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 an NVPTX specific implementation of a semaphore synchronization
  21. mechanism for libgomp. This type is private to the library. This
  22. semaphore implementation uses atomic instructions and busy waiting. */
  23. #ifndef GOMP_SEM_H
  24. #define GOMP_SEM_H 1
  25. typedef int gomp_sem_t;
  26. static inline void
  27. gomp_sem_init (gomp_sem_t *sem, int value)
  28. {
  29. *sem = value;
  30. }
  31. static inline void
  32. gomp_sem_destroy (gomp_sem_t *sem)
  33. {
  34. }
  35. static inline void
  36. gomp_sem_wait (gomp_sem_t *sem)
  37. {
  38. int count = __atomic_load_n (sem, MEMMODEL_ACQUIRE);
  39. for (;;)
  40. {
  41. while (count == 0)
  42. count = __atomic_load_n (sem, MEMMODEL_ACQUIRE);
  43. if (__atomic_compare_exchange_n (sem, &count, count - 1, false,
  44. MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
  45. return;
  46. }
  47. }
  48. static inline void
  49. gomp_sem_post (gomp_sem_t *sem)
  50. {
  51. (void) __atomic_add_fetch (sem, 1, MEMMODEL_RELEASE);
  52. }
  53. static inline int
  54. gomp_sem_getcount (gomp_sem_t *sem)
  55. {
  56. int count = __atomic_load_n (sem, MEMMODEL_RELAXED);
  57. if (count < 0)
  58. return -1;
  59. return count;
  60. }
  61. #endif /* GOMP_SEM_H */