[BZ #2509]
[glibc.git] / nptl / pthread_mutex_trylock.c
blob148a6e919f5328310a2ac56989322fa4be1a17f1
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
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 int oldval;
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. */
44 return EAGAIN;
46 ++mutex->__data.__count;
47 return 0;
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;
56 return 0;
58 break;
60 case PTHREAD_MUTEX_ERRORCHECK_NP:
61 /* Check whether we already hold the mutex. */
62 if (__builtin_expect (mutex->__data.__owner == id, 0))
63 return EDEADLK;
65 /* FALLTHROUGH */
67 case PTHREAD_MUTEX_TIMED_NP:
68 case PTHREAD_MUTEX_ADAPTIVE_NP:
69 /* Normal mutex. */
70 if (lll_mutex_trylock (mutex->__data.__lock) != 0)
71 break;
73 /* Record the ownership. */
74 mutex->__data.__owner = id;
75 ++mutex->__data.__nusers;
77 return 0;
80 case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP:
81 case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP:
82 case PTHREAD_MUTEX_ROBUST_NORMAL_NP:
83 case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP:
84 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
85 &mutex->__data.__list.__next);
87 oldval = mutex->__data.__lock;
90 again:
91 if ((oldval & FUTEX_OWNER_DIED) != 0)
93 /* The previous owner died. Try locking the mutex. */
94 int newval
95 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
96 id, oldval);
98 if (newval != oldval)
100 oldval = newval;
101 goto again;
104 /* We got the mutex. */
105 mutex->__data.__count = 1;
106 /* But it is inconsistent unless marked otherwise. */
107 mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;
109 ENQUEUE_MUTEX (mutex);
110 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
112 /* Note that we deliberately exist here. If we fall
113 through to the end of the function __nusers would be
114 incremented which is not correct because the old
115 owner has to be discounted. */
116 return EOWNERDEAD;
119 /* Check whether we already hold the mutex. */
120 if (__builtin_expect ((oldval & FUTEX_TID_MASK) == id, 0))
122 if (mutex->__data.__kind
123 == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP)
125 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
126 NULL);
127 return EDEADLK;
130 if (mutex->__data.__kind
131 == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP)
133 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
134 NULL);
136 /* Just bump the counter. */
137 if (__builtin_expect (mutex->__data.__count + 1 == 0, 0))
138 /* Overflow of the counter. */
139 return EAGAIN;
141 ++mutex->__data.__count;
143 return 0;
147 oldval = lll_robust_mutex_trylock (mutex->__data.__lock, id);
148 if (oldval != 0 && (oldval & FUTEX_OWNER_DIED) == 0)
150 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
152 return EBUSY;
155 robust:
156 if (__builtin_expect (mutex->__data.__owner
157 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
159 /* This mutex is now not recoverable. */
160 mutex->__data.__count = 0;
161 if (oldval == id)
162 lll_mutex_unlock (mutex->__data.__lock);
163 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
164 return ENOTRECOVERABLE;
167 while ((oldval & FUTEX_OWNER_DIED) != 0);
169 ENQUEUE_MUTEX (mutex);
170 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
172 mutex->__data.__owner = id;
173 ++mutex->__data.__nusers;
174 mutex->__data.__count = 1;
176 return 0;
178 default:
179 /* Correct code cannot set any other type. */
180 return EINVAL;
183 return EBUSY;
185 strong_alias (__pthread_mutex_trylock, pthread_mutex_trylock)