Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* Unlock a mutex. Generic version.
2 Copyright (C) 2000-2019 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/* Unlock MUTEX, rescheduling a waiting thread. */
26int
27__pthread_mutex_unlock (pthread_mutex_t *mutex)
28{
29 struct __pthread *wakeup;
30 const struct __pthread_mutexattr *attr = mutex->__attr;
31
32 if (attr == __PTHREAD_ERRORCHECK_MUTEXATTR)
33 attr = &__pthread_errorcheck_mutexattr;
34 if (attr == __PTHREAD_RECURSIVE_MUTEXATTR)
35 attr = &__pthread_recursive_mutexattr;
36
37 __pthread_spin_lock (&mutex->__lock);
38
39 if (attr == NULL || attr->__mutex_type == PTHREAD_MUTEX_NORMAL)
40 {
41#if defined(ALWAYS_TRACK_MUTEX_OWNER)
42# ifndef NDEBUG
43 if (_pthread_self ())
44 {
45 assert (mutex->__owner);
46 assert (mutex->__owner == _pthread_self ());
47 mutex->__owner = NULL;
48 }
49# endif
50#endif
51 }
52 else
53 switch (attr->__mutex_type)
54 {
55 case PTHREAD_MUTEX_ERRORCHECK:
56 case PTHREAD_MUTEX_RECURSIVE:
57 if (mutex->__owner != _pthread_self ())
58 {
59 __pthread_spin_unlock (&mutex->__lock);
60 return EPERM;
61 }
62
63 if (attr->__mutex_type == PTHREAD_MUTEX_RECURSIVE)
64 if (--mutex->__locks > 0)
65 {
66 __pthread_spin_unlock (&mutex->__lock);
67 return 0;
68 }
69
70 mutex->__owner = 0;
71 break;
72
73 default:
74 LOSE;
75 }
76
77
78 if (mutex->__queue == NULL)
79 {
80 __pthread_spin_unlock (&mutex->__held);
81 __pthread_spin_unlock (&mutex->__lock);
82 return 0;
83 }
84
85 wakeup = mutex->__queue;
86 __pthread_dequeue (wakeup);
87
88#ifndef NDEBUG
89# if !defined (ALWAYS_TRACK_MUTEX_OWNER)
90 if (attr != NULL && attr->__mutex_type != PTHREAD_MUTEX_NORMAL)
91# endif
92 {
93 mutex->__owner = wakeup;
94 }
95#endif
96
97 /* We do not unlock MUTEX->held: we are transferring the ownership
98 to the thread that we are waking up. */
99
100 __pthread_spin_unlock (&mutex->__lock);
101 __pthread_wakeup (wakeup);
102
103 return 0;
104}
105
106strong_alias (__pthread_mutex_unlock, _pthread_mutex_unlock);
107strong_alias (__pthread_mutex_unlock, pthread_mutex_unlock);
108

Warning: That file was not part of the compilation database. It may have many parsing errors.