]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libgomp/lib/contrib/gcc-4.7/libgomp/config/linux/mutex.c
update
[l4.git] / l4 / pkg / libgomp / lib / contrib / gcc-4.7 / libgomp / config / linux / mutex.c
1 /* Copyright (C) 2005, 2008, 2009, 2011 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 General Public License as published by
8    the Free Software Foundation; either version 3, or (at your option)
9    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 General Public License for
14    more details.
15
16    Under Section 7 of GPL version 3, you are granted additional
17    permissions described in the GCC Runtime Library Exception, version
18    3.1, as published by the Free Software Foundation.
19
20    You should have received a copy of the GNU General Public License and
21    a copy of the GCC Runtime Library Exception along with this program;
22    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23    <http://www.gnu.org/licenses/>.  */
24
25 /* This is a Linux specific implementation of a mutex synchronization
26    mechanism for libgomp.  This type is private to the library.  This
27    implementation uses atomic instructions and the futex syscall.  */
28
29 #include "wait.h"
30
31 long int gomp_futex_wake = FUTEX_WAKE | FUTEX_PRIVATE_FLAG;
32 long int gomp_futex_wait = FUTEX_WAIT | FUTEX_PRIVATE_FLAG;
33
34 void
35 gomp_mutex_lock_slow (gomp_mutex_t *mutex, int oldval)
36 {
37   /* First loop spins a while.  */
38   while (oldval == 1)
39     {
40       if (do_spin (mutex, 1))
41         {
42           /* Spin timeout, nothing changed.  Set waiting flag.  */
43           oldval = __atomic_exchange_n (mutex, -1, MEMMODEL_ACQUIRE);
44           if (oldval == 0)
45             return;
46           futex_wait (mutex, -1);
47           break;
48         }
49       else
50         {
51           /* Something changed.  If now unlocked, we're good to go.  */
52           oldval = 0;
53           if (__atomic_compare_exchange_n (mutex, &oldval, 1, false,
54                                            MEMMODEL_ACQUIRE, MEMMODEL_RELAXED))
55             return;
56         }
57     }
58
59   /* Second loop waits until mutex is unlocked.  We always exit this
60      loop with wait flag set, so next unlock will awaken a thread.  */
61   while ((oldval = __atomic_exchange_n (mutex, -1, MEMMODEL_ACQUIRE)))
62     do_wait (mutex, -1);
63 }
64
65 void
66 gomp_mutex_unlock_slow (gomp_mutex_t *mutex)
67 {
68   futex_wake (mutex, 1);
69 }