ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/Daodan/MinGW/include/pthread.h
Revision: 1046
Committed: Mon Aug 29 13:19:38 2016 UTC (9 years, 1 month ago) by alloc
Content type: text/x-chdr
File size: 38356 byte(s)
Log Message:
Daodan: Added Windows MinGW and build batch file

File Contents

# Content
1 /*
2 * pthread.h
3 *
4 * This is an implementation of the threads API of the Single Unix Specification.
5 *
6 * --------------------------------------------------------------------------
7 *
8 * Pthreads-win32 - POSIX Threads Library for Win32
9 * Copyright(C) 1998 John E. Bossom
10 * Copyright(C) 1999,2013 Pthreads-win32 contributors
11 *
12 * Homepage1: http://sourceware.org/pthreads-win32/
13 * Homepage2: http://sourceforge.net/projects/pthreads4w/
14 *
15 * The current list of contributors is contained
16 * in the file CONTRIBUTORS included with the source
17 * code distribution. The list can also be seen at the
18 * following World Wide Web location:
19 * http://sources.redhat.com/pthreads-win32/contributors.html
20 *
21 * This library is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU Lesser General Public
23 * License as published by the Free Software Foundation; either
24 * version 2 of the License, or (at your option) any later version.
25 *
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * Lesser General Public License for more details.
30 *
31 * You should have received a copy of the GNU Lesser General Public
32 * License along with this library in the file COPYING.LIB;
33 * if not, write to the Free Software Foundation, Inc.,
34 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
35 */
36 #ifndef _PTHREAD_H
37 #define _PTHREAD_H
38
39 /* There are three implementations of cancel cleanup.
40 * Note that pthread.h is included in both application
41 * compilation units and also internally for the library.
42 * The code here and within the library aims to work
43 * for all reasonable combinations of environments.
44 *
45 * The three implementations are:
46 *
47 * WIN32 SEH
48 * C
49 * C++
50 *
51 * Please note that exiting a push/pop block via
52 * "return", "exit", "break", or "continue" will
53 * lead to different behaviour amongst applications
54 * depending upon whether the library was built
55 * using SEH, C++, or C. For example, a library built
56 * with SEH will call the cleanup routine, while both
57 * C++ and C built versions will not.
58 */
59
60 /* Define defaults for cleanup code.
61 * Note: Unless the build explicitly defines one of the following, then
62 * we default to standard C style cleanup. This style uses setjmp/longjmp
63 * in the cancellation and thread exit implementations and therefore won't
64 * do stack unwinding if linked to applications that have it (e.g.
65 * C++ apps). This is currently consistent with most/all commercial Unix
66 * POSIX threads implementations.
67 */
68 #if !defined(__CLEANUP_SEH) && !defined(__CLEANUP_CXX) && !defined(__CLEANUP_C)
69 # define __CLEANUP_C
70 #endif
71
72 #if defined(__CLEANUP_SEH) && (!defined(_MSC_VER) && !defined(__PTW32_RC_MSC))
73 #error ERROR [__FILE__, line __LINE__]: SEH is not supported for this compiler.
74 #endif
75
76 #include <_ptw32.h>
77
78 /*
79 * Stop here if we are being included by the resource compiler.
80 */
81 #if !defined(RC_INVOKED)
82
83 #undef __PTW32_LEVEL
84 #undef __PTW32_LEVEL_MAX
85 #define __PTW32_LEVEL_MAX 3
86
87 #if _POSIX_C_SOURCE >= 200112L /* POSIX.1-2001 and later */
88 # define __PTW32_LEVEL __PTW32_LEVEL_MAX /* include everything */
89
90 #elif defined INCLUDE_NP /* earlier than POSIX.1-2001, but... */
91 # define __PTW32_LEVEL 2 /* include non-portable extensions */
92
93 #elif _POSIX_C_SOURCE >= 199309L /* POSIX.1-1993 */
94 # define __PTW32_LEVEL 1 /* include 1b, 1c, and 1d */
95
96 #elif defined _POSIX_SOURCE /* early POSIX */
97 # define __PTW32_LEVEL 0 /* minimal support */
98
99 #else /* unspecified support level */
100 # define __PTW32_LEVEL __PTW32_LEVEL_MAX /* include everything anyway */
101 #endif
102
103 /* -------------------------------------------------------------
104 *
105 * Module: pthread.h
106 *
107 * Purpose:
108 * Provides an implementation of PThreads based upon the
109 * standard:
110 *
111 * POSIX 1003.1-2001
112 * and
113 * The Single Unix Specification version 3
114 *
115 * (these two are equivalent)
116 *
117 * in order to enhance code portability between Windows,
118 * various commercial Unix implementations, and Linux.
119 *
120 * See the ANNOUNCE file for a full list of conforming
121 * routines and defined constants, and a list of missing
122 * routines and constants not defined in this implementation.
123 *
124 * Authors:
125 * There have been many contributors to this library.
126 * The initial implementation was contributed by
127 * John Bossom, and several others have provided major
128 * sections or revisions of parts of the implementation.
129 * Often significant effort has been contributed to
130 * find and fix important bugs and other problems to
131 * improve the reliability of the library, which sometimes
132 * is not reflected in the amount of code which changed as
133 * result.
134 * As much as possible, the contributors are acknowledged
135 * in the ChangeLog file in the source code distribution
136 * where their changes are noted in detail.
137 *
138 * Contributors are listed in the CONTRIBUTORS file.
139 *
140 * As usual, all bouquets go to the contributors, and all
141 * brickbats go to the project maintainer.
142 *
143 * Maintainer:
144 * The code base for this project is coordinated and
145 * eventually pre-tested, packaged, and made available by
146 *
147 * Ross Johnson <rpj@callisto.canberra.edu.au>
148 *
149 * QA Testers:
150 * Ultimately, the library is tested in the real world by
151 * a host of competent and demanding scientists and
152 * engineers who report bugs and/or provide solutions
153 * which are then fixed or incorporated into subsequent
154 * versions of the library. Each time a bug is fixed, a
155 * test case is written to prove the fix and ensure
156 * that later changes to the code don't reintroduce the
157 * same error. The number of test cases is slowly growing
158 * and therefore so is the code reliability.
159 *
160 * Compliance:
161 * See the file ANNOUNCE for the list of implemented
162 * and not-implemented routines and defined options.
163 * Of course, these are all defined is this file as well.
164 *
165 * Web site:
166 * The source code and other information about this library
167 * are available from
168 *
169 * http://sources.redhat.com/pthreads-win32/
170 *
171 * -------------------------------------------------------------
172 */
173 enum
174 { /* Boolean values to make us independent of system includes. */
175 PTW32_FALSE = 0,
176 PTW32_TRUE = (! PTW32_FALSE)
177 };
178
179 #include <time.h>
180 #include <sched.h>
181
182 /* -------------------------------------------------------------
183 *
184 * POSIX 1003.1-2001 Options
185 * =========================
186 *
187 * Options are normally set in <unistd.h>, which is not provided
188 * with pthreads-win32.
189 *
190 * For conformance with the Single Unix Specification (version 3), all of the
191 * options below are defined, and have a value of either -1 (not supported)
192 * or yyyymm[dd]L (supported).
193 *
194 * These options can neither be left undefined nor have a value of 0, because
195 * either indicates that sysconf(), which is not implemented, may be used at
196 * runtime to check the status of the option.
197 *
198 * _POSIX_THREADS (== 20080912L)
199 * If == 20080912L, you can use threads
200 *
201 * _POSIX_THREAD_ATTR_STACKSIZE (== 200809L)
202 * If == 200809L, you can control the size of a thread's
203 * stack
204 * pthread_attr_getstacksize
205 * pthread_attr_setstacksize
206 *
207 * _POSIX_THREAD_ATTR_STACKADDR (== -1)
208 * If == 200809L, you can allocate and control a thread's
209 * stack. If not supported, the following functions
210 * will return ENOSYS, indicating they are not
211 * supported:
212 * pthread_attr_getstackaddr
213 * pthread_attr_setstackaddr
214 *
215 * _POSIX_THREAD_PRIORITY_SCHEDULING (== -1)
216 * If == 200112L, you can use realtime scheduling.
217 * This option indicates that the behaviour of some
218 * implemented functions conforms to the additional TPS
219 * requirements in the standard. E.g. rwlocks favour
220 * writers over readers when threads have equal priority.
221 *
222 * _POSIX_THREAD_PRIO_INHERIT (== -1)
223 * If == 200809L, you can create priority inheritance
224 * mutexes.
225 * pthread_mutexattr_getprotocol +
226 * pthread_mutexattr_setprotocol +
227 *
228 * _POSIX_THREAD_PRIO_PROTECT (== -1)
229 * If == 200809L, you can create priority ceiling mutexes
230 * Indicates the availability of:
231 * pthread_mutex_getprioceiling
232 * pthread_mutex_setprioceiling
233 * pthread_mutexattr_getprioceiling
234 * pthread_mutexattr_getprotocol +
235 * pthread_mutexattr_setprioceiling
236 * pthread_mutexattr_setprotocol +
237 *
238 * _POSIX_THREAD_PROCESS_SHARED (== -1)
239 * If set, you can create mutexes and condition
240 * variables that can be shared with another
241 * process.If set, indicates the availability
242 * of:
243 * pthread_mutexattr_getpshared
244 * pthread_mutexattr_setpshared
245 * pthread_condattr_getpshared
246 * pthread_condattr_setpshared
247 *
248 * _POSIX_THREAD_SAFE_FUNCTIONS (== 200809L)
249 * If == 200809L you can use the special *_r library
250 * functions that provide thread-safe behaviour
251 *
252 * _POSIX_READER_WRITER_LOCKS (== 200809L)
253 * If == 200809L, you can use read/write locks
254 *
255 * _POSIX_SPIN_LOCKS (== 200809L)
256 * If == 200809L, you can use spin locks
257 *
258 * _POSIX_BARRIERS (== 200809L)
259 * If == 200809L, you can use barriers
260 *
261 * _POSIX_ROBUST_MUTEXES (== 200809L)
262 * If == 200809L, you can use robust mutexes
263 * Officially this should also imply
264 * _POSIX_THREAD_PROCESS_SHARED != -1 however
265 * not here yet.
266 *
267 * -------------------------------------------------------------
268 */
269 /* POSIX Options
270 */
271 #undef _POSIX_THREADS
272 #define _POSIX_THREADS 200809L
273
274 #undef _POSIX_READER_WRITER_LOCKS
275 #define _POSIX_READER_WRITER_LOCKS 200809L
276
277 #undef _POSIX_SPIN_LOCKS
278 #define _POSIX_SPIN_LOCKS 200809L
279
280 #undef _POSIX_BARRIERS
281 #define _POSIX_BARRIERS 200809L
282
283 #undef _POSIX_THREAD_SAFE_FUNCTIONS
284 #define _POSIX_THREAD_SAFE_FUNCTIONS 200809L
285
286 #undef _POSIX_THREAD_ATTR_STACKSIZE
287 #define _POSIX_THREAD_ATTR_STACKSIZE 200809L
288
289 #undef _POSIX_ROBUST_MUTEXES
290 #define _POSIX_ROBUST_MUTEXES 200809L
291
292 /* The following options are not supported
293 */
294 #undef _POSIX_THREAD_ATTR_STACKADDR
295 #define _POSIX_THREAD_ATTR_STACKADDR -1
296
297 #undef _POSIX_THREAD_PRIO_INHERIT
298 #define _POSIX_THREAD_PRIO_INHERIT -1
299
300 #undef _POSIX_THREAD_PRIO_PROTECT
301 #define _POSIX_THREAD_PRIO_PROTECT -1
302
303 /* TPS is not fully supported. */
304 #undef _POSIX_THREAD_PRIORITY_SCHEDULING
305 #define _POSIX_THREAD_PRIORITY_SCHEDULING -1
306
307 #undef _POSIX_THREAD_PROCESS_SHARED
308 #define _POSIX_THREAD_PROCESS_SHARED -1
309
310
311 /* POSIX 1003.1-2001 Limits
312 * ===========================
313 *
314 * These limits are normally set in <limits.h>, which is not provided with
315 * pthreads-win32.
316 *
317 * PTHREAD_DESTRUCTOR_ITERATIONS
318 * Maximum number of attempts to destroy
319 * a thread's thread-specific data on
320 * termination (must be at least 4)
321 *
322 * PTHREAD_KEYS_MAX
323 * Maximum number of thread-specific data keys
324 * available per process (must be at least 128)
325 *
326 * PTHREAD_STACK_MIN
327 * Minimum supported stack size for a thread
328 *
329 * PTHREAD_THREADS_MAX
330 * Maximum number of threads supported per
331 * process (must be at least 64).
332 *
333 * SEM_NSEMS_MAX
334 * The maximum number of semaphores a process can have.
335 * (must be at least 256)
336 *
337 * SEM_VALUE_MAX
338 * The maximum value a semaphore can have.
339 * (must be at least 32767)
340 *
341 */
342 #undef _POSIX_THREAD_DESTRUCTOR_ITERATIONS
343 #define _POSIX_THREAD_DESTRUCTOR_ITERATIONS 4
344
345 #undef PTHREAD_DESTRUCTOR_ITERATIONS
346 #define PTHREAD_DESTRUCTOR_ITERATIONS _POSIX_THREAD_DESTRUCTOR_ITERATIONS
347
348 #undef _POSIX_THREAD_KEYS_MAX
349 #define _POSIX_THREAD_KEYS_MAX 128
350
351 #undef PTHREAD_KEYS_MAX
352 #define PTHREAD_KEYS_MAX _POSIX_THREAD_KEYS_MAX
353
354 #undef PTHREAD_STACK_MIN
355 #define PTHREAD_STACK_MIN 0
356
357 #undef _POSIX_THREAD_THREADS_MAX
358 #define _POSIX_THREAD_THREADS_MAX 64
359
360 /* Arbitrary value */
361 #undef PTHREAD_THREADS_MAX
362 #define PTHREAD_THREADS_MAX 2019
363
364 #undef _POSIX_SEM_NSEMS_MAX
365 #define _POSIX_SEM_NSEMS_MAX 256
366
367 /* Arbitrary value */
368 #undef SEM_NSEMS_MAX
369 #define SEM_NSEMS_MAX 1024
370
371 #undef _POSIX_SEM_VALUE_MAX
372 #define _POSIX_SEM_VALUE_MAX 32767
373
374 #undef SEM_VALUE_MAX
375 #define SEM_VALUE_MAX INT_MAX
376
377
378 #if defined(_UWIN) && __PTW32_LEVEL >= __PTW32_LEVEL_MAX
379 # include <sys/types.h>
380 #else
381 /* Generic handle type - intended to provide the lifetime-uniqueness that
382 * a simple pointer can't. It should scale for either
383 * 32 or 64 bit systems.
384 *
385 * The constraint with this approach is that applications must
386 * strictly comply with POSIX, e.g. not assume scalar type, only
387 * compare pthread_t using the API function pthread_equal(), etc.
388 *
389 * Applications can use the element 'p' to compare, e.g. for sorting,
390 * but it will be up to the application to determine if handles are
391 * live or dead, or resurrected for an entirely new/different thread.
392 */
393 typedef struct
394 { void * p; /* Pointer to actual object */
395 #if __PTW32_VERSION_MAJOR > 2
396 size_t x; /* Extra information - reuse count etc */
397 #else
398 unsigned int x; /* Extra information - reuse count etc */
399 #endif
400 } ptw32_handle_t;
401
402 typedef ptw32_handle_t pthread_t;
403 typedef struct pthread_attr_t_ * pthread_attr_t;
404 typedef struct pthread_once_t_ pthread_once_t;
405 typedef struct pthread_key_t_ * pthread_key_t;
406 typedef struct pthread_mutex_t_ * pthread_mutex_t;
407 typedef struct pthread_mutexattr_t_ * pthread_mutexattr_t;
408 typedef struct pthread_cond_t_ * pthread_cond_t;
409 typedef struct pthread_condattr_t_ * pthread_condattr_t;
410 #endif
411
412 typedef struct pthread_rwlock_t_ * pthread_rwlock_t;
413 typedef struct pthread_rwlockattr_t_ * pthread_rwlockattr_t;
414 typedef struct pthread_spinlock_t_ * pthread_spinlock_t;
415 typedef struct pthread_barrier_t_ * pthread_barrier_t;
416 typedef struct pthread_barrierattr_t_ * pthread_barrierattr_t;
417
418 /* ====================
419 * ====================
420 * POSIX Threads
421 * ====================
422 * ====================
423 */
424
425 enum
426 { /* pthread_attr_{get,set}detachstate
427 */
428 PTHREAD_CREATE_JOINABLE = 0, /* Default */
429 PTHREAD_CREATE_DETACHED = 1,
430 /*
431 * pthread_attr_{get,set}inheritsched
432 */
433 PTHREAD_INHERIT_SCHED = 0,
434 PTHREAD_EXPLICIT_SCHED = 1, /* Default */
435 /*
436 * pthread_{get,set}scope
437 */
438 PTHREAD_SCOPE_PROCESS = 0,
439 PTHREAD_SCOPE_SYSTEM = 1, /* Default */
440 /*
441 * pthread_setcancelstate paramters
442 */
443 PTHREAD_CANCEL_ENABLE = 0, /* Default */
444 PTHREAD_CANCEL_DISABLE = 1,
445 /*
446 * pthread_setcanceltype parameters
447 */
448 PTHREAD_CANCEL_ASYNCHRONOUS = 0,
449 PTHREAD_CANCEL_DEFERRED = 1, /* Default */
450 /*
451 * pthread_mutexattr_{get,set}pshared
452 * pthread_condattr_{get,set}pshared
453 */
454 PTHREAD_PROCESS_PRIVATE = 0,
455 PTHREAD_PROCESS_SHARED = 1,
456 /*
457 * pthread_mutexattr_{get,set}robust
458 */
459 PTHREAD_MUTEX_STALLED = 0, /* Default */
460 PTHREAD_MUTEX_ROBUST = 1,
461 /*
462 * pthread_barrier_wait
463 */
464 PTHREAD_BARRIER_SERIAL_THREAD = -1
465 };
466
467 /* ====================
468 * ====================
469 * cancellation
470 * ====================
471 * ====================
472 */
473 #define PTHREAD_CANCELED ((void *)(size_t) -1)
474
475
476 /* ====================
477 * ====================
478 * Once Key
479 * ====================
480 * ====================
481 */
482 #if __PTW32_VERSION_MAJOR > 2
483
484 #define PTHREAD_ONCE_INIT { PTW32_FALSE, 0 }
485
486 struct pthread_once_t_
487 {
488 void * lock; /* MCS lock */
489 int done; /* indicates if user function has been executed */
490 };
491
492 #else
493
494 #define PTHREAD_ONCE_INIT { PTW32_FALSE, 0, 0, 0 }
495
496 struct pthread_once_t_
497 {
498 int done; /* indicates if user function has been executed */
499 void * lock; /* MCS lock */
500 int reserved1;
501 int reserved2;
502 };
503
504 #endif
505
506
507 /* ====================
508 * ====================
509 * Object initialisers
510 * ====================
511 * ====================
512 */
513 #define PTHREAD_MUTEX_INITIALIZER ((pthread_mutex_t)(size_t) -1)
514 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER ((pthread_mutex_t)(size_t) -2)
515 #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER ((pthread_mutex_t)(size_t) -3)
516
517 /* Compatibility with LinuxThreads
518 */
519 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP PTHREAD_RECURSIVE_MUTEX_INITIALIZER
520 #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP PTHREAD_ERRORCHECK_MUTEX_INITIALIZER
521 #define PTHREAD_COND_INITIALIZER ((pthread_cond_t)(size_t) -1)
522 #define PTHREAD_RWLOCK_INITIALIZER ((pthread_rwlock_t)(size_t) -1)
523 #define PTHREAD_SPINLOCK_INITIALIZER ((pthread_spinlock_t)(size_t) -1)
524
525
526 /* Mutex types.
527 */
528 enum
529 { /* Compatibility with LinuxThreads */
530 PTHREAD_MUTEX_FAST_NP,
531 PTHREAD_MUTEX_RECURSIVE_NP,
532 PTHREAD_MUTEX_ERRORCHECK_NP,
533 PTHREAD_MUTEX_TIMED_NP = PTHREAD_MUTEX_FAST_NP,
534 PTHREAD_MUTEX_ADAPTIVE_NP = PTHREAD_MUTEX_FAST_NP,
535 /* For compatibility with POSIX */
536 PTHREAD_MUTEX_NORMAL = PTHREAD_MUTEX_FAST_NP,
537 PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP,
538 PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP,
539 PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
540 };
541
542
543 typedef struct ptw32_cleanup_t ptw32_cleanup_t;
544
545 #if defined(_MSC_VER)
546 /* Disable MSVC 'anachronism used' warning */
547 #pragma warning( disable : 4229 )
548 #endif
549
550 typedef void (* __PTW32_CDECL ptw32_cleanup_callback_t)(void *);
551
552 #if defined(_MSC_VER)
553 #pragma warning( default : 4229 )
554 #endif
555
556 struct ptw32_cleanup_t
557 {
558 ptw32_cleanup_callback_t routine;
559 void *arg;
560 struct ptw32_cleanup_t *prev;
561 };
562
563 #if defined(__CLEANUP_SEH)
564 /* WIN32 SEH version of cancel cleanup.
565 */
566
567 #define pthread_cleanup_push( _rout, _arg ) \
568 { \
569 ptw32_cleanup_t _cleanup; \
570 \
571 _cleanup.routine = (ptw32_cleanup_callback_t)(_rout); \
572 _cleanup.arg = (_arg); \
573 __try \
574 { \
575
576 #define pthread_cleanup_pop( _execute ) \
577 } \
578 __finally \
579 { \
580 if( _execute || AbnormalTermination()) \
581 { \
582 (*(_cleanup.routine))( _cleanup.arg ); \
583 } \
584 } \
585 }
586
587 #else /* !__CLEANUP_SEH */
588
589 #if defined(__CLEANUP_C)
590 /* C implementation of PThreads cancel cleanup
591 */
592
593 #define pthread_cleanup_push( _rout, _arg ) \
594 { \
595 ptw32_cleanup_t _cleanup; \
596 \
597 ptw32_push_cleanup( &_cleanup, (ptw32_cleanup_callback_t) (_rout), (_arg) ); \
598
599 #define pthread_cleanup_pop( _execute ) \
600 (void) ptw32_pop_cleanup( _execute ); \
601 }
602
603 #else /* !__CLEANUP_C */
604
605 #if defined(__CLEANUP_CXX)
606 /* C++ version of cancel cleanup.
607 * - John E. Bossom.
608 */
609 class PThreadCleanup {
610 /*
611 * PThreadCleanup
612 *
613 * Purpose
614 * This class is a C++ helper class that is
615 * used to implement pthread_cleanup_push/
616 * pthread_cleanup_pop.
617 * The destructor of this class automatically
618 * pops the pushed cleanup routine regardless
619 * of how the code exits the scope
620 * (i.e. such as by an exception)
621 */
622 ptw32_cleanup_callback_t cleanUpRout;
623 void * obj;
624 int executeIt;
625
626 public:
627 PThreadCleanup() :
628 cleanUpRout( 0 ),
629 obj( 0 ),
630 executeIt( 0 )
631 /*
632 * No cleanup performed
633 */
634 {
635 }
636
637 PThreadCleanup(
638 ptw32_cleanup_callback_t routine,
639 void * arg ) :
640 cleanUpRout( routine ),
641 obj( arg ),
642 executeIt( 1 )
643 /*
644 * Registers a cleanup routine for 'arg'
645 */
646 {
647 }
648
649 ~PThreadCleanup()
650 {
651 if ( executeIt && ((void *) cleanUpRout != (void *) 0) )
652 {
653 (void) (*cleanUpRout)( obj );
654 }
655 }
656
657 void execute( int exec )
658 {
659 executeIt = exec;
660 }
661 };
662
663 /* C++ implementation of PThreads cancel cleanup;
664 * This implementation takes advantage of a helper
665 * class who's destructor automatically calls the
666 * cleanup routine if we exit our scope weirdly
667 */
668 #define pthread_cleanup_push( _rout, _arg ) \
669 { \
670 PThreadCleanup cleanup((ptw32_cleanup_callback_t)(_rout), \
671 (void *) (_arg) );
672
673 #define pthread_cleanup_pop( _execute ) \
674 cleanup.execute( _execute ); \
675 }
676
677 #else
678
679 #error ERROR [__FILE__, line __LINE__]: Cleanup type undefined.
680
681 #endif /* __CLEANUP_CXX */
682 #endif /* __CLEANUP_C */
683 #endif /* __CLEANUP_SEH */
684
685
686 /*
687 * ===============
688 * ===============
689 * Methods
690 * ===============
691 * ===============
692 */
693
694 __PTW32_BEGIN_C_DECLS
695
696 /* PThread Attribute Functions
697 */
698 __PTW32_DECLSPEC int pthread_attr_init (pthread_attr_t *);
699 __PTW32_DECLSPEC int pthread_attr_destroy (pthread_attr_t *);
700
701 __PTW32_DECLSPEC int pthread_attr_getaffinity_np
702 (const pthread_attr_t *, size_t, cpu_set_t *);
703
704 __PTW32_DECLSPEC int pthread_attr_getdetachstate (const pthread_attr_t *, int *);
705 __PTW32_DECLSPEC int pthread_attr_getstackaddr (const pthread_attr_t *, void **);
706
707 __PTW32_DECLSPEC int pthread_attr_getstacksize (const pthread_attr_t *, size_t *);
708
709 __PTW32_DECLSPEC int pthread_attr_setaffinity_np
710 (pthread_attr_t *, size_t, const cpu_set_t *);
711
712 __PTW32_DECLSPEC int pthread_attr_setdetachstate (pthread_attr_t *, int);
713 __PTW32_DECLSPEC int pthread_attr_setstackaddr (pthread_attr_t *, void *);
714 __PTW32_DECLSPEC int pthread_attr_setstacksize (pthread_attr_t *, size_t);
715
716 __PTW32_DECLSPEC int pthread_attr_getschedparam
717 (const pthread_attr_t *, struct sched_param *);
718
719 __PTW32_DECLSPEC int pthread_attr_setschedparam
720 (pthread_attr_t *, const struct sched_param *);
721
722 __PTW32_DECLSPEC int pthread_attr_setschedpolicy (pthread_attr_t *, int);
723 __PTW32_DECLSPEC int pthread_attr_getschedpolicy (const pthread_attr_t *, int *);
724 __PTW32_DECLSPEC int pthread_attr_setinheritsched (pthread_attr_t *, int);
725
726 __PTW32_DECLSPEC int pthread_attr_getinheritsched
727 (const pthread_attr_t *, int * inheritsched);
728
729 __PTW32_DECLSPEC int pthread_attr_setscope (pthread_attr_t *, int);
730 __PTW32_DECLSPEC int pthread_attr_getscope (const pthread_attr_t *, int *);
731
732 /* PThread Functions
733 */
734 __PTW32_DECLSPEC int pthread_create
735 (pthread_t *, const pthread_attr_t *, void *(__PTW32_CDECL *)(void *), void *);
736
737 __PTW32_DECLSPEC int pthread_detach (pthread_t);
738 __PTW32_DECLSPEC int pthread_equal (pthread_t, pthread_t);
739 __PTW32_DECLSPEC void pthread_exit (void *);
740 __PTW32_DECLSPEC int pthread_join (pthread_t, void **);
741 __PTW32_DECLSPEC pthread_t pthread_self (void);
742 __PTW32_DECLSPEC int pthread_cancel (pthread_t);
743 __PTW32_DECLSPEC int pthread_setcancelstate (int, int *);
744 __PTW32_DECLSPEC int pthread_setcanceltype (int, int *);
745 __PTW32_DECLSPEC void pthread_testcancel (void);
746
747 __PTW32_DECLSPEC int pthread_once
748 (pthread_once_t *, void (__PTW32_CDECL *)(void));
749
750 #if __PTW32_LEVEL >= __PTW32_LEVEL_MAX
751 __PTW32_DECLSPEC ptw32_cleanup_t * ptw32_pop_cleanup (int);
752
753 __PTW32_DECLSPEC void ptw32_push_cleanup
754 (ptw32_cleanup_t *, ptw32_cleanup_callback_t, void *);
755
756 #endif /* __PTW32_LEVEL >= __PTW32_LEVEL_MAX */
757
758 /* Thread Specific Data Functions
759 */
760 __PTW32_DECLSPEC int pthread_key_create
761 (pthread_key_t *, void (__PTW32_CDECL *)(void *));
762
763 __PTW32_DECLSPEC int pthread_key_delete (pthread_key_t);
764 __PTW32_DECLSPEC int pthread_setspecific (pthread_key_t, const void *);
765 __PTW32_DECLSPEC void * pthread_getspecific (pthread_key_t);
766
767
768 /* Mutex Attribute Functions
769 */
770 __PTW32_DECLSPEC int pthread_mutexattr_init (pthread_mutexattr_t *);
771 __PTW32_DECLSPEC int pthread_mutexattr_destroy (pthread_mutexattr_t *);
772
773 __PTW32_DECLSPEC int pthread_mutexattr_getpshared
774 (const pthread_mutexattr_t *, int *);
775
776 __PTW32_DECLSPEC int pthread_mutexattr_setpshared (pthread_mutexattr_t *, int);
777 __PTW32_DECLSPEC int pthread_mutexattr_settype (pthread_mutexattr_t *, int);
778
779 __PTW32_DECLSPEC int pthread_mutexattr_gettype
780 (const pthread_mutexattr_t *, int *);
781
782 __PTW32_DECLSPEC int pthread_mutexattr_setrobust (pthread_mutexattr_t *, int);
783
784 __PTW32_DECLSPEC int pthread_mutexattr_getrobust
785 ( const pthread_mutexattr_t *, int *);
786
787 /* Barrier Attribute Functions
788 */
789 __PTW32_DECLSPEC int pthread_barrierattr_init (pthread_barrierattr_t *);
790 __PTW32_DECLSPEC int pthread_barrierattr_destroy (pthread_barrierattr_t *);
791
792 __PTW32_DECLSPEC int pthread_barrierattr_getpshared
793 (const pthread_barrierattr_t *, int *);
794
795 __PTW32_DECLSPEC int pthread_barrierattr_setpshared
796 (pthread_barrierattr_t *, int);
797
798 /* Mutex Functions
799 */
800 __PTW32_DECLSPEC int pthread_mutex_init
801 (pthread_mutex_t *, const pthread_mutexattr_t *);
802
803 __PTW32_DECLSPEC int pthread_mutex_destroy (pthread_mutex_t *);
804 __PTW32_DECLSPEC int pthread_mutex_lock (pthread_mutex_t *);
805
806 __PTW32_DECLSPEC int pthread_mutex_timedlock
807 (pthread_mutex_t *, const struct timespec *);
808
809 __PTW32_DECLSPEC int pthread_mutex_trylock (pthread_mutex_t *);
810 __PTW32_DECLSPEC int pthread_mutex_unlock (pthread_mutex_t *);
811 __PTW32_DECLSPEC int pthread_mutex_consistent (pthread_mutex_t *);
812
813 /* Spinlock Functions
814 */
815 __PTW32_DECLSPEC int pthread_spin_init (pthread_spinlock_t *, int);
816 __PTW32_DECLSPEC int pthread_spin_destroy (pthread_spinlock_t *);
817 __PTW32_DECLSPEC int pthread_spin_lock (pthread_spinlock_t *);
818 __PTW32_DECLSPEC int pthread_spin_trylock (pthread_spinlock_t *);
819 __PTW32_DECLSPEC int pthread_spin_unlock (pthread_spinlock_t *);
820
821 /* Barrier Functions
822 */
823 __PTW32_DECLSPEC int pthread_barrier_init
824 (pthread_barrier_t *, const pthread_barrierattr_t *, unsigned int);
825
826 __PTW32_DECLSPEC int pthread_barrier_destroy (pthread_barrier_t *);
827 __PTW32_DECLSPEC int pthread_barrier_wait (pthread_barrier_t *);
828
829 /* Condition Variable Attribute Functions
830 */
831 __PTW32_DECLSPEC int pthread_condattr_init (pthread_condattr_t *);
832 __PTW32_DECLSPEC int pthread_condattr_destroy (pthread_condattr_t *);
833
834 __PTW32_DECLSPEC int pthread_condattr_getpshared
835 (const pthread_condattr_t *, int *);
836
837 __PTW32_DECLSPEC int pthread_condattr_setpshared (pthread_condattr_t *, int);
838
839 /* Condition Variable Functions
840 */
841 __PTW32_DECLSPEC int pthread_cond_init
842 (pthread_cond_t *, const pthread_condattr_t *);
843
844 __PTW32_DECLSPEC int pthread_cond_destroy (pthread_cond_t * cond);
845 __PTW32_DECLSPEC int pthread_cond_wait (pthread_cond_t *, pthread_mutex_t *);
846
847 __PTW32_DECLSPEC int pthread_cond_timedwait
848 (pthread_cond_t *, pthread_mutex_t *, const struct timespec *);
849
850 __PTW32_DECLSPEC int pthread_cond_signal (pthread_cond_t *);
851 __PTW32_DECLSPEC int pthread_cond_broadcast (pthread_cond_t *);
852
853 /* Scheduling
854 */
855 __PTW32_DECLSPEC int pthread_setschedparam
856 (pthread_t, int, const struct sched_param *);
857
858 __PTW32_DECLSPEC int pthread_getschedparam
859 (pthread_t, int *, struct sched_param *);
860
861 __PTW32_DECLSPEC int pthread_setconcurrency (int);
862 __PTW32_DECLSPEC int pthread_getconcurrency (void);
863
864 /* Read-Write Lock Functions
865 */
866 __PTW32_DECLSPEC int pthread_rwlock_init
867 (pthread_rwlock_t *, const pthread_rwlockattr_t *);
868
869 __PTW32_DECLSPEC int pthread_rwlock_destroy (pthread_rwlock_t *);
870 __PTW32_DECLSPEC int pthread_rwlock_tryrdlock (pthread_rwlock_t *);
871 __PTW32_DECLSPEC int pthread_rwlock_trywrlock (pthread_rwlock_t *);
872 __PTW32_DECLSPEC int pthread_rwlock_rdlock (pthread_rwlock_t *);
873
874 __PTW32_DECLSPEC int pthread_rwlock_timedrdlock
875 (pthread_rwlock_t *, const struct timespec *);
876
877 __PTW32_DECLSPEC int pthread_rwlock_wrlock (pthread_rwlock_t *);
878 __PTW32_DECLSPEC int pthread_rwlock_timedwrlock
879 (pthread_rwlock_t *, const struct timespec *);
880
881 __PTW32_DECLSPEC int pthread_rwlock_unlock (pthread_rwlock_t *lock);
882 __PTW32_DECLSPEC int pthread_rwlockattr_init (pthread_rwlockattr_t *);
883 __PTW32_DECLSPEC int pthread_rwlockattr_destroy (pthread_rwlockattr_t *);
884
885 __PTW32_DECLSPEC int pthread_rwlockattr_getpshared
886 (const pthread_rwlockattr_t *, int *);
887
888 __PTW32_DECLSPEC int pthread_rwlockattr_setpshared
889 (pthread_rwlockattr_t *, int);
890
891 #if __PTW32_LEVEL >= __PTW32_LEVEL_MAX - 1
892
893 /* Signal Functions. Should be defined in <signal.h> but MSVC and MinGW32
894 * already have signal.h that don't define these.
895 */
896 __PTW32_DECLSPEC int pthread_kill (pthread_t, int);
897
898 /* Non-portable functions
899 */
900
901 /* Compatibility with Linux.
902 */
903 __PTW32_DECLSPEC int pthread_mutexattr_setkind_np (pthread_mutexattr_t *, int);
904 __PTW32_DECLSPEC int pthread_mutexattr_getkind_np (pthread_mutexattr_t *, int *);
905
906 __PTW32_DECLSPEC int pthread_timedjoin_np
907 (pthread_t, void **, const struct timespec *);
908
909 __PTW32_DECLSPEC int pthread_tryjoin_np (pthread_t, void **);
910
911 __PTW32_DECLSPEC int pthread_setaffinity_np
912 (pthread_t, size_t, const cpu_set_t *);
913
914 __PTW32_DECLSPEC int pthread_getaffinity_np (pthread_t, size_t, cpu_set_t *);
915
916 /* Possibly supported by other POSIX threads implementations
917 */
918 __PTW32_DECLSPEC int pthread_delay_np (struct timespec *);
919 __PTW32_DECLSPEC int pthread_num_processors_np (void);
920 __PTW32_DECLSPEC unsigned __int64 pthread_getunique_np (pthread_t);
921
922 /* Useful if an application wants to statically link
923 * the lib rather than load the DLL at run-time.
924 */
925 __PTW32_DECLSPEC int pthread_win32_process_attach_np (void);
926 __PTW32_DECLSPEC int pthread_win32_process_detach_np (void);
927 __PTW32_DECLSPEC int pthread_win32_thread_attach_np (void);
928 __PTW32_DECLSPEC int pthread_win32_thread_detach_np (void);
929
930 /* Returns the first parameter "abstime" modified to represent the current system time.
931 * If "relative" is not NULL it represents an interval to add to "abstime".
932 */
933 __PTW32_DECLSPEC struct timespec *pthread_win32_getabstime_np
934 ( struct timespec *, const struct timespec *);
935
936 /* Features that are auto-detected at load/run time.
937 */
938 __PTW32_DECLSPEC int pthread_win32_test_features_np (int);
939 enum ptw32_features
940 {
941 PTW32_SYSTEM_INTERLOCKED_COMPARE_EXCHANGE = 0x0001, /* System provides it. */
942 PTW32_ALERTABLE_ASYNC_CANCEL = 0x0002 /* Can cancel blocked threads. */
943 };
944
945 /* Register a system time change with the library.
946 * Causes the library to perform various functions
947 * in response to the change. Should be called whenever
948 * the application's top level window receives a
949 * WM_TIMECHANGE message. It can be passed directly to
950 * pthread_create() as a new thread if desired.
951 */
952 __PTW32_DECLSPEC void *pthread_timechange_handler_np (void *);
953
954 #endif /* __PTW32_LEVEL >= __PTW32_LEVEL_MAX - 1 */
955
956 #if __PTW32_LEVEL >= __PTW32_LEVEL_MAX
957
958 /* Returns the Win32 HANDLE for the POSIX thread.
959 */
960 __PTW32_DECLSPEC void *pthread_getw32threadhandle_np (pthread_t);
961 /*
962 * Returns the win32 thread ID for POSIX thread.
963 */
964 __PTW32_DECLSPEC unsigned long pthread_getw32threadid_np (pthread_t);
965
966 /* Sets the POSIX thread name. If _MSC_VER is defined the name should be displayed by
967 * the MSVS debugger.
968 */
969 #if defined(_PTW32_COMPATIBILITY_BSD) || defined(_PTW32_COMPATIBILITY_TRU64)
970 #define PTHREAD_MAX_NAMELEN_NP 16
971 __PTW32_DECLSPEC int pthread_setname_np (pthread_t, const char *, void *);
972 __PTW32_DECLSPEC int pthread_attr_setname_np (pthread_attr_t *, const char *, void *);
973 #else
974 __PTW32_DECLSPEC int pthread_setname_np (pthread_t, const char *);
975 __PTW32_DECLSPEC int pthread_attr_setname_np (pthread_attr_t *, const char *);
976 #endif
977
978 __PTW32_DECLSPEC int pthread_getname_np (pthread_t, char *, int);
979 __PTW32_DECLSPEC int pthread_attr_getname_np (pthread_attr_t *, char *, int);
980
981
982 /* Protected Methods
983 *
984 * This function blocks until the given WIN32 handle
985 * is signalled or pthread_cancel had been called.
986 * This function allows the caller to hook into the
987 * PThreads cancel mechanism. It is implemented using
988 *
989 * WaitForMultipleObjects
990 *
991 * on 'waitHandle' and a manually reset WIN32 Event
992 * used to implement pthread_cancel. The 'timeout'
993 * argument to TimedWait is simply passed to
994 * WaitForMultipleObjects.
995 */
996 __PTW32_DECLSPEC int pthreadCancelableWait (void *);
997 __PTW32_DECLSPEC int pthreadCancelableTimedWait (void *, unsigned long);
998
999 #endif /* __PTW32_LEVEL >= __PTW32_LEVEL_MAX */
1000
1001 /* Declare a thread-safe errno for Open Watcom
1002 * (note: this has not been tested in a long time)
1003 */
1004 #if defined(__WATCOMC__) && !defined(errno)
1005 # if defined(_MT) || defined(_DLL)
1006 __declspec(dllimport) extern int * __cdecl _errno (void);
1007 # define errno (*_errno())
1008 # endif
1009 #endif
1010
1011 /* If pthreads-win32 is compiled as a DLL with MSVC, and
1012 * both it and the application are linked against the static
1013 * C runtime (i.e. with the /MT compiler flag), then the
1014 * application will not see the same C runtime globals as
1015 * the library. These include the errno variable, and the
1016 * termination routine called by terminate(). For details,
1017 * refer to the following links:
1018 *
1019 * http://support.microsoft.com/kb/94248
1020 * (Section 4: Problems Encountered When Using Multiple CRT Libraries)
1021 *
1022 * http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/b4500c0d-1b69-40c7-9ef5-08da1025b5bf
1023 *
1024 * When pthreads-win32 is built with __PTW32_USES_SEPARATE_CRT
1025 * defined, the following features are enabled:
1026 *
1027 * (1) In addition to setting the errno variable when errors
1028 * occur, the library will also call SetLastError() with the
1029 * same value. The application can then use GetLastError()
1030 * to obtain the value of errno. (This pair of routines are
1031 * in kernel32.dll, and so are not affected by the use of
1032 * multiple CRT libraries.)
1033 *
1034 * (2) When C++ or SEH cleanup is used, the library defines
1035 * a function pthread_win32_set_terminate_np(), which can be
1036 * used to set the termination routine that should be called
1037 * when an unhandled exception occurs in a thread function
1038 * (or otherwise inside the library).
1039 *
1040 * Note: "_DLL" implies the /MD compiler flag.
1041 */
1042 #if defined(_MSC_VER) && !defined(_DLL) && !defined(_PTW32_STATIC_LIB)
1043 # define __PTW32_USES_SEPARATE_CRT
1044 #endif
1045
1046 #if defined(__PTW32_USES_SEPARATE_CRT) && (defined(__CLEANUP_CXX) || defined(__CLEANUP_SEH))
1047 typedef void (*ptw32_terminate_handler)();
1048 __PTW32_DECLSPEC ptw32_terminate_handler pthread_win32_set_terminate_np
1049 (ptw32_terminate_handler);
1050 #endif
1051
1052 /* Some compiler environments don't define some things.
1053 */
1054 #if defined(__BORLANDC__)
1055 # define _ftime ftime
1056 # define _timeb timeb
1057 #endif
1058
1059 #if defined(__cplusplus)
1060
1061 /* Internal exceptions
1062 */
1063 class ptw32_exception {};
1064 class ptw32_exception_cancel : public ptw32_exception {};
1065 class ptw32_exception_exit : public ptw32_exception {};
1066
1067 #endif
1068
1069 #if __PTW32_LEVEL >= __PTW32_LEVEL_MAX
1070
1071 /* FIXME: This is only required if the library was built using SEH */
1072 /*
1073 * Get internal SEH tag
1074 */
1075 __PTW32_DECLSPEC unsigned long ptw32_get_exception_services_code (void);
1076
1077 #endif /* __PTW32_LEVEL >= __PTW32_LEVEL_MAX */
1078
1079 #if !defined(__PTW32_BUILD)
1080
1081 #if defined(__CLEANUP_SEH)
1082
1083 /* Redefine the SEH __except keyword to ensure that applications
1084 * propagate our internal exceptions up to the library's internal handlers.
1085 */
1086 #define __except( E ) \
1087 __except( ( GetExceptionCode() == ptw32_get_exception_services_code() ) \
1088 ? EXCEPTION_CONTINUE_SEARCH : ( E ) )
1089
1090 #endif /* __CLEANUP_SEH */
1091
1092 #if defined(__CLEANUP_CXX)
1093
1094 /* Redefine the C++ catch keyword to ensure that applications
1095 * propagate our internal exceptions up to the library's internal handlers.
1096 */
1097 #if defined(_MSC_VER)
1098 /* WARNING: Replace any 'catch( ... )' with 'PtW32CatchAll'
1099 * if you want Pthread-Win32 cancellation and pthread_exit to work.
1100 */
1101 #if !defined(PtW32NoCatchWarn)
1102
1103 #pragma message("Specify \"/DPtW32NoCatchWarn\" compiler flag to skip this message.")
1104 #pragma message("------------------------------------------------------------------")
1105 #pragma message("When compiling applications with MSVC++ and C++ exception handling:")
1106 #pragma message(" Replace any 'catch( ... )' in routines called from POSIX threads")
1107 #pragma message(" with 'PtW32CatchAll' or 'CATCHALL' if you want POSIX thread")
1108 #pragma message(" cancellation and pthread_exit to work. For example:")
1109 #pragma message("")
1110 #pragma message(" #if defined(PtW32CatchAll)")
1111 #pragma message(" PtW32CatchAll")
1112 #pragma message(" #else")
1113 #pragma message(" catch(...)")
1114 #pragma message(" #endif")
1115 #pragma message(" {")
1116 #pragma message(" /* Catchall block processing */")
1117 #pragma message(" }")
1118 #pragma message("------------------------------------------------------------------")
1119
1120 #endif
1121
1122 #define PtW32CatchAll \
1123 catch( ptw32_exception & ) { throw; } \
1124 catch( ... )
1125
1126 #else /* _MSC_VER */
1127
1128 #define catch( E ) \
1129 catch( ptw32_exception & ) { throw; } \
1130 catch( E )
1131
1132 #endif /* _MSC_VER */
1133 #endif /* __CLEANUP_CXX */
1134 #endif /* ! __PTW32_BUILD */
1135
1136 __PTW32_END_C_DECLS
1137
1138 #undef __PTW32_LEVEL
1139 #undef __PTW32_LEVEL_MAX
1140
1141 #endif /* ! RC_INVOKED */
1142 #endif /* !_PTHREAD_H */