1 /* Private libc-internal interface for mutex locks.
2    Copyright (C) 2015-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
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 License as
7    published by the Free Software Foundation; either version 2.1 of the
8    License, or (at your option) any later version.
9 
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.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If
17    not, see <https://www.gnu.org/licenses/>.  */
18 
19 #ifndef _BITS_LIBC_LOCKP_H
20 #define _BITS_LIBC_LOCKP_H 1
21 
22 #include <pthread.h>
23 #include <pthread-functions.h>
24 
25 /* If we check for a weakly referenced symbol and then perform a
26    normal jump to it te code generated for some platforms in case of
27    PIC is unnecessarily slow.  What would happen is that the function
28    is first referenced as data and then it is called indirectly
29    through the PLT.  We can make this a direct jump.  */
30 #ifdef __PIC__
31 # define __libc_maybe_call(FUNC, ARGS, ELSE) \
32   (__extension__ ({ __typeof (FUNC) *_fn = (FUNC); \
33 		    _fn != NULL ? (*_fn) ARGS : ELSE; }))
34 #else
35 # define __libc_maybe_call(FUNC, ARGS, ELSE) \
36   (FUNC != NULL ? FUNC ARGS : ELSE)
37 #endif
38 
39 /* Call thread functions through the function pointer table.  */
40 #if defined SHARED && IS_IN (libc)
41 # define PTFAVAIL(NAME) __libc_pthread_functions_init
42 # define __libc_ptf_call(FUNC, ARGS, ELSE) \
43   (__libc_pthread_functions_init ? PTHFCT_CALL (ptr_##FUNC, ARGS) : ELSE)
44 # define __libc_ptf_call_always(FUNC, ARGS) \
45   PTHFCT_CALL (ptr_##FUNC, ARGS)
46 #elif IS_IN (libpthread)
47 # define PTFAVAIL(NAME) 1
48 # define __libc_ptf_call(FUNC, ARGS, ELSE) \
49   FUNC ARGS
50 # define __libc_ptf_call_always(FUNC, ARGS) \
51   FUNC ARGS
52 #else
53 # define PTFAVAIL(NAME) (NAME != NULL)
54 # define __libc_ptf_call(FUNC, ARGS, ELSE) \
55   __libc_maybe_call (FUNC, ARGS, ELSE)
56 # define __libc_ptf_call_always(FUNC, ARGS) \
57   FUNC ARGS
58 #endif
59 
60 /* Create thread-specific key.  */
61 #define __libc_key_create(KEY, DESTRUCTOR) \
62   __libc_ptf_call (__pthread_key_create, (KEY, DESTRUCTOR), 1)
63 
64 /* Get thread-specific data.  */
65 #define __libc_getspecific(KEY) \
66   __libc_ptf_call (__pthread_getspecific, (KEY), NULL)
67 
68 /* Set thread-specific data.  */
69 #define __libc_setspecific(KEY, VALUE) \
70   __libc_ptf_call (__pthread_setspecific, (KEY, VALUE), 0)
71 
72 
73 /* Functions that are used by this file and are internal to the GNU C
74    library.  */
75 
76 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
77 				 const pthread_mutexattr_t *__mutex_attr);
78 
79 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
80 
81 extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
82 
83 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
84 
85 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
86 
87 extern int __pthread_mutexattr_init (pthread_mutexattr_t *__attr);
88 
89 extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *__attr);
90 
91 extern int __pthread_mutexattr_settype (pthread_mutexattr_t *__attr,
92 					int __kind);
93 
94 extern int __pthread_rwlock_init (pthread_rwlock_t *__rwlock,
95 				  const pthread_rwlockattr_t *__attr);
96 
97 extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
98 
99 extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
100 
101 extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
102 
103 extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
104 
105 extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
106 
107 extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
108 
109 extern int __pthread_once (pthread_once_t *__once_control,
110 			   void (*__init_routine) (void));
111 
112 extern int __pthread_atfork (void (*__prepare) (void),
113 			     void (*__parent) (void),
114 			     void (*__child) (void));
115 
116 
117 
118 /* Make the pthread functions weak so that we can elide them from
119    single-threaded processes.  */
120 #if !defined(__NO_WEAK_PTHREAD_ALIASES) && !IS_IN (libpthread)
121 # ifdef weak_extern
122 weak_extern (__pthread_mutex_init)
123 weak_extern (__pthread_mutex_destroy)
124 weak_extern (__pthread_mutex_lock)
125 weak_extern (__pthread_mutex_trylock)
126 weak_extern (__pthread_mutex_unlock)
127 weak_extern (__pthread_mutexattr_init)
128 weak_extern (__pthread_mutexattr_destroy)
129 weak_extern (__pthread_mutexattr_settype)
130 weak_extern (__pthread_rwlock_init)
131 weak_extern (__pthread_rwlock_destroy)
132 weak_extern (__pthread_rwlock_rdlock)
133 weak_extern (__pthread_rwlock_tryrdlock)
134 weak_extern (__pthread_rwlock_wrlock)
135 weak_extern (__pthread_rwlock_trywrlock)
136 weak_extern (__pthread_rwlock_unlock)
137 weak_extern (__pthread_key_create)
138 weak_extern (__pthread_setspecific)
139 weak_extern (__pthread_getspecific)
140 weak_extern (__pthread_once)
141 weak_extern (__pthread_initialize)
142 weak_extern (__pthread_atfork)
143 weak_extern (__pthread_setcancelstate)
144 # else
145 #  pragma weak __pthread_mutex_init
146 #  pragma weak __pthread_mutex_destroy
147 #  pragma weak __pthread_mutex_lock
148 #  pragma weak __pthread_mutex_trylock
149 #  pragma weak __pthread_mutex_unlock
150 #  pragma weak __pthread_mutexattr_init
151 #  pragma weak __pthread_mutexattr_destroy
152 #  pragma weak __pthread_mutexattr_settype
153 #  pragma weak __pthread_rwlock_destroy
154 #  pragma weak __pthread_rwlock_rdlock
155 #  pragma weak __pthread_rwlock_tryrdlock
156 #  pragma weak __pthread_rwlock_wrlock
157 #  pragma weak __pthread_rwlock_trywrlock
158 #  pragma weak __pthread_rwlock_unlock
159 #  pragma weak __pthread_key_create
160 #  pragma weak __pthread_setspecific
161 #  pragma weak __pthread_getspecific
162 #  pragma weak __pthread_once
163 #  pragma weak __pthread_initialize
164 #  pragma weak __pthread_atfork
165 #  pragma weak __pthread_setcancelstate
166 # endif
167 #endif
168 
169 #endif	/* bits/libc-lockP.h */
170