]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/htl/pt-rwlock-timedwrlock.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / sysdeps / htl / pt-rwlock-timedwrlock.c
CommitLineData
33574c17 1/* Acquire a rwlock for writing. Generic version.
04277e02 2 Copyright (C) 2002-2019 Free Software Foundation, Inc.
33574c17
ST
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
ad2b41bf
ST
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.
33574c17
ST
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
ad2b41bf 13 Lesser General Public License for more details.
33574c17 14
ad2b41bf
ST
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
33574c17
ST
18
19#include <pthread.h>
20#include <assert.h>
21
22#include <pt-internal.h>
23
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. */
27int
28__pthread_rwlock_timedwrlock_internal (struct __pthread_rwlock *rwlock,
29 const struct timespec *abstime)
30{
31 error_t err;
32 int drain;
33 struct __pthread *self;
34
35 __pthread_spin_lock (&rwlock->__lock);
36 if (__pthread_spin_trylock (&rwlock->__held) == 0)
37 /* Successfully acquired the lock. */
38 {
39 assert (rwlock->__readerqueue == 0);
40 assert (rwlock->__writerqueue == 0);
41 assert (rwlock->__readers == 0);
42
43 __pthread_spin_unlock (&rwlock->__lock);
44 return 0;
45 }
46
47 /* The lock is busy. */
48
49 if (abstime != NULL && (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000))
50 return EINVAL;
51
52 self = _pthread_self ();
53
54 /* Add ourselves to the queue. */
55 __pthread_enqueue (&rwlock->__writerqueue, self);
56 __pthread_spin_unlock (&rwlock->__lock);
57
58 /* Block the thread. */
59 if (abstime != NULL)
60 err = __pthread_timedblock (self, abstime, CLOCK_REALTIME);
61 else
62 {
63 err = 0;
64 __pthread_block (self);
65 }
66
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
75 {
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;
80 }
81 __pthread_spin_unlock (&rwlock->__lock);
82
83 if (drain)
84 __pthread_block (self);
85
86 if (err)
87 {
88 assert (err == ETIMEDOUT);
89 return err;
90 }
91
92 assert (rwlock->__readers == 0);
93
94 return 0;
95}
96
97int
98__pthread_rwlock_timedwrlock (struct __pthread_rwlock *rwlock,
99 const struct timespec *abstime)
100{
101 return __pthread_rwlock_timedwrlock_internal (rwlock, abstime);
102}
103weak_alias (__pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock)