pthread.h 42 KB

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