1169695Skan/* Copyright (C) 2005 Free Software Foundation, Inc.
2169695Skan   Contributed by Richard Henderson <rth@redhat.com>.
3169695Skan
4169695Skan   This file is part of the GNU OpenMP Library (libgomp).
5169695Skan
6169695Skan   Libgomp is free software; you can redistribute it and/or modify it
7169695Skan   under the terms of the GNU Lesser General Public License as published by
8169695Skan   the Free Software Foundation; either version 2.1 of the License, or
9169695Skan   (at your option) any later version.
10169695Skan
11169695Skan   Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12169695Skan   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13169695Skan   FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
14169695Skan   more details.
15169695Skan
16169695Skan   You should have received a copy of the GNU Lesser General Public License
17169695Skan   along with libgomp; see the file COPYING.LIB.  If not, write to the
18169695Skan   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19169695Skan   MA 02110-1301, USA.  */
20169695Skan
21169695Skan/* As a special exception, if you link this library with other files, some
22169695Skan   of which are compiled with GCC, to produce an executable, this library
23169695Skan   does not by itself cause the resulting executable to be covered by the
24169695Skan   GNU General Public License.  This exception does not however invalidate
25169695Skan   any other reasons why the executable file might be covered by the GNU
26169695Skan   General Public License.  */
27169695Skan
28169695Skan/* This is the default PTHREADS implementation of the public OpenMP
29169695Skan   locking primitives.
30169695Skan
31169695Skan   Because OpenMP uses different entry points for normal and recursive
32169695Skan   locks, and pthreads uses only one entry point, a system may be able
33169695Skan   to do better and streamline the locking as well as reduce the size
34169695Skan   of the types exported.  */
35169695Skan
36171831Skan/* We need Unix98 extensions to get recursive locks.  On Tru64 UNIX V4.0F,
37171831Skan   the declarations are available without _XOPEN_SOURCE, which actually
38171831Skan   breaks compilation.  */
39171831Skan#ifndef __osf__
40169695Skan#define _XOPEN_SOURCE 500
41171831Skan#endif
42169695Skan
43169695Skan#include "libgomp.h"
44169695Skan
45169695Skan
46169695Skanvoid
47169695Skanomp_init_lock (omp_lock_t *lock)
48169695Skan{
49169695Skan  pthread_mutex_init (lock, NULL);
50169695Skan}
51169695Skan
52169695Skanvoid
53169695Skanomp_destroy_lock (omp_lock_t *lock)
54169695Skan{
55169695Skan  pthread_mutex_destroy (lock);
56169695Skan}
57169695Skan
58169695Skanvoid
59169695Skanomp_set_lock (omp_lock_t *lock)
60169695Skan{
61169695Skan  pthread_mutex_lock (lock);
62169695Skan}
63169695Skan
64169695Skanvoid
65169695Skanomp_unset_lock (omp_lock_t *lock)
66169695Skan{
67169695Skan  pthread_mutex_unlock (lock);
68169695Skan}
69169695Skan
70169695Skanint
71169695Skanomp_test_lock (omp_lock_t *lock)
72169695Skan{
73169695Skan  return pthread_mutex_trylock (lock) == 0;
74169695Skan}
75169695Skan
76169695Skanvoid
77169695Skanomp_init_nest_lock (omp_nest_lock_t *lock)
78169695Skan{
79169695Skan  pthread_mutexattr_t attr;
80169695Skan
81169695Skan  pthread_mutexattr_init (&attr);
82169695Skan  pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
83169695Skan  pthread_mutex_init (&lock->lock, &attr);
84169695Skan  lock->count = 0;
85169695Skan  pthread_mutexattr_destroy (&attr);
86169695Skan}
87169695Skan
88169695Skanvoid
89169695Skanomp_destroy_nest_lock (omp_nest_lock_t *lock)
90169695Skan{
91169695Skan  pthread_mutex_destroy (&lock->lock);
92169695Skan}
93169695Skan
94169695Skanvoid
95169695Skanomp_set_nest_lock (omp_nest_lock_t *lock)
96169695Skan{
97169695Skan  pthread_mutex_lock (&lock->lock);
98169695Skan  lock->count++;
99169695Skan}
100169695Skan
101169695Skanvoid
102169695Skanomp_unset_nest_lock (omp_nest_lock_t *lock)
103169695Skan{
104169695Skan  lock->count--;
105169695Skan  pthread_mutex_unlock (&lock->lock);
106169695Skan}
107169695Skan
108169695Skanint
109169695Skanomp_test_nest_lock (omp_nest_lock_t *lock)
110169695Skan{
111169695Skan  if (pthread_mutex_trylock (&lock->lock) == 0)
112169695Skan    return ++lock->count;
113169695Skan  return 0;
114169695Skan}
115169695Skan
116169695Skanialias (omp_init_lock)
117169695Skanialias (omp_init_nest_lock)
118169695Skanialias (omp_destroy_lock)
119169695Skanialias (omp_destroy_nest_lock)
120169695Skanialias (omp_set_lock)
121169695Skanialias (omp_set_nest_lock)
122169695Skanialias (omp_unset_lock)
123169695Skanialias (omp_unset_nest_lock)
124169695Skanialias (omp_test_lock)
125169695Skanialias (omp_test_nest_lock)
126