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