1 /* Implement a trivial subset of POSIX 1003.1-2008 pthread.h.
3 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
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)
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.
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. */
19 /* Written by Paul Eggert and Glen Lenker. */
21 #ifndef _GL_PTHREAD_H_
24 @PRAGMA_SYSTEM_HEADER@
28 /* The include_next requires a split double-inclusion guard. */
30 # @INCLUDE_NEXT@ @NEXT_PTHREAD_H@
33 #ifndef _GL_PTHREAD_H_
34 #define _GL_PTHREAD_H_
39 #include <sys/types.h>
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;
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 }
63 #define PTHREAD_BARRIER_SERIAL_THREAD (-1)
65 #define PTHREAD_CANCEL_DEFERRED 0
66 #define PTHREAD_CANCEL_ASYNCHRONOUS 1
68 #define PTHREAD_CANCEL_ENABLE 0
69 #define PTHREAD_CANCEL_DISABLE 1
71 #define PTHREAD_CANCELED ((void *) -1)
73 #define PTHREAD_CREATE_JOINABLE 0
74 #define PTHREAD_CREATE_DETACHED 1
76 #define PTHREAD_INHERIT_SCHED 0
77 #define PTHREAD_EXPLICIT_SCHED 1
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
84 #define PTHREAD_MUTEX_STALLED 0
85 #define PTHREAD_MUTEX_ROBUST 1
87 #define PTHREAD_PRIO_NONE 0
88 #define PTHREAD_PRIO_INHERIT 1
89 #define PTHREAD_PRIO_PROTECT 2
91 #define PTHREAD_PROCESS_PRIVATE 0
92 #define PTHREAD_PROCESS_SHARED 1
94 #define PTHREAD_SCOPE_SYSTEM 0
95 #define PTHREAD_SCOPE_PROCESS 1
98 #if ! @HAVE_PTHREAD_T@
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.
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
110 pthread_cond_destroy (pthread_cond_t *cond)
112 /* COND is never seriously used. */
117 pthread_cond_init (pthread_cond_t *restrict cond,
118 pthread_condattr_t const *restrict attr)
120 /* COND is never seriously used. */
125 pthread_cond_signal (pthread_cond_t *cond)
127 /* No threads can currently be blocked on COND. */
132 pthread_cond_wait (pthread_cond_t *restrict cond,
133 pthread_mutex_t *restrict mutex)
135 /* Properly-written applications never come here. */
141 pthread_create (pthread_t *restrict thread,
142 pthread_attr_t const *restrict attr,
143 void * (*start_routine) (void*), void *restrict arg)
145 /* Do not create a thread. */
150 pthread_exit (void *value)
152 /* There is just one thread, so the process exits. */
157 pthread_join (pthread_t thread, void **pvalue)
159 /* Properly-written applications never come here. */
165 pthread_mutexattr_destroy (pthread_mutexattr_t *attr)
171 pthread_mutexattr_init (pthread_mutexattr_t *attr)
177 pthread_mutexattr_settype (pthread_mutexattr_t *attr, int attr_type)
183 pthread_mutex_destroy (pthread_mutex_t *mutex)
185 /* MUTEX is never seriously used. */
190 pthread_mutex_init (pthread_mutex_t *restrict mutex,
191 pthread_mutexattr_t const *restrict attr)
193 /* MUTEX is never seriously used. */
198 pthread_mutex_lock (pthread_mutex_t *mutex)
200 /* There is only one thread, so it always gets the lock. This
201 implementation does not support PTHREAD_MUTEX_ERRORCHECK. */
206 pthread_mutex_trylock (pthread_mutex_t *mutex)
208 return pthread_mutex_lock (mutex);
212 pthread_mutex_unlock (pthread_mutex_t *mutex)
214 /* There is only one thread, so it always unlocks successfully.
215 This implementation does not support robust mutexes or
216 PTHREAD_MUTEX_ERRORCHECK. */
222 #if ! @HAVE_PTHREAD_SPINLOCK_T@
224 /* Approximate spinlocks with mutexes. */
226 typedef pthread_mutex_t pthread_spinlock_t;
229 pthread_spin_init (pthread_spinlock_t *lock, int pshared)
231 return pthread_mutex_init (lock, NULL);
235 pthread_spin_destroy (pthread_spinlock_t *lock)
237 return pthread_mutex_destroy (lock);
241 pthread_spin_lock (pthread_spinlock_t *lock)
243 return pthread_mutex_lock (lock);
247 pthread_spin_trylock (pthread_spinlock_t *lock)
249 return pthread_mutex_trylock (lock);
253 pthread_spin_unlock (pthread_spinlock_t *lock)
255 return pthread_mutex_unlock (lock);
259 #endif /* _GL_PTHREAD_H_ */
260 #endif /* _GL_PTHREAD_H_ */