]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/gcclibs/libgomp/config/linux/lock.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / gcclibs / libgomp / config / linux / lock.c
1 /* Copyright (C) 2005 Free Software Foundation, Inc.
2    Contributed by Richard Henderson <rth@redhat.com>.
3
4    This file is part of the GNU OpenMP Library (libgomp).
5
6    Libgomp is free software; you can redistribute it and/or modify it
7    under the terms of the GNU Lesser General Public License as published by
8    the Free Software Foundation; either version 2.1 of the License, or
9    (at your option) any later version.
10
11    Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13    FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
14    more details.
15
16    You should have received a copy of the GNU Lesser General Public License 
17    along with libgomp; see the file COPYING.LIB.  If not, write to the
18    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20
21 /* As a special exception, if you link this library with other files, some
22    of which are compiled with GCC, to produce an executable, this library
23    does not by itself cause the resulting executable to be covered by the
24    GNU General Public License.  This exception does not however invalidate
25    any other reasons why the executable file might be covered by the GNU
26    General Public License.  */
27
28 /* This is a Linux specific implementation of the public OpenMP locking
29    primitives.  This implementation uses atomic instructions and the futex
30    syscall.  */
31
32 #include "libgomp.h"
33 #include <string.h>
34 #include <unistd.h>
35 #include <sys/syscall.h>
36 #include "futex.h"
37
38
39 /* The internal gomp_mutex_t and the external non-recursive omp_lock_t
40    have the same form.  Re-use it.  */
41
42 void
43 omp_init_lock (omp_lock_t *lock)
44 {
45   gomp_mutex_init (lock);
46 }
47
48 void
49 omp_destroy_lock (omp_lock_t *lock)
50 {
51   gomp_mutex_destroy (lock);
52 }
53
54 void
55 omp_set_lock (omp_lock_t *lock)
56 {
57   gomp_mutex_lock (lock);
58 }
59
60 void
61 omp_unset_lock (omp_lock_t *lock)
62 {
63   gomp_mutex_unlock (lock);
64 }
65
66 int
67 omp_test_lock (omp_lock_t *lock)
68 {
69   return __sync_bool_compare_and_swap (lock, 0, 1);
70 }
71
72 /* The external recursive omp_nest_lock_t form requires additional work.  */
73
74 /* We need an integer to uniquely identify this thread.  Most generally
75    this is the thread's TID, which ideally we'd get this straight from
76    the TLS block where glibc keeps it.  Unfortunately, we can't get at
77    that directly.
78
79    If we don't support (or have disabled) TLS, one function call is as
80    good (or bad) as any other.  Use the syscall all the time.
81
82    On an ILP32 system (defined here as not LP64), we can make do with
83    any thread-local pointer.  Ideally we'd use the TLS base address,
84    since that requires the least amount of arithmetic, but that's not
85    always available directly.  Make do with the gomp_thread pointer
86    since it's handy.  */
87
88 #if !defined (HAVE_TLS)
89 static inline int gomp_tid (void)
90 {
91   return syscall (SYS_gettid);
92 }
93 #elif !defined(__LP64__)
94 static inline int gomp_tid (void)
95 {
96   return (int) gomp_thread ();
97 }
98 #else
99 static __thread int tid_cache;
100 static inline int gomp_tid (void)
101 {
102   int tid = tid_cache;
103   if (__builtin_expect (tid == 0, 0))
104     tid_cache = tid = syscall (SYS_gettid);
105   return tid;
106 }
107 #endif
108
109
110 void
111 omp_init_nest_lock (omp_nest_lock_t *lock)
112 {
113   memset (lock, 0, sizeof (lock));
114 }
115
116 void
117 omp_destroy_nest_lock (omp_nest_lock_t *lock)
118 {
119 }
120
121 void
122 omp_set_nest_lock (omp_nest_lock_t *lock)
123 {
124   int otid, tid = gomp_tid ();
125
126   while (1)
127     {
128       otid = __sync_val_compare_and_swap (&lock->owner, 0, tid);
129       if (otid == 0)
130         {
131           lock->count = 1;
132           return;
133         }
134       if (otid == tid)
135         {
136           lock->count++;
137           return;
138         }
139
140       futex_wait (&lock->owner, otid);
141     }
142 }
143
144 void
145 omp_unset_nest_lock (omp_nest_lock_t *lock)
146 {
147   /* ??? Validate that we own the lock here.  */
148
149   if (--lock->count == 0)
150     {
151       __sync_lock_release (&lock->owner);
152       futex_wake (&lock->owner, 1);
153     }
154 }
155
156 int
157 omp_test_nest_lock (omp_nest_lock_t *lock)
158 {
159   int otid, tid = gomp_tid ();
160
161   otid = __sync_val_compare_and_swap (&lock->owner, 0, tid);
162   if (otid == 0)
163     {
164       lock->count = 1;
165       return 1;
166     }
167   if (otid == tid)
168     return ++lock->count;
169
170   return 0;
171 }
172
173 ialias (omp_init_lock)
174 ialias (omp_init_nest_lock)
175 ialias (omp_destroy_lock)
176 ialias (omp_destroy_nest_lock)
177 ialias (omp_set_lock)
178 ialias (omp_set_nest_lock)
179 ialias (omp_unset_lock)
180 ialias (omp_unset_nest_lock)
181 ialias (omp_test_lock)
182 ialias (omp_test_nest_lock)