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.
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 <https://www.gnu.org/licenses/>. */
23 #include <pt-internal.h>
25 #define LOSE do { * (int *) 0 = 0; } while (1)
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. */
30 __pthread_mutex_timedlock_internal (struct __pthread_mutex
*mutex
,
31 const struct timespec
*abstime
)
35 struct __pthread
*self
;
36 const struct __pthread_mutexattr
*attr
= mutex
->__attr
;
38 if (attr
== __PTHREAD_ERRORCHECK_MUTEXATTR
)
39 attr
= &__pthread_errorcheck_mutexattr
;
40 if (attr
== __PTHREAD_RECURSIVE_MUTEXATTR
)
41 attr
= &__pthread_recursive_mutexattr
;
43 __pthread_spin_lock (&mutex
->__lock
);
44 if (__pthread_spin_trylock (&mutex
->__held
) == 0)
45 /* Successfully acquired the lock. */
47 #ifdef ALWAYS_TRACK_MUTEX_OWNER
49 self
= _pthread_self ();
51 /* The main thread may take a lock before the library is fully
52 initialized, in particular, before the main thread has a
55 assert (mutex
->__owner
== NULL
);
56 mutex
->__owner
= _pthread_self ();
62 switch (attr
->__mutex_type
)
64 case PTHREAD_MUTEX_NORMAL
:
67 case PTHREAD_MUTEX_RECURSIVE
:
69 case PTHREAD_MUTEX_ERRORCHECK
:
70 mutex
->__owner
= _pthread_self ();
77 __pthread_spin_unlock (&mutex
->__lock
);
81 /* The lock is busy. */
83 self
= _pthread_self ();
86 if (attr
== NULL
|| attr
->__mutex_type
== PTHREAD_MUTEX_NORMAL
)
88 #if defined(ALWAYS_TRACK_MUTEX_OWNER)
89 assert (mutex
->__owner
!= self
);
94 switch (attr
->__mutex_type
)
96 case PTHREAD_MUTEX_ERRORCHECK
:
97 if (mutex
->__owner
== self
)
99 __pthread_spin_unlock (&mutex
->__lock
);
104 case PTHREAD_MUTEX_RECURSIVE
:
105 if (mutex
->__owner
== self
)
108 __pthread_spin_unlock (&mutex
->__lock
);
118 #if !defined(ALWAYS_TRACK_MUTEX_OWNER)
119 if (attr
!= NULL
&& attr
->__mutex_type
!= PTHREAD_MUTEX_NORMAL
)
121 assert (mutex
->__owner
);
123 if (abstime
!= NULL
&& ! valid_nanoseconds (abstime
->tv_nsec
))
126 /* Add ourselves to the queue. */
127 __pthread_enqueue (&mutex
->__queue
, self
);
128 __pthread_spin_unlock (&mutex
->__lock
);
130 /* Block the thread. */
132 err
= __pthread_timedblock (self
, abstime
, CLOCK_REALTIME
);
136 __pthread_block (self
);
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. */
148 /* We're still in the queue. Noone attempted to wake us up, i.e. we
150 __pthread_dequeue (self
);
153 __pthread_spin_unlock (&mutex
->__lock
);
156 __pthread_block (self
);
160 assert (err
== ETIMEDOUT
);
164 #if !defined(ALWAYS_TRACK_MUTEX_OWNER)
165 if (attr
!= NULL
&& attr
->__mutex_type
!= PTHREAD_MUTEX_NORMAL
)
168 assert (mutex
->__owner
== self
);
172 switch (attr
->__mutex_type
)
174 case PTHREAD_MUTEX_NORMAL
:
177 case PTHREAD_MUTEX_RECURSIVE
:
178 assert (mutex
->__locks
== 0);
180 case PTHREAD_MUTEX_ERRORCHECK
:
181 mutex
->__owner
= self
;
192 pthread_mutex_timedlock (struct __pthread_mutex
*mutex
,
193 const struct timespec
*abstime
)
195 return __pthread_mutex_timedlock_internal (mutex
, abstime
);