PowerPC: multiarch trunc/truncf for PowerPC64
[glibc.git] / nptl / pthread_rwlock_timedwrlock.c
blob1e1e95821f72742602d6a4f98180a06f4f49f004
1 /* Copyright (C) 2003-2013 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Martin Schwidefsky <schwidefsky@de.ibm.com>, 2003.
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 <errno.h>
20 #include <sysdep.h>
21 #include <lowlevellock.h>
22 #include <pthread.h>
23 #include <pthreadP.h>
26 /* Try to acquire write lock for RWLOCK or return after specfied time. */
27 int
28 pthread_rwlock_timedwrlock (rwlock, abstime)
29 pthread_rwlock_t *rwlock;
30 const struct timespec *abstime;
32 int result = 0;
34 /* Make sure we are alone. */
35 lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
37 while (1)
39 int err;
41 /* Get the rwlock if there is no writer and no reader. */
42 if (rwlock->__data.__writer == 0 && rwlock->__data.__nr_readers == 0)
44 /* Mark self as writer. */
45 rwlock->__data.__writer = THREAD_GETMEM (THREAD_SELF, tid);
46 break;
49 /* Make sure we are not holding the rwlock as a writer. This is
50 a deadlock situation we recognize and report. */
51 if (__builtin_expect (rwlock->__data.__writer
52 == THREAD_GETMEM (THREAD_SELF, tid), 0))
54 result = EDEADLK;
55 break;
58 /* Make sure the passed in timeout value is valid. Ideally this
59 test would be executed once. But since it must not be
60 performed if we would not block at all simply moving the test
61 to the front is no option. Replicating all the code is
62 costly while this test is not. */
63 if (__builtin_expect (abstime->tv_nsec >= 1000000000
64 || abstime->tv_nsec < 0, 0))
66 result = EINVAL;
67 break;
70 /* Work around the fact that the kernel rejects negative timeout values
71 despite them being valid. */
72 if (__builtin_expect (abstime->tv_sec < 0, 0))
74 result = ETIMEDOUT;
75 break;
78 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
79 || !defined lll_futex_timed_wait_bitset)
80 /* Get the current time. So far we support only one clock. */
81 struct timeval tv;
82 (void) gettimeofday (&tv, NULL);
84 /* Convert the absolute timeout value to a relative timeout. */
85 struct timespec rt;
86 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
87 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
88 if (rt.tv_nsec < 0)
90 rt.tv_nsec += 1000000000;
91 --rt.tv_sec;
93 /* Did we already time out? */
94 if (rt.tv_sec < 0)
96 result = ETIMEDOUT;
97 break;
99 #endif
101 /* Remember that we are a writer. */
102 if (++rwlock->__data.__nr_writers_queued == 0)
104 /* Overflow on number of queued writers. */
105 --rwlock->__data.__nr_writers_queued;
106 result = EAGAIN;
107 break;
110 int waitval = rwlock->__data.__writer_wakeup;
112 /* Free the lock. */
113 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
115 /* Wait for the writer or reader(s) to finish. */
116 #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
117 || !defined lll_futex_timed_wait_bitset)
118 err = lll_futex_timed_wait (&rwlock->__data.__writer_wakeup,
119 waitval, &rt, rwlock->__data.__shared);
120 #else
121 err = lll_futex_timed_wait_bitset (&rwlock->__data.__writer_wakeup,
122 waitval, abstime,
123 FUTEX_CLOCK_REALTIME,
124 rwlock->__data.__shared);
125 #endif
127 /* Get the lock. */
128 lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
130 /* To start over again, remove the thread from the writer list. */
131 --rwlock->__data.__nr_writers_queued;
133 /* Did the futex call time out? */
134 if (err == -ETIMEDOUT)
136 result = ETIMEDOUT;
137 break;
141 /* We are done, free the lock. */
142 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
144 return result;