]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/htl/pt-mutex-timedlock.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / htl / pt-mutex-timedlock.c
1 /* Lock a mutex with a timeout. Generic version.
2 Copyright (C) 2000-2020 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 <https://www.gnu.org/licenses/>. */
18
19 #include <pthread.h>
20 #include <assert.h>
21 #include <time.h>
22
23 #include <pt-internal.h>
24
25 #define LOSE do { * (int *) 0 = 0; } while (1)
26
27 /* Try to lock MUTEX, block until *ABSTIME if it is already held. As
28 a GNU extension, if TIMESPEC is NULL then wait forever. */
29 int
30 __pthread_mutex_timedlock_internal (struct __pthread_mutex *mutex,
31 const struct timespec *abstime)
32 {
33 error_t err;
34 int drain;
35 struct __pthread *self;
36 const struct __pthread_mutexattr *attr = mutex->__attr;
37
38 if (attr == __PTHREAD_ERRORCHECK_MUTEXATTR)
39 attr = &__pthread_errorcheck_mutexattr;
40 if (attr == __PTHREAD_RECURSIVE_MUTEXATTR)
41 attr = &__pthread_recursive_mutexattr;
42
43 __pthread_spin_lock (&mutex->__lock);
44 if (__pthread_spin_trylock (&mutex->__held) == 0)
45 /* Successfully acquired the lock. */
46 {
47 #ifdef ALWAYS_TRACK_MUTEX_OWNER
48 # ifndef NDEBUG
49 self = _pthread_self ();
50 if (self != NULL)
51 /* The main thread may take a lock before the library is fully
52 initialized, in particular, before the main thread has a
53 TCB. */
54 {
55 assert (mutex->__owner == NULL);
56 mutex->__owner = _pthread_self ();
57 }
58 # endif
59 #endif
60
61 if (attr != NULL)
62 switch (attr->__mutex_type)
63 {
64 case PTHREAD_MUTEX_NORMAL:
65 break;
66
67 case PTHREAD_MUTEX_RECURSIVE:
68 mutex->__locks = 1;
69 case PTHREAD_MUTEX_ERRORCHECK:
70 mutex->__owner = _pthread_self ();
71 break;
72
73 default:
74 LOSE;
75 }
76
77 __pthread_spin_unlock (&mutex->__lock);
78 return 0;
79 }
80
81 /* The lock is busy. */
82
83 self = _pthread_self ();
84 assert (self);
85
86 if (attr == NULL || attr->__mutex_type == PTHREAD_MUTEX_NORMAL)
87 {
88 #if defined(ALWAYS_TRACK_MUTEX_OWNER)
89 assert (mutex->__owner != self);
90 #endif
91 }
92 else
93 {
94 switch (attr->__mutex_type)
95 {
96 case PTHREAD_MUTEX_ERRORCHECK:
97 if (mutex->__owner == self)
98 {
99 __pthread_spin_unlock (&mutex->__lock);
100 return EDEADLK;
101 }
102 break;
103
104 case PTHREAD_MUTEX_RECURSIVE:
105 if (mutex->__owner == self)
106 {
107 mutex->__locks++;
108 __pthread_spin_unlock (&mutex->__lock);
109 return 0;
110 }
111 break;
112
113 default:
114 LOSE;
115 }
116 }
117
118 #if !defined(ALWAYS_TRACK_MUTEX_OWNER)
119 if (attr != NULL && attr->__mutex_type != PTHREAD_MUTEX_NORMAL)
120 #endif
121 assert (mutex->__owner);
122
123 if (abstime != NULL && ! valid_nanoseconds (abstime->tv_nsec))
124 return EINVAL;
125
126 /* Add ourselves to the queue. */
127 __pthread_enqueue (&mutex->__queue, self);
128 __pthread_spin_unlock (&mutex->__lock);
129
130 /* Block the thread. */
131 if (abstime != NULL)
132 err = __pthread_timedblock (self, abstime, CLOCK_REALTIME);
133 else
134 {
135 err = 0;
136 __pthread_block (self);
137 }
138
139 __pthread_spin_lock (&mutex->__lock);
140 if (self->prevp == NULL)
141 /* Another thread removed us from the queue, which means a wakeup message
142 has been sent. It was either consumed while we were blocking, or
143 queued after we timed out and before we acquired the mutex lock, in
144 which case the message queue must be drained. */
145 drain = err ? 1 : 0;
146 else
147 {
148 /* We're still in the queue. Noone attempted to wake us up, i.e. we
149 timed out. */
150 __pthread_dequeue (self);
151 drain = 0;
152 }
153 __pthread_spin_unlock (&mutex->__lock);
154
155 if (drain)
156 __pthread_block (self);
157
158 if (err)
159 {
160 assert (err == ETIMEDOUT);
161 return err;
162 }
163
164 #if !defined(ALWAYS_TRACK_MUTEX_OWNER)
165 if (attr != NULL && attr->__mutex_type != PTHREAD_MUTEX_NORMAL)
166 #endif
167 {
168 assert (mutex->__owner == self);
169 }
170
171 if (attr != NULL)
172 switch (attr->__mutex_type)
173 {
174 case PTHREAD_MUTEX_NORMAL:
175 break;
176
177 case PTHREAD_MUTEX_RECURSIVE:
178 assert (mutex->__locks == 0);
179 mutex->__locks = 1;
180 case PTHREAD_MUTEX_ERRORCHECK:
181 mutex->__owner = self;
182 break;
183
184 default:
185 LOSE;
186 }
187
188 return 0;
189 }
190
191 int
192 pthread_mutex_timedlock (struct __pthread_mutex *mutex,
193 const struct timespec *abstime)
194 {
195 return __pthread_mutex_timedlock_internal (mutex, abstime);
196 }