sys_stat: Fix 'implicit declaration of function' warning on OS/2 kLIBC.
[gnulib.git] / lib / pthread.in.h
blob1deef7dcf4aff92a5ef770232a96d1c1efd9a844
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)
8 any later version.
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. */
20 #if __GNUC__ >= 3
21 @PRAGMA_SYSTEM_HEADER@
22 #endif
23 @PRAGMA_COLUMNS@
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@
35 #else
36 /* Normal invocation convention. */
38 #ifndef _@GUARD_PREFIX@_PTHREAD_H_
40 #if @HAVE_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
49 #endif
51 #ifndef _@GUARD_PREFIX@_PTHREAD_H_
52 #define _@GUARD_PREFIX@_PTHREAD_H_
54 #define __need_system_stdlib_h
55 #include <stdlib.h>
56 #undef __need_system_stdlib_h
59 /* The pthreads-win32 <pthread.h> defines a couple of broken macros. */
60 #undef asctime_r
61 #undef ctime_r
62 #undef gmtime_r
63 #undef localtime_r
64 #undef rand_r
65 #undef strtok_r
67 #include <errno.h>
68 #include <sched.h>
69 #include <sys/types.h>
70 #include <time.h>
72 #ifndef _GL_INLINE_HEADER_BEGIN
73 #error "Please include config.h first."
74 #endif
75 _GL_INLINE_HEADER_BEGIN
76 #ifndef _GL_PTHREAD_INLINE
77 # define _GL_PTHREAD_INLINE _GL_INLINE
78 #endif
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
95 # endif
96 #endif
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
137 #endif
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
150 now. */
152 _GL_PTHREAD_INLINE int
153 pthread_cond_destroy (pthread_cond_t *cond)
155 /* COND is never seriously used. */
156 return 0;
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. */
164 return 0;
167 _GL_PTHREAD_INLINE int
168 pthread_cond_signal (pthread_cond_t *cond)
170 /* No threads can currently be blocked on COND. */
171 return 0;
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. */
179 abort ();
180 return 0;
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. */
189 return EAGAIN;
192 _GL_PTHREAD_INLINE void
193 pthread_exit (void *value)
195 /* There is just one thread, so the process exits. */
196 exit (0);
199 _GL_PTHREAD_INLINE int
200 pthread_join (pthread_t thread, void **pvalue)
202 /* Properly-written applications never come here. */
203 abort ();
204 return 0;
207 _GL_PTHREAD_INLINE int
208 pthread_mutexattr_destroy (pthread_mutexattr_t *attr)
210 return 0;
213 _GL_PTHREAD_INLINE int
214 pthread_mutexattr_init (pthread_mutexattr_t *attr)
216 return 0;
219 _GL_PTHREAD_INLINE int
220 pthread_mutexattr_settype (pthread_mutexattr_t *attr, int attr_type)
222 return 0;
225 _GL_PTHREAD_INLINE int
226 pthread_mutex_destroy (pthread_mutex_t *mutex)
228 /* MUTEX is never seriously used. */
229 return 0;
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. */
237 return 0;
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. */
245 return 0;
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. */
260 return 0;
263 # define GNULIB_defined_pthread_functions 1
264 # endif
266 #endif
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
307 # endif
309 #endif
311 _GL_INLINE_HEADER_END
313 #endif /* _@GUARD_PREFIX@_PTHREAD_H_ */
314 #endif /* _@GUARD_PREFIX@_PTHREAD_H_ */
315 #endif