Allow multiple gnulib generated include files to be combined.
[gnulib.git] / lib / pthread.in.h
1 /* Implement a trivial subset of POSIX 1003.1-2008 pthread.h.
2
3    Copyright (C) 2009-2011 Free Software Foundation, Inc.
4
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.
9
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.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* Written by Paul Eggert and Glen Lenker.  */
20
21 #ifndef _@GUARD_PREFIX@_PTHREAD_H_
22
23 #if __GNUC__ >= 3
24 @PRAGMA_SYSTEM_HEADER@
25 #endif
26 @PRAGMA_COLUMNS@
27
28 /* The include_next requires a split double-inclusion guard.  */
29 #if @HAVE_PTHREAD_H@
30 # @INCLUDE_NEXT@ @NEXT_PTHREAD_H@
31 #endif
32
33 #ifndef _@GUARD_PREFIX@_PTHREAD_H_
34 #define _@GUARD_PREFIX@_PTHREAD_H_
35
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <sched.h>
39 #include <sys/types.h>
40 #include <time.h>
41
42 #if ! @HAVE_PTHREAD_T@
43 # if !GNULIB_defined_pthread_types
44  typedef int pthread_t;
45  typedef int pthread_attr_t;
46  typedef int pthread_barrier_t;
47  typedef int pthread_barrierattr_t;
48  typedef int pthread_cond_t;
49  typedef int pthread_condattr_t;
50  typedef int pthread_key_t;
51  typedef int pthread_mutex_t;
52  typedef int pthread_mutexattr_t;
53  typedef int pthread_once_t;
54  typedef int pthread_rwlock_t;
55  typedef int pthread_rwlockattr_t;
56 #  define GNULIB_defined_pthread_types 1
57 # endif
58 #endif
59
60 #ifndef PTHREAD_COND_INITIALIZER
61 #define PTHREAD_COND_INITIALIZER { 0 }
62 #define PTHREAD_MUTEX_INITIALIZER { 0 }
63 #define PTHREAD_ONCE_INIT { 0 }
64 #define PTHREAD_RWLOCK_INITIALIZER { 0 }
65
66 #define PTHREAD_BARRIER_SERIAL_THREAD (-1)
67
68 #define PTHREAD_CANCEL_DEFERRED 0
69 #define PTHREAD_CANCEL_ASYNCHRONOUS 1
70
71 #define PTHREAD_CANCEL_ENABLE 0
72 #define PTHREAD_CANCEL_DISABLE 1
73
74 #define PTHREAD_CANCELED ((void *) -1)
75
76 #define PTHREAD_CREATE_JOINABLE 0
77 #define PTHREAD_CREATE_DETACHED 1
78
79 #define PTHREAD_INHERIT_SCHED 0
80 #define PTHREAD_EXPLICIT_SCHED 1
81
82 #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
83 #define PTHREAD_MUTEX_NORMAL 0
84 #define PTHREAD_MUTEX_ERRORCHECK 1
85 #define PTHREAD_MUTEX_RECURSIVE 2
86
87 #define PTHREAD_MUTEX_STALLED 0
88 #define PTHREAD_MUTEX_ROBUST 1
89
90 #define PTHREAD_PRIO_NONE 0
91 #define PTHREAD_PRIO_INHERIT 1
92 #define PTHREAD_PRIO_PROTECT 2
93
94 #define PTHREAD_PROCESS_PRIVATE 0
95 #define PTHREAD_PROCESS_SHARED 1
96
97 #define PTHREAD_SCOPE_SYSTEM 0
98 #define PTHREAD_SCOPE_PROCESS 1
99 #endif
100
101 #if ! @HAVE_PTHREAD_T@
102
103 # if !GNULIB_defined_pthread_functions
104
105 /* Provide substitutes for the thread functions that should work
106    adequately on a single-threaded implementation, where
107    pthread_create always fails.  The goal is to let programs compile
108    on non-pthread hosts with minimal runtime overhead.
109
110    Omit interfaces that have not been analyzed and for which we do not
111    know what to do, so that they elicit a compile-time error for
112    now.  */
113
114 static inline int
115 pthread_cond_destroy (pthread_cond_t *cond)
116 {
117   /* COND is never seriously used.  */
118   return 0;
119 }
120
121 static inline int
122 pthread_cond_init (pthread_cond_t *restrict cond,
123                    pthread_condattr_t const *restrict attr)
124 {
125   /* COND is never seriously used.  */
126   return 0;
127 }
128
129 static inline int
130 pthread_cond_signal (pthread_cond_t *cond)
131 {
132   /* No threads can currently be blocked on COND.  */
133   return 0;
134 }
135
136 static inline int
137 pthread_cond_wait (pthread_cond_t *restrict cond,
138                    pthread_mutex_t *restrict mutex)
139 {
140   /* Properly-written applications never come here.  */
141   abort ();
142   return 0;
143 }
144
145 static inline int
146 pthread_create (pthread_t *restrict thread,
147                 pthread_attr_t const *restrict attr,
148                 void * (*start_routine) (void*), void *restrict arg)
149 {
150   /* Do not create a thread.  */
151   return EAGAIN;
152 }
153
154 static inline void
155 pthread_exit (void *value)
156 {
157   /* There is just one thread, so the process exits.  */
158   exit (0);
159 }
160
161 static inline int
162 pthread_join (pthread_t thread, void **pvalue)
163 {
164   /* Properly-written applications never come here.  */
165   abort ();
166   return 0;
167 }
168
169 static inline int
170 pthread_mutexattr_destroy (pthread_mutexattr_t *attr)
171 {
172   return 0;
173 }
174
175 static inline int
176 pthread_mutexattr_init (pthread_mutexattr_t *attr)
177 {
178   return 0;
179 }
180
181 static inline int
182 pthread_mutexattr_settype (pthread_mutexattr_t *attr, int attr_type)
183 {
184   return 0;
185 }
186
187 static inline int
188 pthread_mutex_destroy (pthread_mutex_t *mutex)
189 {
190   /* MUTEX is never seriously used.  */
191   return 0;
192 }
193
194 static inline int
195 pthread_mutex_init (pthread_mutex_t *restrict mutex,
196                     pthread_mutexattr_t const *restrict attr)
197 {
198   /* MUTEX is never seriously used.  */
199   return 0;
200 }
201
202 static inline int
203 pthread_mutex_lock (pthread_mutex_t *mutex)
204 {
205   /* There is only one thread, so it always gets the lock.  This
206      implementation does not support PTHREAD_MUTEX_ERRORCHECK.  */
207   return 0;
208 }
209
210 static inline int
211 pthread_mutex_trylock (pthread_mutex_t *mutex)
212 {
213   return pthread_mutex_lock (mutex);
214 }
215
216 static inline int
217 pthread_mutex_unlock (pthread_mutex_t *mutex)
218 {
219   /* There is only one thread, so it always unlocks successfully.
220      This implementation does not support robust mutexes or
221      PTHREAD_MUTEX_ERRORCHECK.  */
222   return 0;
223 }
224
225 #  define GNULIB_defined_pthread_functions 1
226 # endif
227
228 #endif
229
230 #if ! @HAVE_PTHREAD_SPINLOCK_T@
231
232 # if !GNULIB_defined_pthread_spinlock_t
233
234 /* Approximate spinlocks with mutexes.  */
235
236 typedef pthread_mutex_t pthread_spinlock_t;
237
238 static inline int
239 pthread_spin_init (pthread_spinlock_t *lock, int pshared)
240 {
241   return pthread_mutex_init (lock, NULL);
242 }
243
244 static inline int
245 pthread_spin_destroy (pthread_spinlock_t *lock)
246 {
247   return pthread_mutex_destroy (lock);
248 }
249
250 static inline int
251 pthread_spin_lock (pthread_spinlock_t *lock)
252 {
253   return pthread_mutex_lock (lock);
254 }
255
256 static inline int
257 pthread_spin_trylock (pthread_spinlock_t *lock)
258 {
259   return pthread_mutex_trylock (lock);
260 }
261
262 static inline int
263 pthread_spin_unlock (pthread_spinlock_t *lock)
264 {
265   return pthread_mutex_unlock (lock);
266 }
267
268 #  define GNULIB_defined_pthread_spinlock_t 1
269 # endif
270
271 #endif
272
273 #endif /* _@GUARD_PREFIX@_PTHREAD_H_ */
274 #endif /* _@GUARD_PREFIX@_PTHREAD_H_ */