Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* Acquire a rwlock for reading. Generic version.
2 Copyright (C) 2002-2019 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 <http://www.gnu.org/licenses/>. */
18
19#include <pthread.h>
20#include <assert.h>
21
22#include <pt-internal.h>
23
24/* Acquire the rwlock *RWLOCK for reading blocking until *ABSTIME if
25 it is already held. As a GNU extension, if TIMESPEC is NULL then
26 wait forever. */
27int
28__pthread_rwlock_timedrdlock_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 rwlock->__readers = 1;
44 __pthread_spin_unlock (&rwlock->__lock);
45 return 0;
46 }
47 else
48 /* Lock is held, but is held by a reader? */
49 if (rwlock->__readers > 0)
50 /* Just add ourself to number of readers. */
51 {
52 assert (rwlock->__readerqueue == 0);
53 rwlock->__readers++;
54 __pthread_spin_unlock (&rwlock->__lock);
55 return 0;
56 }
57
58 /* The lock is busy. */
59
60 /* Better be blocked by a writer. */
61 assert (rwlock->__readers == 0);
62
63 if (abstime != NULL && (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000))
64 return EINVAL;
65
66 self = _pthread_self ();
67
68 /* Add ourself to the queue. */
69 __pthread_enqueue (&rwlock->__readerqueue, self);
70 __pthread_spin_unlock (&rwlock->__lock);
71
72 /* Block the thread. */
73 if (abstime != NULL)
74 err = __pthread_timedblock (self, abstime, CLOCK_REALTIME);
75 else
76 {
77 err = 0;
78 __pthread_block (self);
79 }
80
81 __pthread_spin_lock (&rwlock->__lock);
82 if (self->prevp == NULL)
83 /* Another thread removed us from the queue, which means a wakeup message
84 has been sent. It was either consumed while we were blocking, or
85 queued after we timed out and before we acquired the rwlock lock, in
86 which case the message queue must be drained. */
87 drain = err ? 1 : 0;
88 else
89 {
90 /* We're still in the queue. Noone attempted to wake us up, i.e. we
91 timed out. */
92 __pthread_dequeue (self);
93 drain = 0;
94 }
95 __pthread_spin_unlock (&rwlock->__lock);
96
97 if (drain)
98 __pthread_block (self);
99
100 if (err)
101 {
102 assert (err == ETIMEDOUT);
103 return err;
104 }
105
106 /* The reader count has already been increment by whoever woke us
107 up. */
108
109 assert (rwlock->__readers > 0);
110
111 return 0;
112}
113
114int
115__pthread_rwlock_timedrdlock (struct __pthread_rwlock *rwlock,
116 const struct timespec *abstime)
117{
118 return __pthread_rwlock_timedrdlock_internal (rwlock, abstime);
119}
120weak_alias (__pthread_rwlock_timedrdlock, pthread_rwlock_timedrdlock)
121

Warning: That file was not part of the compilation database. It may have many parsing errors.