pthread.h 39 KB

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