1 /* Implement a trivial subset of POSIX 1003.1-2008 pthread.h.
3 Copyright (C) 2009-2019 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert and Glen Lenker. */
21 @PRAGMA_SYSTEM_HEADER@
25 #if defined _GL_ALREADY_INCLUDING_PTHREAD_H
26 /* Special invocation convention:
27 On Android, we have a sequence of nested includes
28 <pthread.h> -> <time.h> -> <sys/time.h> -> <sys/select.h> ->
29 <signal.h> -> <pthread.h>.
30 In this situation, PTHREAD_COND_INITIALIZER is not yet defined,
31 therefore we should not attempt to define PTHREAD_MUTEX_NORMAL etc. */
33 #@INCLUDE_NEXT@ @NEXT_PTHREAD_H@
36 /* Normal invocation convention. */
38 #ifndef _@GUARD_PREFIX@_PTHREAD_H_
42 # define _GL_ALREADY_INCLUDING_PTHREAD_H
44 /* The include_next requires a split double-inclusion guard. */
45 # @INCLUDE_NEXT@ @NEXT_PTHREAD_H@
47 # undef _GL_ALREADY_INCLUDING_PTHREAD_H
51 #ifndef _@GUARD_PREFIX@_PTHREAD_H_
52 #define _@GUARD_PREFIX@_PTHREAD_H_
54 #define __need_system_stdlib_h
56 #undef __need_system_stdlib_h
59 /* The pthreads-win32 <pthread.h> defines a couple of broken macros. */
69 #include <sys/types.h>
72 #ifndef _GL_INLINE_HEADER_BEGIN
73 #error "Please include config.h first."
75 _GL_INLINE_HEADER_BEGIN
76 #ifndef _GL_PTHREAD_INLINE
77 # define _GL_PTHREAD_INLINE _GL_INLINE
80 #if ! @HAVE_PTHREAD_T@
81 # if !GNULIB_defined_pthread_types
82 typedef int pthread_t
;
83 typedef int pthread_attr_t
;
84 typedef int pthread_barrier_t
;
85 typedef int pthread_barrierattr_t
;
86 typedef int pthread_cond_t
;
87 typedef int pthread_condattr_t
;
88 typedef int pthread_key_t
;
89 typedef int pthread_mutex_t
;
90 typedef int pthread_mutexattr_t
;
91 typedef int pthread_once_t
;
92 typedef int pthread_rwlock_t
;
93 typedef int pthread_rwlockattr_t
;
94 # define GNULIB_defined_pthread_types 1
98 #ifndef PTHREAD_COND_INITIALIZER
99 #define PTHREAD_COND_INITIALIZER { 0 }
100 #define PTHREAD_MUTEX_INITIALIZER { 0 }
101 #define PTHREAD_ONCE_INIT { 0 }
102 #define PTHREAD_RWLOCK_INITIALIZER { 0 }
104 #define PTHREAD_BARRIER_SERIAL_THREAD (-1)
106 #define PTHREAD_CANCEL_DEFERRED 0
107 #define PTHREAD_CANCEL_ASYNCHRONOUS 1
109 #define PTHREAD_CANCEL_ENABLE 0
110 #define PTHREAD_CANCEL_DISABLE 1
112 #define PTHREAD_CANCELED ((void *) -1)
114 #define PTHREAD_CREATE_JOINABLE 0
115 #define PTHREAD_CREATE_DETACHED 1
117 #define PTHREAD_INHERIT_SCHED 0
118 #define PTHREAD_EXPLICIT_SCHED 1
120 #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
121 #define PTHREAD_MUTEX_NORMAL 0
122 #define PTHREAD_MUTEX_ERRORCHECK 1
123 #define PTHREAD_MUTEX_RECURSIVE 2
125 #define PTHREAD_MUTEX_STALLED 0
126 #define PTHREAD_MUTEX_ROBUST 1
128 #define PTHREAD_PRIO_NONE 0
129 #define PTHREAD_PRIO_INHERIT 1
130 #define PTHREAD_PRIO_PROTECT 2
132 #define PTHREAD_PROCESS_PRIVATE 0
133 #define PTHREAD_PROCESS_SHARED 1
135 #define PTHREAD_SCOPE_SYSTEM 0
136 #define PTHREAD_SCOPE_PROCESS 1
139 #if ! @HAVE_PTHREAD_T@
141 # if !GNULIB_defined_pthread_functions
143 /* Provide substitutes for the thread functions that should work
144 adequately on a single-threaded implementation, where
145 pthread_create always fails. The goal is to let programs compile
146 on non-pthread hosts with minimal runtime overhead.
148 Omit interfaces that have not been analyzed and for which we do not
149 know what to do, so that they elicit a compile-time error for
152 _GL_PTHREAD_INLINE
int
153 pthread_cond_destroy (pthread_cond_t
*cond
)
155 /* COND is never seriously used. */
159 _GL_PTHREAD_INLINE
int
160 pthread_cond_init (pthread_cond_t
*restrict cond
,
161 pthread_condattr_t
const *restrict attr
)
163 /* COND is never seriously used. */
167 _GL_PTHREAD_INLINE
int
168 pthread_cond_signal (pthread_cond_t
*cond
)
170 /* No threads can currently be blocked on COND. */
174 _GL_PTHREAD_INLINE
int
175 pthread_cond_wait (pthread_cond_t
*restrict cond
,
176 pthread_mutex_t
*restrict mutex
)
178 /* Properly-written applications never come here. */
183 _GL_PTHREAD_INLINE
int
184 pthread_create (pthread_t
*restrict thread
,
185 pthread_attr_t
const *restrict attr
,
186 void * (*start_routine
) (void*), void *restrict arg
)
188 /* Do not create a thread. */
192 _GL_PTHREAD_INLINE
void
193 pthread_exit (void *value
)
195 /* There is just one thread, so the process exits. */
199 _GL_PTHREAD_INLINE
int
200 pthread_join (pthread_t thread
, void **pvalue
)
202 /* Properly-written applications never come here. */
207 _GL_PTHREAD_INLINE
int
208 pthread_mutexattr_destroy (pthread_mutexattr_t
*attr
)
213 _GL_PTHREAD_INLINE
int
214 pthread_mutexattr_init (pthread_mutexattr_t
*attr
)
219 _GL_PTHREAD_INLINE
int
220 pthread_mutexattr_settype (pthread_mutexattr_t
*attr
, int attr_type
)
225 _GL_PTHREAD_INLINE
int
226 pthread_mutex_destroy (pthread_mutex_t
*mutex
)
228 /* MUTEX is never seriously used. */
232 _GL_PTHREAD_INLINE
int
233 pthread_mutex_init (pthread_mutex_t
*restrict mutex
,
234 pthread_mutexattr_t
const *restrict attr
)
236 /* MUTEX is never seriously used. */
240 _GL_PTHREAD_INLINE
int
241 pthread_mutex_lock (pthread_mutex_t
*mutex
)
243 /* There is only one thread, so it always gets the lock. This
244 implementation does not support PTHREAD_MUTEX_ERRORCHECK. */
248 _GL_PTHREAD_INLINE
int
249 pthread_mutex_trylock (pthread_mutex_t
*mutex
)
251 return pthread_mutex_lock (mutex
);
254 _GL_PTHREAD_INLINE
int
255 pthread_mutex_unlock (pthread_mutex_t
*mutex
)
257 /* There is only one thread, so it always unlocks successfully.
258 This implementation does not support robust mutexes or
259 PTHREAD_MUTEX_ERRORCHECK. */
263 # define GNULIB_defined_pthread_functions 1
268 #if ! @HAVE_PTHREAD_SPINLOCK_T@
270 # if !GNULIB_defined_pthread_spinlock_t
272 /* Approximate spinlocks with mutexes. */
274 typedef pthread_mutex_t pthread_spinlock_t
;
276 _GL_PTHREAD_INLINE
int
277 pthread_spin_init (pthread_spinlock_t
*lock
, int pshared
)
279 return pthread_mutex_init (lock
, NULL
);
282 _GL_PTHREAD_INLINE
int
283 pthread_spin_destroy (pthread_spinlock_t
*lock
)
285 return pthread_mutex_destroy (lock
);
288 _GL_PTHREAD_INLINE
int
289 pthread_spin_lock (pthread_spinlock_t
*lock
)
291 return pthread_mutex_lock (lock
);
294 _GL_PTHREAD_INLINE
int
295 pthread_spin_trylock (pthread_spinlock_t
*lock
)
297 return pthread_mutex_trylock (lock
);
300 _GL_PTHREAD_INLINE
int
301 pthread_spin_unlock (pthread_spinlock_t
*lock
)
303 return pthread_mutex_unlock (lock
);
306 # define GNULIB_defined_pthread_spinlock_t 1
311 _GL_INLINE_HEADER_END
313 #endif /* _@GUARD_PREFIX@_PTHREAD_H_ */
314 #endif /* _@GUARD_PREFIX@_PTHREAD_H_ */