1 /* Unlock a mutex. Generic version.
2 Copyright (C) 2000-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 /* Unlock MUTEX, rescheduling a waiting thread. */
27 __pthread_mutex_unlock (pthread_mutex_t
*mutex
)
29 struct __pthread
*wakeup
;
30 const struct __pthread_mutexattr
*attr
= mutex
->__attr
;
32 if (attr
== __PTHREAD_ERRORCHECK_MUTEXATTR
)
33 attr
= &__pthread_errorcheck_mutexattr
;
34 if (attr
== __PTHREAD_RECURSIVE_MUTEXATTR
)
35 attr
= &__pthread_recursive_mutexattr
;
37 __pthread_spin_lock (&mutex
->__lock
);
39 if (attr
== NULL
|| attr
->__mutex_type
== PTHREAD_MUTEX_NORMAL
)
41 #if defined(ALWAYS_TRACK_MUTEX_OWNER)
45 assert (mutex
->__owner
);
46 assert (mutex
->__owner
== _pthread_self ());
47 mutex
->__owner
= NULL
;
53 switch (attr
->__mutex_type
)
55 case PTHREAD_MUTEX_ERRORCHECK
:
56 case PTHREAD_MUTEX_RECURSIVE
:
57 if (mutex
->__owner
!= _pthread_self ())
59 __pthread_spin_unlock (&mutex
->__lock
);
63 if (attr
->__mutex_type
== PTHREAD_MUTEX_RECURSIVE
)
64 if (--mutex
->__locks
> 0)
66 __pthread_spin_unlock (&mutex
->__lock
);
78 if (mutex
->__queue
== NULL
)
80 __pthread_spin_unlock (&mutex
->__held
);
81 __pthread_spin_unlock (&mutex
->__lock
);
85 wakeup
= mutex
->__queue
;
86 __pthread_dequeue (wakeup
);
89 # if !defined (ALWAYS_TRACK_MUTEX_OWNER)
90 if (attr
!= NULL
&& attr
->__mutex_type
!= PTHREAD_MUTEX_NORMAL
)
93 mutex
->__owner
= wakeup
;
97 /* We do not unlock MUTEX->held: we are transferring the ownership
98 to the thread that we are waking up. */
100 __pthread_spin_unlock (&mutex
->__lock
);
101 __pthread_wakeup (wakeup
);
106 strong_alias (__pthread_mutex_unlock
, _pthread_mutex_unlock
);
107 strong_alias (__pthread_mutex_unlock
, pthread_mutex_unlock
);