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@
27 /* The include_next requires a split double-inclusion guard. */
29 # @INCLUDE_NEXT@ @NEXT_PTHREAD_H@
32 #ifndef _GL_PTHREAD_H_
33 #define _GL_PTHREAD_H_
38 #include <sys/types.h>
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;
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 }
62 #define PTHREAD_BARRIER_SERIAL_THREAD (-1)
64 #define PTHREAD_CANCEL_DEFERRED 0
65 #define PTHREAD_CANCEL_ASYNCHRONOUS 1
67 #define PTHREAD_CANCEL_ENABLE 0
68 #define PTHREAD_CANCEL_DISABLE 1
70 #define PTHREAD_CANCELED ((void *) -1)
72 #define PTHREAD_CREATE_JOINABLE 0
73 #define PTHREAD_CREATE_DETACHED 1
75 #define PTHREAD_INHERIT_SCHED 0
76 #define PTHREAD_EXPLICIT_SCHED 1
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
83 #define PTHREAD_MUTEX_STALLED 0
84 #define PTHREAD_MUTEX_ROBUST 1
86 #define PTHREAD_PRIO_NONE 0
87 #define PTHREAD_PRIO_INHERIT 1
88 #define PTHREAD_PRIO_PROTECT 2
90 #define PTHREAD_PROCESS_PRIVATE 0
91 #define PTHREAD_PROCESS_SHARED 1
93 #define PTHREAD_SCOPE_SYSTEM 0
94 #define PTHREAD_SCOPE_PROCESS 1
97 #if ! @HAVE_PTHREAD_T@
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.
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
109 pthread_cond_destroy (pthread_cond_t *cond)
111 /* COND is never seriously used. */
116 pthread_cond_init (pthread_cond_t *restrict cond,
117 pthread_condattr_t const *restrict attr)
119 /* COND is never seriously used. */
124 pthread_cond_signal (pthread_cond_t *cond)
126 /* No threads can currently be blocked on COND. */
131 pthread_cond_wait (pthread_cond_t *restrict cond,
132 pthread_mutex_t *restrict mutex)
134 /* Properly-written applications never come here. */
140 pthread_create (pthread_t *restrict thread,
141 pthread_attr_t const *restrict attr,
142 void * (*start_routine) (void*), void *restrict arg)
144 /* Do not create a thread. */
149 pthread_exit (void *value)
151 /* There is just one thread, so the process exits. */
156 pthread_join (pthread_t thread, void **pvalue)
158 /* Properly-written applications never come here. */
164 pthread_mutexattr_destroy (pthread_mutexattr_t *attr)
170 pthread_mutexattr_init (pthread_mutexattr_t *attr)
176 pthread_mutexattr_settype (pthread_mutexattr_t *attr, int attr_type)
182 pthread_mutex_destroy (pthread_mutex_t *mutex)
184 /* MUTEX is never seriously used. */
189 pthread_mutex_init (pthread_mutex_t *restrict mutex,
190 pthread_mutexattr_t const *restrict attr)
192 /* MUTEX is never seriously used. */
197 pthread_mutex_lock (pthread_mutex_t *mutex)
199 /* There is only one thread, so it always gets the lock. This
200 implementation does not support PTHREAD_MUTEX_ERRORCHECK. */
205 pthread_mutex_trylock (pthread_mutex_t *mutex)
207 return pthread_mutex_lock (mutex);
211 pthread_mutex_unlock (pthread_mutex_t *mutex)
213 /* There is only one thread, so it always unlocks successfully.
214 This implementation does not support robust mutexes or
215 PTHREAD_MUTEX_ERRORCHECK. */
221 #if ! @HAVE_PTHREAD_SPINLOCK_T@
223 /* Approximate spinlocks with mutexes. */
225 typedef pthread_mutex_t pthread_spinlock_t;
228 pthread_spin_init (pthread_spinlock_t *lock, int pshared)
230 return pthread_mutex_init (lock, NULL);
234 pthread_spin_destroy (pthread_spinlock_t *lock)
236 return pthread_mutex_destroy (lock);
240 pthread_spin_lock (pthread_spinlock_t *lock)
242 return pthread_mutex_lock (lock);
246 pthread_spin_trylock (pthread_spinlock_t *lock)
248 return pthread_mutex_trylock (lock);
252 pthread_spin_unlock (pthread_spinlock_t *lock)
254 return pthread_mutex_unlock (lock);
258 #endif /* _GL_PTHREAD_H_ */
259 #endif /* _GL_PTHREAD_H_ */