Remove i486 subdirectory
[glibc.git] / nptl / pthread_rwlock_wrlock.c
blob60fa909340790b80e9116f9df842fdd8f1d351c0
1 /* Copyright (C) 2003-2015 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 <futex-internal.h>
23 #include <pthread.h>
24 #include <pthreadP.h>
25 #include <stap-probe.h>
26 #include <elide.h>
29 /* Acquire write lock for RWLOCK. */
30 static int __attribute__((noinline))
31 __pthread_rwlock_wrlock_slow (pthread_rwlock_t *rwlock)
33 int result = 0;
34 int futex_shared =
35 rwlock->__data.__shared == LLL_PRIVATE ? FUTEX_PRIVATE : FUTEX_SHARED;
37 /* Caller has taken the lock. */
39 while (1)
41 /* Make sure we are not holding the rwlock as a writer. This is
42 a deadlock situation we recognize and report. */
43 if (__builtin_expect (rwlock->__data.__writer
44 == THREAD_GETMEM (THREAD_SELF, tid), 0))
46 result = EDEADLK;
47 break;
50 /* Remember that we are a writer. */
51 if (++rwlock->__data.__nr_writers_queued == 0)
53 /* Overflow on number of queued writers. */
54 --rwlock->__data.__nr_writers_queued;
55 result = EAGAIN;
56 break;
59 int waitval = rwlock->__data.__writer_wakeup;
61 /* Free the lock. */
62 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
64 /* Wait for the writer or reader(s) to finish. We do not check the
65 return value because we decide how to continue based on the state of
66 the rwlock. */
67 futex_wait_simple (&rwlock->__data.__writer_wakeup, waitval,
68 futex_shared);
70 /* Get the lock. */
71 lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
73 /* To start over again, remove the thread from the writer list. */
74 --rwlock->__data.__nr_writers_queued;
76 /* Get the rwlock if there is no writer and no reader. */
77 if (rwlock->__data.__writer == 0 && rwlock->__data.__nr_readers == 0)
79 /* Mark self as writer. */
80 rwlock->__data.__writer = THREAD_GETMEM (THREAD_SELF, tid);
82 LIBC_PROBE (wrlock_acquire_write, 1, rwlock);
83 break;
87 /* We are done, free the lock. */
88 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
90 return result;
93 /* Fast path of acquiring write lock for RWLOCK. */
95 int
96 __pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
98 LIBC_PROBE (wrlock_entry, 1, rwlock);
100 if (ELIDE_LOCK (rwlock->__data.__rwelision,
101 rwlock->__data.__lock == 0
102 && rwlock->__data.__writer == 0
103 && rwlock->__data.__nr_readers == 0))
104 return 0;
106 /* Make sure we are alone. */
107 lll_lock (rwlock->__data.__lock, rwlock->__data.__shared);
109 /* Get the rwlock if there is no writer and no reader. */
110 if (__glibc_likely((rwlock->__data.__writer |
111 rwlock->__data.__nr_readers) == 0))
113 /* Mark self as writer. */
114 rwlock->__data.__writer = THREAD_GETMEM (THREAD_SELF, tid);
116 LIBC_PROBE (wrlock_acquire_write, 1, rwlock);
118 /* We are done, free the lock. */
119 lll_unlock (rwlock->__data.__lock, rwlock->__data.__shared);
121 return 0;
124 return __pthread_rwlock_wrlock_slow (rwlock);
128 weak_alias (__pthread_rwlock_wrlock, pthread_rwlock_wrlock)
129 hidden_def (__pthread_rwlock_wrlock)