pthread: Add enough so that coreutils/src/sort.c compiles.
[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 #define _GL_PTHREAD_H_
23
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <sched.h>
27 #include <sys/types.h>
28 #include <time.h>
29
30 #ifndef HAVE_PTHREAD_T
31  typedef int pthread_t;
32  typedef int pthread_attr_t;
33  typedef int pthread_barrier_t;
34  typedef int pthread_barrierattr_t;
35  typedef int pthread_cond_t;
36  typedef int pthread_condattr_t;
37  typedef int pthread_key_t;
38  typedef int pthread_mutex_t;
39  typedef int pthread_mutexattr_t;
40  typedef int pthread_once_t;
41  typedef int pthread_rwlock_t;
42  typedef int pthread_rwlockattr_t;
43  typedef int pthread_spinlock_t;
44 #endif
45
46 #define PTHREAD_COND_INITIALIZER { 0 }
47 #define PTHREAD_MUTEX_INITIALIZER { 0 }
48 #define PTHREAD_ONCE_INIT { 0 }
49 #define PTHREAD_RWLOCK_INITIALIZER { 0 }
50
51 #define PTHREAD_BARRIER_SERIAL_THREAD (-1)
52
53 #define PTHREAD_CANCEL_DEFERRED 0
54 #define PTHREAD_CANCEL_ASYNCHRONOUS 1
55
56 #define PTHREAD_CANCEL_ENABLE 0
57 #define PTHREAD_CANCEL_DISABLE 1
58
59 #define PTHREAD_CANCELED ((void *) -1)
60
61 #define PTHREAD_CREATE_JOINABLE 0
62 #define PTHREAD_CREATE_DETACHED 1
63
64 #define PTHREAD_INHERIT_SCHED 0
65 #define PTHREAD_EXPLICIT_SCHED 1
66
67 #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
68 #define PTHREAD_MUTEX_NORMAL 0
69 #define PTHREAD_MUTEX_ERRORCHECK 1
70 #define PTHREAD_MUTEX_RECURSIVE 2
71
72 #define PTHREAD_MUTEX_STALLED 0
73 #define PTHREAD_MUTEX_ROBUST 1
74
75 #define PTHREAD_PRIO_NONE 0
76 #define PTHREAD_PRIO_INHERIT 1
77 #define PTHREAD_PRIO_PROTECT 2
78
79 #define PTHREAD_PROCESS_PRIVATE 0
80 #define PTHREAD_PROCESS_SHARED 1
81
82 #define PTHREAD_SCOPE_SYSTEM 0
83 #define PTHREAD_SCOPE_PROCESS 1
84
85 /* Provide substitutes for the thread functions that should work
86    adequately on a single-threaded implementation, where
87    pthread_create always fails.  The goal is to let programs compile
88    on non-pthread hosts with minimal runtime overhead.
89
90    Omit interfaces that have not been analyzed and for which we do not
91    know what to do, so that they elicit a compile-time error for
92    now.  */
93
94 static inline int
95 pthread_cond_destroy (pthread_cond_t *cond)
96 {
97   /* COND is never seriously used.  */
98   return 0;
99 }
100
101 static inline int
102 pthread_cond_init (pthread_cond_t *restrict cond,
103                    pthread_condattr_t const *restrict attr)
104 {
105   /* COND is never seriously used.  */
106   return 0;
107 }
108
109 static inline int
110 pthread_cond_signal (pthread_cond_t *cond)
111 {
112   /* No threads can currently be blocked on COND.  */
113   return 0;
114 }
115
116 static inline int
117 pthread_cond_wait (pthread_cond_t *restrict cond,
118                    pthread_mutex_t *restrict mutex)
119 {
120   /* Properly-written applications never come here.  */
121   abort ();
122   return 0;
123 }
124
125 static inline int
126 pthread_create (pthread_t *restrict thread,
127                 pthread_attr_t const *restrict attr,
128                 void * (*start_routine) (void*), void *restrict arg)
129 {
130   /* Do not create a thread.  */
131   return EAGAIN;
132 }
133
134 static inline void
135 pthread_exit (void *value)
136 {
137   /* There is just one thread, so the process exits.  */
138   exit (0);
139 }
140
141 static inline int
142 pthread_join (pthread_t thread, void **pvalue)
143 {
144   /* Properly-written applications never come here.  */
145   abort ();
146   return 0;
147 }
148
149 static inline int
150 pthread_mutex_destroy (pthread_mutex_t *mutex)
151 {
152   /* MUTEX is never seriously used.  */
153   return 0;
154 }
155
156 static inline int
157 pthread_mutex_init (pthread_mutex_t *restrict mutex,
158                     pthread_mutexattr_t const *restrict attr)
159 {
160   /* MUTEX is never seriously used.  */
161   return 0;
162 }
163
164 static inline int
165 pthread_mutex_lock (pthread_mutex_t *mutex)
166 {
167   /* There is only one thread, so it always gets the lock.  This
168      implementation does not support PTHREAD_MUTEX_ERRORCHECK.  */
169   return 0;
170 }
171
172 static inline int
173 pthread_mutex_unlock (pthread_mutex_t *mutex)
174 {
175   /* There is only one thread, so it always unlocks successfully.
176      This implementation does not support robust mutexes or
177      PTHREAD_MUTEX_ERRORCHECK.  */
178   return 0;
179 }
180
181 static inline int
182 pthread_spin_init (pthread_spinlock_t *lock, int pshared)
183 {
184   /* LOCK is never seriously used.  */
185   return 0;
186 }
187
188 static inline int
189 pthread_spin_lock (pthread_spinlock_t *lock)
190 {
191   /* Only one thread, so it always gets the lock.  */
192   return 0;
193 }
194
195 static inline int
196 pthread_spin_trylock (pthread_spinlock_t *lock)
197 {
198   /* Only one thread, so it always gets the lock.  Assume that a
199      thread never tries a lock that it already holds.  */
200   return 0;
201 }
202
203 static inline int
204 pthread_spin_unlock (pthread_spinlock_t *lock)
205 {
206   /* Only one thread, so spin locks are no-ops.  */
207   return 0;
208 }
209
210 #endif /* _GL_PTHREAD_H_ */