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/>. */
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. */
27 __pthread_mutex_trylock (struct __pthread_mutex
*mutex
)
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)
44 self
= _pthread_self ();
46 /* The main thread may take a lock before the library is fully
47 initialized, in particular, before the main thread has a
50 assert (mutex
->__owner
== NULL
);
51 mutex
->__owner
= _pthread_self ();
57 switch (attr
->__mutex_type
)
59 case PTHREAD_MUTEX_NORMAL
:
62 case PTHREAD_MUTEX_RECURSIVE
:
64 case PTHREAD_MUTEX_ERRORCHECK
:
65 mutex
->__owner
= _pthread_self ();
72 __pthread_spin_unlock (&mutex
->__lock
);
80 self
= _pthread_self ();
81 switch (attr
->__mutex_type
)
83 case PTHREAD_MUTEX_NORMAL
:
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. */
92 case PTHREAD_MUTEX_RECURSIVE
:
93 if (mutex
->__owner
== self
)
105 __pthread_spin_unlock (&mutex
->__lock
);
110 strong_alias (__pthread_mutex_trylock
, _pthread_mutex_trylock
);
111 strong_alias (__pthread_mutex_trylock
, pthread_mutex_trylock
);