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,2003 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 )
38 #if defined(_POSIX_SOURCE)
43 #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309
46 /* Include 1b, 1c and 1d */
49 #if defined(INCLUDE_NP)
52 /* Include Non-Portable extensions */
55 #define PTW32_LEVEL_MAX 3
57 #if !defined(PTW32_LEVEL)
58 #define PTW32_LEVEL PTW32_LEVEL_MAX
59 /* Include everything */
63 # define HAVE_STRUCT_TIMESPEC 1
64 # define HAVE_SIGNAL_H 1
66 # pragma comment(lib, "pthread")
70 * -------------------------------------------------------------
76 * Provides an implementation of PThreads based upon the
81 * The Single Unix Specification version 3
83 * (these two are equivalent)
85 * in order to enhance code portability between Windows,
86 * various commercial Unix implementations, and Linux.
88 * See the ANNOUNCE file for a full list of conforming
89 * routines and defined constants, and a list of missing
90 * routines and constants not defined in this implementation.
93 * There have been many contributors to this library.
94 * The initial implementation was contributed by
95 * John Bossom, and several others have provided major
96 * sections or revisions of parts of the implementation.
97 * Often significant effort has been contributed to
98 * find and fix important bugs and other problems to
99 * improve the reliability of the library, which sometimes
100 * is not reflected in the amount of code which changed as
102 * As much as possible, the contributors are acknowledged
103 * in the ChangeLog file in the source code distribution
104 * where their changes are noted in detail.
106 * Contributors are listed in the CONTRIBUTORS file.
108 * As usual, all bouquets go to the contributors, and all
109 * brickbats go to the project maintainer.
112 * The code base for this project is coordinated and
113 * eventually pre-tested, packaged, and made available by
115 * Ross Johnson <rpj@ise.canberra.edu.au>
118 * Ultimately, the library is tested in the real world by
119 * a host of competent and demanding scientists and
120 * engineers who report bugs and/or provide solutions
121 * which are then fixed or incorporated into subsequent
122 * versions of the library. Each time a bug is fixed, a
123 * test case is written to prove the fix and ensure
124 * that later changes to the code don't reintroduce the
125 * same error. The number of test cases is slowly growing
126 * and therefore so is the code reliability.
129 * See the file ANNOUNCE for the list of implemented
130 * and not-implemented routines and defined options.
131 * Of course, these are all defined is this file as well.
134 * The source code and other information about this library
137 * http://sources.redhat.com/pthreads-win32/
139 * -------------------------------------------------------------
142 /* Try to avoid including windows.h */
143 #if defined(__MINGW32__) && defined(__cplusplus)
145 * FIXME: The pthreadGCE.dll build gets linker unresolved errors
146 * on pthread_key_create() unless windows.h is included here.
147 * It appears to have something to do with an argument type mismatch.
148 * Looking at tsd.o with 'nm' shows this line:
149 * 00000000 T _pthread_key_create__FPP14pthread_key_t_PFPv_v
151 * 00000000 T _pthread_key_create
153 #define PTW32_INCLUDE_WINDOWS_H
156 #ifdef PTW32_INCLUDE_WINDOWS_H
168 #endif /* HAVE_CONFIG_H */
170 #if PTW32_LEVEL >= PTW32_LEVEL_MAX
172 /* Try to avoid including windows.h */
173 #if defined(__MINGW32__) && defined(__cplusplus)
175 * FIXME: The pthreadGCE.dll build gets linker unresolved errors
176 * on pthread_key_create() unless windows.h is included here.
177 * It appears to have something to do with an argument type mismatch.
178 * Looking at tsd.o with 'nm' shows this line:
179 * 00000000 T _pthread_key_create__FPP14pthread_key_t_PFPv_v
181 * 00000000 T _pthread_key_create
183 #define PTW32_INCLUDE_WINDOWS_H
186 #ifdef PTW32_INCLUDE_WINDOWS_H
192 #else /* NEED_FTIME */
193 /* use native WIN32 time API */
194 #endif /* NEED_FTIME */
198 #endif /* HAVE_SIGNAL_H */
204 * Boolean values to make us independent of system includes.
208 PTW32_TRUE
= (! PTW32_FALSE
)
212 * This is a duplicate of what is in the autoconf config.h,
213 * which is only used when building the pthread-win32 libraries.
216 #ifndef PTW32_CONFIG_H
221 # if defined(_UWIN) || defined(__MINGW32__)
230 #if PTW32_LEVEL >= PTW32_LEVEL_MAX
232 #include "need_errno.h"
236 #endif /* PTW32_LEVEL >= PTW32_LEVEL_MAX */
239 * Several systems don't define ENOTSUP. If not, we use
240 * the same value as Solaris.
247 # define ETIMEDOUT 10060 /* This is the value in winsock.h. */
253 * To avoid including windows.h we define only those things that we
254 * actually need from it. I don't like the potential incompatibility that
255 * this creates with future versions of windows.
257 #ifndef PTW32_INCLUDE_WINDOWS_H
259 # define PTW32__HANDLE_DEF
260 # define HANDLE void *
263 # define PTW32__DWORD_DEF
264 # define DWORD unsigned long
268 #endif /* PTW32_LEVEL >= PTW32_LEVEL_MAX */
270 #ifndef HAVE_STRUCT_TIMESPEC
275 #endif /* HAVE_STRUCT_TIMESPEC */
279 #endif /* SIG_BLOCK */
282 #define SIG_UNBLOCK 1
283 #endif /* SIG_UNBLOCK */
286 #define SIG_SETMASK 2
287 #endif /* SIG_SETMASK */
292 #endif /* __cplusplus */
295 * -------------------------------------------------------------
297 * POSIX 1003.1-2001 Options
298 * =========================
300 * _POSIX_THREADS (set)
301 * If set, you can use threads
303 * _POSIX_THREAD_ATTR_STACKSIZE (set)
304 * If set, you can control the size of a thread's
306 * pthread_attr_getstacksize
307 * pthread_attr_setstacksize
309 * _POSIX_THREAD_ATTR_STACKADDR (not set)
310 * If set, you can allocate and control a thread's
311 * stack. If not supported, the following functions
312 * will return ENOSYS, indicating they are not
314 * pthread_attr_getstackaddr
315 * pthread_attr_setstackaddr
317 * _POSIX_THREAD_PRIORITY_SCHEDULING (set)
318 * If set, you can use realtime scheduling.
319 * Indicates the availability of:
320 * pthread_attr_getinheritsched
321 * pthread_attr_getschedparam
322 * pthread_attr_getschedpolicy
323 * pthread_attr_getscope
324 * pthread_attr_setinheritsched
325 * pthread_attr_setschedparam
326 * pthread_attr_setschedpolicy
327 * pthread_attr_setscope
328 * pthread_getschedparam
329 * pthread_setschedparam
330 * sched_get_priority_max
331 * sched_get_priority_min
332 * sched_rr_set_interval
334 * _POSIX_THREAD_PRIO_INHERIT (not set)
335 * If set, you can create priority inheritance
337 * pthread_mutexattr_getprotocol +
338 * pthread_mutexattr_setprotocol +
340 * _POSIX_THREAD_PRIO_PROTECT (not set)
341 * If set, you can create priority ceiling mutexes
342 * Indicates the availability of:
343 * pthread_mutex_getprioceiling
344 * pthread_mutex_setprioceiling
345 * pthread_mutexattr_getprioceiling
346 * pthread_mutexattr_getprotocol +
347 * pthread_mutexattr_setprioceiling
348 * pthread_mutexattr_setprotocol +
350 * _POSIX_THREAD_PROCESS_SHARED (not set)
351 * If set, you can create mutexes and condition
352 * variables that can be shared with another
353 * process.If set, indicates the availability
355 * pthread_mutexattr_getpshared
356 * pthread_mutexattr_setpshared
357 * pthread_condattr_getpshared
358 * pthread_condattr_setpshared
360 * _POSIX_THREAD_SAFE_FUNCTIONS (set)
361 * If set you can use the special *_r library
362 * functions that provide thread-safe behaviour
364 * _POSIX_READER_WRITER_LOCKS (set)
365 * If set, you can use read/write locks
367 * _POSIX_SPIN_LOCKS (set)
368 * If set, you can use spin locks
370 * _POSIX_BARRIERS (set)
371 * If set, you can use barriers
373 * + These functions provide both 'inherit' and/or
374 * 'protect' protocol, based upon these macro
377 * POSIX 1003.1-2001 Limits
378 * ===========================
380 * PTHREAD_DESTRUCTOR_ITERATIONS
381 * Maximum number of attempts to destroy
382 * a thread's thread-specific data on
383 * termination (must be at least 4)
386 * Maximum number of thread-specific data keys
387 * available per process (must be at least 128)
390 * Minimum supported stack size for a thread
392 * PTHREAD_THREADS_MAX
393 * Maximum number of threads supported per
394 * process (must be at least 64).
396 * _POSIX_SEM_NSEMS_MAX
397 * The maximum number of semaphores a process can have.
398 * (only defined if not already defined)
400 * _POSIX_SEM_VALUE_MAX
401 * The maximum value a semaphore can have.
402 * (only defined if not already defined)
404 * -------------------------------------------------------------
410 #ifndef _POSIX_THREADS
411 #define _POSIX_THREADS
414 #ifndef _POSIX_READER_WRITER_LOCKS
415 #define _POSIX_READER_WRITER_LOCKS
418 #ifndef _POSIX_SPIN_LOCKS
419 #define _POSIX_SPIN_LOCKS
422 #ifndef _POSIX_BARRIERS
423 #define _POSIX_BARRIERS
426 #define _POSIX_THREAD_SAFE_FUNCTIONS
427 #define _POSIX_THREAD_ATTR_STACKSIZE
428 #define _POSIX_THREAD_PRIORITY_SCHEDULING
430 #if defined( KLUDGE )
432 * The following are not supported
434 #define _POSIX_THREAD_ATTR_STACKADDR
435 #define _POSIX_THREAD_PRIO_INHERIT
436 #define _POSIX_THREAD_PRIO_PROTECT
437 #define _POSIX_THREAD_PROCESS_SHARED
444 * PTHREAD_DESTRUCTOR_ITERATIONS
445 * Standard states this must be at least
449 * WIN32 permits only 64 TLS keys per process.
450 * This limitation could be worked around by
451 * simply simulating keys.
454 * POSIX specifies 0 which is also the value WIN32
455 * interprets as allowing the system to
456 * set the size to that of the main thread. The
457 * maximum stack size in Win32 is 1Meg. WIN32
458 * allocates more stack as required up to the 1Meg
461 * PTHREAD_THREADS_MAX
462 * Not documented by WIN32. Wrote a test program
463 * that kept creating threads until it failed
464 * revealed this approximate number (Windows NT).
465 * This number is somewhat less for Windows 9x
466 * and is effectively less than 64. Perhaps this
467 * constant should be set at DLL load time.
470 #define PTHREAD_DESTRUCTOR_ITERATIONS 4
471 #define PTHREAD_KEYS_MAX 64
472 #define PTHREAD_STACK_MIN 0
473 #define PTHREAD_THREADS_MAX 2019
474 #ifndef _POSIX_SEM_NSEMS_MAX
475 /* Not used and only an arbitrary value. */
476 # define _POSIX_SEM_NSEMS_MAX 1024
478 #ifndef _POSIX_SEM_VALUE_MAX
479 # define _POSIX_SEM_VALUE_MAX (INT_MAX/2)
482 #if __GNUC__ && ! defined (__declspec)
483 # error Please upgrade your GNU compiler to one that supports __declspec.
487 * When building the DLL code, you should define PTW32_BUILD so that
488 * the variables/functions are exported correctly. When using the DLL,
489 * do NOT define PTW32_BUILD, and then the variables/functions will
490 * be imported correctly.
494 # define PTW32_DLLPORT __declspec (dllexport)
496 # define PTW32_DLLPORT __declspec (dllimport)
500 #if defined(_UWIN) && PTW32_LEVEL >= PTW32_LEVEL_MAX
501 # include <sys/types.h>
503 typedef struct pthread_t_
*pthread_t
;
504 typedef struct pthread_attr_t_
*pthread_attr_t
;
505 typedef struct pthread_once_t_ pthread_once_t
;
506 typedef struct pthread_key_t_
*pthread_key_t
;
507 typedef struct pthread_mutex_t_
*pthread_mutex_t
;
508 typedef struct pthread_mutexattr_t_
*pthread_mutexattr_t
;
509 typedef struct pthread_cond_t_
*pthread_cond_t
;
510 typedef struct pthread_condattr_t_
*pthread_condattr_t
;
512 typedef struct pthread_rwlock_t_
*pthread_rwlock_t
;
513 typedef struct pthread_rwlockattr_t_
*pthread_rwlockattr_t
;
514 typedef struct pthread_spinlock_t_
*pthread_spinlock_t
;
515 typedef struct pthread_barrier_t_
*pthread_barrier_t
;
516 typedef struct pthread_barrierattr_t_
*pthread_barrierattr_t
;
519 * ====================
520 * ====================
522 * ====================
523 * ====================
528 * pthread_attr_{get,set}detachstate
530 PTHREAD_CREATE_JOINABLE
= 0, /* Default */
531 PTHREAD_CREATE_DETACHED
= 1,
534 * pthread_attr_{get,set}inheritsched
536 PTHREAD_INHERIT_SCHED
= 0,
537 PTHREAD_EXPLICIT_SCHED
= 1, /* Default */
540 * pthread_{get,set}scope
542 PTHREAD_SCOPE_PROCESS
= 0,
543 PTHREAD_SCOPE_SYSTEM
= 1, /* Default */
546 * pthread_setcancelstate paramters
548 PTHREAD_CANCEL_ENABLE
= 0, /* Default */
549 PTHREAD_CANCEL_DISABLE
= 1,
552 * pthread_setcanceltype parameters
554 PTHREAD_CANCEL_ASYNCHRONOUS
= 0,
555 PTHREAD_CANCEL_DEFERRED
= 1, /* Default */
558 * pthread_mutexattr_{get,set}pshared
559 * pthread_condattr_{get,set}pshared
561 PTHREAD_PROCESS_PRIVATE
= 0,
562 PTHREAD_PROCESS_SHARED
= 1,
565 * pthread_barrier_wait
567 PTHREAD_BARRIER_SERIAL_THREAD
= -1
571 * ====================
572 * ====================
574 * ====================
575 * ====================
577 #define PTHREAD_CANCELED ((void *) -1)
581 * ====================
582 * ====================
584 * ====================
585 * ====================
587 #define PTHREAD_ONCE_INIT { PTW32_FALSE, -1 }
589 struct pthread_once_t_
591 int done
; /* indicates if user function executed */
592 long started
; /* First thread to increment this value */
593 /* to zero executes the user function */
598 * ====================
599 * ====================
600 * Object initialisers
601 * ====================
602 * ====================
604 #define PTHREAD_MUTEX_INITIALIZER ((pthread_mutex_t) -1)
606 #define PTHREAD_COND_INITIALIZER ((pthread_cond_t) -1)
608 #define PTHREAD_RWLOCK_INITIALIZER ((pthread_rwlock_t) -1)
610 #define PTHREAD_SPINLOCK_INITIALIZER ((pthread_spinlock_t) -1)
618 /* Compatibility with LinuxThreads */
619 PTHREAD_MUTEX_FAST_NP
,
620 PTHREAD_MUTEX_RECURSIVE_NP
,
621 PTHREAD_MUTEX_ERRORCHECK_NP
,
622 PTHREAD_MUTEX_TIMED_NP
= PTHREAD_MUTEX_FAST_NP
,
623 PTHREAD_MUTEX_ADAPTIVE_NP
= PTHREAD_MUTEX_FAST_NP
,
624 /* For compatibility with POSIX */
625 PTHREAD_MUTEX_NORMAL
= PTHREAD_MUTEX_FAST_NP
,
626 PTHREAD_MUTEX_RECURSIVE
= PTHREAD_MUTEX_RECURSIVE_NP
,
627 PTHREAD_MUTEX_ERRORCHECK
= PTHREAD_MUTEX_ERRORCHECK_NP
,
628 PTHREAD_MUTEX_DEFAULT
= PTHREAD_MUTEX_NORMAL
632 /* There are three implementations of cancel cleanup.
633 * Note that pthread.h is included in both application
634 * compilation units and also internally for the library.
635 * The code here and within the library aims to work
636 * for all reasonable combinations of environments.
638 * The three implementations are:
644 * Please note that exiting a push/pop block via
645 * "return", "exit", "break", or "continue" will
646 * lead to different behaviour amongst applications
647 * depending upon whether the library was built
648 * using SEH, C++, or C. For example, a library built
649 * with SEH will call the cleanup routine, while both
650 * C++ and C built versions will not.
654 * Define defaults for cleanup code.
655 * Note: Unless the build explicitly defines one of the following, then
656 * we default to standard C style cleanup. This style uses setjmp/longjmp
657 * in the cancelation and thread exit implementations and therefore won't
658 * do stack unwinding if linked to applications that have it (e.g.
659 * C++ apps). This is currently consistent with most/all commercial Unix
660 * POSIX threads implementations.
662 #if !defined( __CLEANUP_SEH ) && !defined( __CLEANUP_CXX ) && !defined( __CLEANUP_C )
666 #if defined( __CLEANUP_SEH ) && defined(__GNUC__)
667 #error ERROR [__FILE__, line __LINE__]: GNUC does not support SEH.
670 typedef struct ptw32_cleanup_t ptw32_cleanup_t
;
671 typedef void (__cdecl
*ptw32_cleanup_callback_t
)(void *);
673 struct ptw32_cleanup_t
675 ptw32_cleanup_callback_t routine
;
677 struct ptw32_cleanup_t
*prev
;
682 * WIN32 SEH version of cancel cleanup.
685 #define pthread_cleanup_push( _rout, _arg ) \
687 ptw32_cleanup_t _cleanup; \
689 _cleanup.routine = (ptw32_cleanup_callback_t)(_rout); \
690 _cleanup.arg = (_arg); \
694 #define pthread_cleanup_pop( _execute ) \
698 if( _execute || AbnormalTermination()) \
700 (*(_cleanup.routine))( _cleanup.arg ); \
705 #else /* __CLEANUP_SEH */
710 * C implementation of PThreads cancel cleanup
713 #define pthread_cleanup_push( _rout, _arg ) \
715 ptw32_cleanup_t _cleanup; \
717 ptw32_push_cleanup( &_cleanup, (ptw32_cleanup_callback_t) (_rout), (_arg) ); \
719 #define pthread_cleanup_pop( _execute ) \
720 (void) ptw32_pop_cleanup( _execute ); \
723 #else /* __CLEANUP_C */
728 * C++ version of cancel cleanup.
732 class PThreadCleanup
{
737 * This class is a C++ helper class that is
738 * used to implement pthread_cleanup_push/
739 * pthread_cleanup_pop.
740 * The destructor of this class automatically
741 * pops the pushed cleanup routine regardless
742 * of how the code exits the scope
743 * (i.e. such as by an exception)
745 ptw32_cleanup_callback_t cleanUpRout
;
755 * No cleanup performed
761 ptw32_cleanup_callback_t routine
,
763 cleanUpRout( routine
),
767 * Registers a cleanup routine for 'arg'
774 if ( executeIt
&& ((void *) cleanUpRout
!= (void *) 0) )
776 (void) (*cleanUpRout
)( obj
);
780 void execute( int exec
)
787 * C++ implementation of PThreads cancel cleanup;
788 * This implementation takes advantage of a helper
789 * class who's destructor automatically calls the
790 * cleanup routine if we exit our scope weirdly
792 #define pthread_cleanup_push( _rout, _arg ) \
794 PThreadCleanup cleanup((ptw32_cleanup_callback_t)(_rout), \
797 #define pthread_cleanup_pop( _execute ) \
798 cleanup.execute( _execute ); \
803 #error ERROR [__FILE__, line __LINE__]: Cleanup type undefined.
805 #endif /* __CLEANUP_CXX */
807 #endif /* __CLEANUP_C */
809 #endif /* __CLEANUP_SEH */
820 * PThread Attribute Functions
822 PTW32_DLLPORT
int pthread_attr_init (pthread_attr_t
* attr
);
824 PTW32_DLLPORT
int pthread_attr_destroy (pthread_attr_t
* attr
);
826 PTW32_DLLPORT
int pthread_attr_getdetachstate (const pthread_attr_t
* attr
,
829 PTW32_DLLPORT
int pthread_attr_getstackaddr (const pthread_attr_t
* attr
,
832 PTW32_DLLPORT
int pthread_attr_getstacksize (const pthread_attr_t
* attr
,
835 PTW32_DLLPORT
int pthread_attr_setdetachstate (pthread_attr_t
* attr
,
838 PTW32_DLLPORT
int pthread_attr_setstackaddr (pthread_attr_t
* attr
,
841 PTW32_DLLPORT
int pthread_attr_setstacksize (pthread_attr_t
* attr
,
844 PTW32_DLLPORT
int pthread_attr_getschedparam (const pthread_attr_t
*attr
,
845 struct sched_param
*param
);
847 PTW32_DLLPORT
int pthread_attr_setschedparam (pthread_attr_t
*attr
,
848 const struct sched_param
*param
);
850 PTW32_DLLPORT
int pthread_attr_setschedpolicy (pthread_attr_t
*,
853 PTW32_DLLPORT
int pthread_attr_getschedpolicy (pthread_attr_t
*,
856 PTW32_DLLPORT
int pthread_attr_setinheritsched(pthread_attr_t
* attr
,
859 PTW32_DLLPORT
int pthread_attr_getinheritsched(pthread_attr_t
* attr
,
862 PTW32_DLLPORT
int pthread_attr_setscope (pthread_attr_t
*,
865 PTW32_DLLPORT
int pthread_attr_getscope (const pthread_attr_t
*,
871 PTW32_DLLPORT
int pthread_create (pthread_t
* tid
,
872 const pthread_attr_t
* attr
,
873 void *(*start
) (void *),
876 PTW32_DLLPORT
int pthread_detach (pthread_t tid
);
878 PTW32_DLLPORT
int pthread_equal (pthread_t t1
,
881 PTW32_DLLPORT
void pthread_exit (void *value_ptr
);
883 PTW32_DLLPORT
int pthread_join (pthread_t thread
,
886 PTW32_DLLPORT pthread_t
pthread_self (void);
888 PTW32_DLLPORT
int pthread_cancel (pthread_t thread
);
890 PTW32_DLLPORT
int pthread_setcancelstate (int state
,
893 PTW32_DLLPORT
int pthread_setcanceltype (int type
,
896 PTW32_DLLPORT
void pthread_testcancel (void);
898 PTW32_DLLPORT
int pthread_once (pthread_once_t
* once_control
,
899 void (*init_routine
) (void));
901 #if PTW32_LEVEL >= PTW32_LEVEL_MAX
902 PTW32_DLLPORT ptw32_cleanup_t
*ptw32_pop_cleanup (int execute
);
904 PTW32_DLLPORT
void ptw32_push_cleanup (ptw32_cleanup_t
* cleanup
,
905 void (*routine
) (void *),
907 #endif /* PTW32_LEVEL >= PTW32_LEVEL_MAX */
910 * Thread Specific Data Functions
912 PTW32_DLLPORT
int pthread_key_create (pthread_key_t
* key
,
913 void (*destructor
) (void *));
915 PTW32_DLLPORT
int pthread_key_delete (pthread_key_t key
);
917 PTW32_DLLPORT
int pthread_setspecific (pthread_key_t key
,
920 PTW32_DLLPORT
void *pthread_getspecific (pthread_key_t key
);
924 * Mutex Attribute Functions
926 PTW32_DLLPORT
int pthread_mutexattr_init (pthread_mutexattr_t
* attr
);
928 PTW32_DLLPORT
int pthread_mutexattr_destroy (pthread_mutexattr_t
* attr
);
930 PTW32_DLLPORT
int pthread_mutexattr_getpshared (const pthread_mutexattr_t
934 PTW32_DLLPORT
int pthread_mutexattr_setpshared (pthread_mutexattr_t
* attr
,
937 PTW32_DLLPORT
int pthread_mutexattr_settype (pthread_mutexattr_t
* attr
, int kind
);
938 PTW32_DLLPORT
int pthread_mutexattr_gettype (pthread_mutexattr_t
* attr
, int *kind
);
941 * Barrier Attribute Functions
943 PTW32_DLLPORT
int pthread_barrierattr_init (pthread_barrierattr_t
* attr
);
945 PTW32_DLLPORT
int pthread_barrierattr_destroy (pthread_barrierattr_t
* attr
);
947 PTW32_DLLPORT
int pthread_barrierattr_getpshared (const pthread_barrierattr_t
951 PTW32_DLLPORT
int pthread_barrierattr_setpshared (pthread_barrierattr_t
* attr
,
957 PTW32_DLLPORT
int pthread_mutex_init (pthread_mutex_t
* mutex
,
958 const pthread_mutexattr_t
* attr
);
960 PTW32_DLLPORT
int pthread_mutex_destroy (pthread_mutex_t
* mutex
);
962 PTW32_DLLPORT
int pthread_mutex_lock (pthread_mutex_t
* mutex
);
964 PTW32_DLLPORT
int pthread_mutex_timedlock(pthread_mutex_t
*mutex
,
965 const struct timespec
*abstime
);
967 PTW32_DLLPORT
int pthread_mutex_trylock (pthread_mutex_t
* mutex
);
969 PTW32_DLLPORT
int pthread_mutex_unlock (pthread_mutex_t
* mutex
);
974 PTW32_DLLPORT
int pthread_spin_init (pthread_spinlock_t
* lock
, int pshared
);
976 PTW32_DLLPORT
int pthread_spin_destroy (pthread_spinlock_t
* lock
);
978 PTW32_DLLPORT
int pthread_spin_lock (pthread_spinlock_t
* lock
);
980 PTW32_DLLPORT
int pthread_spin_trylock (pthread_spinlock_t
* lock
);
982 PTW32_DLLPORT
int pthread_spin_unlock (pthread_spinlock_t
* lock
);
987 PTW32_DLLPORT
int pthread_barrier_init (pthread_barrier_t
* barrier
,
988 const pthread_barrierattr_t
* attr
,
991 PTW32_DLLPORT
int pthread_barrier_destroy (pthread_barrier_t
* barrier
);
993 PTW32_DLLPORT
int pthread_barrier_wait (pthread_barrier_t
* barrier
);
996 * Condition Variable Attribute Functions
998 PTW32_DLLPORT
int pthread_condattr_init (pthread_condattr_t
* attr
);
1000 PTW32_DLLPORT
int pthread_condattr_destroy (pthread_condattr_t
* attr
);
1002 PTW32_DLLPORT
int pthread_condattr_getpshared (const pthread_condattr_t
* attr
,
1005 PTW32_DLLPORT
int pthread_condattr_setpshared (pthread_condattr_t
* attr
,
1009 * Condition Variable Functions
1011 PTW32_DLLPORT
int pthread_cond_init (pthread_cond_t
* cond
,
1012 const pthread_condattr_t
* attr
);
1014 PTW32_DLLPORT
int pthread_cond_destroy (pthread_cond_t
* cond
);
1016 PTW32_DLLPORT
int pthread_cond_wait (pthread_cond_t
* cond
,
1017 pthread_mutex_t
* mutex
);
1019 PTW32_DLLPORT
int pthread_cond_timedwait (pthread_cond_t
* cond
,
1020 pthread_mutex_t
* mutex
,
1021 const struct timespec
*abstime
);
1023 PTW32_DLLPORT
int pthread_cond_signal (pthread_cond_t
* cond
);
1025 PTW32_DLLPORT
int pthread_cond_broadcast (pthread_cond_t
* cond
);
1030 PTW32_DLLPORT
int pthread_setschedparam (pthread_t thread
,
1032 const struct sched_param
*param
);
1034 PTW32_DLLPORT
int pthread_getschedparam (pthread_t thread
,
1036 struct sched_param
*param
);
1038 PTW32_DLLPORT
int pthread_setconcurrency (int);
1040 PTW32_DLLPORT
int pthread_getconcurrency (void);
1043 * Read-Write Lock Functions
1045 PTW32_DLLPORT
int pthread_rwlock_init(pthread_rwlock_t
*lock
,
1046 const pthread_rwlockattr_t
*attr
);
1048 PTW32_DLLPORT
int pthread_rwlock_destroy(pthread_rwlock_t
*lock
);
1050 PTW32_DLLPORT
int pthread_rwlock_tryrdlock(pthread_rwlock_t
*);
1052 PTW32_DLLPORT
int pthread_rwlock_trywrlock(pthread_rwlock_t
*);
1054 PTW32_DLLPORT
int pthread_rwlock_rdlock(pthread_rwlock_t
*lock
);
1056 PTW32_DLLPORT
int pthread_rwlock_timedrdlock(pthread_rwlock_t
*lock
,
1057 const struct timespec
*abstime
);
1059 PTW32_DLLPORT
int pthread_rwlock_wrlock(pthread_rwlock_t
*lock
);
1061 PTW32_DLLPORT
int pthread_rwlock_timedwrlock(pthread_rwlock_t
*lock
,
1062 const struct timespec
*abstime
);
1064 PTW32_DLLPORT
int pthread_rwlock_unlock(pthread_rwlock_t
*lock
);
1066 PTW32_DLLPORT
int pthread_rwlockattr_init (pthread_rwlockattr_t
* attr
);
1068 PTW32_DLLPORT
int pthread_rwlockattr_destroy (pthread_rwlockattr_t
* attr
);
1070 PTW32_DLLPORT
int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t
* attr
,
1073 PTW32_DLLPORT
int pthread_rwlockattr_setpshared (pthread_rwlockattr_t
* attr
,
1076 #if PTW32_LEVEL >= PTW32_LEVEL_MAX - 1
1079 * Signal Functions. Should be defined in <signal.h> but MSVC and MinGW32
1080 * already have signal.h that don't define these.
1082 PTW32_DLLPORT
int pthread_kill(pthread_t thread
, int sig
);
1085 * Non-portable functions
1089 * Compatibility with Linux.
1091 PTW32_DLLPORT
int pthread_mutexattr_setkind_np(pthread_mutexattr_t
* attr
,
1093 PTW32_DLLPORT
int pthread_mutexattr_getkind_np(pthread_mutexattr_t
* attr
,
1097 * Possibly supported by other POSIX threads implementations
1099 PTW32_DLLPORT
int pthread_delay_np (struct timespec
* interval
);
1100 PTW32_DLLPORT
int pthread_num_processors_np(void);
1103 * Useful if an application wants to statically link
1104 * the lib rather than load the DLL at run-time.
1106 PTW32_DLLPORT
int pthread_win32_process_attach_np(void);
1107 PTW32_DLLPORT
int pthread_win32_process_detach_np(void);
1108 PTW32_DLLPORT
int pthread_win32_thread_attach_np(void);
1109 PTW32_DLLPORT
int pthread_win32_thread_detach_np(void);
1112 * Register a system time change with the library.
1113 * Causes the library to perform various functions
1114 * in response to the change. Should be called whenever
1115 * the application's top level window receives a
1116 * WM_TIMECHANGE message. It can be passed directly to
1117 * pthread_create() as a new thread if desired.
1119 PTW32_DLLPORT
void * pthread_timechange_handler_np(void *);
1121 #endif /*PTW32_LEVEL >= PTW32_LEVEL_MAX - 1 */
1123 #if PTW32_LEVEL >= PTW32_LEVEL_MAX
1126 * Returns the Win32 HANDLE for the POSIX thread.
1128 PTW32_DLLPORT HANDLE
pthread_getw32threadhandle_np(pthread_t thread
);
1134 * This function blocks until the given WIN32 handle
1135 * is signaled or pthread_cancel had been called.
1136 * This function allows the caller to hook into the
1137 * PThreads cancel mechanism. It is implemented using
1139 * WaitForMultipleObjects
1141 * on 'waitHandle' and a manually reset WIN32 Event
1142 * used to implement pthread_cancel. The 'timeout'
1143 * argument to TimedWait is simply passed to
1144 * WaitForMultipleObjects.
1146 PTW32_DLLPORT
int pthreadCancelableWait (HANDLE waitHandle
);
1147 PTW32_DLLPORT
int pthreadCancelableTimedWait (HANDLE waitHandle
,
1150 #endif /* PTW32_LEVEL >= PTW32_LEVEL_MAX */
1153 * Thread-Safe C Runtime Library Mappings.
1156 # if defined(NEED_ERRNO)
1157 PTW32_DLLPORT
int * _errno( void );
1160 # if (defined(_MT) || defined(_DLL))
1161 __declspec(dllimport
) extern int * __cdecl
_errno(void);
1162 # define errno (*_errno())
1169 * WIN32 C runtime library had been made thread-safe
1170 * without affecting the user interface. Provide
1171 * mappings from the UNIX thread-safe versions to
1172 * the standard C runtime library calls.
1173 * Only provide function mappings for functions that
1174 * actually exist on WIN32.
1177 #if !defined(__MINGW32__)
1178 #define strtok_r( _s, _sep, _lasts ) \
1179 ( *(_lasts) = strtok( (_s), (_sep) ) )
1180 #endif /* !__MINGW32__ */
1182 #define asctime_r( _tm, _buf ) \
1183 ( strcpy( (_buf), asctime( (_tm) ) ), \
1186 #define ctime_r( _clock, _buf ) \
1187 ( strcpy( (_buf), ctime( (_clock) ) ), \
1190 #define gmtime_r( _clock, _result ) \
1191 ( *(_result) = *gmtime( (_clock) ), \
1194 #define localtime_r( _clock, _result ) \
1195 ( *(_result) = *localtime( (_clock) ), \
1198 #define rand_r( _seed ) \
1199 ( _seed == _seed? rand() : rand() )
1205 * Internal exceptions
1207 class ptw32_exception
{};
1208 class ptw32_exception_cancel
: public ptw32_exception
{};
1209 class ptw32_exception_exit
: public ptw32_exception
{};
1213 #if PTW32_LEVEL >= PTW32_LEVEL_MAX
1215 /* FIXME: This is only required if the library was built using SEH */
1217 * Get internal SEH tag
1219 PTW32_DLLPORT DWORD
ptw32_get_exception_services_code(void);
1221 #endif /* PTW32_LEVEL >= PTW32_LEVEL_MAX */
1225 #ifdef __CLEANUP_SEH
1228 * Redefine the SEH __except keyword to ensure that applications
1229 * propagate our internal exceptions up to the library's internal handlers.
1231 #define __except( E ) \
1232 __except( ( GetExceptionCode() == ptw32_get_exception_services_code() ) \
1233 ? EXCEPTION_CONTINUE_SEARCH : ( E ) )
1235 #endif /* __CLEANUP_SEH */
1237 #ifdef __CLEANUP_CXX
1240 * Redefine the C++ catch keyword to ensure that applications
1241 * propagate our internal exceptions up to the library's internal handlers.
1245 * WARNING: Replace any 'catch( ... )' with 'PtW32CatchAll'
1246 * if you want Pthread-Win32 cancelation and pthread_exit to work.
1249 #ifndef PtW32NoCatchWarn
1251 #pragma message("Specify \"/DPtW32NoCatchWarn\" compiler flag to skip this message.")
1252 #pragma message("------------------------------------------------------------------")
1253 #pragma message("When compiling applications with MSVC++ and C++ exception handling:")
1254 #pragma message(" Replace any 'catch( ... )' in routines called from POSIX threads")
1255 #pragma message(" with 'PtW32CatchAll' or 'CATCHALL' if you want POSIX thread")
1256 #pragma message(" cancelation and pthread_exit to work. For example:")
1258 #pragma message(" #ifdef PtW32CatchAll")
1259 #pragma message(" PtW32CatchAll")
1260 #pragma message(" #else")
1261 #pragma message(" catch(...)")
1262 #pragma message(" #endif")
1263 #pragma message(" {")
1264 #pragma message(" /* Catchall block processing */")
1265 #pragma message(" }")
1266 #pragma message("------------------------------------------------------------------")
1270 #define PtW32CatchAll \
1271 catch( ptw32_exception & ) { throw; } \
1274 #else /* _MSC_VER */
1276 #define catch( E ) \
1277 catch( ptw32_exception & ) { throw; } \
1280 #endif /* _MSC_VER */
1282 #endif /* __CLEANUP_CXX */
1284 #endif /* ! PTW32_BUILD */
1287 } /* End of extern "C" */
1288 #endif /* __cplusplus */
1290 #ifdef PTW32__HANDLE_DEF
1293 #ifdef PTW32__DWORD_DEF
1298 #undef PTW32_LEVEL_MAX
1300 #endif /* PTHREAD_H */