annotate libgomp/config/linux/sem.h @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1 /* Copyright (C) 2005-2018 Free Software Foundation, Inc.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 Contributed by Richard Henderson <rth@redhat.com>.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
111
kono
parents: 0
diff changeset
4 This file is part of the GNU Offloading and Multi Processing Library
kono
parents: 0
diff changeset
5 (libgomp).
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 Libgomp is free software; you can redistribute it and/or modify it
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 the Free Software Foundation; either version 3, or (at your option)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 any later version.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 more details.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 Under Section 7 of GPL version 3, you are granted additional
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 permissions described in the GCC Runtime Library Exception, version
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 3.1, as published by the Free Software Foundation.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 You should have received a copy of the GNU General Public License and
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 a copy of the GCC Runtime Library Exception along with this program;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 <http://www.gnu.org/licenses/>. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 /* This is a Linux specific implementation of a semaphore synchronization
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 mechanism for libgomp. This type is private to the library. This
111
kono
parents: 0
diff changeset
28 counting semaphore implementation uses atomic instructions and the
kono
parents: 0
diff changeset
29 futex syscall, and a single 32-bit int to store semaphore state.
kono
parents: 0
diff changeset
30 The low 31 bits are the count, the top bit is a flag set when some
kono
parents: 0
diff changeset
31 threads may be waiting. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 #ifndef GOMP_SEM_H
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 #define GOMP_SEM_H 1
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35
111
kono
parents: 0
diff changeset
36 #include <limits.h> /* For INT_MIN */
kono
parents: 0
diff changeset
37
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 typedef int gomp_sem_t;
111
kono
parents: 0
diff changeset
39 #define SEM_WAIT INT_MIN
kono
parents: 0
diff changeset
40 #define SEM_INC 1
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
41
111
kono
parents: 0
diff changeset
42 extern void gomp_sem_wait_slow (gomp_sem_t *, int);
kono
parents: 0
diff changeset
43 extern void gomp_sem_post_slow (gomp_sem_t *);
kono
parents: 0
diff changeset
44
kono
parents: 0
diff changeset
45 static inline void
kono
parents: 0
diff changeset
46 gomp_sem_init (gomp_sem_t *sem, int value)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 {
111
kono
parents: 0
diff changeset
48 *sem = value * SEM_INC;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
50
111
kono
parents: 0
diff changeset
51 static inline void
kono
parents: 0
diff changeset
52 gomp_sem_destroy (gomp_sem_t *sem)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 {
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
55
111
kono
parents: 0
diff changeset
56 static inline void
kono
parents: 0
diff changeset
57 gomp_sem_wait (gomp_sem_t *sem)
kono
parents: 0
diff changeset
58 {
kono
parents: 0
diff changeset
59 int count = *sem;
kono
parents: 0
diff changeset
60
kono
parents: 0
diff changeset
61 while ((count & ~SEM_WAIT) != 0)
kono
parents: 0
diff changeset
62 if (__atomic_compare_exchange_n (sem, &count, count - SEM_INC, true,
kono
parents: 0
diff changeset
63 MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
kono
parents: 0
diff changeset
64 return;
kono
parents: 0
diff changeset
65 gomp_sem_wait_slow (sem, count);
kono
parents: 0
diff changeset
66 }
kono
parents: 0
diff changeset
67
kono
parents: 0
diff changeset
68 static inline void
kono
parents: 0
diff changeset
69 gomp_sem_post (gomp_sem_t *sem)
kono
parents: 0
diff changeset
70 {
kono
parents: 0
diff changeset
71 int count = *sem;
kono
parents: 0
diff changeset
72
kono
parents: 0
diff changeset
73 /* Clear SEM_WAIT here so that if there are no more waiting threads
kono
parents: 0
diff changeset
74 we transition back to the uncontended state that does not make
kono
parents: 0
diff changeset
75 futex syscalls. If there are waiting threads then when one is
kono
parents: 0
diff changeset
76 awoken it will set SEM_WAIT again, so other waiting threads are
kono
parents: 0
diff changeset
77 woken on a future gomp_sem_post. Furthermore, the awoken thread
kono
parents: 0
diff changeset
78 will wake other threads in case gomp_sem_post was called again
kono
parents: 0
diff changeset
79 before it had time to set SEM_WAIT. */
kono
parents: 0
diff changeset
80 while (!__atomic_compare_exchange_n (sem, &count,
kono
parents: 0
diff changeset
81 (count + SEM_INC) & ~SEM_WAIT, true,
kono
parents: 0
diff changeset
82 MEMMODEL_RELEASE, MEMMODEL_RELAXED))
kono
parents: 0
diff changeset
83 continue;
kono
parents: 0
diff changeset
84
kono
parents: 0
diff changeset
85 if (__builtin_expect (count & SEM_WAIT, 0))
kono
parents: 0
diff changeset
86 gomp_sem_post_slow (sem);
kono
parents: 0
diff changeset
87 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 #endif /* GOMP_SEM_H */