OSDN Git Service

PR libgomp/29494
[pf3gnuchains/gcc-fork.git] / libgomp / config / posix / 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 the default PTHREADS implementation of the public OpenMP
29    locking primitives.
30
31    Because OpenMP uses different entry points for normal and recursive
32    locks, and pthreads uses only one entry point, a system may be able
33    to do better and streamline the locking as well as reduce the size
34    of the types exported.  */
35
36 /* We need Unix98 extensions to get recursive locks.  */
37 #define _XOPEN_SOURCE 500
38
39 #include "libgomp.h"
40
41
42 void
43 omp_init_lock (omp_lock_t *lock)
44 {
45   pthread_mutex_init (lock, NULL);
46 }
47
48 void
49 omp_destroy_lock (omp_lock_t *lock)
50 {
51   pthread_mutex_destroy (lock);
52 }
53
54 void
55 omp_set_lock (omp_lock_t *lock)
56 {
57   pthread_mutex_lock (lock);
58 }
59
60 void
61 omp_unset_lock (omp_lock_t *lock)
62 {
63   pthread_mutex_unlock (lock);
64 }
65
66 int
67 omp_test_lock (omp_lock_t *lock)
68 {
69   return pthread_mutex_trylock (lock) == 0;
70 }
71
72 void
73 omp_init_nest_lock (omp_nest_lock_t *lock)
74 {
75   pthread_mutexattr_t attr;
76
77   pthread_mutexattr_init (&attr);
78   pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
79   pthread_mutex_init (&lock->lock, &attr);
80   lock->count = 0;
81   pthread_mutexattr_destroy (&attr);
82 }
83
84 void
85 omp_destroy_nest_lock (omp_nest_lock_t *lock)
86 {
87   pthread_mutex_destroy (&lock->lock);
88 }
89
90 void
91 omp_set_nest_lock (omp_nest_lock_t *lock)
92 {
93   pthread_mutex_lock (&lock->lock);
94   lock->count++;
95 }
96
97 void
98 omp_unset_nest_lock (omp_nest_lock_t *lock)
99 {
100   lock->count--;
101   pthread_mutex_unlock (&lock->lock);
102 }
103
104 int
105 omp_test_nest_lock (omp_nest_lock_t *lock)
106 {
107   if (pthread_mutex_trylock (&lock->lock) == 0)
108     return ++lock->count;
109   return 0;
110 }
111
112 ialias (omp_init_lock)
113 ialias (omp_init_nest_lock)
114 ialias (omp_destroy_lock)
115 ialias (omp_destroy_nest_lock)
116 ialias (omp_set_lock)
117 ialias (omp_set_nest_lock)
118 ialias (omp_unset_lock)
119 ialias (omp_unset_nest_lock)
120 ialias (omp_test_lock)
121 ialias (omp_test_nest_lock)