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