1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
/* Copyright (C) 2008 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by David Bartley <dtbartle@csclub.uwaterloo.ca>, 2008.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#include <errno.h>
#include <pthreadP.h>
#include <synchP.h>
#include <sys/synch.h>
#include <stdbool.h>
#include <assert.h>
int __mutex_lock_fast (mutex_t *mutex, bool try)
{
if (mutex->mutex_lockword32 == LOCKWORD32_UNSET_NO_WAITERS)
{
/* The mutex is not held by anyone so try to grab it. */
if (mutex->mutex_type & LOCK_SHARED)
{
uint64_t new_lockword64 = LOCKWORD64_SET_NO_WAITERS |
(THREAD_GETMEM (THREAD_SELF, pid) << MUTEX_OWNERPID_SHIFT);
uint64_t old_lockword64 = atomic_compare_and_exchange_val_acq (
&mutex->mutex_lockword64, new_lockword64,
LOCKWORD64_UNSET_NO_WAITERS);
if (__builtin_expect (old_lockword64 ==
LOCKWORD64_UNSET_NO_WAITERS, 1))
{
mutex->mutex_owner = THREAD_GETMEM (THREAD_SELF, tid);
return 0;
}
}
else
{
uint32_t old_lockword32 = atomic_compare_and_exchange_val_acq (
&mutex->mutex_lockword32, LOCKWORD32_SET_NO_WAITERS,
LOCKWORD32_UNSET_NO_WAITERS);
if (__builtin_expect (old_lockword32 ==
LOCKWORD32_UNSET_NO_WAITERS, 1))
{
mutex->mutex_owner = THREAD_GETMEM (THREAD_SELF, tid);
return 0;
}
}
}
else if ((mutex->mutex_type & LOCK_RECURSIVE) && MUTEX_IS_OWNER (mutex))
{
/* Recursively held lock. */
if (mutex->mutex_rcount == RECURSION_MAX)
return EAGAIN;
mutex->mutex_rcount++;
return 0;
}
else if ((mutex->mutex_type & LOCK_ERRORCHECK) && MUTEX_IS_OWNER (mutex))
{
/* Error checking: lock already held. */
return EDEADLK;
}
else if (try && mutex->mutex_lockbyte == LOCKBYTE_SET)
{
/* Tried to lock but lock was held. */
return EBUSY;
}
/* Need to use the slow code. */
return -1;
}
int __mutex_unlock_fast (mutex_t *mutex)
{
if ((mutex->mutex_type & LOCK_RECURSIVE) && MUTEX_IS_OWNER (mutex) &&
mutex->mutex_rcount > 0)
{
/* Recursively held lock. */
--mutex->mutex_rcount;
return 0;
}
else if ((mutex->mutex_type & LOCK_ERRORCHECK) && MUTEX_NOT_OWNER (mutex))
{
/* error checking: lock not held by this thread */
return EPERM;
}
else if (mutex->mutex_lockword32 == LOCKWORD32_SET_NO_WAITERS)
{
/* We need to clear the owner before we fully unlock. Otherwise
we have a race condition where we might clear the owner after
another thread sets the lock word. If we fail to unlock here
we don't need to reset the owner as we're just going to unlock
in the kernel (which doesn't check this field anyhow). */
mutex->mutex_owner = 0;
/* Nobody is waiting on the mutex so we can try to release it. */
if (mutex->mutex_type & LOCK_SHARED)
{
uint64_t test_lockword64 = LOCKWORD64_SET_NO_WAITERS |
(mutex->mutex_ownerpid << MUTEX_OWNERPID_SHIFT);
(THREAD_GETMEM (THREAD_SELF, pid) << MUTEX_OWNERPID_SHIFT);
uint64_t old_lockword64 = atomic_compare_and_exchange_val_acq (
&mutex->mutex_lockword64, LOCKWORD64_UNSET_NO_WAITERS,
test_lockword64);
if (__builtin_expect (old_lockword64 == test_lockword64, 1))
{
return 0;
}
}
else
{
uint32_t old_lockword32 = atomic_compare_and_exchange_val_acq (
&mutex->mutex_lockword32, LOCKWORD32_UNSET_NO_WAITERS,
LOCKWORD32_SET_NO_WAITERS);
if (__builtin_expect (old_lockword32 ==
LOCKWORD32_SET_NO_WAITERS, 1))
{
return 0;
}
}
}
/* Need to use the slow code. */
return -1;
}
|