pthread.h
上传用户:fzchengxin
上传日期:2013-10-17
资源大小:2070k
文件大小:34k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /* This is the POSIX thread API (POSIX 1003).
  2.  *
  3.  * --------------------------------------------------------------------------
  4.  *
  5.  * Pthreads-win32 - POSIX Threads Library for Win32
  6.  * Copyright(C) 1998 John E. Bossom
  7.  * Copyright(C) 1999,2002 Pthreads-win32 contributors
  8.  * 
  9.  * Contact Email: rpj@ise.canberra.edu.au
  10.  * 
  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
  16.  * 
  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.
  21.  * 
  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.
  26.  * 
  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
  31.  */
  32. #if !defined( PTHREAD_H )
  33. #define PTHREAD_H
  34. #undef PTW32_LEVEL
  35. #if defined(_POSIX_SOURCE)
  36. #define PTW32_LEVEL 0
  37. /* Early POSIX */
  38. #endif
  39. #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309
  40. #undef PTW32_LEVEL
  41. #define PTW32_LEVEL 1
  42. /* Include 1b, 1c and 1d */
  43. #endif
  44. #if defined(INCLUDE_NP)
  45. #undef PTW32_LEVEL
  46. #define PTW32_LEVEL 2
  47. /* Include Non-Portable extensions */
  48. #endif
  49. #define PTW32_LEVEL_MAX 3
  50. #if !defined(PTW32_LEVEL)
  51. #define PTW32_LEVEL PTW32_LEVEL_MAX
  52. /* Include everything */
  53. #endif
  54. #ifdef _UWIN
  55. #   define HAVE_STRUCT_TIMESPEC 1
  56. #   define HAVE_SIGNAL_H 1
  57. #   undef HAVE_CONFIG_H
  58. #   pragma comment(lib, "pthread")
  59. #endif
  60. /*
  61.  * -------------------------------------------------------------
  62.  *
  63.  *
  64.  * Module: pthread.h
  65.  *
  66.  * Purpose:
  67.  * Provides an implementation of PThreads based upon the
  68.  * standard:
  69.  *
  70.  * POSIX 1003.1c-1995 (POSIX.1c)
  71.  *
  72.  * Parts of the implementation also comply with the
  73.  * Open Group Unix 98 specification in order to enhance
  74.  * code portability between Windows, various commercial
  75.  * Unix implementations, and Linux.
  76.  *
  77.  * See the ANNOUNCE file for a full list of conforming
  78.  * routines and defined constants, and a list of missing
  79.  * routines and constants not defined in this implementation.
  80.  *
  81.  * Authors:
  82.  * There have been many contributors to this library.
  83.  * The initial implementation was contributed by
  84.  * John Bossom, and several others have provided major
  85.  * sections or revisions of parts of the implementation.
  86.  * Often significant effort has been contributed to
  87.  * find and fix important bugs and other problems to
  88.  * improve the reliability of the library, which sometimes
  89.  * is not reflected in the amount of code which changed as
  90.  * result.
  91.  * As much as possible, the contributors are acknowledged
  92.  * in the ChangeLog file in the source code distribution
  93.  * where their changes are noted in detail.
  94.  *
  95.  * Contributors are listed in the CONTRIBUTORS file.
  96.  *
  97.  * As usual, all bouquets go to the contributors, and all
  98.  * brickbats go to the project maintainer.
  99.  *
  100.  * Maintainer:
  101.  * The code base for this project is coordinated and
  102.  * eventually pre-tested, packaged, and made available by
  103.  *
  104.  * Ross Johnson <rpj@ise.canberra.edu.au>
  105.  *
  106.  * QA Testers:
  107.  * Ultimately, the library is tested in the real world by
  108.  * a host of competent and demanding scientists and
  109.  * engineers who report bugs and/or provide solutions
  110.  * which are then fixed or incorporated into subsequent
  111.  * versions of the library. Each time a bug is fixed, a
  112.  * test case is written to prove the fix and ensure
  113.  * that later changes to the code don't reintroduce the
  114.  * same error. The number of test cases is slowly growing
  115.  * and therefore so is the code reliability.
  116.  *
  117.  * Compliance:
  118.  * See the file ANNOUNCE for the list of implemented
  119.  * and not-implemented routines and defined options.
  120.  * Of course, these are all defined is this file as well.
  121.  *
  122.  * Web site:
  123.  * The source code and other information about this library
  124.  * are available from
  125.  *
  126.  * http://sources.redhat.com/pthreads-win32/
  127.  *
  128.  * -------------------------------------------------------------
  129.  */
  130. /* Try to avoid including windows.h */
  131. #if defined(__MINGW32__) && defined(__cplusplus)
  132. /*
  133.  * FIXME: The pthreadGCE.dll build gets linker unresolved errors
  134.  * on pthread_key_create() unless windows.h is included here.
  135.  * It appears to have something to do with an argument type mismatch.
  136.  * Looking at tsd.o with 'nm' shows this line:
  137.  * 00000000 T _pthread_key_create__FPP14pthread_key_t_PFPv_v
  138.  * instead of
  139.  * 00000000 T _pthread_key_create
  140.  */
  141. #define PTW32_INCLUDE_WINDOWS_H
  142. #endif
  143. #ifdef PTW32_INCLUDE_WINDOWS_H
  144. #include <windows.h>
  145. #endif
  146. /*
  147.  * -----------------
  148.  * autoconf switches
  149.  * -----------------
  150.  */
  151. #if HAVE_CONFIG_H
  152. #include "config.h"
  153. #endif /* HAVE_CONFIG_H */
  154. #if PTW32_LEVEL >= PTW32_LEVEL_MAX
  155. /* Try to avoid including windows.h */
  156. #if defined(__MINGW32__) && defined(__cplusplus)
  157. /*
  158.  * FIXME: The pthreadGCE.dll build gets linker unresolved errors
  159.  * on pthread_key_create() unless windows.h is included here.
  160.  * It appears to have something to do with an argument type mismatch.
  161.  * Looking at tsd.o with 'nm' shows this line:
  162.  * 00000000 T _pthread_key_create__FPP14pthread_key_t_PFPv_v
  163.  * instead of
  164.  * 00000000 T _pthread_key_create
  165.  */
  166. #define PTW32_INCLUDE_WINDOWS_H
  167. #endif
  168. #ifdef PTW32_INCLUDE_WINDOWS_H
  169. #include <windows.h>
  170. #endif
  171. #ifndef NEED_FTIME
  172. #include <time.h>
  173. #else /* NEED_FTIME */
  174. /* use native WIN32 time API */
  175. #endif /* NEED_FTIME */
  176. #if HAVE_SIGNAL_H
  177. #include <signal.h>
  178. #endif /* HAVE_SIGNAL_H */
  179. #include <setjmp.h>
  180. /*
  181.  * This is a duplicate of what is in the autoconf config.h,
  182.  * which is only used when building the pthread-win32 libraries.
  183.  */
  184. #ifndef PTW32_CONFIG_H
  185. #  if defined(WINCE)
  186. #    define NEED_ERRNO
  187. #    define NEED_SEM
  188. #  endif
  189. #  if defined(_UWIN) || defined(__MINGW32__)
  190. #    define HAVE_MODE_T
  191. #  endif
  192. #endif
  193. /*
  194.  *
  195.  */
  196. #if PTW32_LEVEL >= PTW32_LEVEL_MAX
  197. #ifdef NEED_ERRNO
  198. #include "need_errno.h"
  199. #else
  200. #include <errno.h>
  201. #endif
  202. #endif /* PTW32_LEVEL >= PTW32_LEVEL_MAX */
  203. /*
  204.  * Several systems don't define ENOTSUP. If not, we use
  205.  * the same value as Solaris.
  206.  */
  207. #ifndef ENOTSUP
  208. #  define ENOTSUP 48
  209. #endif
  210. #ifndef ETIMEDOUT
  211. #  define ETIMEDOUT 10060     /* This is the value in winsock.h. */
  212. #endif
  213. #include <sched.h>
  214. /*
  215.  * To avoid including windows.h we define only those things that we
  216.  * actually need from it. I don't like the potential incompatibility that
  217.  * this creates with future versions of windows.
  218.  */
  219. #ifndef PTW32_INCLUDE_WINDOWS_H
  220. #ifndef HANDLE
  221. # define PTW32__HANDLE_DEF
  222. # define HANDLE void *
  223. #endif
  224. #ifndef DWORD
  225. # define PTW32__DWORD_DEF
  226. # define DWORD unsigned long
  227. #endif
  228. #endif
  229. #endif /* PTW32_LEVEL >= PTW32_LEVEL_MAX */
  230. #ifndef HAVE_STRUCT_TIMESPEC
  231. struct timespec {
  232. long tv_sec;
  233. long tv_nsec;
  234. };
  235. #endif /* HAVE_STRUCT_TIMESPEC */
  236. #ifndef SIG_BLOCK
  237. #define SIG_BLOCK 0
  238. #endif /* SIG_BLOCK */
  239. #ifndef SIG_UNBLOCK 
  240. #define SIG_UNBLOCK 1
  241. #endif /* SIG_UNBLOCK */
  242. #ifndef SIG_SETMASK
  243. #define SIG_SETMASK 2
  244. #endif /* SIG_SETMASK */
  245. #ifdef __cplusplus
  246. extern "C"
  247. {
  248. #endif /* __cplusplus */
  249. /*
  250.  * -------------------------------------------------------------
  251.  *
  252.  * POSIX 1003.1c-1995 Options
  253.  * ===========================
  254.  *
  255.  * _POSIX_THREADS (set)
  256.  * If set, you can use threads
  257.  *
  258.  * _POSIX_THREAD_ATTR_STACKSIZE (set)
  259.  * If set, you can control the size of a thread's
  260.  * stack
  261.  * pthread_attr_getstacksize
  262.  * pthread_attr_setstacksize
  263.  *
  264.  * _POSIX_THREAD_ATTR_STACKADDR (not set)
  265.  * If set, you can allocate and control a thread's
  266.  * stack. If not supported, the following functions
  267.  * will return ENOSYS, indicating they are not
  268.  * supported:
  269.  * pthread_attr_getstackaddr
  270.  * pthread_attr_setstackaddr
  271.  *
  272.  * _POSIX_THREAD_PRIORITY_SCHEDULING (set)
  273.  * If set, you can use realtime scheduling.
  274.  * Indicates the availability of:
  275.  * pthread_attr_getinheritsched
  276.  * pthread_attr_getschedparam
  277.  * pthread_attr_getschedpolicy
  278.  * pthread_attr_getscope
  279.  * pthread_attr_setinheritsched
  280.  * pthread_attr_setschedparam
  281.  * pthread_attr_setschedpolicy
  282.  * pthread_attr_setscope
  283.  * pthread_getschedparam
  284.  * pthread_setschedparam
  285.  * sched_get_priority_max
  286.  * sched_get_priority_min
  287.  * sched_rr_set_interval
  288.  *
  289.  * _POSIX_THREAD_PRIO_INHERIT (not set)
  290.  * If set, you can create priority inheritance
  291.  * mutexes.
  292.  * pthread_mutexattr_getprotocol +
  293.  * pthread_mutexattr_setprotocol +
  294.  *
  295.  * _POSIX_THREAD_PRIO_PROTECT (not set)
  296.  * If set, you can create priority ceiling mutexes
  297.  * Indicates the availability of:
  298.  * pthread_mutex_getprioceiling
  299.  * pthread_mutex_setprioceiling
  300.  * pthread_mutexattr_getprioceiling
  301.  * pthread_mutexattr_getprotocol   +
  302.  * pthread_mutexattr_setprioceiling
  303.  * pthread_mutexattr_setprotocol   +
  304.  *
  305.  * _POSIX_THREAD_PROCESS_SHARED (not set)
  306.  * If set, you can create mutexes and condition
  307.  * variables that can be shared with another
  308.  * process.If set, indicates the availability
  309.  * of:
  310.  * pthread_mutexattr_getpshared
  311.  * pthread_mutexattr_setpshared
  312.  * pthread_condattr_getpshared
  313.  * pthread_condattr_setpshared
  314.  *
  315.  * _POSIX_THREAD_SAFE_FUNCTIONS (set)
  316.  * If set you can use the special *_r library
  317.  * functions that provide thread-safe behaviour
  318.  *
  319.  * + These functions provide both 'inherit' and/or
  320.  *   'protect' protocol, based upon these macro
  321.  *   settings.
  322.  *
  323.  * POSIX 1003.1c-1995 Limits
  324.  * ===========================
  325.  *
  326.  * PTHREAD_DESTRUCTOR_ITERATIONS
  327.  * Maximum number of attempts to destroy
  328.  * a thread's thread-specific data on
  329.  * termination (must be at least 4)
  330.  *
  331.  * PTHREAD_KEYS_MAX
  332.  * Maximum number of thread-specific data keys
  333.  * available per process (must be at least 128)
  334.  *
  335.  * PTHREAD_STACK_MIN
  336.  * Minimum supported stack size for a thread
  337.  *
  338.  * PTHREAD_THREADS_MAX
  339.  * Maximum number of threads supported per
  340.  * process (must be at least 64).
  341.  *
  342.  *
  343.  * POSIX 1003.1j/D10-1999 Options
  344.  * ==============================
  345.  *
  346.  * _POSIX_READER_WRITER_LOCKS (set)
  347.  * If set, you can use read/write locks
  348.  *
  349.  * _POSIX_SPIN_LOCKS (set)
  350.  * If set, you can use spin locks
  351.  *
  352.  * _POSIX_BARRIERS (set)
  353.  * If set, you can use barriers
  354.  *
  355.  * -------------------------------------------------------------
  356.  */
  357. /*
  358.  * POSIX Options
  359.  */
  360. #ifndef _POSIX_THREADS
  361. #define _POSIX_THREADS
  362. #endif
  363. #ifndef _POSIX_READER_WRITER_LOCKS
  364. #define _POSIX_READER_WRITER_LOCKS
  365. #endif
  366. #ifndef _POSIX_SPIN_LOCKS
  367. #define _POSIX_SPIN_LOCKS
  368. #endif
  369. #ifndef _POSIX_BARRIERS
  370. #define _POSIX_BARRIERS
  371. #endif
  372. #define _POSIX_THREAD_SAFE_FUNCTIONS
  373. #define _POSIX_THREAD_ATTR_STACKSIZE
  374. #define _POSIX_THREAD_PRIORITY_SCHEDULING
  375. #if defined( KLUDGE )
  376. /*
  377.  * The following are not supported
  378.  */
  379. #define _POSIX_THREAD_ATTR_STACKADDR
  380. #define _POSIX_THREAD_PRIO_INHERIT
  381. #define _POSIX_THREAD_PRIO_PROTECT
  382. #define _POSIX_THREAD_PROCESS_SHARED
  383. #endif /* KLUDGE */
  384. /*
  385.  * POSIX Limits
  386.  *
  387.  * PTHREAD_DESTRUCTOR_ITERATIONS
  388.  * Standard states this must be at least
  389.  * 4.
  390.  *
  391.  * PTHREAD_KEYS_MAX
  392.  * WIN32 permits only 64 TLS keys per process.
  393.  * This limitation could be worked around by
  394.  * simply simulating keys.
  395.  *
  396.  * PTHREADS_STACK_MIN
  397.  * POSIX specifies 0 which is also the value WIN32
  398.  * interprets as allowing the system to
  399.  * set the size to that of the main thread. The
  400.  * maximum stack size in Win32 is 1Meg. WIN32
  401.  * allocates more stack as required up to the 1Meg
  402.  * limit.
  403.  *
  404.  * PTHREAD_THREADS_MAX
  405.  * Not documented by WIN32. Wrote a test program
  406.  * that kept creating threads until it failed
  407.  * revealed this approximate number (Windows NT).
  408.  * This number is somewhat less for Windows 9x
  409.  * and is effectively less than 64. Perhaps this
  410.  * constant should be set at DLL load time.
  411.  *
  412.  */
  413. #define PTHREAD_DESTRUCTOR_ITERATIONS    4
  414. #define PTHREAD_KEYS_MAX   64
  415. #define PTHREAD_STACK_MIN    0
  416. #define PTHREAD_THREADS_MAX 2019
  417. #if __GNUC__ && ! defined (__declspec)
  418. # error Please upgrade your GNU compiler to one that supports __declspec.
  419. #endif
  420. /*
  421.  * When building the DLL code, you should define PTW32_BUILD so that
  422.  * the variables/functions are exported correctly. When using the DLL,
  423.  * do NOT define PTW32_BUILD, and then the variables/functions will
  424.  * be imported correctly.
  425.  */
  426. #ifdef _DLL
  427. #  ifdef PTW32_BUILD
  428. #    define PTW32_DLLPORT __declspec (dllexport)
  429. #  else
  430. #    define PTW32_DLLPORT __declspec (dllimport)
  431. #  endif
  432. #endif
  433. #if defined(_UWIN) && PTW32_LEVEL >= PTW32_LEVEL_MAX
  434. #   include <sys/types.h>
  435. #else
  436. typedef struct pthread_t_ *pthread_t;
  437. typedef struct pthread_attr_t_ *pthread_attr_t;
  438. typedef struct pthread_once_t_ pthread_once_t;
  439. typedef struct pthread_key_t_ *pthread_key_t;
  440. typedef struct pthread_mutex_t_ *pthread_mutex_t;
  441. typedef struct pthread_mutexattr_t_ *pthread_mutexattr_t;
  442. typedef struct pthread_cond_t_ *pthread_cond_t;
  443. typedef struct pthread_condattr_t_ *pthread_condattr_t;
  444. #endif
  445. typedef struct pthread_rwlock_t_ *pthread_rwlock_t;
  446. typedef struct pthread_rwlockattr_t_ *pthread_rwlockattr_t;
  447. typedef struct pthread_spinlock_t_ *pthread_spinlock_t;
  448. typedef struct pthread_barrier_t_ *pthread_barrier_t;
  449. typedef struct pthread_barrierattr_t_ *pthread_barrierattr_t;
  450. /*
  451.  * ====================
  452.  * ====================
  453.  * POSIX Threads
  454.  * ====================
  455.  * ====================
  456.  */
  457. enum {
  458. /*
  459.  * pthread_attr_{get,set}detachstate
  460.  */
  461.   PTHREAD_CREATE_JOINABLE = 0,  /* Default */
  462.   PTHREAD_CREATE_DETACHED = 1,
  463. /*
  464.  * pthread_attr_{get,set}inheritsched
  465.  */
  466.   PTHREAD_INHERIT_SCHED  = 0,
  467.   PTHREAD_EXPLICIT_SCHED = 1,  /* Default */
  468. /*
  469.  * pthread_{get,set}scope
  470.  */
  471.   PTHREAD_SCOPE_PROCESS  = 0,
  472.   PTHREAD_SCOPE_SYSTEM = 1,  /* Default */
  473. /*
  474.  * pthread_setcancelstate paramters
  475.  */
  476.   PTHREAD_CANCEL_ENABLE  = 0,  /* Default */
  477.   PTHREAD_CANCEL_DISABLE = 1,
  478. /*
  479.  * pthread_setcanceltype parameters
  480.  */
  481.   PTHREAD_CANCEL_ASYNCHRONOUS = 0,
  482.   PTHREAD_CANCEL_DEFERRED = 1,  /* Default */
  483. /*
  484.  * pthread_mutexattr_{get,set}pshared
  485.  * pthread_condattr_{get,set}pshared
  486.  */
  487.   PTHREAD_PROCESS_PRIVATE = 0,
  488.   PTHREAD_PROCESS_SHARED = 1,
  489. /*
  490.  * pthread_barrier_wait
  491.  */
  492.   PTHREAD_BARRIER_SERIAL_THREAD = -1
  493. };
  494. /*
  495.  * ====================
  496.  * ====================
  497.  * Cancelation
  498.  * ====================
  499.  * ====================
  500.  */
  501. #define PTHREAD_CANCELED       ((void *) -1)
  502. /*
  503.  * ====================
  504.  * ====================
  505.  * Once Key
  506.  * ====================
  507.  * ====================
  508.  */
  509. #define PTHREAD_ONCE_INIT { FALSE, -1 }
  510. struct pthread_once_t_
  511. {
  512.   int done;     /* indicates if user function executed  */
  513.   long started;      /* First thread to increment this value */
  514.     /* to zero executes the user function   */
  515. };
  516. /*
  517.  * ====================
  518.  * ====================
  519.  * Object initialisers
  520.  * ====================
  521.  * ====================
  522.  */
  523. #define PTHREAD_MUTEX_INITIALIZER ((pthread_mutex_t) -1)
  524. #define PTHREAD_COND_INITIALIZER ((pthread_cond_t) -1)
  525. #define PTHREAD_RWLOCK_INITIALIZER ((pthread_rwlock_t) -1)
  526. #define PTHREAD_SPINLOCK_INITIALIZER ((pthread_spinlock_t) -1)
  527. /*
  528.  * Mutex types.
  529.  */
  530. enum
  531. {
  532.   /* Compatibility with LinuxThreads */
  533.   PTHREAD_MUTEX_FAST_NP,
  534.   PTHREAD_MUTEX_RECURSIVE_NP,
  535.   PTHREAD_MUTEX_ERRORCHECK_NP,
  536.   PTHREAD_MUTEX_TIMED_NP = PTHREAD_MUTEX_FAST_NP,
  537.   PTHREAD_MUTEX_ADAPTIVE_NP = PTHREAD_MUTEX_FAST_NP,
  538.   /* For compatibility with POSIX */
  539.   PTHREAD_MUTEX_NORMAL = PTHREAD_MUTEX_FAST_NP,
  540.   PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP,
  541.   PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP,
  542.   PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
  543. };
  544. /* There are three implementations of cancel cleanup.
  545.  * Note that pthread.h is included in both application
  546.  * compilation units and also internally for the library.
  547.  * The code here and within the library aims to work
  548.  * for all reasonable combinations of environments.
  549.  *
  550.  * The three implementations are:
  551.  *
  552.  *   WIN32 SEH
  553.  *   C
  554.  *   C++
  555.  *
  556.  * Please note that exiting a push/pop block via
  557.  * "return", "exit", "break", or "continue" will
  558.  * lead to different behaviour amongst applications
  559.  * depending upon whether the library was built
  560.  * using SEH, C++, or C. For example, a library built
  561.  * with SEH will call the cleanup routine, while both
  562.  * C++ and C built versions will not.
  563.  */
  564. /*
  565.  * Define defaults for cleanup code.
  566.  * Note: Unless the build explicitly defines one of the following, then
  567.  * we default to standard C style cleanup. This style uses setjmp/longjmp
  568.  * in the cancelation and thread exit implementations and therefore won't
  569.  * do stack unwinding if linked to applications that have it (e.g.
  570.  * C++ apps). This is currently consistent with most/all commercial Unix
  571.  * POSIX threads implementations.
  572.  */
  573. #if !defined( __CLEANUP_SEH ) && !defined( __CLEANUP_CXX ) && !defined( __CLEANUP_C )
  574. # define __CLEANUP_C
  575. #endif
  576. #if defined( __CLEANUP_SEH ) && defined(__GNUC__)
  577. #error ERROR [__FILE__, line __LINE__]: GNUC does not support SEH.
  578. #endif
  579. typedef struct ptw32_cleanup_t ptw32_cleanup_t;
  580. typedef void (__cdecl *ptw32_cleanup_callback_t)(void *);
  581. struct ptw32_cleanup_t
  582. {
  583.   ptw32_cleanup_callback_t routine;
  584.   void *arg;
  585.   struct ptw32_cleanup_t *prev;
  586. };
  587. #ifdef __CLEANUP_SEH
  588. /*
  589.  * WIN32 SEH version of cancel cleanup.
  590.  */
  591. #define pthread_cleanup_push( _rout, _arg ) 
  592.     ptw32_cleanup_t _cleanup; 
  593.     
  594. _cleanup.routine = (ptw32_cleanup_callback_t)(_rout); 
  595.     _cleanup.arg = (_arg); 
  596.     __try 
  597.       { 
  598. #define pthread_cleanup_pop( _execute ) 
  599.       } 
  600.     __finally 
  601.     if( _execute || AbnormalTermination()) 
  602.       { 
  603.   (*(_cleanup.routine))( _cleanup.arg ); 
  604.       } 
  605. }
  606. #else /* __CLEANUP_SEH */
  607. #ifdef __CLEANUP_C
  608. /*
  609.  * C implementation of PThreads cancel cleanup
  610.  */
  611. #define pthread_cleanup_push( _rout, _arg ) 
  612.     ptw32_cleanup_t _cleanup; 
  613.     
  614.     ptw32_push_cleanup( &_cleanup, (ptw32_cleanup_callback_t) (_rout), (_arg) ); 
  615. #define pthread_cleanup_pop( _execute ) 
  616.     (void) ptw32_pop_cleanup( _execute ); 
  617. }
  618. #else /* __CLEANUP_C */
  619. #ifdef __CLEANUP_CXX
  620. /*
  621.  * C++ version of cancel cleanup.
  622.  * - John E. Bossom.
  623.  */
  624. class PThreadCleanup {
  625.   /*
  626.    * PThreadCleanup
  627.    *
  628.    * Purpose
  629.    *   This class is a C++ helper class that is
  630.    *   used to implement pthread_cleanup_push/
  631.    *   pthread_cleanup_pop.
  632.    *   The destructor of this class automatically
  633.    *   pops the pushed cleanup routine regardless
  634.    *   of how the code exits the scope
  635.    *   (i.e. such as by an exception)
  636.    */
  637.       ptw32_cleanup_callback_t cleanUpRout;
  638.   void   *   obj;
  639.   int   executeIt;
  640. public:
  641.   PThreadCleanup() :
  642.     cleanUpRout( 0 ),
  643.     obj( 0 ),
  644.     executeIt( 0 )
  645.     /*
  646.      * No cleanup performed
  647.      */
  648.     {
  649.     }
  650.   PThreadCleanup(
  651.      ptw32_cleanup_callback_t routine,
  652.  void  *  arg ) :
  653.     cleanUpRout( routine ),
  654.     obj( arg ),
  655.     executeIt( 1 )
  656.     /*
  657.      * Registers a cleanup routine for 'arg'
  658.      */
  659.     {
  660.     }
  661.   ~PThreadCleanup()
  662.     {
  663.       if ( executeIt && ((void *) cleanUpRout != (void *) 0) )
  664. {
  665.   (void) (*cleanUpRout)( obj );
  666. }
  667.     }
  668.   void execute( int exec )
  669.     {
  670.       executeIt = exec;
  671.     }
  672. };
  673. /*
  674.  * C++ implementation of PThreads cancel cleanup;
  675.  * This implementation takes advantage of a helper
  676.  * class who's destructor automatically calls the
  677.  * cleanup routine if we exit our scope weirdly
  678.  */
  679. #define pthread_cleanup_push( _rout, _arg ) 
  680.     PThreadCleanup  cleanup((ptw32_cleanup_callback_t)(_rout), 
  681.     (void *) (_arg) );
  682. #define pthread_cleanup_pop( _execute ) 
  683.     cleanup.execute( _execute ); 
  684. }
  685. #else
  686. #error ERROR [__FILE__, line __LINE__]: Cleanup type undefined.
  687. #endif /* __CLEANUP_CXX */
  688. #endif /* __CLEANUP_C */
  689. #endif /* __CLEANUP_SEH */
  690. /*
  691.  * ===============
  692.  * ===============
  693.  * Methods
  694.  * ===============
  695.  * ===============
  696.  */
  697. /*
  698.  * PThread Attribute Functions
  699.  */
  700. PTW32_DLLPORT int pthread_attr_init (pthread_attr_t * attr);
  701. PTW32_DLLPORT int pthread_attr_destroy (pthread_attr_t * attr);
  702. PTW32_DLLPORT int pthread_attr_getdetachstate (const pthread_attr_t * attr,
  703.  int *detachstate);
  704. PTW32_DLLPORT int pthread_attr_getstackaddr (const pthread_attr_t * attr,
  705.        void **stackaddr);
  706. PTW32_DLLPORT int pthread_attr_getstacksize (const pthread_attr_t * attr,
  707.        size_t * stacksize);
  708. PTW32_DLLPORT int pthread_attr_setdetachstate (pthread_attr_t * attr,
  709.  int detachstate);
  710. PTW32_DLLPORT int pthread_attr_setstackaddr (pthread_attr_t * attr,
  711.        void *stackaddr);
  712. PTW32_DLLPORT int pthread_attr_setstacksize (pthread_attr_t * attr,
  713.        size_t stacksize);
  714. PTW32_DLLPORT int pthread_attr_getschedparam (const pthread_attr_t *attr,
  715. struct sched_param *param);
  716. PTW32_DLLPORT int pthread_attr_setschedparam (pthread_attr_t *attr,
  717. const struct sched_param *param);
  718. PTW32_DLLPORT int pthread_attr_setschedpolicy (pthread_attr_t *,
  719.  int);
  720. PTW32_DLLPORT int pthread_attr_getschedpolicy (pthread_attr_t *,
  721.  int *);
  722. PTW32_DLLPORT int pthread_attr_setinheritsched(pthread_attr_t * attr,
  723.  int inheritsched);
  724. PTW32_DLLPORT int pthread_attr_getinheritsched(pthread_attr_t * attr,
  725.  int * inheritsched);
  726. PTW32_DLLPORT int pthread_attr_setscope (pthread_attr_t *,
  727.    int);
  728. PTW32_DLLPORT int pthread_attr_getscope (const pthread_attr_t *,
  729.    int *);
  730. /*
  731.  * PThread Functions
  732.  */
  733. PTW32_DLLPORT int pthread_create (pthread_t * tid,
  734.     const pthread_attr_t * attr,
  735.     void *(*start) (void *),
  736.     void *arg);
  737. PTW32_DLLPORT int pthread_detach (pthread_t tid);
  738. PTW32_DLLPORT int pthread_equal (pthread_t t1,
  739.    pthread_t t2);
  740. PTW32_DLLPORT void pthread_exit (void *value_ptr);
  741. PTW32_DLLPORT int pthread_join (pthread_t thread,
  742.   void **value_ptr);
  743. PTW32_DLLPORT pthread_t pthread_self (void);
  744. PTW32_DLLPORT int pthread_cancel (pthread_t thread);
  745. PTW32_DLLPORT int pthread_setcancelstate (int state,
  746.     int *oldstate);
  747. PTW32_DLLPORT int pthread_setcanceltype (int type,
  748.    int *oldtype);
  749. PTW32_DLLPORT void pthread_testcancel (void);
  750. PTW32_DLLPORT int pthread_once (pthread_once_t * once_control,
  751.   void (*init_routine) (void));
  752. #if PTW32_LEVEL >= PTW32_LEVEL_MAX
  753. PTW32_DLLPORT ptw32_cleanup_t *ptw32_pop_cleanup (int execute);
  754. PTW32_DLLPORT void ptw32_push_cleanup (ptw32_cleanup_t * cleanup,
  755.  void (*routine) (void *),
  756.  void *arg);
  757. #endif /* PTW32_LEVEL >= PTW32_LEVEL_MAX */
  758. /*
  759.  * Thread Specific Data Functions
  760.  */
  761. PTW32_DLLPORT int pthread_key_create (pthread_key_t * key,
  762. void (*destructor) (void *));
  763. PTW32_DLLPORT int pthread_key_delete (pthread_key_t key);
  764. PTW32_DLLPORT int pthread_setspecific (pthread_key_t key,
  765.  const void *value);
  766. PTW32_DLLPORT void *pthread_getspecific (pthread_key_t key);
  767. /*
  768.  * Mutex Attribute Functions
  769.  */
  770. PTW32_DLLPORT int pthread_mutexattr_init (pthread_mutexattr_t * attr);
  771. PTW32_DLLPORT int pthread_mutexattr_destroy (pthread_mutexattr_t * attr);
  772. PTW32_DLLPORT int pthread_mutexattr_getpshared (const pthread_mutexattr_t
  773.   * attr,
  774.   int *pshared);
  775. PTW32_DLLPORT int pthread_mutexattr_setpshared (pthread_mutexattr_t * attr,
  776.   int pshared);
  777. PTW32_DLLPORT int pthread_mutexattr_settype (pthread_mutexattr_t * attr, int kind);
  778. PTW32_DLLPORT int pthread_mutexattr_gettype (pthread_mutexattr_t * attr, int *kind);
  779. /*
  780.  * Barrier Attribute Functions
  781.  */
  782. PTW32_DLLPORT int pthread_barrierattr_init (pthread_barrierattr_t * attr);
  783. PTW32_DLLPORT int pthread_barrierattr_destroy (pthread_barrierattr_t * attr);
  784. PTW32_DLLPORT int pthread_barrierattr_getpshared (const pthread_barrierattr_t
  785.     * attr,
  786.     int *pshared);
  787. PTW32_DLLPORT int pthread_barrierattr_setpshared (pthread_barrierattr_t * attr,
  788.     int pshared);
  789. /*
  790.  * Mutex Functions
  791.  */
  792. PTW32_DLLPORT int pthread_mutex_init (pthread_mutex_t * mutex,
  793. const pthread_mutexattr_t * attr);
  794. PTW32_DLLPORT int pthread_mutex_destroy (pthread_mutex_t * mutex);
  795. PTW32_DLLPORT int pthread_mutex_lock (pthread_mutex_t * mutex);
  796. PTW32_DLLPORT int pthread_mutex_timedlock(pthread_mutex_t *mutex,
  797.     const struct timespec *abstime);
  798. PTW32_DLLPORT int pthread_mutex_trylock (pthread_mutex_t * mutex);
  799. PTW32_DLLPORT int pthread_mutex_unlock (pthread_mutex_t * mutex);
  800. /*
  801.  * Spinlock Functions
  802.  */
  803. PTW32_DLLPORT int pthread_spin_init (pthread_spinlock_t * lock, int pshared);
  804. PTW32_DLLPORT int pthread_spin_destroy (pthread_spinlock_t * lock);
  805. PTW32_DLLPORT int pthread_spin_lock (pthread_spinlock_t * lock);
  806. PTW32_DLLPORT int pthread_spin_trylock (pthread_spinlock_t * lock);
  807. PTW32_DLLPORT int pthread_spin_unlock (pthread_spinlock_t * lock);
  808. /*
  809.  * Barrier Functions
  810.  */
  811. PTW32_DLLPORT int pthread_barrier_init (pthread_barrier_t * barrier,
  812.   const pthread_barrierattr_t * attr,
  813.   unsigned int count);
  814. PTW32_DLLPORT int pthread_barrier_destroy (pthread_barrier_t * barrier);
  815. PTW32_DLLPORT int pthread_barrier_wait (pthread_barrier_t * barrier);
  816. /*
  817.  * Condition Variable Attribute Functions
  818.  */
  819. PTW32_DLLPORT int pthread_condattr_init (pthread_condattr_t * attr);
  820. PTW32_DLLPORT int pthread_condattr_destroy (pthread_condattr_t * attr);
  821. PTW32_DLLPORT int pthread_condattr_getpshared (const pthread_condattr_t * attr,
  822.  int *pshared);
  823. PTW32_DLLPORT int pthread_condattr_setpshared (pthread_condattr_t * attr,
  824.  int pshared);
  825. /*
  826.  * Condition Variable Functions
  827.  */
  828. PTW32_DLLPORT int pthread_cond_init (pthread_cond_t * cond,
  829.        const pthread_condattr_t * attr);
  830. PTW32_DLLPORT int pthread_cond_destroy (pthread_cond_t * cond);
  831. PTW32_DLLPORT int pthread_cond_wait (pthread_cond_t * cond,
  832.        pthread_mutex_t * mutex);
  833. PTW32_DLLPORT int pthread_cond_timedwait (pthread_cond_t * cond,
  834.     pthread_mutex_t * mutex,
  835.     const struct timespec *abstime);
  836. PTW32_DLLPORT int pthread_cond_signal (pthread_cond_t * cond);
  837. PTW32_DLLPORT int pthread_cond_broadcast (pthread_cond_t * cond);
  838. /*
  839.  * Scheduling
  840.  */
  841. PTW32_DLLPORT int pthread_setschedparam (pthread_t thread,
  842.    int policy,
  843.    const struct sched_param *param);
  844. PTW32_DLLPORT int pthread_getschedparam (pthread_t thread,
  845.    int *policy,
  846.    struct sched_param *param);
  847. PTW32_DLLPORT int pthread_setconcurrency (int);
  848.  
  849. PTW32_DLLPORT int pthread_getconcurrency (void);
  850. /*
  851.  * Read-Write Lock Functions
  852.  */
  853. PTW32_DLLPORT int pthread_rwlock_init(pthread_rwlock_t *lock,
  854. const pthread_rwlockattr_t *attr);
  855. PTW32_DLLPORT int pthread_rwlock_destroy(pthread_rwlock_t *lock);
  856. PTW32_DLLPORT int pthread_rwlock_tryrdlock(pthread_rwlock_t *);
  857. PTW32_DLLPORT int pthread_rwlock_trywrlock(pthread_rwlock_t *);
  858. PTW32_DLLPORT int pthread_rwlock_rdlock(pthread_rwlock_t *lock);
  859. PTW32_DLLPORT int pthread_rwlock_timedrdlock(pthread_rwlock_t *lock,
  860.        const struct timespec *abstime);
  861. PTW32_DLLPORT int pthread_rwlock_wrlock(pthread_rwlock_t *lock);
  862. PTW32_DLLPORT int pthread_rwlock_timedwrlock(pthread_rwlock_t *lock,
  863.        const struct timespec *abstime);
  864. PTW32_DLLPORT int pthread_rwlock_unlock(pthread_rwlock_t *lock);
  865. PTW32_DLLPORT int pthread_rwlockattr_init (pthread_rwlockattr_t * attr);
  866. PTW32_DLLPORT int pthread_rwlockattr_destroy (pthread_rwlockattr_t * attr);
  867. PTW32_DLLPORT int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t * attr,
  868.    int *pshared);
  869. PTW32_DLLPORT int pthread_rwlockattr_setpshared (pthread_rwlockattr_t * attr,
  870.    int pshared);
  871. #if PTW32_LEVEL >= PTW32_LEVEL_MAX - 1
  872. /*
  873.  * Non-portable functions
  874.  */
  875. /*
  876.  * Compatibility with Linux.
  877.  */
  878. PTW32_DLLPORT int pthread_mutexattr_setkind_np(pthread_mutexattr_t * attr,
  879.  int kind);
  880. PTW32_DLLPORT int pthread_mutexattr_getkind_np(pthread_mutexattr_t * attr,
  881.  int *kind);
  882. /*
  883.  * Possibly supported by other POSIX threads implementations
  884.  */
  885. PTW32_DLLPORT int pthread_delay_np (struct timespec * interval);
  886. PTW32_DLLPORT int pthread_num_processors_np(void);
  887. /*
  888.  * Useful if an application wants to statically link
  889.  * the lib rather than load the DLL at run-time.
  890.  */
  891. PTW32_DLLPORT int pthread_win32_process_attach_np(void);
  892. PTW32_DLLPORT int pthread_win32_process_detach_np(void);
  893. PTW32_DLLPORT int pthread_win32_thread_attach_np(void);
  894. PTW32_DLLPORT int pthread_win32_thread_detach_np(void);
  895. /*
  896.  * Register a system time change with the library.
  897.  * Causes the library to perform various functions
  898.  * in response to the change. Should be called whenever
  899.  * the application's top level window receives a
  900.  * WM_TIMECHANGE message. It can be passed directly to
  901.  * pthread_create() as a new thread if desired.
  902.  */
  903. PTW32_DLLPORT void * pthread_timechange_handler_np(void *);
  904. #endif /*PTW32_LEVEL >= PTW32_LEVEL_MAX - 1 */
  905. #if PTW32_LEVEL >= PTW32_LEVEL_MAX
  906. /*
  907.  * Returns the Win32 HANDLE for the POSIX thread.
  908.  */
  909. PTW32_DLLPORT HANDLE pthread_getw32threadhandle_np(pthread_t thread);
  910. /*
  911.  * Protected Methods
  912.  *
  913.  * This function blocks until the given WIN32 handle
  914.  * is signaled or pthread_cancel had been called.
  915.  * This function allows the caller to hook into the
  916.  * PThreads cancel mechanism. It is implemented using
  917.  *
  918.  * WaitForMultipleObjects
  919.  *
  920.  * on 'waitHandle' and a manually reset WIN32 Event
  921.  * used to implement pthread_cancel. The 'timeout'
  922.  * argument to TimedWait is simply passed to
  923.  * WaitForMultipleObjects.
  924.  */
  925. PTW32_DLLPORT int pthreadCancelableWait (HANDLE waitHandle);
  926. PTW32_DLLPORT int pthreadCancelableTimedWait (HANDLE waitHandle,
  927. DWORD timeout);
  928. #endif /* PTW32_LEVEL >= PTW32_LEVEL_MAX */
  929. /*
  930.  * Thread-Safe C Runtime Library Mappings.
  931.  */
  932. #ifndef _UWIN
  933. #  if defined(NEED_ERRNO)
  934.      PTW32_DLLPORT int * _errno( void );
  935. #  else
  936. #    ifndef errno
  937. #      if (defined(_MT) || defined(_DLL))
  938.  __declspec(dllimport) extern int * __cdecl _errno(void);
  939. #  define errno (*_errno())
  940. #      endif
  941. #    endif
  942. #  endif
  943. #endif
  944. /*
  945.  * WIN32 C runtime library had been made thread-safe
  946.  * without affecting the user interface. Provide
  947.  * mappings from the UNIX thread-safe versions to
  948.  * the standard C runtime library calls.
  949.  * Only provide function mappings for functions that
  950.  * actually exist on WIN32.
  951.  */
  952. #if !defined(__MINGW32__)
  953. #define strtok_r( _s, _sep, _lasts ) 
  954. ( *(_lasts) = strtok( (_s), (_sep) ) )
  955. #endif /* !__MINGW32__ */
  956. #define asctime_r( _tm, _buf ) 
  957. ( strcpy( (_buf), asctime( (_tm) ) ), 
  958.   (_buf) )
  959. #define ctime_r( _clock, _buf ) 
  960. ( strcpy( (_buf), ctime( (_clock) ) ),
  961.   (_buf) )
  962. #define gmtime_r( _clock, _result ) 
  963. ( *(_result) = *gmtime( (_clock) ), 
  964.   (_result) )
  965. #define localtime_r( _clock, _result ) 
  966. ( *(_result) = *localtime( (_clock) ), 
  967.   (_result) )
  968. #define rand_r( _seed ) 
  969. ( _seed == _seed? rand() : rand() )
  970. #ifdef __cplusplus
  971. /*
  972.  * Internal exceptions
  973.  */
  974. class ptw32_exception {};
  975. class ptw32_exception_cancel : public ptw32_exception {};
  976. class ptw32_exception_exit   : public ptw32_exception {};
  977. #endif
  978. #if PTW32_LEVEL >= PTW32_LEVEL_MAX
  979. /* FIXME: This is only required if the library was built using SEH */
  980. /*
  981.  * Get internal SEH tag
  982.  */
  983. PTW32_DLLPORT DWORD ptw32_get_exception_services_code(void);
  984. #endif /* PTW32_LEVEL >= PTW32_LEVEL_MAX */
  985. #ifndef PTW32_BUILD
  986. #ifdef __CLEANUP_SEH
  987. /*
  988.  * Redefine the SEH __except keyword to ensure that applications
  989.  * propagate our internal exceptions up to the library's internal handlers.
  990.  */
  991. #define __except( E ) 
  992. __except( ( GetExceptionCode() == ptw32_get_exception_services_code() ) 
  993.  ? EXCEPTION_CONTINUE_SEARCH : ( E ) )
  994. #endif /* __CLEANUP_SEH */
  995. #ifdef __CLEANUP_CXX
  996. /*
  997.  * Redefine the C++ catch keyword to ensure that applications
  998.  * propagate our internal exceptions up to the library's internal handlers.
  999.  */
  1000. #ifdef _MSC_VER
  1001. /*
  1002.  * WARNING: Replace any 'catch( ... )' with 'PtW32CatchAll'
  1003.  * if you want Pthread-Win32 cancelation and pthread_exit to work.
  1004.  */
  1005. #ifndef PtW32NoCatchWarn
  1006. #pragma message("Specify "/DPtW32NoCatchWarn" compiler flag to skip this message.")
  1007. #pragma message("------------------------------------------------------------------")
  1008. #pragma message("When compiling applications with MSVC++ and C++ exception handling:")
  1009. #pragma message("  Replace any 'catch( ... )' in routines called from POSIX threads")
  1010. #pragma message("  with 'PtW32CatchAll' or 'CATCHALL' if you want POSIX thread")
  1011. #pragma message("  cancelation and pthread_exit to work. For example:")
  1012. #pragma message("")
  1013. #pragma message("    #ifdef PtW32CatchAll")
  1014. #pragma message("      PtW32CatchAll")
  1015. #pragma message("    #else")
  1016. #pragma message("      catch(...)")
  1017. #pragma message("    #endif")
  1018. #pragma message("  {")
  1019. #pragma message("    /* Catchall block processing */")
  1020. #pragma message("  }")
  1021. #pragma message("------------------------------------------------------------------")
  1022. #endif
  1023. #define PtW32CatchAll 
  1024. catch( ptw32_exception & ) { throw; } 
  1025. catch( ... )
  1026. #else /* _MSC_VER */
  1027. #define catch( E ) 
  1028. catch( ptw32_exception & ) { throw; } 
  1029. catch( E )
  1030. #endif /* _MSC_VER */
  1031. #endif /* __CLEANUP_CXX */
  1032. #endif /* ! PTW32_BUILD */
  1033. #ifdef __cplusplus
  1034. } /* End of extern "C" */
  1035. #endif /* __cplusplus */
  1036. #ifdef PTW32__HANDLE_DEF
  1037. # undef HANDLE
  1038. #endif
  1039. #ifdef PTW32__DWORD_DEF
  1040. # undef DWORD
  1041. #endif
  1042. #undef PTW32_LEVEL
  1043. #undef PTW32_LEVEL_MAX
  1044. #endif /* PTHREAD_H */