]> sourceware.org Git - newlib-cygwin.git/blame - winsup/cygwin/thread.h
2002-09-11 Robert Collins <rbtcollins@hotmail.com>
[newlib-cygwin.git] / winsup / cygwin / thread.h
CommitLineData
1fd5e000
CF
1/* thread.h: Locking and threading module definitions
2
b31c68c4 3 Copyright 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
1fd5e000
CF
4
5 Written by Marco Fuykschot <marco@ddi.nl>
9a08b2c0 6 Major update 2001 Robert Collins <rbtcollins@hotmail.com>
462f4eff 7
1fd5e000
CF
8This file is part of Cygwin.
9
10This software is a copyrighted work licensed under the terms of the
11Cygwin license. Please consult the file "CYGWIN_LICENSE" for
12details. */
13
14#ifndef _CYGNUS_THREADS_
15#define _CYGNUS_THREADS_
16
17#define LOCK_FD_LIST 1
18#define LOCK_MEMORY_LIST 2
19#define LOCK_MMAP_LIST 3
20#define LOCK_DLL_LIST 4
1fd5e000
CF
21
22#define WRITE_LOCK 1
23#define READ_LOCK 2
24
25extern "C"
26{
27#if defined (_CYG_THREAD_FAILSAFE) && defined (_MT_SAFE)
5c83f260 28 void AssertResourceOwner (int, int);
1fd5e000 29#else
f2aeff27 30#define AssertResourceOwner(i,ii)
1fd5e000
CF
31#endif
32}
33
34#ifndef _MT_SAFE
35
36#define SetResourceLock(i,n,c)
37#define ReleaseResourceLock(i,n,c)
38
39#else
40
5c83f260 41#include <pthread.h>
9a08b2c0 42#include <signal.h>
1fd5e000
CF
43#include <pwd.h>
44#include <grp.h>
bccd5e0d 45#define _NOMNTENT_FUNCS
1fd5e000 46#include <mntent.h>
1fd5e000 47
9a08b2c0 48extern "C"
1fd5e000 49{
9a08b2c0 50
f2aeff27
CF
51struct _winsup_t
52{
53 /*
5c83f260
RC
54 Needed for the group functions
55 */
de4e0d30 56 struct __group16 _grp;
f2aeff27
CF
57 char *_namearray[2];
58 int _grp_pos;
59
60 /* console.cc */
61 unsigned _rarg;
62
63 /* dlfcn.cc */
64 int _dl_error;
65 char _dl_buffer[256];
66
67 /* passwd.cc */
68 struct passwd _res;
69 char _pass[_PASSWORD_LEN];
70 int _pw_pos;
71
72 /* path.cc */
73 struct mntent mntbuf;
74 int _iteration;
75 DWORD available_drives;
f97adf98
CF
76 char mnt_type[80];
77 char mnt_opts[80];
78 char mnt_fsname[MAX_PATH];
79 char mnt_dir[MAX_PATH];
f2aeff27
CF
80
81 /* strerror */
82 char _strerror_buf[20];
83
84 /* sysloc.cc */
85 char *_process_ident;
86 int _process_logopt;
87 int _process_facility;
88 int _process_logmask;
89
90 /* times.cc */
91 char timezone_buf[20];
92 struct tm _localtime_buf;
93
94 /* uinfo.cc */
17db1105 95 char _username[UNLEN + 1];
cb19ccf4
CV
96
97 /* net.cc */
98 char *_ntoa_buf;
99 struct protoent *_protoent_buf;
100 struct servent *_servent_buf;
101 struct hostent *_hostent_buf;
f2aeff27
CF
102};
103
104
105struct __reent_t
106{
107 struct _reent *_clib;
108 struct _winsup_t *_winsup;
109};
1fd5e000 110
f2aeff27
CF
111_reent *_reent_clib ();
112_winsup_t *_reent_winsup ();
113void SetResourceLock (int, int, const char *) __attribute__ ((regparm (3)));
114void ReleaseResourceLock (int, int, const char *)
115 __attribute__ ((regparm (3)));
1fd5e000
CF
116
117#ifdef _CYG_THREAD_FAILSAFE
f2aeff27 118void AssertResourceOwner (int, int);
1fd5e000
CF
119#else
120#define AssertResourceOwner(i,ii)
121#endif
122}
123
124class per_process;
125class pinfo;
126
127class ResourceLocks
128{
129public:
5c83f260
RC
130 ResourceLocks ()
131 {
132 }
1dc16fc7
CF
133 LPCRITICAL_SECTION Lock (int);
134 void Init ();
135 void Delete ();
1fd5e000 136#ifdef _CYG_THREAD_FAILSAFE
1dc16fc7
CF
137 DWORD owner;
138 DWORD count;
1fd5e000 139#endif
9a08b2c0 140private:
1dc16fc7
CF
141 CRITICAL_SECTION lock;
142 bool inited;
1fd5e000
CF
143};
144
9a08b2c0
CF
145#define PTHREAD_MAGIC 0xdf0df045
146#define PTHREAD_MUTEX_MAGIC PTHREAD_MAGIC+1
147#define PTHREAD_KEY_MAGIC PTHREAD_MAGIC+2
148#define PTHREAD_ATTR_MAGIC PTHREAD_MAGIC+3
149#define PTHREAD_MUTEXATTR_MAGIC PTHREAD_MAGIC+4
150#define PTHREAD_COND_MAGIC PTHREAD_MAGIC+5
151#define PTHREAD_CONDATTR_MAGIC PTHREAD_MAGIC+6
152#define SEM_MAGIC PTHREAD_MAGIC+7
5c83f260
RC
153#define PTHREAD_ONCE_MAGIC PTHREAD_MAGIC+8;
154
155/* verifyable_object should not be defined here - it's a general purpose class */
1fd5e000 156
9a08b2c0
CF
157class verifyable_object
158{
159public:
160 long magic;
1fd5e000 161
5c83f260
RC
162 verifyable_object (long);
163 ~verifyable_object ();
9a08b2c0
CF
164};
165
9c510edc 166typedef enum
86336f4f
RC
167{
168 VALID_OBJECT,
169 INVALID_OBJECT,
170 VALID_STATIC_OBJECT
171} verifyable_object_state;
172
173verifyable_object_state verifyable_object_isvalid (void const *, long);
174verifyable_object_state verifyable_object_isvalid (void const *, long, void *);
1fd5e000 175
5c83f260
RC
176class pthread_key:public verifyable_object
177{
178public:
179
180 DWORD dwTlsIndex;
181 int set (const void *);
182 void *get ();
183
184 pthread_key (void (*)(void *));
185 ~pthread_key ();
186};
187
188/* FIXME: test using multiple inheritance and merging key_destructor into pthread_key
189 * for efficiency */
190class pthread_key_destructor
191{
192public:
193 void (*destructor) (void *);
194 pthread_key_destructor *InsertAfter (pthread_key_destructor * node);
195 pthread_key_destructor *UnlinkNext ();
196 pthread_key_destructor *Next ();
197
198 pthread_key_destructor (void (*thedestructor) (void *), pthread_key * key);
199 pthread_key_destructor *next;
200 pthread_key *key;
201};
202
203class pthread_key_destructor_list
204{
205public:
206 void Insert (pthread_key_destructor * node);
207/* remove a given dataitem, wherever in the list it is */
208 pthread_key_destructor *Remove (pthread_key_destructor * item);
209/* get the first item and remove at the same time */
210 pthread_key_destructor *Pop ();
211 pthread_key_destructor *Remove (pthread_key * key);
212 void IterateNull ();
213private:
214 pthread_key_destructor * head;
215};
216
217
9a08b2c0 218class pthread_attr:public verifyable_object
1fd5e000 219{
9a08b2c0
CF
220public:
221 int joinable;
5c83f260
RC
222 int contentionscope;
223 int inheritsched;
224 struct sched_param schedparam;
9a08b2c0
CF
225 size_t stacksize;
226
5c83f260
RC
227 pthread_attr ();
228 ~pthread_attr ();
1fd5e000
CF
229};
230
43c3c4e3
RC
231class pthread_mutexattr:public verifyable_object
232{
233public:
234 int pshared;
235 int mutextype;
236 pthread_mutexattr ();
237 ~pthread_mutexattr ();
238};
239
240class pthread_mutex:public verifyable_object
241{
242public:
243 CRITICAL_SECTION criticalsection;
244 HANDLE win32_obj_id;
245 LONG condwaits;
246 int pshared;
247 class pthread_mutex * next;
248
249 int Lock ();
250 int TryLock ();
251 int UnLock ();
252 void fixup_after_fork ();
253
254 pthread_mutex (pthread_mutexattr * = NULL);
255 pthread_mutex (pthread_mutex_t *, pthread_mutexattr *);
256 ~pthread_mutex ();
257};
258
9a08b2c0 259class pthread:public verifyable_object
1fd5e000
CF
260{
261public:
9a08b2c0
CF
262 HANDLE win32_obj_id;
263 class pthread_attr attr;
264 void *(*function) (void *);
1dc16fc7
CF
265 void *arg;
266 void *return_ptr;
267 bool suspended;
5c83f260 268 int cancelstate, canceltype;
d288c1c7 269 HANDLE cancel_event;
e9259cb2 270 pthread_t joiner;
5c83f260
RC
271 // int joinable;
272
1dc16fc7
CF
273 /* signal handling */
274 struct sigaction *sigs;
275 sigset_t *sigmask;
276 LONG *sigtodo;
4e786173
RC
277 virtual void create (void *(*)(void *), pthread_attr *, void *);
278
279 pthread ();
280 virtual ~pthread ();
9a08b2c0 281
4e786173
RC
282 static void initMainThread(pthread *, HANDLE);
283 static bool isGoodObject(pthread_t *);
9a08b2c0 284
4e786173 285 virtual void exit (void *value_ptr);
d288c1c7 286
4e786173
RC
287 virtual int cancel ();
288 virtual void testcancel ();
d288c1c7
RC
289 static void static_cancel_self ();
290
4e786173
RC
291 virtual int setcancelstate (int state, int *oldstate);
292 virtual int setcanceltype (int type, int *oldtype);
d288c1c7 293
4e786173
RC
294 virtual void push_cleanup_handler (__pthread_cleanup_handler *handler);
295 virtual void pop_cleanup_handler (int const execute);
007276b3
RC
296
297 static pthread* self ();
43c3c4e3 298 static void *thread_init_wrapper (void *);
007276b3 299
4e786173
RC
300 virtual unsigned long getsequence_np();
301
9a08b2c0 302private:
5c83f260 303 DWORD thread_id;
f6709c07 304 __pthread_cleanup_handler *cleanup_stack;
43c3c4e3 305 pthread_mutex mutex;
007276b3 306
43c3c4e3
RC
307 friend int __pthread_join (pthread_t * thread, void **return_val);
308 friend int __pthread_detach (pthread_t * thread);
077d8b23 309
007276b3 310 void pop_all_cleanup_handlers (void);
4e786173
RC
311 void precreate (pthread_attr *);
312 void postcreate ();
313 void setThreadIdtoCurrent();
314 static void setTlsSelfPointer(pthread *);
315 void cancel_self ();
316 DWORD getThreadId ();
317};
318
319class pthreadNull : public pthread
320{
321 public:
322 static pthread *getNullpthread();
323 ~pthreadNull();
324
325 /* From pthread These should never get called
326 * as the ojbect is not verifyable
327 */
328 void create (void *(*)(void *), pthread_attr *, void *);
329 void exit (void *value_ptr);
330 int cancel ();
331 void testcancel ();
332 int setcancelstate (int state, int *oldstate);
333 int setcanceltype (int type, int *oldtype);
334 void push_cleanup_handler (__pthread_cleanup_handler *handler);
335 void pop_cleanup_handler (int const execute);
336 unsigned long getsequence_np();
337
338 private:
339 pthreadNull ();
340 static pthreadNull _instance;
1fd5e000
CF
341};
342
9a08b2c0 343class pthread_condattr:public verifyable_object
1fd5e000
CF
344{
345public:
1dc16fc7 346 int shared;
9a08b2c0 347
462f4eff
CF
348 pthread_condattr ();
349 ~pthread_condattr ();
1fd5e000
CF
350};
351
9a08b2c0 352class pthread_cond:public verifyable_object
5ccbf4b6
CF
353{
354public:
355 int shared;
356 LONG waiting;
cbce4980 357 LONG ExitingWait;
9a08b2c0 358 pthread_mutex *mutex;
68ebd3f6
RC
359 /* to allow atomic behaviour for cond_broadcast */
360 pthread_mutex_t cond_access;
9a08b2c0 361 HANDLE win32_obj_id;
f9229ef7 362 class pthread_cond * next;
5ccbf4b6 363 int TimedWait (DWORD dwMilliseconds);
9a08b2c0
CF
364 void BroadCast ();
365 void Signal ();
f9229ef7 366 void fixup_after_fork ();
9a08b2c0 367
5c83f260
RC
368 pthread_cond (pthread_condattr *);
369 ~pthread_cond ();
370};
371
372class pthread_once
373{
374public:
375 pthread_mutex_t mutex;
376 int state;
5ccbf4b6 377};
1fd5e000 378
9a08b2c0
CF
379/* shouldn't be here */
380class semaphore:public verifyable_object
1fd5e000 381{
9a08b2c0
CF
382public:
383 HANDLE win32_obj_id;
f9229ef7 384 class semaphore * next;
9a08b2c0 385 int shared;
f9229ef7 386 long currentvalue;
9a08b2c0
CF
387 void Wait ();
388 void Post ();
389 int TryWait ();
f9229ef7 390 void fixup_after_fork ();
9a08b2c0 391
5c83f260
RC
392 semaphore (int, unsigned int);
393 ~semaphore ();
9a08b2c0
CF
394};
395
39b6859a
CF
396class callback
397{
398public:
399 void (*cb)(void);
400 class callback * next;
401};
402
1fd5e000
CF
403class MTinterface
404{
405public:
1dc16fc7
CF
406 // General
407 DWORD reent_index;
9a08b2c0
CF
408 DWORD thread_self_dwTlsIndex;
409 /* we may get 0 for the Tls index.. grrr */
410 int indexallocated;
5c83f260 411 int concurrency;
e6b98fc8 412 long int threadcount;
1fd5e000 413
1dc16fc7
CF
414 // Used for main thread data, and sigproc thread
415 struct __reent_t reents;
416 struct _winsup_t winsup_reent;
9a08b2c0 417 pthread mainthread;
1fd5e000 418
5c83f260 419 pthread_key_destructor_list destructors;
39b6859a
CF
420 callback *pthread_prepare;
421 callback *pthread_child;
422 callback *pthread_parent;
5c83f260 423
ac9841a0
RC
424 // list of mutex's. USE THREADSAFE INSERTS AND DELETES.
425 class pthread_mutex * mutexs;
f9229ef7
RC
426 class pthread_cond * conds;
427 class semaphore * semaphores;
9450ad0d 428
166b2571 429 void Init (int);
f9229ef7 430 void fixup_after_fork (void);
1fd5e000 431
077d8b23
CF
432 MTinterface ():reent_index (0), indexallocated (0), threadcount (1)
433 {
434 pthread_prepare = NULL;
435 pthread_child = NULL;
436 pthread_parent = NULL;
437 }
1fd5e000
CF
438};
439
39b6859a
CF
440void __pthread_atforkprepare(void);
441void __pthread_atforkparent(void);
442void __pthread_atforkchild(void);
1fd5e000 443
d288c1c7
RC
444/* Cancellation */
445int __pthread_cancel (pthread_t thread);
446
007276b3 447/* Thread Exit */
007276b3
RC
448int __pthread_join (pthread_t * thread, void **return_val);
449int __pthread_detach (pthread_t * thread);
450
1fd5e000
CF
451extern "C"
452{
1fd5e000 453/* ThreadCreation */
5c83f260
RC
454int __pthread_create (pthread_t * thread, const pthread_attr_t * attr,
455 void *(*start_routine) (void *), void *arg);
456int __pthread_once (pthread_once_t *, void (*)(void));
39b6859a 457int __pthread_atfork(void (*)(void), void (*)(void), void (*)(void));
5c83f260
RC
458
459int __pthread_attr_init (pthread_attr_t * attr);
460int __pthread_attr_destroy (pthread_attr_t * attr);
461int __pthread_attr_setdetachstate (pthread_attr_t *, int);
462int __pthread_attr_getdetachstate (const pthread_attr_t *, int *);
463int __pthread_attr_setstacksize (pthread_attr_t * attr, size_t size);
464int __pthread_attr_getstacksize (const pthread_attr_t * attr, size_t * size);
465
466int __pthread_attr_getinheritsched (const pthread_attr_t *, int *);
467int __pthread_attr_getschedparam (const pthread_attr_t *,
468 struct sched_param *);
469int __pthread_attr_getschedpolicy (const pthread_attr_t *, int *);
470int __pthread_attr_getscope (const pthread_attr_t *, int *);
471int __pthread_attr_getstackaddr (const pthread_attr_t *, void **);
472int __pthread_attr_setinheritsched (pthread_attr_t *, int);
473int __pthread_attr_setschedparam (pthread_attr_t *,
474 const struct sched_param *);
475int __pthread_attr_setschedpolicy (pthread_attr_t *, int);
476int __pthread_attr_setscope (pthread_attr_t *, int);
477int __pthread_attr_setstackaddr (pthread_attr_t *, void *);
478
1fd5e000 479/* Thread suspend */
5c83f260
RC
480int __pthread_suspend (pthread_t * thread);
481int __pthread_continue (pthread_t * thread);
1fd5e000 482
1fd5e000 483/* Thread SpecificData */
5c83f260
RC
484int __pthread_key_create (pthread_key_t * key, void (*destructor) (void *));
485int __pthread_key_delete (pthread_key_t key);
486int __pthread_setspecific (pthread_key_t key, const void *value);
487void *__pthread_getspecific (pthread_key_t key);
1fd5e000 488
5ccbf4b6 489/* Thead synchroniation */
5c83f260
RC
490int __pthread_cond_destroy (pthread_cond_t * cond);
491int __pthread_cond_init (pthread_cond_t * cond,
492 const pthread_condattr_t * attr);
493int __pthread_cond_signal (pthread_cond_t * cond);
494int __pthread_cond_broadcast (pthread_cond_t * cond);
5c83f260
RC
495int __pthread_condattr_init (pthread_condattr_t * condattr);
496int __pthread_condattr_destroy (pthread_condattr_t * condattr);
497int __pthread_condattr_getpshared (const pthread_condattr_t * attr,
498 int *pshared);
499int __pthread_condattr_setpshared (pthread_condattr_t * attr, int pshared);
1fd5e000
CF
500
501/* Thread signal */
5c83f260
RC
502int __pthread_kill (pthread_t thread, int sig);
503int __pthread_sigmask (int operation, const sigset_t * set,
504 sigset_t * old_set);
1fd5e000
CF
505
506/* ID */
5c83f260 507int __pthread_equal (pthread_t * t1, pthread_t * t2);
1fd5e000 508
1fd5e000 509/* Mutexes */
5c83f260
RC
510int __pthread_mutex_init (pthread_mutex_t *, const pthread_mutexattr_t *);
511int __pthread_mutex_lock (pthread_mutex_t *);
512int __pthread_mutex_trylock (pthread_mutex_t *);
513int __pthread_mutex_unlock (pthread_mutex_t *);
514int __pthread_mutex_destroy (pthread_mutex_t *);
515int __pthread_mutex_setprioceiling (pthread_mutex_t * mutex,
516 int prioceiling, int *old_ceiling);
517int __pthread_mutex_getprioceiling (const pthread_mutex_t * mutex,
518 int *prioceiling);
519
520
521int __pthread_mutexattr_destroy (pthread_mutexattr_t *);
522int __pthread_mutexattr_getprioceiling (const pthread_mutexattr_t *, int *);
523int __pthread_mutexattr_getprotocol (const pthread_mutexattr_t *, int *);
524int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *, int *);
525int __pthread_mutexattr_gettype (const pthread_mutexattr_t *, int *);
526int __pthread_mutexattr_init (pthread_mutexattr_t *);
527int __pthread_mutexattr_setprioceiling (pthread_mutexattr_t *, int);
528int __pthread_mutexattr_setprotocol (pthread_mutexattr_t *, int);
529int __pthread_mutexattr_setpshared (pthread_mutexattr_t *, int);
530int __pthread_mutexattr_settype (pthread_mutexattr_t *, int);
531
532
533/* Scheduling */
534int __pthread_getconcurrency (void);
535int __pthread_setconcurrency (int new_level);
536int __pthread_getschedparam (pthread_t thread, int *policy,
537 struct sched_param *param);
538int __pthread_setschedparam (pthread_t thread, int policy,
539 const struct sched_param *param);
540
541/* cancelability states */
5c83f260 542
1fd5e000 543/* Semaphores */
5c83f260
RC
544int __sem_init (sem_t * sem, int pshared, unsigned int value);
545int __sem_destroy (sem_t * sem);
546int __sem_wait (sem_t * sem);
547int __sem_trywait (sem_t * sem);
548int __sem_post (sem_t * sem);
1fd5e000
CF
549};
550
b0e82b74 551#endif // MT_SAFE
1fd5e000 552
b0e82b74 553#endif // _CYGNUS_THREADS_
This page took 0.184257 seconds and 5 git commands to generate.