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