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