* locales/as_IN: Fix currency_symbol, abday for Sunday, abmon for
[glibc/pb-stable.git] / nptl / pthread_rwlock_timedwrlock.c
blob97c0598f96f9866558e493799269559572a8262f
1 /* Copyright (C) 2003, 2004 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <sysdep.h>
22 #include <lowlevellock.h>
23 #include <pthread.h>
24 #include <pthreadP.h>
27 /* Try to acquire write lock for RWLOCK or return after specfied time. */
28 int
29 pthread_rwlock_timedwrlock (rwlock, abstime)
30 pthread_rwlock_t *rwlock;
31 const struct timespec *abstime;
33 int result = 0;
35 /* Make sure we are along. */
36 lll_mutex_lock (rwlock->__data.__lock);
38 while (1)
40 int err;
42 /* Get the rwlock if there is no writer and no reader. */
43 if (rwlock->__data.__writer == 0 && rwlock->__data.__nr_readers == 0)
45 /* Mark self as writer. */
46 rwlock->__data.__writer = THREAD_GETMEM (THREAD_SELF, tid);
47 break;
50 /* Make sure we are not holding the rwlock as a writer. This is
51 a deadlock situation we recognize and report. */
52 if (__builtin_expect (rwlock->__data.__writer
53 == THREAD_GETMEM (THREAD_SELF, tid), 0))
55 result = EDEADLK;
56 break;
59 /* Make sure the passed in timeout value is valid. Ideally this
60 test would be executed once. But since it must not be
61 performed if we would not block at all simply moving the test
62 to the front is no option. Replicating all the code is
63 costly while this test is not. */
64 if (__builtin_expect (abstime->tv_nsec >= 1000000000
65 || abstime->tv_nsec < 0, 0))
67 result = EINVAL;
68 break;
71 /* Get the current time. So far we support only one clock. */
72 struct timeval tv;
73 (void) gettimeofday (&tv, NULL);
75 /* Convert the absolute timeout value to a relative timeout. */
76 struct timespec rt;
77 rt.tv_sec = abstime->tv_sec - tv.tv_sec;
78 rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
79 if (rt.tv_nsec < 0)
81 rt.tv_nsec += 1000000000;
82 --rt.tv_sec;
84 /* Did we already time out? */
85 if (rt.tv_sec < 0)
87 result = ETIMEDOUT;
88 break;
91 /* Remember that we are a writer. */
92 if (++rwlock->__data.__nr_writers_queued == 0)
94 /* Overflow on number of queued writers. */
95 --rwlock->__data.__nr_writers_queued;
96 result = EAGAIN;
97 break;
100 int waitval = rwlock->__data.__writer_wakeup;
102 /* Free the lock. */
103 lll_mutex_unlock (rwlock->__data.__lock);
105 /* Wait for the writer or reader(s) to finish. */
106 err = lll_futex_timed_wait (&rwlock->__data.__writer_wakeup,
107 waitval, &rt);
109 /* Get the lock. */
110 lll_mutex_lock (rwlock->__data.__lock);
112 /* To start over again, remove the thread from the writer list. */
113 --rwlock->__data.__nr_writers_queued;
115 /* Did the futex call time out? */
116 if (err == -ETIMEDOUT)
118 result = ETIMEDOUT;
119 break;
123 /* We are done, free the lock. */
124 lll_mutex_unlock (rwlock->__data.__lock);
126 return result;