| 1 |
/* Copyright (C) 2005-2025 Free Software Foundation, Inc. |
| 2 |
Contributed by Richard Henderson <[email protected]>. |
| 3 |
|
| 4 |
This file is part of the GNU Offloading and Multi Processing Library |
| 5 |
(libgomp). |
| 6 |
|
| 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. |
| 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 |
| 14 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 15 |
more details. |
| 16 |
|
| 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/>. */ |
| 25 |
|
| 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. */ |
| 32 |
|
| 33 |
#ifndef GOMP_SEM_H |
| 34 |
#define GOMP_SEM_H 1 |
| 35 |
|
| 36 |
typedef int gomp_sem_t; |
| 37 |
#define SEM_WAIT (-__INT_MAX__ - 1) |
| 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 *); |
| 42 |
|
| 43 |
static inline void |
| 44 |
gomp_sem_init (gomp_sem_t *sem, int value) |
| 45 |
{ |
| 46 |
*sem = value * SEM_INC; |
| 47 |
} |
| 48 |
|
| 49 |
static inline void |
| 50 |
gomp_sem_destroy (gomp_sem_t *sem) |
| 51 |
{ |
| 52 |
} |
| 53 |
|
| 54 |
static inline void |
| 55 |
gomp_sem_wait (gomp_sem_t *sem) |
| 56 |
{ |
| 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); |
| 64 |
} |
| 65 |
|
| 66 |
static inline void |
| 67 |
gomp_sem_post (gomp_sem_t *sem) |
| 68 |
{ |
| 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; |
| 82 |
|
| 83 |
if (__builtin_expect (count & SEM_WAIT, 0)) |
| 84 |
gomp_sem_post_slow (sem); |
| 85 |
} |
| 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 |
} |
| 95 |
#endif /* GOMP_SEM_H */ |