Use clock_settime to implement stime; withdraw stime.
[glibc.git] / sysdeps / htl / pt-rwlock-timedrdlock.c
blobd64c7a73903b2c606088abaccab08723dcf7e5b7
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.
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/>. */
19 #include <pthread.h>
20 #include <assert.h>
21 #include <time.h>
23 #include <pt-internal.h>
25 /* Acquire the rwlock *RWLOCK for reading blocking until *ABSTIME if
26 it is already held. As a GNU extension, if TIMESPEC is NULL then
27 wait forever. */
28 int
29 __pthread_rwlock_timedrdlock_internal (struct __pthread_rwlock *rwlock,
30 const struct timespec *abstime)
32 error_t err;
33 int drain;
34 struct __pthread *self;
36 __pthread_spin_lock (&rwlock->__lock);
37 if (__pthread_spin_trylock (&rwlock->__held) == 0)
38 /* Successfully acquired the lock. */
40 assert (rwlock->__readerqueue == 0);
41 assert (rwlock->__writerqueue == 0);
42 assert (rwlock->__readers == 0);
44 rwlock->__readers = 1;
45 __pthread_spin_unlock (&rwlock->__lock);
46 return 0;
48 else
49 /* Lock is held, but is held by a reader? */
50 if (rwlock->__readers > 0)
51 /* Just add ourself to number of readers. */
53 assert (rwlock->__readerqueue == 0);
54 rwlock->__readers++;
55 __pthread_spin_unlock (&rwlock->__lock);
56 return 0;
59 /* The lock is busy. */
61 /* Better be blocked by a writer. */
62 assert (rwlock->__readers == 0);
64 if (abstime != NULL && ! valid_nanoseconds (abstime->tv_nsec))
65 return EINVAL;
67 self = _pthread_self ();
69 /* Add ourself to the queue. */
70 __pthread_enqueue (&rwlock->__readerqueue, self);
71 __pthread_spin_unlock (&rwlock->__lock);
73 /* Block the thread. */
74 if (abstime != NULL)
75 err = __pthread_timedblock (self, abstime, CLOCK_REALTIME);
76 else
78 err = 0;
79 __pthread_block (self);
82 __pthread_spin_lock (&rwlock->__lock);
83 if (self->prevp == NULL)
84 /* Another thread removed us from the queue, which means a wakeup message
85 has been sent. It was either consumed while we were blocking, or
86 queued after we timed out and before we acquired the rwlock lock, in
87 which case the message queue must be drained. */
88 drain = err ? 1 : 0;
89 else
91 /* We're still in the queue. Noone attempted to wake us up, i.e. we
92 timed out. */
93 __pthread_dequeue (self);
94 drain = 0;
96 __pthread_spin_unlock (&rwlock->__lock);
98 if (drain)
99 __pthread_block (self);
101 if (err)
103 assert (err == ETIMEDOUT);
104 return err;
107 /* The reader count has already been increment by whoever woke us
108 up. */
110 assert (rwlock->__readers > 0);
112 return 0;
116 __pthread_rwlock_timedrdlock (struct __pthread_rwlock *rwlock,
117 const struct timespec *abstime)
119 return __pthread_rwlock_timedrdlock_internal (rwlock, abstime);
121 weak_alias (__pthread_rwlock_timedrdlock, pthread_rwlock_timedrdlock)