1 /* Lock a mutex with a timeout. 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/>. */
22 #include <pt-internal.h>
24 #define LOSE do { * (int *) 0 = 0; } while (1)
26 /* Try to lock MUTEX, block until *ABSTIME if it is already held. As
27 a GNU extension, if TIMESPEC is NULL then wait forever. */
29 __pthread_mutex_timedlock_internal (struct __pthread_mutex
*mutex
,
30 const struct timespec
*abstime
)
34 struct __pthread
*self
;
35 const struct __pthread_mutexattr
*attr
= mutex
->__attr
;
37 if (attr
== __PTHREAD_ERRORCHECK_MUTEXATTR
)
38 attr
= &__pthread_errorcheck_mutexattr
;
39 if (attr
== __PTHREAD_RECURSIVE_MUTEXATTR
)
40 attr
= &__pthread_recursive_mutexattr
;
42 __pthread_spin_lock (&mutex
->__lock
);
43 if (__pthread_spin_trylock (&mutex
->__held
) == 0)
44 /* Successfully acquired the lock. */
46 #ifdef ALWAYS_TRACK_MUTEX_OWNER
48 self
= _pthread_self ();
50 /* The main thread may take a lock before the library is fully
51 initialized, in particular, before the main thread has a
54 assert (mutex
->__owner
== NULL
);
55 mutex
->__owner
= _pthread_self ();
61 switch (attr
->__mutex_type
)
63 case PTHREAD_MUTEX_NORMAL
:
66 case PTHREAD_MUTEX_RECURSIVE
:
68 case PTHREAD_MUTEX_ERRORCHECK
:
69 mutex
->__owner
= _pthread_self ();
76 __pthread_spin_unlock (&mutex
->__lock
);
80 /* The lock is busy. */
82 self
= _pthread_self ();
85 if (attr
== NULL
|| attr
->__mutex_type
== PTHREAD_MUTEX_NORMAL
)
87 #if defined(ALWAYS_TRACK_MUTEX_OWNER)
88 assert (mutex
->__owner
!= self
);
93 switch (attr
->__mutex_type
)
95 case PTHREAD_MUTEX_ERRORCHECK
:
96 if (mutex
->__owner
== self
)
98 __pthread_spin_unlock (&mutex
->__lock
);
103 case PTHREAD_MUTEX_RECURSIVE
:
104 if (mutex
->__owner
== self
)
107 __pthread_spin_unlock (&mutex
->__lock
);
117 #if !defined(ALWAYS_TRACK_MUTEX_OWNER)
118 if (attr
!= NULL
&& attr
->__mutex_type
!= PTHREAD_MUTEX_NORMAL
)
120 assert (mutex
->__owner
);
122 if (abstime
!= NULL
&& (abstime
->tv_nsec
< 0 || abstime
->tv_nsec
>= 1000000000))
125 /* Add ourselves to the queue. */
126 __pthread_enqueue (&mutex
->__queue
, self
);
127 __pthread_spin_unlock (&mutex
->__lock
);
129 /* Block the thread. */
131 err
= __pthread_timedblock (self
, abstime
, CLOCK_REALTIME
);
135 __pthread_block (self
);
138 __pthread_spin_lock (&mutex
->__lock
);
139 if (self
->prevp
== NULL
)
140 /* Another thread removed us from the queue, which means a wakeup message
141 has been sent. It was either consumed while we were blocking, or
142 queued after we timed out and before we acquired the mutex lock, in
143 which case the message queue must be drained. */
147 /* We're still in the queue. Noone attempted to wake us up, i.e. we
149 __pthread_dequeue (self
);
152 __pthread_spin_unlock (&mutex
->__lock
);
155 __pthread_block (self
);
159 assert (err
== ETIMEDOUT
);
163 #if !defined(ALWAYS_TRACK_MUTEX_OWNER)
164 if (attr
!= NULL
&& attr
->__mutex_type
!= PTHREAD_MUTEX_NORMAL
)
167 assert (mutex
->__owner
== self
);
171 switch (attr
->__mutex_type
)
173 case PTHREAD_MUTEX_NORMAL
:
176 case PTHREAD_MUTEX_RECURSIVE
:
177 assert (mutex
->__locks
== 0);
179 case PTHREAD_MUTEX_ERRORCHECK
:
180 mutex
->__owner
= self
;
191 pthread_mutex_timedlock (struct __pthread_mutex
*mutex
,
192 const struct timespec
*abstime
)
194 return __pthread_mutex_timedlock_internal (mutex
, abstime
);