]>
Commit | Line | Data |
---|---|---|
99dee823 | 1 | /* Copyright (C) 2005-2021 Free Software Foundation, Inc. |
953ff289 DN |
2 | Contributed by Richard Henderson <rth@redhat.com>. |
3 | ||
f1f3453e TS |
4 | This file is part of the GNU Offloading and Multi Processing Library |
5 | (libgomp). | |
953ff289 DN |
6 | |
7 | Libgomp is free software; you can redistribute it and/or modify it | |
748086b7 JJ |
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. | |
953ff289 DN |
11 | |
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 | |
748086b7 | 14 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
953ff289 DN |
15 | more details. |
16 | ||
748086b7 JJ |
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. | |
20 | ||
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/>. */ | |
953ff289 DN |
25 | |
26 | /* This is a Linux specific implementation of a semaphore synchronization | |
27 | mechanism for libgomp. This type is private to the library. This | |
3e348fcc AM |
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. */ | |
953ff289 DN |
32 | |
33 | #ifndef GOMP_SEM_H | |
34 | #define GOMP_SEM_H 1 | |
35 | ||
3e348fcc AM |
36 | #include <limits.h> /* For INT_MIN */ |
37 | ||
953ff289 | 38 | typedef int gomp_sem_t; |
3e348fcc AM |
39 | #define SEM_WAIT INT_MIN |
40 | #define SEM_INC 1 | |
41 | ||
42 | extern void gomp_sem_wait_slow (gomp_sem_t *, int); | |
43 | extern void gomp_sem_post_slow (gomp_sem_t *); | |
953ff289 | 44 | |
3e348fcc AM |
45 | static inline void |
46 | gomp_sem_init (gomp_sem_t *sem, int value) | |
953ff289 | 47 | { |
3e348fcc | 48 | *sem = value * SEM_INC; |
953ff289 DN |
49 | } |
50 | ||
3e348fcc AM |
51 | static inline void |
52 | gomp_sem_destroy (gomp_sem_t *sem) | |
953ff289 | 53 | { |
953ff289 DN |
54 | } |
55 | ||
3e348fcc AM |
56 | static inline void |
57 | gomp_sem_wait (gomp_sem_t *sem) | |
953ff289 | 58 | { |
3e348fcc AM |
59 | int count = *sem; |
60 | ||
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); | |
953ff289 DN |
66 | } |
67 | ||
3e348fcc AM |
68 | static inline void |
69 | gomp_sem_post (gomp_sem_t *sem) | |
953ff289 | 70 | { |
3e348fcc AM |
71 | int count = *sem; |
72 | ||
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; | |
953ff289 | 84 | |
3e348fcc AM |
85 | if (__builtin_expect (count & SEM_WAIT, 0)) |
86 | gomp_sem_post_slow (sem); | |
87 | } | |
953ff289 | 88 | #endif /* GOMP_SEM_H */ |