Define XTABS to TAB3 on alpha to match Linux 4.16.
[glibc.git] / sysdeps / htl / pt-rwlock-timedwrlock.c
blob4452dd9af4f8ab4fa4c22b7982ab91de44f274a0
1 /* Acquire a rwlock for writing. Generic version.
2 Copyright (C) 2002-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/>. */
19 #include <pthread.h>
20 #include <assert.h>
22 #include <pt-internal.h>
24 /* Acquire RWLOCK for writing blocking until *ABSTIME if we cannot get
25 it. As a special GNU extension, if ABSTIME is NULL then the wait
26 shall not time out. */
27 int
28 __pthread_rwlock_timedwrlock_internal (struct __pthread_rwlock *rwlock,
29 const struct timespec *abstime)
31 error_t err;
32 int drain;
33 struct __pthread *self;
35 __pthread_spin_lock (&rwlock->__lock);
36 if (__pthread_spin_trylock (&rwlock->__held) == 0)
37 /* Successfully acquired the lock. */
39 assert (rwlock->__readerqueue == 0);
40 assert (rwlock->__writerqueue == 0);
41 assert (rwlock->__readers == 0);
43 __pthread_spin_unlock (&rwlock->__lock);
44 return 0;
47 /* The lock is busy. */
49 if (abstime != NULL && (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000))
50 return EINVAL;
52 self = _pthread_self ();
54 /* Add ourselves to the queue. */
55 __pthread_enqueue (&rwlock->__writerqueue, self);
56 __pthread_spin_unlock (&rwlock->__lock);
58 /* Block the thread. */
59 if (abstime != NULL)
60 err = __pthread_timedblock (self, abstime, CLOCK_REALTIME);
61 else
63 err = 0;
64 __pthread_block (self);
67 __pthread_spin_lock (&rwlock->__lock);
68 if (self->prevp == NULL)
69 /* Another thread removed us from the queue, which means a wakeup message
70 has been sent. It was either consumed while we were blocking, or
71 queued after we timed out and before we acquired the rwlock lock, in
72 which case the message queue must be drained. */
73 drain = err ? 1 : 0;
74 else
76 /* We're still in the queue. Noone attempted to wake us up, i.e. we
77 timed out. */
78 __pthread_dequeue (self);
79 drain = 0;
81 __pthread_spin_unlock (&rwlock->__lock);
83 if (drain)
84 __pthread_block (self);
86 if (err)
88 assert (err == ETIMEDOUT);
89 return err;
92 assert (rwlock->__readers == 0);
94 return 0;
97 int
98 __pthread_rwlock_timedwrlock (struct __pthread_rwlock *rwlock,
99 const struct timespec *abstime)
101 return __pthread_rwlock_timedwrlock_internal (rwlock, abstime);
103 weak_alias (__pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock)