master
1/* Private libc-internal interface for mutex locks.
2 Copyright (C) 2015-2025 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
76extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
77 const pthread_mutexattr_t *__mutex_attr);
78extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
79libc_hidden_proto (__pthread_mutex_destroy)
80
81extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
82
83extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
84
85extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
86
87extern int __pthread_mutexattr_init (pthread_mutexattr_t *__attr);
88libc_hidden_proto (__pthread_mutexattr_init)
89
90extern int __pthread_mutexattr_destroy (pthread_mutexattr_t *__attr);
91libc_hidden_proto (__pthread_mutexattr_destroy)
92
93extern int __pthread_rwlock_init (pthread_rwlock_t *__rwlock,
94 const pthread_rwlockattr_t *__attr);
95libc_hidden_proto (__pthread_rwlock_init)
96
97extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
98libc_hidden_proto (__pthread_rwlock_destroy)
99
100extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
101libc_hidden_proto (__pthread_rwlock_rdlock)
102
103extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
104libc_hidden_proto (__pthread_rwlock_tryrdlock)
105
106extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
107libc_hidden_proto (__pthread_rwlock_wrlock)
108
109extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
110libc_hidden_proto (__pthread_rwlock_trywrlock)
111
112extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
113libc_hidden_proto (__pthread_rwlock_unlock)
114
115extern int __pthread_once (pthread_once_t *__once_control,
116 void (*__init_routine) (void));
117libc_hidden_proto (__pthread_once);
118
119extern int __pthread_atfork (void (*__prepare) (void),
120 void (*__parent) (void),
121 void (*__child) (void));
122
123extern int __pthread_setcancelstate (int state, int *oldstate);
124libc_hidden_proto (__pthread_setcancelstate)
125/* Make the pthread functions weak so that we can elide them from
126 single-threaded processes. */
127#if !defined(__NO_WEAK_PTHREAD_ALIASES) && !IS_IN (libpthread)
128# ifdef weak_extern
129weak_extern (__pthread_initialize)
130weak_extern (__pthread_atfork)
131# else
132# pragma weak __pthread_initialize
133# pragma weak __pthread_atfork
134# endif
135#endif
136
137#endif /* bits/libc-lockP.h */