pthread.h 43 KB

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