]>
Commit | Line | Data |
---|---|---|
a945c346 | 1 | /* Copyright (C) 2005-2024 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 | ||
36 | typedef int gomp_sem_t; | |
42f10ba5 | 37 | #define SEM_WAIT (-__INT_MAX__ - 1) |
3e348fcc AM |
38 | #define SEM_INC 1 |
39 | ||
40 | extern void gomp_sem_wait_slow (gomp_sem_t *, int); | |
41 | extern void gomp_sem_post_slow (gomp_sem_t *); | |
953ff289 | 42 | |
3e348fcc AM |
43 | static inline void |
44 | gomp_sem_init (gomp_sem_t *sem, int value) | |
953ff289 | 45 | { |
3e348fcc | 46 | *sem = value * SEM_INC; |
953ff289 DN |
47 | } |
48 | ||
3e348fcc AM |
49 | static inline void |
50 | gomp_sem_destroy (gomp_sem_t *sem) | |
953ff289 | 51 | { |
953ff289 DN |
52 | } |
53 | ||
3e348fcc AM |
54 | static inline void |
55 | gomp_sem_wait (gomp_sem_t *sem) | |
953ff289 | 56 | { |
3e348fcc AM |
57 | int count = *sem; |
58 | ||
59 | while ((count & ~SEM_WAIT) != 0) | |
60 | if (__atomic_compare_exchange_n (sem, &count, count - SEM_INC, true, | |
61 | MEMMODEL_ACQUIRE, MEMMODEL_RELAXED)) | |
62 | return; | |
63 | gomp_sem_wait_slow (sem, count); | |
953ff289 DN |
64 | } |
65 | ||
3e348fcc AM |
66 | static inline void |
67 | gomp_sem_post (gomp_sem_t *sem) | |
953ff289 | 68 | { |
3e348fcc AM |
69 | int count = *sem; |
70 | ||
71 | /* Clear SEM_WAIT here so that if there are no more waiting threads | |
72 | we transition back to the uncontended state that does not make | |
73 | futex syscalls. If there are waiting threads then when one is | |
74 | awoken it will set SEM_WAIT again, so other waiting threads are | |
75 | woken on a future gomp_sem_post. Furthermore, the awoken thread | |
76 | will wake other threads in case gomp_sem_post was called again | |
77 | before it had time to set SEM_WAIT. */ | |
78 | while (!__atomic_compare_exchange_n (sem, &count, | |
79 | (count + SEM_INC) & ~SEM_WAIT, true, | |
80 | MEMMODEL_RELEASE, MEMMODEL_RELAXED)) | |
81 | continue; | |
953ff289 | 82 | |
3e348fcc AM |
83 | if (__builtin_expect (count & SEM_WAIT, 0)) |
84 | gomp_sem_post_slow (sem); | |
85 | } | |
d3b41bde JJ |
86 | |
87 | static inline int | |
88 | gomp_sem_getcount (gomp_sem_t *sem) | |
89 | { | |
90 | int count = __atomic_load_n (sem, MEMMODEL_RELAXED); | |
91 | if ((count & SEM_WAIT) != 0) | |
92 | return -1; | |
93 | return count / SEM_INC; | |
94 | } | |
953ff289 | 95 | #endif /* GOMP_SEM_H */ |