* elf/tst-tls8.c (do_test): Use $ORIGIN in module names.
[glibc.git] / nptl / pthread_mutex_trylock.c
blobae73ecc39bbdf24913a9e3f94348e93aeef666bf
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
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include "pthreadP.h"
24 #include <lowlevellock.h>
27 int
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. */
43 return EAGAIN;
45 ++mutex->__data.__count;
46 return 0;
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;
55 return 0;
57 break;
59 case PTHREAD_MUTEX_ERRORCHECK_NP:
60 /* Check whether we already hold the mutex. */
61 if (__builtin_expect (mutex->__data.__owner == id, 0))
62 return EDEADLK;
64 /* FALLTHROUGH */
66 case PTHREAD_MUTEX_TIMED_NP:
67 case PTHREAD_MUTEX_ADAPTIVE_NP:
68 /* Normal mutex. */
69 if (lll_mutex_trylock (mutex->__data.__lock) != 0)
70 break;
72 /* Record the ownership. */
73 mutex->__data.__owner = id;
74 ++mutex->__data.__nusers;
76 return 0;
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. */
86 return EAGAIN;
88 ++mutex->__data.__count;
90 return 0;
93 /* We have to get the mutex. */
94 if (lll_mutex_trylock (mutex->__data.__lock) == 0)
96 mutex->__data.__count = 1;
98 goto robust;
101 break;
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))
106 return EDEADLK;
108 /* FALLTHROUGH */
110 case PTHREAD_MUTEX_ROBUST_PRIVATE_NP:
111 case PTHREAD_MUTEX_ROBUST_PRIVATE_ADAPTIVE_NP:
112 if (lll_mutex_trylock (mutex->__data.__lock) != 0)
113 break;
115 robust:
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. */
130 int retval = 0;
131 if (__builtin_expect (mutex->__data.__owner
132 == PTHREAD_MUTEX_OWNERDEAD, 0))
134 retval = EOWNERDEAD;
135 /* We signal ownership of a not yet recovered robust
136 mutex by storing the negative thread ID. */
137 id = -id;
140 ENQUEUE_MUTEX (mutex);
142 mutex->__data.__owner = id;
143 ++mutex->__data.__nusers;
145 return retval
147 default:
148 /* Correct code cannot set any other type. */
149 return EINVAL;
152 return EBUSY;
154 strong_alias (__pthread_mutex_trylock, pthread_mutex_trylock)