]> sourceware.org Git - newlib-cygwin.git/blame - winsup/cygwin/thread.h
* configure.in: Remove Makefile in build, host and target modules
[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
ed9fe455
TP
124class nativeMutex
125{
126public:
127 bool init ();
128 bool lock ();
129 void unlock ();
130private:
131 HANDLE theHandle;
132};
133
1fd5e000
CF
134class per_process;
135class pinfo;
136
137class ResourceLocks
138{
139public:
5c83f260
RC
140 ResourceLocks ()
141 {
142 }
1dc16fc7
CF
143 LPCRITICAL_SECTION Lock (int);
144 void Init ();
145 void Delete ();
1fd5e000 146#ifdef _CYG_THREAD_FAILSAFE
1dc16fc7
CF
147 DWORD owner;
148 DWORD count;
1fd5e000 149#endif
9a08b2c0 150private:
1dc16fc7
CF
151 CRITICAL_SECTION lock;
152 bool inited;
1fd5e000
CF
153};
154
9a08b2c0
CF
155#define PTHREAD_MAGIC 0xdf0df045
156#define PTHREAD_MUTEX_MAGIC PTHREAD_MAGIC+1
157#define PTHREAD_KEY_MAGIC PTHREAD_MAGIC+2
158#define PTHREAD_ATTR_MAGIC PTHREAD_MAGIC+3
159#define PTHREAD_MUTEXATTR_MAGIC PTHREAD_MAGIC+4
160#define PTHREAD_COND_MAGIC PTHREAD_MAGIC+5
161#define PTHREAD_CONDATTR_MAGIC PTHREAD_MAGIC+6
162#define SEM_MAGIC PTHREAD_MAGIC+7
5c83f260
RC
163#define PTHREAD_ONCE_MAGIC PTHREAD_MAGIC+8;
164
165/* verifyable_object should not be defined here - it's a general purpose class */
1fd5e000 166
9a08b2c0
CF
167class verifyable_object
168{
169public:
170 long magic;
1fd5e000 171
aea1f301
CF
172 verifyable_object (long);
173 virtual ~verifyable_object ();
9a08b2c0
CF
174};
175
9c510edc 176typedef enum
86336f4f
RC
177{
178 VALID_OBJECT,
179 INVALID_OBJECT,
180 VALID_STATIC_OBJECT
181} verifyable_object_state;
182
183verifyable_object_state verifyable_object_isvalid (void const *, long);
184verifyable_object_state verifyable_object_isvalid (void const *, long, void *);
1fd5e000 185
af428c1e
RC
186/* interface */
187template <class ListNode> class List {
188public:
189 List();
190 void Insert (ListNode *aNode);
191 ListNode *Remove ( ListNode *aNode);
192 ListNode *Pop ();
193 void forEach (void (*)(ListNode *aNode));
194protected:
195 ListNode *head;
196};
197
5c83f260
RC
198class pthread_key:public verifyable_object
199{
200public:
4e2822f1 201 static bool isGoodObject (pthread_key_t const *);
af428c1e 202 static void runAllDestructors ();
5c83f260
RC
203
204 DWORD dwTlsIndex;
f1f13795 205
5c83f260 206 int set (const void *);
20b94ee9 207 void *get () const;
5c83f260 208
20b94ee9 209 pthread_key (void (*)(void *));
aea1f301 210 ~pthread_key ();
f1f13795
RC
211 static void fixup_before_fork();
212 static void fixup_after_fork();
af428c1e
RC
213
214 /* List support calls */
215 class pthread_key *next;
f1f13795
RC
216private:
217 // lists of objects. USE THREADSAFE INSERTS AND DELETES.
af428c1e
RC
218 static List<pthread_key> keys;
219 static void saveAKey (pthread_key *);
220 static void restoreAKey (pthread_key *);
221 static void destroyAKey (pthread_key *);
f1f13795
RC
222 void saveKeyToBuffer ();
223 void recreateKeyFromBuffer ();
20b94ee9 224 void (*destructor) (void *);
da6a08de 225 void run_destructor ();
af428c1e 226 void *fork_buf;
5c83f260
RC
227};
228
20b94ee9 229/* implementation */
af428c1e
RC
230template <class ListNode>
231List<ListNode>::List<ListNode> () : head(NULL)
232{
233}
20b94ee9
RC
234template <class ListNode> void
235List<ListNode>::Insert (ListNode *aNode)
236{
237 if (!aNode)
aea1f301 238 return;
af428c1e 239 aNode->next = (ListNode *) InterlockedExchangePointer (&head, aNode);
20b94ee9
RC
240}
241template <class ListNode> ListNode *
242List<ListNode>::Remove ( ListNode *aNode)
243{
244 if (!aNode)
aea1f301 245 return NULL;
20b94ee9 246 if (!head)
aea1f301 247 return NULL;
20b94ee9 248 if (aNode == head)
aea1f301 249 return Pop ();
20b94ee9 250 ListNode *resultPrev = head;
af428c1e 251 while (resultPrev && resultPrev->next && !(aNode == resultPrev->next))
aea1f301 252 resultPrev = resultPrev->next;
20b94ee9 253 if (resultPrev)
aea1f301 254 return (ListNode *)InterlockedExchangePointer (&resultPrev->next, resultPrev->next->next);
20b94ee9
RC
255 return NULL;
256}
257template <class ListNode> ListNode *
258List<ListNode>::Pop ()
259{
af428c1e 260 return (ListNode *) InterlockedExchangePointer (&head, head->next);
20b94ee9 261}
af428c1e
RC
262/* poor mans generic programming. */
263template <class ListNode> void
264List<ListNode>::forEach (void (*callback)(ListNode *))
5c83f260 265{
af428c1e 266 ListNode *aNode = head;
9d1e72a1 267 while (aNode)
aea1f301
CF
268 {
269 callback (aNode);
270 aNode = aNode->next;
271 }
af428c1e 272}
5c83f260 273
9a08b2c0 274class pthread_attr:public verifyable_object
1fd5e000 275{
9a08b2c0 276public:
4e2822f1 277 static bool isGoodObject(pthread_attr_t const *);
9a08b2c0 278 int joinable;
5c83f260
RC
279 int contentionscope;
280 int inheritsched;
281 struct sched_param schedparam;
9a08b2c0
CF
282 size_t stacksize;
283
aea1f301
CF
284 pthread_attr ();
285 ~pthread_attr ();
1fd5e000
CF
286};
287
43c3c4e3
RC
288class pthread_mutexattr:public verifyable_object
289{
290public:
4e2822f1 291 static bool isGoodObject(pthread_mutexattr_t const *);
43c3c4e3
RC
292 int pshared;
293 int mutextype;
aea1f301
CF
294 pthread_mutexattr ();
295 ~pthread_mutexattr ();
43c3c4e3
RC
296};
297
298class pthread_mutex:public verifyable_object
299{
300public:
ed9fe455
TP
301 static bool isGoodObject (pthread_mutex_t const *);
302 static bool isGoodInitializer (pthread_mutex_t const *);
303 static bool isGoodInitializerOrObject (pthread_mutex_t const *);
e9350b65 304 static bool isGoodInitializerOrBadObject (pthread_mutex_t const *mutex);
eb208df0
RC
305 static void initMutex ();
306 static int init (pthread_mutex_t *, const pthread_mutexattr_t *);
9d1e72a1 307
43c3c4e3
RC
308 CRITICAL_SECTION criticalsection;
309 HANDLE win32_obj_id;
310 LONG condwaits;
311 int pshared;
312 class pthread_mutex * next;
313
314 int Lock ();
315 int TryLock ();
316 int UnLock ();
317 void fixup_after_fork ();
318
319 pthread_mutex (pthread_mutexattr * = NULL);
320 pthread_mutex (pthread_mutex_t *, pthread_mutexattr *);
321 ~pthread_mutex ();
ed9fe455 322
eb208df0 323private:
06175d0a 324 static nativeMutex mutexInitializationLock;
43c3c4e3
RC
325};
326
9a08b2c0 327class pthread:public verifyable_object
1fd5e000
CF
328{
329public:
9a08b2c0
CF
330 HANDLE win32_obj_id;
331 class pthread_attr attr;
332 void *(*function) (void *);
1dc16fc7
CF
333 void *arg;
334 void *return_ptr;
335 bool suspended;
5c83f260 336 int cancelstate, canceltype;
d288c1c7 337 HANDLE cancel_event;
e9259cb2 338 pthread_t joiner;
5c83f260
RC
339 // int joinable;
340
1dc16fc7
CF
341 /* signal handling */
342 struct sigaction *sigs;
343 sigset_t *sigmask;
344 LONG *sigtodo;
4e786173
RC
345 virtual void create (void *(*)(void *), pthread_attr *, void *);
346
aea1f301
CF
347 pthread ();
348 virtual ~pthread ();
9a08b2c0 349
f8c8e13b 350 static void initMainThread (bool);
aea1f301
CF
351 static bool isGoodObject(pthread_t const *);
352 static void atforkprepare();
353 static void atforkparent();
354 static void atforkchild();
9a08b2c0 355
aea1f301
CF
356 /* API calls */
357 static int cancel (pthread_t);
358 static int join (pthread_t * thread, void **return_val);
359 static int detach (pthread_t * thread);
360 static int create (pthread_t * thread, const pthread_attr_t * attr,
01f58e41 361 void *(*start_routine) (void *), void *arg);
aea1f301
CF
362 static int once (pthread_once_t *, void (*)(void));
363 static int atfork(void (*)(void), void (*)(void), void (*)(void));
364 static int suspend (pthread_t * thread);
365 static int resume (pthread_t * thread);
01f58e41 366
aea1f301 367 virtual void exit (void *value_ptr);
d288c1c7 368
aea1f301 369 virtual int cancel ();
9d1e72a1 370
aea1f301
CF
371 virtual void testcancel ();
372 static void static_cancel_self ();
d288c1c7 373
aea1f301
CF
374 virtual int setcancelstate (int state, int *oldstate);
375 virtual int setcanceltype (int type, int *oldtype);
d288c1c7 376
aea1f301
CF
377 virtual void push_cleanup_handler (__pthread_cleanup_handler *handler);
378 virtual void pop_cleanup_handler (int const execute);
007276b3 379
aea1f301
CF
380 static pthread* self ();
381 static void *thread_init_wrapper (void *);
007276b3 382
aea1f301 383 virtual unsigned long getsequence_np();
4e786173 384
9a08b2c0 385private:
aea1f301
CF
386 DWORD thread_id;
387 __pthread_cleanup_handler *cleanup_stack;
388 pthread_mutex mutex;
389
390 void pop_all_cleanup_handlers (void);
391 void precreate (pthread_attr *);
392 void postcreate ();
f8c8e13b
RC
393 void setThreadIdtoCurrent ();
394 static void setTlsSelfPointer (pthread *);
395 static pthread *getTlsSelfPointer ();
aea1f301
CF
396 void cancel_self ();
397 DWORD getThreadId ();
f8c8e13b 398 void initCurrentThread ();
4e786173
RC
399};
400
401class pthreadNull : public pthread
402{
d04cf16c 403 public:
aea1f301
CF
404 static pthread *getNullpthread();
405 ~pthreadNull();
406
407 /* From pthread These should never get called
408 * as the ojbect is not verifyable
409 */
410 void create (void *(*)(void *), pthread_attr *, void *);
411 void exit (void *value_ptr);
412 int cancel ();
413 void testcancel ();
414 int setcancelstate (int state, int *oldstate);
415 int setcanceltype (int type, int *oldtype);
416 void push_cleanup_handler (__pthread_cleanup_handler *handler);
417 void pop_cleanup_handler (int const execute);
418 unsigned long getsequence_np();
4e786173
RC
419
420 private:
aea1f301
CF
421 pthreadNull ();
422 static pthreadNull _instance;
1fd5e000
CF
423};
424
9a08b2c0 425class pthread_condattr:public verifyable_object
1fd5e000
CF
426{
427public:
8b1978c3 428 static bool isGoodObject(pthread_condattr_t const *);
1dc16fc7 429 int shared;
9a08b2c0 430
462f4eff
CF
431 pthread_condattr ();
432 ~pthread_condattr ();
1fd5e000
CF
433};
434
9a08b2c0 435class pthread_cond:public verifyable_object
5ccbf4b6
CF
436{
437public:
ed9fe455
TP
438 static bool isGoodObject (pthread_cond_t const *);
439 static bool isGoodInitializer (pthread_cond_t const *);
440 static bool isGoodInitializerOrObject (pthread_cond_t const *);
441 static bool isGoodInitializerOrBadObject (pthread_cond_t const *);
442 static void initMutex ();
443 static int init (pthread_cond_t *, const pthread_condattr_t *);
444
5ccbf4b6
CF
445 int shared;
446 LONG waiting;
cbce4980 447 LONG ExitingWait;
9a08b2c0 448 pthread_mutex *mutex;
68ebd3f6
RC
449 /* to allow atomic behaviour for cond_broadcast */
450 pthread_mutex_t cond_access;
9a08b2c0 451 HANDLE win32_obj_id;
f9229ef7 452 class pthread_cond * next;
5ccbf4b6 453 int TimedWait (DWORD dwMilliseconds);
9a08b2c0
CF
454 void BroadCast ();
455 void Signal ();
f9229ef7 456 void fixup_after_fork ();
9a08b2c0 457
aea1f301
CF
458 pthread_cond (pthread_condattr *);
459 ~pthread_cond ();
ed9fe455
TP
460
461private:
462 static nativeMutex condInitializationLock;
5c83f260
RC
463};
464
465class pthread_once
466{
467public:
468 pthread_mutex_t mutex;
469 int state;
5ccbf4b6 470};
1fd5e000 471
9a08b2c0
CF
472/* shouldn't be here */
473class semaphore:public verifyable_object
1fd5e000 474{
9a08b2c0 475public:
8b1978c3 476 static bool isGoodObject(sem_t const *);
01f58e41
RC
477 /* API calls */
478 static int init (sem_t * sem, int pshared, unsigned int value);
479 static int destroy (sem_t * sem);
480 static int wait (sem_t * sem);
481 static int trywait (sem_t * sem);
482 static int post (sem_t * sem);
9d1e72a1 483
9a08b2c0 484 HANDLE win32_obj_id;
f9229ef7 485 class semaphore * next;
9a08b2c0 486 int shared;
f9229ef7 487 long currentvalue;
9a08b2c0
CF
488 void Wait ();
489 void Post ();
490 int TryWait ();
f9229ef7 491 void fixup_after_fork ();
9a08b2c0 492
aea1f301
CF
493 semaphore (int, unsigned int);
494 ~semaphore ();
9a08b2c0
CF
495};
496
39b6859a
CF
497class callback
498{
499public:
500 void (*cb)(void);
501 class callback * next;
502};
503
1fd5e000
CF
504class MTinterface
505{
506public:
1dc16fc7 507 // General
5c83f260 508 int concurrency;
e6b98fc8 509 long int threadcount;
1fd5e000 510
1dc16fc7
CF
511 // Used for main thread data, and sigproc thread
512 struct __reent_t reents;
513 struct _winsup_t winsup_reent;
1fd5e000 514
39b6859a
CF
515 callback *pthread_prepare;
516 callback *pthread_child;
517 callback *pthread_parent;
5c83f260 518
f1f13795 519 // lists of pthread objects. USE THREADSAFE INSERTS AND DELETES.
ac9841a0 520 class pthread_mutex * mutexs;
f9229ef7
RC
521 class pthread_cond * conds;
522 class semaphore * semaphores;
9450ad0d 523
f8c8e13b
RC
524 pthread_key reent_key;
525 pthread_key thread_self_key;
526
166b2571 527 void Init (int);
f1f13795 528 void fixup_before_fork (void);
f9229ef7 529 void fixup_after_fork (void);
1fd5e000 530
f8c8e13b
RC
531 MTinterface () :
532 concurrency (0), threadcount (1),
533 pthread_prepare (NULL), pthread_child (NULL), pthread_parent (NULL),
534 mutexs (NULL), conds (NULL), semaphores (NULL),
535 reent_key (NULL), thread_self_key (NULL)
aea1f301 536 {
aea1f301 537 }
1fd5e000
CF
538};
539
f8c8e13b
RC
540#define MT_INTERFACE user_data->threadinterface
541
1fd5e000
CF
542extern "C"
543{
5c83f260
RC
544int __pthread_attr_init (pthread_attr_t * attr);
545int __pthread_attr_destroy (pthread_attr_t * attr);
546int __pthread_attr_setdetachstate (pthread_attr_t *, int);
547int __pthread_attr_getdetachstate (const pthread_attr_t *, int *);
548int __pthread_attr_setstacksize (pthread_attr_t * attr, size_t size);
549int __pthread_attr_getstacksize (const pthread_attr_t * attr, size_t * size);
550
551int __pthread_attr_getinheritsched (const pthread_attr_t *, int *);
552int __pthread_attr_getschedparam (const pthread_attr_t *,
553 struct sched_param *);
554int __pthread_attr_getschedpolicy (const pthread_attr_t *, int *);
555int __pthread_attr_getscope (const pthread_attr_t *, int *);
556int __pthread_attr_getstackaddr (const pthread_attr_t *, void **);
557int __pthread_attr_setinheritsched (pthread_attr_t *, int);
558int __pthread_attr_setschedparam (pthread_attr_t *,
559 const struct sched_param *);
560int __pthread_attr_setschedpolicy (pthread_attr_t *, int);
561int __pthread_attr_setscope (pthread_attr_t *, int);
562int __pthread_attr_setstackaddr (pthread_attr_t *, void *);
563
1fd5e000 564/* Thread SpecificData */
5c83f260
RC
565int __pthread_key_create (pthread_key_t * key, void (*destructor) (void *));
566int __pthread_key_delete (pthread_key_t key);
567int __pthread_setspecific (pthread_key_t key, const void *value);
568void *__pthread_getspecific (pthread_key_t key);
1fd5e000 569
5ccbf4b6 570/* Thead synchroniation */
5c83f260 571int __pthread_cond_destroy (pthread_cond_t * cond);
5c83f260
RC
572int __pthread_cond_signal (pthread_cond_t * cond);
573int __pthread_cond_broadcast (pthread_cond_t * cond);
5c83f260
RC
574int __pthread_condattr_init (pthread_condattr_t * condattr);
575int __pthread_condattr_destroy (pthread_condattr_t * condattr);
576int __pthread_condattr_getpshared (const pthread_condattr_t * attr,
577 int *pshared);
578int __pthread_condattr_setpshared (pthread_condattr_t * attr, int pshared);
1fd5e000
CF
579
580/* Thread signal */
5c83f260
RC
581int __pthread_kill (pthread_t thread, int sig);
582int __pthread_sigmask (int operation, const sigset_t * set,
583 sigset_t * old_set);
1fd5e000
CF
584
585/* ID */
5c83f260 586int __pthread_equal (pthread_t * t1, pthread_t * t2);
1fd5e000 587
1fd5e000 588/* Mutexes */
5c83f260
RC
589int __pthread_mutex_lock (pthread_mutex_t *);
590int __pthread_mutex_trylock (pthread_mutex_t *);
591int __pthread_mutex_unlock (pthread_mutex_t *);
592int __pthread_mutex_destroy (pthread_mutex_t *);
593int __pthread_mutex_setprioceiling (pthread_mutex_t * mutex,
594 int prioceiling, int *old_ceiling);
595int __pthread_mutex_getprioceiling (const pthread_mutex_t * mutex,
596 int *prioceiling);
597
598
599int __pthread_mutexattr_destroy (pthread_mutexattr_t *);
600int __pthread_mutexattr_getprioceiling (const pthread_mutexattr_t *, int *);
601int __pthread_mutexattr_getprotocol (const pthread_mutexattr_t *, int *);
602int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *, int *);
603int __pthread_mutexattr_gettype (const pthread_mutexattr_t *, int *);
604int __pthread_mutexattr_init (pthread_mutexattr_t *);
605int __pthread_mutexattr_setprioceiling (pthread_mutexattr_t *, int);
606int __pthread_mutexattr_setprotocol (pthread_mutexattr_t *, int);
607int __pthread_mutexattr_setpshared (pthread_mutexattr_t *, int);
608int __pthread_mutexattr_settype (pthread_mutexattr_t *, int);
609
610
611/* Scheduling */
612int __pthread_getconcurrency (void);
613int __pthread_setconcurrency (int new_level);
614int __pthread_getschedparam (pthread_t thread, int *policy,
615 struct sched_param *param);
616int __pthread_setschedparam (pthread_t thread, int policy,
617 const struct sched_param *param);
618
1fd5e000 619};
b0e82b74 620#endif // MT_SAFE
1fd5e000 621
b0e82b74 622#endif // _CYGNUS_THREADS_
This page took 0.211529 seconds and 5 git commands to generate.