sem.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* Copyright (C) 2005-2015 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 a Linux specific implementation of a semaphore synchronization
  21. mechanism for libgomp. This type is private to the library. This
  22. counting semaphore implementation uses atomic instructions and the
  23. futex syscall, and a single 32-bit int to store semaphore state.
  24. The low 31 bits are the count, the top bit is a flag set when some
  25. threads may be waiting. */
  26. #ifndef GOMP_SEM_H
  27. #define GOMP_SEM_H 1
  28. #include <limits.h> /* For INT_MIN */
  29. typedef int gomp_sem_t;
  30. #define SEM_WAIT INT_MIN
  31. #define SEM_INC 1
  32. extern void gomp_sem_wait_slow (gomp_sem_t *, int);
  33. extern void gomp_sem_post_slow (gomp_sem_t *);
  34. static inline void
  35. gomp_sem_init (gomp_sem_t *sem, int value)
  36. {
  37. *sem = value * SEM_INC;
  38. }
  39. static inline void
  40. gomp_sem_destroy (gomp_sem_t *sem)
  41. {
  42. }
  43. static inline void
  44. gomp_sem_wait (gomp_sem_t *sem)
  45. {
  46. int count = *sem;
  47. while ((count & ~SEM_WAIT) != 0)
  48. if (__atomic_compare_exchange_n (sem, &count, count - SEM_INC, true,
  49. MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
  50. return;
  51. gomp_sem_wait_slow (sem, count);
  52. }
  53. static inline void
  54. gomp_sem_post (gomp_sem_t *sem)
  55. {
  56. int count = *sem;
  57. /* Clear SEM_WAIT here so that if there are no more waiting threads
  58. we transition back to the uncontended state that does not make
  59. futex syscalls. If there are waiting threads then when one is
  60. awoken it will set SEM_WAIT again, so other waiting threads are
  61. woken on a future gomp_sem_post. Furthermore, the awoken thread
  62. will wake other threads in case gomp_sem_post was called again
  63. before it had time to set SEM_WAIT. */
  64. while (!__atomic_compare_exchange_n (sem, &count,
  65. (count + SEM_INC) & ~SEM_WAIT, true,
  66. MEMMODEL_RELEASE, MEMMODEL_RELAXED))
  67. continue;
  68. if (__builtin_expect (count & SEM_WAIT, 0))
  69. gomp_sem_post_slow (sem);
  70. }
  71. #endif /* GOMP_SEM_H */