pthread.h 42 KB

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