Pass more vector types to append_pattern_def_seq
[official-gcc.git] / libgomp / config / linux / sem.h
blobb85a6a5cec199b84bc98c7b50a1325b23c4b411f
1 /* Copyright (C) 2005-2018 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@redhat.com>.
4 This file is part of the GNU Offloading and Multi Processing Library
5 (libgomp).
7 Libgomp is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 /* This is a Linux specific implementation of a semaphore synchronization
27 mechanism for libgomp. This type is private to the library. This
28 counting semaphore implementation uses atomic instructions and the
29 futex syscall, and a single 32-bit int to store semaphore state.
30 The low 31 bits are the count, the top bit is a flag set when some
31 threads may be waiting. */
33 #ifndef GOMP_SEM_H
34 #define GOMP_SEM_H 1
36 #include <limits.h> /* For INT_MIN */
38 typedef int gomp_sem_t;
39 #define SEM_WAIT INT_MIN
40 #define SEM_INC 1
42 extern void gomp_sem_wait_slow (gomp_sem_t *, int);
43 extern void gomp_sem_post_slow (gomp_sem_t *);
45 static inline void
46 gomp_sem_init (gomp_sem_t *sem, int value)
48 *sem = value * SEM_INC;
51 static inline void
52 gomp_sem_destroy (gomp_sem_t *sem)
56 static inline void
57 gomp_sem_wait (gomp_sem_t *sem)
59 int count = *sem;
61 while ((count & ~SEM_WAIT) != 0)
62 if (__atomic_compare_exchange_n (sem, &count, count - SEM_INC, true,
63 MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
64 return;
65 gomp_sem_wait_slow (sem, count);
68 static inline void
69 gomp_sem_post (gomp_sem_t *sem)
71 int count = *sem;
73 /* Clear SEM_WAIT here so that if there are no more waiting threads
74 we transition back to the uncontended state that does not make
75 futex syscalls. If there are waiting threads then when one is
76 awoken it will set SEM_WAIT again, so other waiting threads are
77 woken on a future gomp_sem_post. Furthermore, the awoken thread
78 will wake other threads in case gomp_sem_post was called again
79 before it had time to set SEM_WAIT. */
80 while (!__atomic_compare_exchange_n (sem, &count,
81 (count + SEM_INC) & ~SEM_WAIT, true,
82 MEMMODEL_RELEASE, MEMMODEL_RELAXED))
83 continue;
85 if (__builtin_expect (count & SEM_WAIT, 0))
86 gomp_sem_post_slow (sem);
88 #endif /* GOMP_SEM_H */