pthread.h 42 KB

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