| 1 |
/* Copyright (C) 2005-2025 Free Software Foundation, Inc. |
| 2 |
Contributed by Richard Henderson <[email protected]>. |
| 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 |
|
| 36 |
void |
| 37 |
gomp_init_lock_30 (omp_lock_t *lock) |
| 38 |
{ |
| 39 |
gomp_mutex_init (lock); |
| 40 |
} |
| 41 |
|
| 42 |
void |
| 43 |
gomp_destroy_lock_30 (omp_lock_t *lock) |
| 44 |
{ |
| 45 |
gomp_mutex_destroy (lock); |
| 46 |
} |
| 47 |
|
| 48 |
void |
| 49 |
gomp_set_lock_30 (omp_lock_t *lock) |
| 50 |
{ |
| 51 |
gomp_mutex_lock (lock); |
| 52 |
} |
| 53 |
|
| 54 |
void |
| 55 |
gomp_unset_lock_30 (omp_lock_t *lock) |
| 56 |
{ |
| 57 |
gomp_mutex_unlock (lock); |
| 58 |
} |
| 59 |
|
| 60 |
int |
| 61 |
gomp_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 |
|
| 69 |
void |
| 70 |
gomp_init_nest_lock_30 (omp_nest_lock_t *lock) |
| 71 |
{ |
| 72 |
memset (lock, '\0', sizeof (*lock)); |
| 73 |
} |
| 74 |
|
| 75 |
void |
| 76 |
gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock) |
| 77 |
{ |
| 78 |
} |
| 79 |
|
| 80 |
void |
| 81 |
gomp_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 |
|
| 94 |
void |
| 95 |
gomp_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 |
|
| 104 |
int |
| 105 |
gomp_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 |
} |