summaryrefslogtreecommitdiff
path: root/pthread.h
blob: e9278c263a0752aa514fdfbc80ea3ccd4d9a4756 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* This is the POSIX thread API (POSIX 1003).

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

/* FIXME: do not include function prototypes for functions which are
   not yet implemented.  This will allow us to keep a better handle on
   where we're at. */

#ifndef _PTHREADS_H
#define _PTHREADS_H

/* Convert these to defined when implemented. */
#define _POSIX_THREAD_ATTR_STACKSIZE
#ifdef _POSIX_THREAD_ATTR_STACKADDR
#undef _POSIX_THREAD_ATTR_STACKADDR
#endif

#if HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <windows.h>
#include <time.h>

#if HAVE_SIGNAL_H
#include <signal.h>
#endif /* HAVE_SIGNAL_H */

#ifndef HAVE_STRUCT_TIMESPEC
struct timespec {
	int tv_sec;
	int tv_nsec;
};
#endif /* HAVE_STRUCT_TIMESPEC */

#ifndef SIG_BLOCK
#define SIG_BLOCK 0
#endif /* SIG_BLOCK */

#ifndef SIG_UNBLOCK 
#define SIG_UNBLOCK 1
#endif /* SIG_UNBLOCK */

#ifndef SIG_SETMASK
#define SIG_SETMASK 2
#endif /* SIG_SETMASK */

#define PTHREAD_STACK_MIN   65535

/* Thread scheduling policies */

#define SCHED_OTHER 0
#define SCHED_FIFO  1
#define SCHED_RR    2

#define SCHED_MIN   SCHED_OTHER
#define SCHED_MAX   SCHED_RR

/* Cancelation return value.
   This value must be neither NULL nor the value of any
   pointer to an object in memory. */
#define PTHREAD_CANCELED            ((void *) 1)

#define PTHREAD_MUTEX_INITIALIZER {0 /* ignore internals */ }

typedef struct _pthread * pthread_t;
typedef struct {
	int valid;
	CRITICAL_SECTION cs;
} pthread_mutex_t;
typedef DWORD pthread_key_t;


/*                                      Related constants */
typedef struct {
  long valid;

#ifdef _POSIX_THREAD_ATTR_STACKSIZE
  size_t stacksize;                  /* PTHREAD_STACK_MIN */
#endif

  int detachedstate;                 /* PTHREAD_CREATE_DETACHED
					PTHREAD_CREATE_JOINABLE */

#if HAVE_SIGSET_T
  sigset_t sigmask;
#endif /* HAVE_SIGSET_T */

  int priority;

} pthread_attr_t;

/* I don't know why this structure isn't in some kind of namespace.
   According to my O'Reilly book, this is what this struct is
   called. */
struct sched_param {
  int sched_priority;
};

enum { SIGNAL, BROADCAST, NUM_EVENTS };

typedef struct {
  /* Signal and broadcast event HANDLEs. */
  HANDLE events[NUM_EVENTS];

  /* Count of the number of waiters. */
  unsigned waiters_count;
  
  /* Serialize access to waiters_count. */
  pthread_mutex_t waiters_count_lock;
} pthread_cond_t;

typedef struct { void * dummy; } pthread_condattr_t;
typedef struct { void * dummy; } pthread_mutexattr_t;

typedef struct {
  unsigned short flag;
  pthread_mutex_t lock;
} pthread_once_t;

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

int pthread_atfork (void (*prepare)(void),
		    void (*parent)(void),
		    void (*child)(void));

int pthread_create(pthread_t *thread,
		   const pthread_attr_t *attr,
		   void * (*start_routine) (void *),
		   void * arg);

void pthread_exit(void *value);

pthread_t pthread_self(void);

int pthread_equal(pthread_t t1, pthread_t t2);

int pthread_join(pthread_t thread, void ** valueptr);

int pthread_detach(pthread_t thread);

int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));

/* Functions for manipulating thread attribute objects. */

int pthread_attr_init(pthread_attr_t *attr);

int pthread_attr_destroy(pthread_attr_t *attr);

int pthread_attr_setstacksize(pthread_attr_t *attr,
			      size_t stacksize);

int pthread_attr_getstacksize(const pthread_attr_t *attr,
			      size_t *stacksize);

int pthread_attr_setstackaddr(pthread_attr_t *attr,
			      void *stackaddr);

int pthread_attr_getstackaddr(const pthread_attr_t *attr,
			      void **stackaddr);

int pthread_attr_getschedparam(const pthread_attr_t *attr,
			       struct sched_param *param);

int pthread_attr_setschedparam(pthread_attr_t *attr,
			       const struct sched_param *param);

int pthread_attr_getdetachstate(const pthread_attr_t *attr,
				int *detachstate);

int pthread_attr_setdetachstate(pthread_attr_t *attr,
				int detachstate);
  
int pthread_setschedparam(pthread_t thread,
			  int policy,
			  const struct sched_param *param);

int pthread_getschedparam(pthread_t thread,
			  int *policy,
			  struct sched_param *param);

int sched_get_priority_min(int policy);

int sched_get_priority_max(int policy);

