PR sanitizer/85029
[official-gcc.git] / libgomp / lock.c
blob09597a1e559a8f7b2f9ba8aecf73952220325f4f
1 /* Copyright (C) 2005-2018 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@redhat.com>.
4 This file is part of the GNU Offloading and Multi Processing Library
5 (libgomp).
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.
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.
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.
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/>. */
26 /* This is a generic implementation of the public OpenMP locking primitives in
27 terms of internal gomp_mutex_t. It is not meant to be compiled on its own.
28 It is #include'd from config/{linux,nvptx}/lock.c. */
30 #include <string.h>
31 #include "libgomp.h"
33 /* The internal gomp_mutex_t and the external non-recursive omp_lock_t
34 have the same form. Re-use it. */
36 void
37 gomp_init_lock_30 (omp_lock_t *lock)
39 gomp_mutex_init (lock);
42 void
43 gomp_destroy_lock_30 (omp_lock_t *lock)
45 gomp_mutex_destroy (lock);
48 void
49 gomp_set_lock_30 (omp_lock_t *lock)
51 gomp_mutex_lock (lock);
54 void
55 gomp_unset_lock_30 (omp_lock_t *lock)
57 gomp_mutex_unlock (lock);
60 int
61 gomp_test_lock_30 (omp_lock_t *lock)
63 int oldval = 0;
65 return __atomic_compare_exchange_n (lock, &oldval, 1, false,
66 MEMMODEL_ACQUIRE, MEMMODEL_RELAXED);
69 void
70 gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
72 memset (lock, '\0', sizeof (*lock));
75 void
76 gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
80 void
81 gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
83 void *me = gomp_icv (true);
85 if (lock->owner != me)
87 gomp_mutex_lock (&lock->lock);
88 lock->owner = me;
91 lock->count++;
94 void
95 gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
97 if (--lock->count == 0)
99 lock->owner = NULL;
100 gomp_mutex_unlock (&lock->lock);
105 gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
107 void *me = gomp_icv (true);
108 int oldval;
110 if (lock->owner == me)
111 return ++lock->count;
113 oldval = 0;
114 if (__atomic_compare_exchange_n (&lock->lock, &oldval, 1, false,
115 MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
117 lock->owner = me;
118 lock->count = 1;
119 return 1;
122 return 0;