1 /* Copyright (C) 2003-2022 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <errno.h>
19 #include <sysdep.h>
20 #include <futex-internal.h>
21 #include <pthread.h>
22 #include <pthreadP.h>
23 #include <stap-probe.h>
24 
25 #include "pthread_rwlock_common.c"
26 
27 /* See pthread_rwlock_common.c for an overview.  */
28 int
___pthread_rwlock_unlock(pthread_rwlock_t * rwlock)29 ___pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
30 {
31   LIBC_PROBE (rwlock_unlock, 1, rwlock);
32 
33   /* We distinguish between having acquired a read vs. a write lock by looking
34      at the writer TID.  If it's equal to our TID, we must be the writer
35      because nobody else can have stored this value.  Also, if we are a
36      reader, we will read from the wrunlock store with value 0 by the most
37      recent writer because that writer happens-before us.  */
38   if (atomic_load_relaxed (&rwlock->__data.__cur_writer)
39       == THREAD_GETMEM (THREAD_SELF, tid))
40       __pthread_rwlock_wrunlock (rwlock);
41   else
42     __pthread_rwlock_rdunlock (rwlock);
43   return 0;
44 }
45 versioned_symbol (libc, ___pthread_rwlock_unlock, pthread_rwlock_unlock,
46 		  GLIBC_2_34);
47 strong_alias (___pthread_rwlock_unlock, __pthread_rwlock_unlock)
48 libc_hidden_ver (___pthread_rwlock_unlock, __pthread_rwlock_unlock)
49 
50 #if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_1, GLIBC_2_34)
51 compat_symbol (libpthread, ___pthread_rwlock_unlock, pthread_rwlock_unlock,
52 	       GLIBC_2_1);
53 #endif
54 #if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_2, GLIBC_2_34)
55 compat_symbol (libpthread, ___pthread_rwlock_unlock, __pthread_rwlock_unlock,
56 	       GLIBC_2_2);
57 #endif
58