int pthread_setcancelstate(int state,
			   int *oldstate);

int pthread_setcanceltype(int type,
			  int *oldtype);

/* Functions for manipulating cond. var. attribute objects. */

int pthread_condattr_init(pthread_condattr_t *attr);

int pthread_condattr_setpshared(pthread_condattr_t *attr,
			       int pshared);

int pthread_condattr_getpshared(pthread_condattr_t *attr,
				int *pshared);

int pthread_condattr_destroy(pthread_condattr_t *attr);

/* Functions for manipulating mutex attribute objects. */

int pthread_mutexattr_init(pthread_mutexattr_t *attr);

int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);

/* Primitives for condition variables. */

int pthread_cond_init(pthread_cond_t *cv,
		      const pthread_condattr_t *attr);

int pthread_cond_broadcast(pthread_cond_t *cv);

int pthread_cond_signal(pthread_cond_t *cv);

int pthread_cond_timedwait(pthread_cond_t *cv,
			   pthread_mutex_t *mutex,
			   const struct timespec *abstime);

int pthread_cond_wait(pthread_cond_t *cv,
		      pthread_mutex_t *mutex);

int pthread_cond_destroy(pthread_cond_t *cv);

/* Primitives for mutexes. */

int pthread_mutex_init(pthread_mutex_t *mutex,
		       const pthread_mutexattr_t *attr);

int pthread_mutex_destroy(pthread_mutex_t *mutex);

int pthread_mutex_lock(pthread_mutex_t *mutex);

int pthread_mutex_trylock(pthread_mutex_t *mutex);

int pthread_mutex_unlock(pthread_mutex_t *mutex);

/* Primitives for thread-specific data (TSD). */

int pthread_key_create(pthread_key_t *key,
		       void (*destructor)(void *));

int pthread_setspecific(pthread_key_t key, void *value);

void *pthread_getspecific(pthread_key_t key);

int pthread_key_delete(pthread_key_t key);

/* Signal handling. */

#if HAVE_SIGSET_T
int pthread_sigmask(int how,
		    const sigset_t *set,
		    sigset_t *oset);
#endif /* HAVE_SIGSET_T */

/* Thread cancelation functions. */

void pthread_testcancel(void);

int pthread_cancel(pthread_t thread);

#ifdef __cplusplus
}
#endif /* __cplusplus */

/* Constants declared in global.c */
extern const int _POSIX_THREAD_THREADS_MAX;
extern const int _POSIX_THREAD_DESTRUCTOR_ITERATIONS;
extern const int _POSIX_THREAD_KEYS_MAX;

extern const int _pthread_create_joinable;
extern const int _pthread_create_detached;

extern const int _pthread_cancel_enable;
extern const int _pthread_cancel_disable;

extern const int _pthread_cancel_asynchronous;
extern const int _pthread_cancel_deferred;

#define PTHREAD_DESTRUCTOR_ITERATIONS _POSIX_THREAD_DESTRUCTOR_ITERATIONS
#define PTHREAD_KEYS_MAX _POSIX_THREAD_KEYS_MAX
#define PTHREAD_THREADS_MAX _POSIX_THREAD_THREADS_MAX

#define PTHREAD_CREATE_JOINABLE     _pthread_create_joinable
#define PTHREAD_CREATE_DETACHED     _pthread_create_detached

/* Cancelability attributes */
#define PTHREAD_CANCEL_ENABLE       _pthread_cancel_enable
#define PTHREAD_CANCEL_DISABLE      _pthread_cancel_disable

#define PTHREAD_CANCEL_ASYNCHRONOUS _pthread_cancel_asynchronous
#define PTHREAD_CANCEL_DEFERRED     _pthread_cancel_deferred


/* The following #defines implement POSIX cleanup handlers.
   The standard requires that these functions be used as statements and
   be used pairwise in the same scope. The standard suggests that, in C, they
   may be implemented as macros starting and ending the same block.

   POSIX requires that applications observe scoping requirements, but
   doesn't say if the implemention must enforce them. The macros below
   partially enforce scope but can lead to compile or runtime errors. */

enum {
  _PTHREAD_HANDLER_POP_LIFO,
  _PTHREAD_HANDLER_POP_FIFO
};

enum {
  _PTHREAD_CLEANUP_STACK,
  _PTHREAD_DESTRUCTOR_STACK,
  _PTHREAD_FORKPREPARE_STACK,
  _PTHREAD_FORKPARENT_STACK,
  _PTHREAD_FORKCHILD_STACK
};

#ifdef pthread_cleanup_push
#undef pthread_cleanup_push
#endif

#define pthread_cleanup_push(routine, arg) \
{ \
  (void ) _pthread_handler_push(_PTHREAD_CLEANUP_STACK, \
				_PTHREAD_HANDLER_POP_LIFO, routine, arg);

#ifdef pthread_cleanup_pop
#undef pthread_cleanup_pop
#endif

#define pthread_cleanup_pop(execute) \
  _pthread_handler_pop(_PTHREAD_CLEANUP_STACK, execute);\
}

#endif /* _PTHREADS_H */