1 /* Copyright (C) 2002, 2003, 2005, 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 #include <lowlevellock.h>
28 __pthread_mutex_trylock (mutex
)
29 pthread_mutex_t
*mutex
;
32 pid_t id
= THREAD_GETMEM (THREAD_SELF
, tid
);
34 switch (__builtin_expect (mutex
->__data
.__kind
, PTHREAD_MUTEX_TIMED_NP
))
36 /* Recursive mutex. */
37 case PTHREAD_MUTEX_RECURSIVE_NP
:
38 /* Check whether we already hold the mutex. */
39 if (mutex
->__data
.__owner
== id
)
41 /* Just bump the counter. */
42 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
43 /* Overflow of the counter. */
46 ++mutex
->__data
.__count
;
50 if (lll_mutex_trylock (mutex
->__data
.__lock
) == 0)
52 /* Record the ownership. */
53 mutex
->__data
.__owner
= id
;
54 mutex
->__data
.__count
= 1;
55 ++mutex
->__data
.__nusers
;
60 case PTHREAD_MUTEX_ERRORCHECK_NP
:
61 /* Check whether we already hold the mutex. */
62 if (__builtin_expect (mutex
->__data
.__owner
== id
, 0))
67 case PTHREAD_MUTEX_TIMED_NP
:
68 case PTHREAD_MUTEX_ADAPTIVE_NP
:
70 if (lll_mutex_trylock (mutex
->__data
.__lock
) != 0)
73 /* Record the ownership. */
74 mutex
->__data
.__owner
= id
;
75 ++mutex
->__data
.__nusers
;
80 case PTHREAD_MUTEX_ROBUST_PRIVATE_RECURSIVE_NP
:
81 case PTHREAD_MUTEX_ROBUST_PRIVATE_ERRORCHECK_NP
:
82 case PTHREAD_MUTEX_ROBUST_PRIVATE_NP
:
83 case PTHREAD_MUTEX_ROBUST_PRIVATE_ADAPTIVE_NP
:
84 oldval
= mutex
->__data
.__lock
;
87 if ((oldval
& FUTEX_OWNER_DIED
) != 0)
89 /* The previous owner died. Try locking the mutex. */
92 = atomic_compare_and_exchange_val_acq (&mutex
->__data
.__lock
,
96 if ((newval
& FUTEX_OWNER_DIED
) == 0)
101 /* We got the mutex. */
102 mutex
->__data
.__count
= 1;
103 /* But it is inconsistent unless marked otherwise. */
104 mutex
->__data
.__owner
= PTHREAD_MUTEX_INCONSISTENT
;
106 ENQUEUE_MUTEX (mutex
);
108 /* Note that we deliberately exist here. If we fall
109 through to the end of the function __nusers would be
110 incremented which is not correct because the old
111 owner has to be discounted. */
116 /* Check whether we already hold the mutex. */
117 if (__builtin_expect ((mutex
->__data
.__lock
& FUTEX_TID_MASK
)
120 if (mutex
->__data
.__kind
121 == PTHREAD_MUTEX_ROBUST_PRIVATE_ERRORCHECK_NP
)
124 if (mutex
->__data
.__kind
125 == PTHREAD_MUTEX_ROBUST_PRIVATE_RECURSIVE_NP
)
127 /* Just bump the counter. */
128 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
129 /* Overflow of the counter. */
132 ++mutex
->__data
.__count
;
138 oldval
= lll_robust_mutex_trylock (mutex
->__data
.__lock
, id
);
139 if (oldval
!= 0 && (oldval
& FUTEX_OWNER_DIED
) == 0)
143 if (__builtin_expect (mutex
->__data
.__owner
144 == PTHREAD_MUTEX_NOTRECOVERABLE
, 0))
146 /* This mutex is now not recoverable. */
147 mutex
->__data
.__count
= 0;
149 lll_mutex_unlock (mutex
->__data
.__lock
);
150 return ENOTRECOVERABLE
;
153 while ((oldval
& FUTEX_OWNER_DIED
) != 0);
155 ENQUEUE_MUTEX (mutex
);
157 mutex
->__data
.__owner
= id
;
158 ++mutex
->__data
.__nusers
;
159 mutex
->__data
.__count
= 1;
164 /* Correct code cannot set any other type. */
170 strong_alias (__pthread_mutex_trylock
, pthread_mutex_trylock
)