pthread.h 42 KB

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