1 /* Copyright (C) 2002, 2003, 2005 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
;
31 pid_t id
= THREAD_GETMEM (THREAD_SELF
, tid
);
33 switch (__builtin_expect (mutex
->__data
.__kind
, PTHREAD_MUTEX_TIMED_NP
))
35 /* Recursive mutex. */
36 case PTHREAD_MUTEX_RECURSIVE_NP
:
37 /* Check whether we already hold the mutex. */
38 if (mutex
->__data
.__owner
== id
)
40 /* Just bump the counter. */
41 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
42 /* Overflow of the counter. */
45 ++mutex
->__data
.__count
;
49 if (lll_mutex_trylock (mutex
->__data
.__lock
) == 0)
51 /* Record the ownership. */
52 mutex
->__data
.__owner
= id
;
53 mutex
->__data
.__count
= 1;
54 ++mutex
->__data
.__nusers
;
59 case PTHREAD_MUTEX_ERRORCHECK_NP
:
60 /* Check whether we already hold the mutex. */
61 if (__builtin_expect (mutex
->__data
.__owner
== id
, 0))
66 case PTHREAD_MUTEX_TIMED_NP
:
67 case PTHREAD_MUTEX_ADAPTIVE_NP
:
69 if (lll_mutex_trylock (mutex
->__data
.__lock
) != 0)
72 /* Record the ownership. */
73 mutex
->__data
.__owner
= id
;
74 ++mutex
->__data
.__nusers
;
79 case PTHREAD_MUTEX_ROBUST_PRIVATE_RECURSIVE_NP
:
80 /* Check whether we already hold the mutex. */
81 if (abs (mutex
->__data
.__owner
) == id
)
83 /* Just bump the counter. */
84 if (__builtin_expect (mutex
->__data
.__count
+ 1 == 0, 0))
85 /* Overflow of the counter. */
88 ++mutex
->__data
.__count
;
93 /* We have to get the mutex. */
94 if (lll_mutex_trylock (mutex
->__data
.__lock
) == 0)
96 mutex
->__data
.__count
= 1;
103 case PTHREAD_MUTEX_ROBUST_PRIVATE_ERRORCHECK_NP
:
104 /* Check whether we already hold the mutex. */
105 if (__builtin_expect (abs (mutex
->__data
.__owner
) == id
, 0))
110 case PTHREAD_MUTEX_ROBUST_PRIVATE_NP
:
111 case PTHREAD_MUTEX_ROBUST_PRIVATE_ADAPTIVE_NP
:
112 if (lll_mutex_trylock (mutex
->__data
.__lock
) != 0)
116 if (__builtin_expect (mutex
->__data
.__owner
117 == PTHREAD_MUTEX_NOTRECOVERABLE
, 0))
119 /* This mutex is now not recoverable. */
120 mutex
->__data
.__count
= 0;
121 lll_mutex_unlock (mutex
->__data
.__lock
);
122 return ENOTRECOVERABLE
;
125 /* This mutex is either healthy or we can try to recover it. */
126 assert (mutex
->__data
.__owner
== 0
127 || mutex
->__data
.__owner
== PTHREAD_MUTEX_OWNERDEAD
);
129 /* Record the ownership. */
131 if (__builtin_expect (mutex
->__data
.__owner
132 == PTHREAD_MUTEX_OWNERDEAD
, 0))
135 /* We signal ownership of a not yet recovered robust
136 mutex by storing the negative thread ID. */
140 ENQUEUE_MUTEX (mutex
);
142 mutex
->__data
.__owner
= id
;
143 ++mutex
->__data
.__nusers
;
148 /* Correct code cannot set any other type. */
154 strong_alias (__pthread_mutex_trylock
, pthread_mutex_trylock
)