]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/htl/pt-mutex-trylock.c
e9ad4f320444e5331e9ae8d30dbd5bf8e230f1f5
[thirdparty/glibc.git] / sysdeps / htl / pt-mutex-trylock.c
1 /* Try to Lock a mutex. Generic version.
2 Copyright (C) 2002-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <pthread.h>
20
21 #include <pt-internal.h>
22
23 #define LOSE do { * (int *) 0 = 0; } while (1)
24
25 /* Lock MUTEX, return EBUSY if we can't get it. */
26 int
27 __pthread_mutex_trylock (struct __pthread_mutex *mutex)
28 {
29 int err;
30 struct __pthread *self;
31 const struct __pthread_mutexattr *attr = mutex->__attr;
32
33 if (attr == __PTHREAD_ERRORCHECK_MUTEXATTR)
34 attr = &__pthread_errorcheck_mutexattr;
35 if (attr == __PTHREAD_RECURSIVE_MUTEXATTR)
36 attr = &__pthread_recursive_mutexattr;
37
38 __pthread_spin_lock (&mutex->__lock);
39 if (__pthread_spin_trylock (&mutex->__held) == 0)
40 /* Acquired the lock. */
41 {
42 #if defined(ALWAYS_TRACK_MUTEX_OWNER)
43 # ifndef NDEBUG
44 self = _pthread_self ();
45 if (self != NULL)
46 /* The main thread may take a lock before the library is fully
47 initialized, in particular, before the main thread has a
48 TCB. */
49 {
50 assert (mutex->__owner == NULL);
51 mutex->__owner = _pthread_self ();
52 }
53 # endif
54 #endif
55
56 if (attr != NULL)
57 switch (attr->__mutex_type)
58 {
59 case PTHREAD_MUTEX_NORMAL:
60 break;
61
62 case PTHREAD_MUTEX_RECURSIVE:
63 mutex->__locks = 1;
64 case PTHREAD_MUTEX_ERRORCHECK:
65 mutex->__owner = _pthread_self ();
66 break;
67
68 default:
69 LOSE;
70 }
71
72 __pthread_spin_unlock (&mutex->__lock);
73 return 0;
74 }
75
76 err = EBUSY;
77
78 if (attr != NULL)
79 {
80 self = _pthread_self ();
81 switch (attr->__mutex_type)
82 {
83 case PTHREAD_MUTEX_NORMAL:
84 break;
85
86 case PTHREAD_MUTEX_ERRORCHECK:
87 /* We could check if MUTEX->OWNER is SELF, however, POSIX
88 does not permit pthread_mutex_trylock to return EDEADLK
89 instead of EBUSY, only pthread_mutex_lock. */
90 break;
91
92 case PTHREAD_MUTEX_RECURSIVE:
93 if (mutex->__owner == self)
94 {
95 mutex->__locks++;
96 err = 0;
97 }
98 break;
99
100 default:
101 LOSE;
102 }
103 }
104
105 __pthread_spin_unlock (&mutex->__lock);
106
107 return err;
108 }
109
110 strong_alias (__pthread_mutex_trylock, _pthread_mutex_trylock);
111 strong_alias (__pthread_mutex_trylock, pthread_mutex_trylock);