Adjust name of ld.so in test-container.c.
[glibc.git] / sysdeps / htl / pt-rwlock-unlock.c
blob44b26d2b1c38f42c6336453fe9eac12dd30b7892
1 /* Unlock a rwlock. Generic version.
2 Copyright (C) 2000-2018 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 <http://www.gnu.org/licenses/>. */
19 #include <pthread.h>
21 #include <pt-internal.h>
23 /* Unlock *RWLOCK, rescheduling a waiting writer thread or, if there
24 are no threads waiting for a write lock, rescheduling the reader
25 threads. */
26 int
27 __pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
29 struct __pthread *wakeup;
31 __pthread_spin_lock (&rwlock->__lock);
33 assert (__pthread_spin_trylock (&rwlock->__held) == EBUSY);
35 if (rwlock->__readers > 1)
36 /* There are other readers. */
38 rwlock->__readers--;
39 __pthread_spin_unlock (&rwlock->__lock);
40 return 0;
43 if (rwlock->__readers == 1)
44 /* Last reader. */
45 rwlock->__readers = 0;
48 /* Wake someone else up. Try the writer queue first, then the
49 reader queue if that is empty. */
51 if (rwlock->__writerqueue)
53 wakeup = rwlock->__writerqueue;
54 __pthread_dequeue (wakeup);
56 /* We do not unlock RWLOCK->held: we are transferring the ownership
57 to the thread that we are waking up. */
59 __pthread_spin_unlock (&rwlock->__lock);
60 __pthread_wakeup (wakeup);
62 return 0;
65 if (rwlock->__readerqueue)
67 unsigned n = 0;
69 __pthread_queue_iterate (rwlock->__readerqueue, wakeup)
70 n++;
73 struct __pthread *wakeups[n];
74 unsigned i = 0;
76 __pthread_dequeuing_iterate (rwlock->__readerqueue, wakeup)
77 wakeups[i++] = wakeup;
79 rwlock->__readers += n;
80 rwlock->__readerqueue = 0;
82 __pthread_spin_unlock (&rwlock->__lock);
84 for (i = 0; i < n; i++)
85 __pthread_wakeup (wakeups[i]);
88 return 0;
92 /* Noone is waiting. Just unlock it. */
94 __pthread_spin_unlock (&rwlock->__held);
95 __pthread_spin_unlock (&rwlock->__lock);
96 return 0;
98 weak_alias (__pthread_rwlock_unlock, pthread_rwlock_unlock);