1 /* Copyright (C) 2005-2016 Free Software Foundation, Inc.
2    Contributed by Richard Henderson <rth@redhat.com>.
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 #include <limits.h> /* For INT_MIN */
37 
38 typedef int gomp_sem_t;
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 *);
44 
45 static inline void
gomp_sem_init(gomp_sem_t * sem,int value)46 gomp_sem_init (gomp_sem_t *sem, int value)
47 {
48   *sem = value * SEM_INC;
49 }
50 
51 static inline void
gomp_sem_destroy(gomp_sem_t * sem)52 gomp_sem_destroy (gomp_sem_t *sem)
53 {
54 }
55 
56 static inline void
gomp_sem_wait(gomp_sem_t * sem)57 gomp_sem_wait (gomp_sem_t *sem)
58 {
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);
66 }
67 
68 static inline void
gomp_sem_post(gomp_sem_t * sem)69 gomp_sem_post (gomp_sem_t *sem)
70 {
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;
84 
85   if (__builtin_expect (count & SEM_WAIT, 0))
86     gomp_sem_post_slow (sem);
87 }
88 #endif /* GOMP_SEM_H */
89