1/* Copyright (C) 2005-2017 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 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. */
29
30#include <string.h>
31#include "libgomp.h"
32
33/* The internal gomp_mutex_t and the external non-recursive omp_lock_t
34 have the same form. Re-use it. */
35
36void
37gomp_init_lock_30 (omp_lock_t *lock)
38{
39 gomp_mutex_init (lock);
40}
41
42void
43gomp_destroy_lock_30 (omp_lock_t *lock)
44{
45 gomp_mutex_destroy (lock);
46}
47
48void
49gomp_set_lock_30 (omp_lock_t *lock)
50{
51 gomp_mutex_lock (lock);
52}
53
54void
55gomp_unset_lock_30 (omp_lock_t *lock)
56{
57 gomp_mutex_unlock (lock);
58}
59
60int
61gomp_test_lock_30 (omp_lock_t *lock)
62{
63 int oldval = 0;
64
65 return __atomic_compare_exchange_n (lock, &oldval, 1, false,
66 MEMMODEL_ACQUIRE, MEMMODEL_RELAXED);
67}
68
69void
70gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
71{
72 memset (lock, '\0', sizeof (*lock));
73}
74
75void
76gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
77{
78}
79
80void
81gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
82{
83 void *me = gomp_icv (true);
84
85 if (lock->owner != me)
86 {
87 gomp_mutex_lock (&lock->lock);
88 lock->owner = me;
89 }
90
91 lock->count++;
92}
93
94void
95gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
96{
97 if (--lock->count == 0)
98 {
99 lock->owner = NULL;
100 gomp_mutex_unlock (&lock->lock);
101 }
102}
103
104int
105gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
106{
107 void *me = gomp_icv (true);
108 int oldval;
109
110 if (lock->owner == me)
111 return ++lock->count;
112
113 oldval = 0;
114 if (__atomic_compare_exchange_n (&lock->lock, &oldval, 1, false,
115 MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
116 {
117 lock->owner = me;
118 lock->count = 1;
119 return 1;
120 }
121
122 return 0;
123}
124