Define XTABS to TAB3 on alpha to match Linux 4.16.
[glibc.git] / sysdeps / htl / pt-mutex-trylock.c
blobe9ad4f320444e5331e9ae8d30dbd5bf8e230f1f5
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.
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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <pthread.h>
21 #include <pt-internal.h>
23 #define LOSE do { * (int *) 0 = 0; } while (1)
25 /* Lock MUTEX, return EBUSY if we can't get it. */
26 int
27 __pthread_mutex_trylock (struct __pthread_mutex *mutex)
29 int err;
30 struct __pthread *self;
31 const struct __pthread_mutexattr *attr = mutex->__attr;
33 if (attr == __PTHREAD_ERRORCHECK_MUTEXATTR)
34 attr = &__pthread_errorcheck_mutexattr;
35 if (attr == __PTHREAD_RECURSIVE_MUTEXATTR)
36 attr = &__pthread_recursive_mutexattr;
38 __pthread_spin_lock (&mutex->__lock);
39 if (__pthread_spin_trylock (&mutex->__held) == 0)
40 /* Acquired the lock. */
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. */
50 assert (mutex->__owner == NULL);
51 mutex->__owner = _pthread_self ();
53 # endif
54 #endif
56 if (attr != NULL)
57 switch (attr->__mutex_type)
59 case PTHREAD_MUTEX_NORMAL:
60 break;
62 case PTHREAD_MUTEX_RECURSIVE:
63 mutex->__locks = 1;
64 case PTHREAD_MUTEX_ERRORCHECK:
65 mutex->__owner = _pthread_self ();
66 break;
68 default:
69 LOSE;
72 __pthread_spin_unlock (&mutex->__lock);
73 return 0;
76 err = EBUSY;
78 if (attr != NULL)
80 self = _pthread_self ();
81 switch (attr->__mutex_type)
83 case PTHREAD_MUTEX_NORMAL:
84 break;
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;
92 case PTHREAD_MUTEX_RECURSIVE:
93 if (mutex->__owner == self)
95 mutex->__locks++;
96 err = 0;
98 break;
100 default:
101 LOSE;
105 __pthread_spin_unlock (&mutex->__lock);
107 return err;
110 strong_alias (__pthread_mutex_trylock, _pthread_mutex_trylock);
111 strong_alias (__pthread_mutex_trylock, pthread_mutex_trylock);