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