1 /* This is an implementation of the threads API of POSIX 1003.1-2001.
3 * --------------------------------------------------------------------------
5 * Pthreads-win32 - POSIX Threads Library for Win32
6 * Copyright(C) 1998 John E. Bossom
7 * Copyright(C) 1999,2005 Pthreads-win32 contributors
9 * Contact Email: rpj@callisto.canberra.edu.au
11 * The current list of contributors is contained
12 * in the file CONTRIBUTORS included with the source
13 * code distribution. The list can also be seen at the
14 * following World Wide Web location:
15 * http://sources.redhat.com/pthreads-win32/contributors.html
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2 of the License, or (at your option) any later version.
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with this library in the file COPYING.LIB;
29 * if not, write to the Free Software Foundation, Inc.,
30 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
33 #if !defined( PTHREAD_H )
34 #ifdef _PTHREAD_H // Test added by JE - 12-12-2009
35 #error "ptw32/pthread.h conflicts with an existing pthread library"
37 // Now make sure we can't accidentally include a conflicting library !!
42 * See the README file for an explanation of the pthreads-win32 version
43 * numbering scheme and how the DLL is named etc.
45 #define PTW32_VERSION 2,8,0,0
46 #define PTW32_VERSION_STRING "2, 8, 0, 0\0"
48 /* There are three implementations of cancel cleanup.
49 * Note that pthread.h is included in both application
50 * compilation units and also internally for the library.
51 * The code here and within the library aims to work
52 * for all reasonable combinations of environments.
54 * The three implementations are:
60 * Please note that exiting a push/pop block via
61 * "return", "exit", "break", or "continue" will
62 * lead to different behaviour amongst applications
63 * depending upon whether the library was built
64 * using SEH, C++, or C. For example, a library built
65 * with SEH will call the cleanup routine, while both
66 * C++ and C built versions will not.
70 * Define defaults for cleanup code.
71 * Note: Unless the build explicitly defines one of the following, then
72 * we default to standard C style cleanup. This style uses setjmp/longjmp
73 * in the cancelation and thread exit implementations and therefore won't
74 * do stack unwinding if linked to applications that have it (e.g.
75 * C++ apps). This is currently consistent with most/all commercial Unix
76 * POSIX threads implementations.
78 #if !defined( __CLEANUP_SEH ) && !defined( __CLEANUP_CXX ) && !defined( __CLEANUP_C )
82 #if defined( __CLEANUP_SEH ) && ( !defined( _MSC_VER ) && !defined(PTW32_RC_MSC))
83 #error ERROR [__FILE__, line __LINE__]: SEH is not supported for this compiler.
87 * Stop here if we are being included by the resource compiler.
93 #if defined(_POSIX_SOURCE)
98 #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309
100 #define PTW32_LEVEL 1
101 /* Include 1b, 1c and 1d */
104 #if defined(INCLUDE_NP)
106 #define PTW32_LEVEL 2
107 /* Include Non-Portable extensions */
110 #define PTW32_LEVEL_MAX 3
112 #if !defined(PTW32_LEVEL)
113 #define PTW32_LEVEL PTW32_LEVEL_MAX
114 /* Include everything */
118 # define HAVE_STRUCT_TIMESPEC 1
119 # define HAVE_SIGNAL_H 1
120 # undef HAVE_CONFIG_H
121 # pragma comment(lib, "pthread")
125 * -------------------------------------------------------------
131 * Provides an implementation of PThreads based upon the
136 * The Single Unix Specification version 3
138 * (these two are equivalent)
140 * in order to enhance code portability between Windows,
141 * various commercial Unix implementations, and Linux.
143 * See the ANNOUNCE file for a full list of conforming
144 * routines and defined constants, and a list of missing
145 * routines and constants not defined in this implementation.
148 * There have been many contributors to this library.
149 * The initial implementation was contributed by
150 * John Bossom, and several others have provided major
151 * sections or revisions of parts of the implementation.
152 * Often significant effort has been contributed to
153 * find and fix important bugs and other problems to
154 * improve the reliability of the library, which sometimes
155 * is not reflected in the amount of code which changed as
157 * As much as possible, the contributors are acknowledged
158 * in the ChangeLog file in the source code distribution
159 * where their changes are noted in detail.
161 * Contributors are listed in the CONTRIBUTORS file.
163 * As usual, all bouquets go to the contributors, and all
164 * brickbats go to the project maintainer.
167 * The code base for this project is coordinated and
168 * eventually pre-tested, packaged, and made available by
170 * Ross Johnson <rpj@callisto.canberra.edu.au>
173 * Ultimately, the library is tested in the real world by
174 * a host of competent and demanding scientists and
175 * engineers who report bugs and/or provide solutions
176 * which are then fixed or incorporated into subsequent
177 * versions of the library. Each time a bug is fixed, a
178 * test case is written to prove the fix and ensure
179 * that later changes to the code don't reintroduce the
180 * same error. The number of test cases is slowly growing
181 * and therefore so is the code reliability.
184 * See the file ANNOUNCE for the list of implemented
185 * and not-implemented routines and defined options.
186 * Of course, these are all defined is this file as well.
189 * The source code and other information about this library
192 * http://sources.redhat.com/pthreads-win32/
194 * -------------------------------------------------------------
197 /* Try to avoid including windows.h */
198 #if defined(__MINGW32__) && defined(__cplusplus)
199 #define PTW32_INCLUDE_WINDOWS_H
202 #ifdef PTW32_INCLUDE_WINDOWS_H
206 #if defined(_MSC_VER) && _MSC_VER < 1300 || defined(__DMC__)
208 * VC++6.0 or early compiler's header has no DWORD_PTR type.
210 typedef unsigned long DWORD_PTR;
220 #endif /* HAVE_CONFIG_H */
224 #else /* NEED_FTIME */
225 /* use native WIN32 time API */
226 #endif /* NEED_FTIME */
230 #endif /* HAVE_SIGNAL_H */
236 * Boolean values to make us independent of system includes.
240 PTW32_TRUE = (! PTW32_FALSE)
244 * This is a duplicate of what is in the autoconf config.h,
245 * which is only used when building the pthread-win32 libraries.
248 #ifndef PTW32_CONFIG_H
253 # if defined(_UWIN) || defined(__MINGW32__)
262 #if PTW32_LEVEL >= PTW32_LEVEL_MAX
264 #include "need_errno.h"
268 #endif /* PTW32_LEVEL >= PTW32_LEVEL_MAX */
271 * Several systems don't define some error numbers.
274 # define ENOTSUP 48 /* This is the value in Solaris. */
278 # define ETIMEDOUT 10060 /* This is the value in winsock.h. */
282 # define ENOSYS 140 /* Semi-arbitrary value */
287 # define EDEADLK EDEADLOCK
289 # define EDEADLK 36 /* This is the value in MSVC. */
293 #include <ardourext/sched.h>
296 * To avoid including windows.h we define only those things that we
297 * actually need from it.
299 #ifndef PTW32_INCLUDE_WINDOWS_H
301 # define PTW32__HANDLE_DEF
302 # define HANDLE void *
305 # define PTW32__DWORD_DEF
306 # define DWORD unsigned long
310 #ifndef HAVE_STRUCT_TIMESPEC
311 #define HAVE_STRUCT_TIMESPEC 1
316 #endif /* HAVE_STRUCT_TIMESPEC */
320 #endif /* SIG_BLOCK */
323 #define SIG_UNBLOCK 1
324 #endif /* SIG_UNBLOCK */
327 #define SIG_SETMASK 2
328 #endif /* SIG_SETMASK */
333 #endif /* __cplusplus */
336 * -------------------------------------------------------------
338 * POSIX 1003.1-2001 Options
339 * =========================
341 * Options are normally set in <unistd.h>, which is not provided
342 * with pthreads-win32.
344 * For conformance with the Single Unix Specification (version 3), all of the
345 * options below are defined, and have a value of either -1 (not supported)
346 * or 200112L (supported).
348 * These options can neither be left undefined nor have a value of 0, because
349 * either indicates that sysconf(), which is not implemented, may be used at
350 * runtime to check the status of the option.
352 * _POSIX_THREADS (== 200112L)
353 * If == 200112L, you can use threads
355 * _POSIX_THREAD_ATTR_STACKSIZE (== 200112L)
356 * If == 200112L, you can control the size of a thread's
358 * pthread_attr_getstacksize
359 * pthread_attr_setstacksize
361 * _POSIX_THREAD_ATTR_STACKADDR (== -1)
362 * If == 200112L, you can allocate and control a thread's
363 * stack. If not supported, the following functions
364 * will return ENOSYS, indicating they are not
366 * pthread_attr_getstackaddr
367 * pthread_attr_setstackaddr
369 * _POSIX_THREAD_PRIORITY_SCHEDULING (== -1)
370 * If == 200112L, you can use realtime scheduling.
371 * This option indicates that the behaviour of some
372 * implemented functions conforms to the additional TPS
373 * requirements in the standard. E.g. rwlocks favour
374 * writers over readers when threads have equal priority.
376 * _POSIX_THREAD_PRIO_INHERIT (== -1)
377 * If == 200112L, you can create priority inheritance
379 * pthread_mutexattr_getprotocol +
380 * pthread_mutexattr_setprotocol +
382 * _POSIX_THREAD_PRIO_PROTECT (== -1)
383 * If == 200112L, you can create priority ceiling mutexes
384 * Indicates the availability of:
385 * pthread_mutex_getprioceiling
386 * pthread_mutex_setprioceiling
387 * pthread_mutexattr_getprioceiling
388 * pthread_mutexattr_getprotocol +
389 * pthread_mutexattr_setprioceiling
390 * pthread_mutexattr_setprotocol +
392 * _POSIX_THREAD_PROCESS_SHARED (== -1)
393 * If set, you can create mutexes and condition
394 * variables that can be shared with another
395 * process.If set, indicates the availability
397 * pthread_mutexattr_getpshared
398 * pthread_mutexattr_setpshared
399 * pthread_condattr_getpshared
400 * pthread_condattr_setpshared
402 * _POSIX_THREAD_SAFE_FUNCTIONS (== 200112L)
403 * If == 200112L you can use the special *_r library
404 * functions that provide thread-safe behaviour
406 * _POSIX_READER_WRITER_LOCKS (== 200112L)
407 * If == 200112L, you can use read/write locks
409 * _POSIX_SPIN_LOCKS (== 200112L)
410 * If == 200112L, you can use spin locks
412 * _POSIX_BARRIERS (== 200112L)
413 * If == 200112L, you can use barriers
415 * + These functions provide both 'inherit' and/or
416 * 'protect' protocol, based upon these macro
419 * -------------------------------------------------------------
425 #undef _POSIX_THREADS
426 #define _POSIX_THREADS 200112L
428 #undef _POSIX_READER_WRITER_LOCKS
429 #define _POSIX_READER_WRITER_LOCKS 200112L
431 #undef _POSIX_SPIN_LOCKS
432 #define _POSIX_SPIN_LOCKS 200112L
434 #undef _POSIX_BARRIERS
435 #define _POSIX_BARRIERS 200112L
437 #undef _POSIX_THREAD_SAFE_FUNCTIONS
438 #define _POSIX_THREAD_SAFE_FUNCTIONS 200112L
440 #undef _POSIX_THREAD_ATTR_STACKSIZE
441 #define _POSIX_THREAD_ATTR_STACKSIZE 200112L
444 * The following options are not supported
446 #undef _POSIX_THREAD_ATTR_STACKADDR
447 #define _POSIX_THREAD_ATTR_STACKADDR -1
449 #undef _POSIX_THREAD_PRIO_INHERIT
450 #define _POSIX_THREAD_PRIO_INHERIT -1
452 #undef _POSIX_THREAD_PRIO_PROTECT
453 #define _POSIX_THREAD_PRIO_PROTECT -1
455 /* TPS is not fully supported. */
456 #undef _POSIX_THREAD_PRIORITY_SCHEDULING
457 #define _POSIX_THREAD_PRIORITY_SCHEDULING -1
459 #undef _POSIX_THREAD_PROCESS_SHARED
460 #define _POSIX_THREAD_PROCESS_SHARED -1
464 * POSIX 1003.1-2001 Limits
465 * ===========================
467 * These limits are normally set in <limits.h>, which is not provided with
470 * PTHREAD_DESTRUCTOR_ITERATIONS
471 * Maximum number of attempts to destroy
472 * a thread's thread-specific data on
473 * termination (must be at least 4)
476 * Maximum number of thread-specific data keys
477 * available per process (must be at least 128)
480 * Minimum supported stack size for a thread
482 * PTHREAD_THREADS_MAX
483 * Maximum number of threads supported per
484 * process (must be at least 64).
487 * The maximum number of semaphores a process can have.
488 * (must be at least 256)
491 * The maximum value a semaphore can have.
492 * (must be at least 32767)
495 #undef _POSIX_THREAD_DESTRUCTOR_ITERATIONS
496 #define _POSIX_THREAD_DESTRUCTOR_ITERATIONS 4
498 #undef PTHREAD_DESTRUCTOR_ITERATIONS
499 #define PTHREAD_DESTRUCTOR_ITERATIONS _POSIX_THREAD_DESTRUCTOR_ITERATIONS
501 #undef _POSIX_THREAD_KEYS_MAX
502 #define _POSIX_THREAD_KEYS_MAX 128
504 #undef PTHREAD_KEYS_MAX
505 #define PTHREAD_KEYS_MAX _POSIX_THREAD_KEYS_MAX
507 #undef PTHREAD_STACK_MIN
508 #define PTHREAD_STACK_MIN 0
510 #undef _POSIX_THREAD_THREADS_MAX
511 #define _POSIX_THREAD_THREADS_MAX 64
513 /* Arbitrary value */
514 #undef PTHREAD_THREADS_MAX
515 #define PTHREAD_THREADS_MAX 2019
517 #undef _POSIX_SEM_NSEMS_MAX
518 #define _POSIX_SEM_NSEMS_MAX 256
520 /* Arbitrary value */
522 #define SEM_NSEMS_MAX 1024
524 #undef _POSIX_SEM_VALUE_MAX
525 #define _POSIX_SEM_VALUE_MAX 32767
528 #define SEM_VALUE_MAX INT_MAX
531 #if __GNUC__ && ! defined (__declspec)
532 # error Please upgrade your GNU compiler to one that supports __declspec.
536 * When building the DLL code, you should define PTW32_BUILD so that
537 * the variables/functions are exported correctly. When using the DLL,
538 * do NOT define PTW32_BUILD, and then the variables/functions will
539 * be imported correctly.
541 #ifndef PTW32_STATIC_LIB
543 # define PTW32_DLLPORT __declspec (dllexport)
545 # define PTW32_DLLPORT __declspec (dllimport)
548 # define PTW32_DLLPORT
552 * The Open Watcom C/C++ compiler uses a non-standard calling convention
553 * that passes function args in registers unless __cdecl is explicitly specified
554 * in exposed function prototypes.
556 * We force all calls to cdecl even though this could slow Watcom code down
557 * slightly. If you know that the Watcom compiler will be used to build both
558 * the DLL and application, then you can probably define this as a null string.
559 * Remember that pthread.h (this file) is used for both the DLL and application builds.
561 #define PTW32_CDECL __cdecl
563 #if defined(_UWIN) && PTW32_LEVEL >= PTW32_LEVEL_MAX
564 # include <sys/types.h>
567 * Generic handle type - intended to extend uniqueness beyond
568 * that available with a simple pointer. It should scale for either
572 void * p; /* Pointer to actual object */
573 unsigned int x; /* Extra information - reuse count etc */
576 typedef ptw32_handle_t pthread_t;
577 typedef struct pthread_attr_t_ * pthread_attr_t;
578 typedef struct pthread_once_t_ pthread_once_t;
579 typedef struct pthread_key_t_ * pthread_key_t;
580 typedef struct pthread_mutex_t_ * pthread_mutex_t;
581 typedef struct pthread_mutexattr_t_ * pthread_mutexattr_t;
582 typedef struct pthread_cond_t_ * pthread_cond_t;
583 typedef struct pthread_condattr_t_ * pthread_condattr_t;
585 typedef struct pthread_rwlock_t_ * pthread_rwlock_t;
586 typedef struct pthread_rwlockattr_t_ * pthread_rwlockattr_t;
587 typedef struct pthread_spinlock_t_ * pthread_spinlock_t;
588 typedef struct pthread_barrier_t_ * pthread_barrier_t;
589 typedef struct pthread_barrierattr_t_ * pthread_barrierattr_t;
592 * ====================
593 * ====================
595 * ====================
596 * ====================
601 * pthread_attr_{get,set}detachstate
603 PTHREAD_CREATE_JOINABLE = 0, /* Default */
604 PTHREAD_CREATE_DETACHED = 1,
607 * pthread_attr_{get,set}inheritsched
609 PTHREAD_INHERIT_SCHED = 0,
610 PTHREAD_EXPLICIT_SCHED = 1, /* Default */
613 * pthread_{get,set}scope
615 PTHREAD_SCOPE_PROCESS = 0,
616 PTHREAD_SCOPE_SYSTEM = 1, /* Default */
619 * pthread_setcancelstate paramters
621 PTHREAD_CANCEL_ENABLE = 0, /* Default */
622 PTHREAD_CANCEL_DISABLE = 1,
625 * pthread_setcanceltype parameters
627 PTHREAD_CANCEL_ASYNCHRONOUS = 0,
628 PTHREAD_CANCEL_DEFERRED = 1, /* Default */
631 * pthread_mutexattr_{get,set}pshared
632 * pthread_condattr_{get,set}pshared
634 PTHREAD_PROCESS_PRIVATE = 0,
635 PTHREAD_PROCESS_SHARED = 1,
638 * pthread_barrier_wait
640 PTHREAD_BARRIER_SERIAL_THREAD = -1
644 * ====================
645 * ====================
647 * ====================
648 * ====================
650 #define PTHREAD_CANCELED ((void *) -1)
654 * ====================
655 * ====================
657 * ====================
658 * ====================
660 #define PTHREAD_ONCE_INIT { PTW32_FALSE, 0, 0, 0}
662 struct pthread_once_t_
664 int done; /* indicates if user function has been executed */
672 * ====================
673 * ====================
674 * Object initialisers
675 * ====================
676 * ====================
678 #define PTHREAD_MUTEX_INITIALIZER ((pthread_mutex_t) -1)
679 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER ((pthread_mutex_t) -2)
680 #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER ((pthread_mutex_t) -3)
683 * Compatibility with LinuxThreads
685 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP PTHREAD_RECURSIVE_MUTEX_INITIALIZER
686 #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP PTHREAD_ERRORCHECK_MUTEX_INITIALIZER
688 #define PTHREAD_COND_INITIALIZER ((pthread_cond_t) -1)
690 #define PTHREAD_RWLOCK_INITIALIZER ((pthread_rwlock_t) -1)
692 #define PTHREAD_SPINLOCK_INITIALIZER ((pthread_spinlock_t) -1)
700 /* Compatibility with LinuxThreads */
701 PTHREAD_MUTEX_FAST_NP,
702 PTHREAD_MUTEX_RECURSIVE_NP,
703 PTHREAD_MUTEX_ERRORCHECK_NP,
704 PTHREAD_MUTEX_TIMED_NP = PTHREAD_MUTEX_FAST_NP,
705 PTHREAD_MUTEX_ADAPTIVE_NP = PTHREAD_MUTEX_FAST_NP,
706 /* For compatibility with POSIX */
707 PTHREAD_MUTEX_NORMAL = PTHREAD_MUTEX_FAST_NP,
708 PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP,
709 PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP,
710 PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
714 typedef struct ptw32_cleanup_t ptw32_cleanup_t;
716 #if defined(_MSC_VER)
717 /* Disable MSVC 'anachronism used' warning */
718 #pragma warning( disable : 4229 )
721 typedef void (* PTW32_CDECL ptw32_cleanup_callback_t)(void *);
723 #if defined(_MSC_VER)
724 #pragma warning( default : 4229 )
727 struct ptw32_cleanup_t
729 ptw32_cleanup_callback_t routine;
731 struct ptw32_cleanup_t *prev;
736 * WIN32 SEH version of cancel cleanup.
739 #define pthread_cleanup_push( _rout, _arg ) \
741 ptw32_cleanup_t _cleanup; \
743 _cleanup.routine = (ptw32_cleanup_callback_t)(_rout); \
744 _cleanup.arg = (_arg); \
748 #define pthread_cleanup_pop( _execute ) \
752 if( _execute || AbnormalTermination()) \
754 (*(_cleanup.routine))( _cleanup.arg ); \
759 #else /* __CLEANUP_SEH */
764 * C implementation of PThreads cancel cleanup
767 #define pthread_cleanup_push( _rout, _arg ) \
769 ptw32_cleanup_t _cleanup; \
771 ptw32_push_cleanup( &_cleanup, (ptw32_cleanup_callback_t) (_rout), (_arg) ); \
773 #define pthread_cleanup_pop( _execute ) \
774 (void) ptw32_pop_cleanup( _execute ); \
777 #else /* __CLEANUP_C */
782 * C++ version of cancel cleanup.
786 class PThreadCleanup {
791 * This class is a C++ helper class that is
792 * used to implement pthread_cleanup_push/
793 * pthread_cleanup_pop.
794 * The destructor of this class automatically
795 * pops the pushed cleanup routine regardless
796 * of how the code exits the scope
797 * (i.e. such as by an exception)
799 ptw32_cleanup_callback_t cleanUpRout;
809 * No cleanup performed
815 ptw32_cleanup_callback_t routine,
817 cleanUpRout( routine ),
821 * Registers a cleanup routine for 'arg'
828 if ( executeIt && ((void *) cleanUpRout != (void *) 0) )
830 (void) (*cleanUpRout)( obj );
834 void execute( int exec )
841 * C++ implementation of PThreads cancel cleanup;
842 * This implementation takes advantage of a helper
843 * class who's destructor automatically calls the
844 * cleanup routine if we exit our scope weirdly
846 #define pthread_cleanup_push( _rout, _arg ) \
848 PThreadCleanup cleanup((ptw32_cleanup_callback_t)(_rout), \
851 #define pthread_cleanup_pop( _execute ) \
852 cleanup.execute( _execute ); \
857 #error ERROR [__FILE__, line __LINE__]: Cleanup type undefined.
859 #endif /* __CLEANUP_CXX */
861 #endif /* __CLEANUP_C */
863 #endif /* __CLEANUP_SEH */
874 * PThread Attribute Functions
876 PTW32_DLLPORT int PTW32_CDECL pthread_attr_init (pthread_attr_t * attr);
878 PTW32_DLLPORT int PTW32_CDECL pthread_attr_destroy (pthread_attr_t * attr);
880 PTW32_DLLPORT int PTW32_CDECL pthread_attr_getdetachstate (const pthread_attr_t * attr,
883 PTW32_DLLPORT int PTW32_CDECL pthread_attr_getstackaddr (const pthread_attr_t * attr,
886 PTW32_DLLPORT int PTW32_CDECL pthread_attr_getstacksize (const pthread_attr_t * attr,
889 PTW32_DLLPORT int PTW32_CDECL pthread_attr_setdetachstate (pthread_attr_t * attr,
892 PTW32_DLLPORT int PTW32_CDECL pthread_attr_setstackaddr (pthread_attr_t * attr,
895 PTW32_DLLPORT int PTW32_CDECL pthread_attr_setstacksize (pthread_attr_t * attr,
898 PTW32_DLLPORT int PTW32_CDECL pthread_attr_getschedparam (const pthread_attr_t *attr,
899 struct sched_param *param);
901 PTW32_DLLPORT int PTW32_CDECL pthread_attr_setschedparam (pthread_attr_t *attr,
902 const struct sched_param *param);
904 PTW32_DLLPORT int PTW32_CDECL pthread_attr_setschedpolicy (pthread_attr_t *,
907 PTW32_DLLPORT int PTW32_CDECL pthread_attr_getschedpolicy (pthread_attr_t *,
910 PTW32_DLLPORT int PTW32_CDECL pthread_attr_setinheritsched(pthread_attr_t * attr,
913 PTW32_DLLPORT int PTW32_CDECL pthread_attr_getinheritsched(pthread_attr_t * attr,
916 PTW32_DLLPORT int PTW32_CDECL pthread_attr_setscope (pthread_attr_t *,
919 PTW32_DLLPORT int PTW32_CDECL pthread_attr_getscope (const pthread_attr_t *,
925 PTW32_DLLPORT int PTW32_CDECL pthread_create (pthread_t * tid,
926 const pthread_attr_t * attr,
927 void *(*start) (void *),
930 PTW32_DLLPORT int PTW32_CDECL pthread_detach (pthread_t tid);
932 PTW32_DLLPORT int PTW32_CDECL pthread_equal (pthread_t t1,
935 PTW32_DLLPORT void PTW32_CDECL pthread_exit (void *value_ptr);
937 PTW32_DLLPORT int PTW32_CDECL pthread_join (pthread_t thread,
940 PTW32_DLLPORT pthread_t PTW32_CDECL pthread_self (void);
942 PTW32_DLLPORT int PTW32_CDECL pthread_cancel (pthread_t thread);
944 PTW32_DLLPORT int PTW32_CDECL pthread_setcancelstate (int state,
947 PTW32_DLLPORT int PTW32_CDECL pthread_setcanceltype (int type,
950 PTW32_DLLPORT void PTW32_CDECL pthread_testcancel (void);
952 PTW32_DLLPORT int PTW32_CDECL pthread_once (pthread_once_t * once_control,
953 void (*init_routine) (void));
955 #if PTW32_LEVEL >= PTW32_LEVEL_MAX
956 PTW32_DLLPORT ptw32_cleanup_t * PTW32_CDECL ptw32_pop_cleanup (int execute);
958 PTW32_DLLPORT void PTW32_CDECL ptw32_push_cleanup (ptw32_cleanup_t * cleanup,
959 void (*routine) (void *),
961 #endif /* PTW32_LEVEL >= PTW32_LEVEL_MAX */
964 * Thread Specific Data Functions
966 PTW32_DLLPORT int PTW32_CDECL pthread_key_create (pthread_key_t * key,
967 void (*destructor) (void *));
969 PTW32_DLLPORT int PTW32_CDECL pthread_key_delete (pthread_key_t key);
971 PTW32_DLLPORT int PTW32_CDECL pthread_setspecific (pthread_key_t key,
974 PTW32_DLLPORT void * PTW32_CDECL pthread_getspecific (pthread_key_t key);
978 * Mutex Attribute Functions
980 PTW32_DLLPORT int PTW32_CDECL pthread_mutexattr_init (pthread_mutexattr_t * attr);
982 PTW32_DLLPORT int PTW32_CDECL pthread_mutexattr_destroy (pthread_mutexattr_t * attr);
984 PTW32_DLLPORT int PTW32_CDECL pthread_mutexattr_getpshared (const pthread_mutexattr_t
988 PTW32_DLLPORT int PTW32_CDECL pthread_mutexattr_setpshared (pthread_mutexattr_t * attr,
991 PTW32_DLLPORT int PTW32_CDECL pthread_mutexattr_settype (pthread_mutexattr_t * attr, int kind);
992 PTW32_DLLPORT int PTW32_CDECL pthread_mutexattr_gettype (pthread_mutexattr_t * attr, int *kind);
995 * Barrier Attribute Functions
997 PTW32_DLLPORT int PTW32_CDECL pthread_barrierattr_init (pthread_barrierattr_t * attr);
999 PTW32_DLLPORT int PTW32_CDECL pthread_barrierattr_destroy (pthread_barrierattr_t * attr);
1001 PTW32_DLLPORT int PTW32_CDECL pthread_barrierattr_getpshared (const pthread_barrierattr_t
1005 PTW32_DLLPORT int PTW32_CDECL pthread_barrierattr_setpshared (pthread_barrierattr_t * attr,
1011 PTW32_DLLPORT int PTW32_CDECL pthread_mutex_init (pthread_mutex_t * mutex,
1012 const pthread_mutexattr_t * attr);
1014 PTW32_DLLPORT int PTW32_CDECL pthread_mutex_destroy (pthread_mutex_t * mutex);
1016 PTW32_DLLPORT int PTW32_CDECL pthread_mutex_lock (pthread_mutex_t * mutex);
1018 PTW32_DLLPORT int PTW32_CDECL pthread_mutex_timedlock(pthread_mutex_t *mutex,
1019 const struct timespec *abstime);
1021 PTW32_DLLPORT int PTW32_CDECL pthread_mutex_trylock (pthread_mutex_t * mutex);
1023 PTW32_DLLPORT int PTW32_CDECL pthread_mutex_unlock (pthread_mutex_t * mutex);
1026 * Spinlock Functions
1028 PTW32_DLLPORT int PTW32_CDECL pthread_spin_init (pthread_spinlock_t * lock, int pshared);
1030 PTW32_DLLPORT int PTW32_CDECL pthread_spin_destroy (pthread_spinlock_t * lock);
1032 PTW32_DLLPORT int PTW32_CDECL pthread_spin_lock (pthread_spinlock_t * lock);
1034 PTW32_DLLPORT int PTW32_CDECL pthread_spin_trylock (pthread_spinlock_t * lock);
1036 PTW32_DLLPORT int PTW32_CDECL pthread_spin_unlock (pthread_spinlock_t * lock);
1041 PTW32_DLLPORT int PTW32_CDECL pthread_barrier_init (pthread_barrier_t * barrier,
1042 const pthread_barrierattr_t * attr,
1043 unsigned int count);
1045 PTW32_DLLPORT int PTW32_CDECL pthread_barrier_destroy (pthread_barrier_t * barrier);
1047 PTW32_DLLPORT int PTW32_CDECL pthread_barrier_wait (pthread_barrier_t * barrier);
1050 * Condition Variable Attribute Functions
1052 PTW32_DLLPORT int PTW32_CDECL pthread_condattr_init (pthread_condattr_t * attr);
1054 PTW32_DLLPORT int PTW32_CDECL pthread_condattr_destroy (pthread_condattr_t * attr);
1056 PTW32_DLLPORT int PTW32_CDECL pthread_condattr_getpshared (const pthread_condattr_t * attr,
1059 PTW32_DLLPORT int PTW32_CDECL pthread_condattr_setpshared (pthread_condattr_t * attr,
1063 * Condition Variable Functions
1065 PTW32_DLLPORT int PTW32_CDECL pthread_cond_init (pthread_cond_t * cond,
1066 const pthread_condattr_t * attr);
1068 PTW32_DLLPORT int PTW32_CDECL pthread_cond_destroy (pthread_cond_t * cond);
1070 PTW32_DLLPORT int PTW32_CDECL pthread_cond_wait (pthread_cond_t * cond,
1071 pthread_mutex_t * mutex);
1073 PTW32_DLLPORT int PTW32_CDECL pthread_cond_timedwait (pthread_cond_t * cond,
1074 pthread_mutex_t * mutex,
1075 const struct timespec *abstime);
1077 PTW32_DLLPORT int PTW32_CDECL pthread_cond_signal (pthread_cond_t * cond);
1079 PTW32_DLLPORT int PTW32_CDECL pthread_cond_broadcast (pthread_cond_t * cond);
1084 PTW32_DLLPORT int PTW32_CDECL pthread_setschedparam (pthread_t thread,
1086 const struct sched_param *param);
1088 PTW32_DLLPORT int PTW32_CDECL pthread_getschedparam (pthread_t thread,
1090 struct sched_param *param);
1092 PTW32_DLLPORT int PTW32_CDECL pthread_setconcurrency (int);
1094 PTW32_DLLPORT int PTW32_CDECL pthread_getconcurrency (void);
1097 * Read-Write Lock Functions
1099 PTW32_DLLPORT int PTW32_CDECL pthread_rwlock_init(pthread_rwlock_t *lock,
1100 const pthread_rwlockattr_t *attr);
1102 PTW32_DLLPORT int PTW32_CDECL pthread_rwlock_destroy(pthread_rwlock_t *lock);
1104 PTW32_DLLPORT int PTW32_CDECL pthread_rwlock_tryrdlock(pthread_rwlock_t *);
1106 PTW32_DLLPORT int PTW32_CDECL pthread_rwlock_trywrlock(pthread_rwlock_t *);
1108 PTW32_DLLPORT int PTW32_CDECL pthread_rwlock_rdlock(pthread_rwlock_t *lock);
1110 PTW32_DLLPORT int PTW32_CDECL pthread_rwlock_timedrdlock(pthread_rwlock_t *lock,
1111 const struct timespec *abstime);
1113 PTW32_DLLPORT int PTW32_CDECL pthread_rwlock_wrlock(pthread_rwlock_t *lock);
1115 PTW32_DLLPORT int PTW32_CDECL pthread_rwlock_timedwrlock(pthread_rwlock_t *lock,
1116 const struct timespec *abstime);
1118 PTW32_DLLPORT int PTW32_CDECL pthread_rwlock_unlock(pthread_rwlock_t *lock);
1120 PTW32_DLLPORT int PTW32_CDECL pthread_rwlockattr_init (pthread_rwlockattr_t * attr);
1122 PTW32_DLLPORT int PTW32_CDECL pthread_rwlockattr_destroy (pthread_rwlockattr_t * attr);
1124 PTW32_DLLPORT int PTW32_CDECL pthread_rwlockattr_getpshared (const pthread_rwlockattr_t * attr,
1127 PTW32_DLLPORT int PTW32_CDECL pthread_rwlockattr_setpshared (pthread_rwlockattr_t * attr,
1130 #if PTW32_LEVEL >= PTW32_LEVEL_MAX - 1
1133 * Signal Functions. Should be defined in <signal.h> but MSVC and MinGW32
1134 * already have signal.h that don't define these.
1136 PTW32_DLLPORT int PTW32_CDECL pthread_kill(pthread_t thread, int sig);
1139 * Non-portable functions
1143 * Compatibility with Linux.
1145 PTW32_DLLPORT int PTW32_CDECL pthread_mutexattr_setkind_np(pthread_mutexattr_t * attr,
1147 PTW32_DLLPORT int PTW32_CDECL pthread_mutexattr_getkind_np(pthread_mutexattr_t * attr,
1151 * Possibly supported by other POSIX threads implementations
1153 PTW32_DLLPORT int PTW32_CDECL pthread_delay_np (struct timespec * interval);
1154 PTW32_DLLPORT int PTW32_CDECL pthread_num_processors_np(void);
1157 * Useful if an application wants to statically link
1158 * the lib rather than load the DLL at run-time.
1160 PTW32_DLLPORT int PTW32_CDECL pthread_win32_process_attach_np(void);
1161 PTW32_DLLPORT int PTW32_CDECL pthread_win32_process_detach_np(void);
1162 PTW32_DLLPORT int PTW32_CDECL pthread_win32_thread_attach_np(void);
1163 PTW32_DLLPORT int PTW32_CDECL pthread_win32_thread_detach_np(void);
1166 * Features that are auto-detected at load/run time.
1168 PTW32_DLLPORT int PTW32_CDECL pthread_win32_test_features_np(int);
1169 enum ptw32_features {
1170 PTW32_SYSTEM_INTERLOCKED_COMPARE_EXCHANGE = 0x0001, /* System provides it. */
1171 PTW32_ALERTABLE_ASYNC_CANCEL = 0x0002 /* Can cancel blocked threads. */
1175 * Register a system time change with the library.
1176 * Causes the library to perform various functions
1177 * in response to the change. Should be called whenever
1178 * the application's top level window receives a
1179 * WM_TIMECHANGE message. It can be passed directly to
1180 * pthread_create() as a new thread if desired.
1182 PTW32_DLLPORT void * PTW32_CDECL pthread_timechange_handler_np(void *);
1184 #endif /*PTW32_LEVEL >= PTW32_LEVEL_MAX - 1 */
1186 #if PTW32_LEVEL >= PTW32_LEVEL_MAX
1189 * Returns the Win32 HANDLE for the POSIX thread.
1191 PTW32_DLLPORT HANDLE PTW32_CDECL pthread_getw32threadhandle_np(pthread_t thread);
1197 * This function blocks until the given WIN32 handle
1198 * is signaled or pthread_cancel had been called.
1199 * This function allows the caller to hook into the
1200 * PThreads cancel mechanism. It is implemented using
1202 * WaitForMultipleObjects
1204 * on 'waitHandle' and a manually reset WIN32 Event
1205 * used to implement pthread_cancel. The 'timeout'
1206 * argument to TimedWait is simply passed to
1207 * WaitForMultipleObjects.
1209 PTW32_DLLPORT int PTW32_CDECL pthreadCancelableWait (HANDLE waitHandle);
1210 PTW32_DLLPORT int PTW32_CDECL pthreadCancelableTimedWait (HANDLE waitHandle,
1213 #endif /* PTW32_LEVEL >= PTW32_LEVEL_MAX */
1216 * Thread-Safe C Runtime Library Mappings.
1219 # if defined(NEED_ERRNO)
1220 PTW32_DLLPORT int * PTW32_CDECL _errno( void );
1223 # if (defined(_MT) || defined(_DLL))
1224 __declspec(dllimport) extern int * __cdecl _errno(void);
1225 # define errno (*_errno())
1232 * WIN32 C runtime library had been made thread-safe
1233 * without affecting the user interface. Provide
1234 * mappings from the UNIX thread-safe versions to
1235 * the standard C runtime library calls.
1236 * Only provide function mappings for functions that
1237 * actually exist on WIN32.
1240 #if !defined(__MINGW32__)
1241 #define strtok_r( _s, _sep, _lasts ) \
1242 ( *(_lasts) = strtok( (_s), (_sep) ) )
1243 #endif /* !__MINGW32__ */
1245 #define asctime_r( _tm, _buf ) \
1246 ( strcpy( (_buf), asctime( (_tm) ) ), \
1249 #define ctime_r( _clock, _buf ) \
1250 ( strcpy( (_buf), ctime( (_clock) ) ), \
1253 #define gmtime_r( _clock, _result ) \
1254 ( *(_result) = *gmtime( (_clock) ), \
1257 #define localtime_r( _clock, _result ) \
1258 ( *(_result) = *localtime( (_clock) ), \
1261 #define rand_r( _seed ) \
1262 ( _seed == _seed? rand() : rand() )
1266 * Some compiler environments don't define some things.
1268 #if defined(__BORLANDC__)
1269 # define _ftime ftime
1270 # define _timeb timeb
1276 * Internal exceptions
1278 class ptw32_exception {};
1279 class ptw32_exception_cancel : public ptw32_exception {};
1280 class ptw32_exception_exit : public ptw32_exception {};
1284 #if PTW32_LEVEL >= PTW32_LEVEL_MAX
1286 /* FIXME: This is only required if the library was built using SEH */
1288 * Get internal SEH tag
1290 PTW32_DLLPORT DWORD PTW32_CDECL ptw32_get_exception_services_code(void);
1292 #endif /* PTW32_LEVEL >= PTW32_LEVEL_MAX */
1296 #ifdef __CLEANUP_SEH
1299 * Redefine the SEH __except keyword to ensure that applications
1300 * propagate our internal exceptions up to the library's internal handlers.
1302 #define __except( E ) \
1303 __except( ( GetExceptionCode() == ptw32_get_exception_services_code() ) \
1304 ? EXCEPTION_CONTINUE_SEARCH : ( E ) )
1306 #endif /* __CLEANUP_SEH */
1308 #ifdef __CLEANUP_CXX
1311 * Redefine the C++ catch keyword to ensure that applications
1312 * propagate our internal exceptions up to the library's internal handlers.
1316 * WARNING: Replace any 'catch( ... )' with 'PtW32CatchAll'
1317 * if you want Pthread-Win32 cancelation and pthread_exit to work.
1320 #ifndef PtW32NoCatchWarn
1322 #pragma message("Specify \"/DPtW32NoCatchWarn\" compiler flag to skip this message.")
1323 #pragma message("------------------------------------------------------------------")
1324 #pragma message("When compiling applications with MSVC++ and C++ exception handling:")
1325 #pragma message(" Replace any 'catch( ... )' in routines called from POSIX threads")
1326 #pragma message(" with 'PtW32CatchAll' or 'CATCHALL' if you want POSIX thread")
1327 #pragma message(" cancelation and pthread_exit to work. For example:")
1329 #pragma message(" #ifdef PtW32CatchAll")
1330 #pragma message(" PtW32CatchAll")
1331 #pragma message(" #else")
1332 #pragma message(" catch(...)")
1333 #pragma message(" #endif")
1334 #pragma message(" {")
1335 #pragma message(" /* Catchall block processing */")
1336 #pragma message(" }")
1337 #pragma message("------------------------------------------------------------------")
1341 #define PtW32CatchAll \
1342 catch( ptw32_exception & ) { throw; } \
1345 #else /* _MSC_VER */
1347 #define catch( E ) \
1348 catch( ptw32_exception & ) { throw; } \
1351 #endif /* _MSC_VER */
1353 #endif /* __CLEANUP_CXX */
1355 #endif /* ! PTW32_BUILD */
1358 } /* End of extern "C" */
1359 #endif /* __cplusplus */
1361 #ifdef PTW32__HANDLE_DEF
1364 #ifdef PTW32__DWORD_DEF
1369 #undef PTW32_LEVEL_MAX
1371 #endif /* ! RC_INVOKED */
1373 #endif /* PTHREAD_H */