implement.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. /*
  2. * implement.h
  3. *
  4. * Definitions that don't need to be public.
  5. *
  6. * Keeps all the internals out of pthread.h
  7. *
  8. * --------------------------------------------------------------------------
  9. *
  10. * Pthreads-win32 - POSIX Threads Library for Win32
  11. * Copyright(C) 1998 John E. Bossom
  12. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  13. *
  14. * Contact Email: [email protected]
  15. *
  16. * The current list of contributors is contained
  17. * in the file CONTRIBUTORS included with the source
  18. * code distribution. The list can also be seen at the
  19. * following World Wide Web location:
  20. * http://sources.redhat.com/pthreads-win32/contributors.html
  21. *
  22. * This library is free software; you can redistribute it and/or
  23. * modify it under the terms of the GNU Lesser General Public
  24. * License as published by the Free Software Foundation; either
  25. * version 2 of the License, or (at your option) any later version.
  26. *
  27. * This library is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  30. * Lesser General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Lesser General Public
  33. * License along with this library in the file COPYING.LIB;
  34. * if not, write to the Free Software Foundation, Inc.,
  35. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  36. */
  37. #if !defined(_IMPLEMENT_H)
  38. #define _IMPLEMENT_H
  39. #if !defined(_WIN32_WINNT)
  40. #define _WIN32_WINNT 0x0400
  41. #endif
  42. #include <windows.h>
  43. /*
  44. * In case windows.h doesn't define it (e.g. WinCE perhaps)
  45. */
  46. #if defined(WINCE)
  47. typedef VOID (APIENTRY *PAPCFUNC)(DWORD dwParam);
  48. #endif
  49. /*
  50. * note: ETIMEDOUT is correctly defined in winsock.h
  51. */
  52. #include <winsock.h>
  53. /*
  54. * In case ETIMEDOUT hasn't been defined above somehow.
  55. */
  56. #if !defined(ETIMEDOUT)
  57. # define ETIMEDOUT 10060 /* This is the value in winsock.h. */
  58. #endif
  59. #if !defined(malloc)
  60. #include <malloc.h>
  61. #endif
  62. #if defined(__CLEANUP_C)
  63. # include <setjmp.h>
  64. #endif
  65. #if !defined(INT_MAX)
  66. #include <limits.h>
  67. #endif
  68. /* use local include files during development */
  69. #include "semaphore.h"
  70. #include "sched.h"
  71. #if defined(HAVE_C_INLINE) || defined(__cplusplus)
  72. #define INLINE inline
  73. #else
  74. #define INLINE
  75. #endif
  76. #if defined(_MSC_VER) && _MSC_VER < 1300
  77. /*
  78. * MSVC 6 does not use the "volatile" qualifier
  79. */
  80. #define PTW32_INTERLOCKED_VOLATILE
  81. #else
  82. #define PTW32_INTERLOCKED_VOLATILE volatile
  83. #endif
  84. #define PTW32_INTERLOCKED_LONG long
  85. #define PTW32_INTERLOCKED_SIZE size_t
  86. #define PTW32_INTERLOCKED_PVOID PVOID
  87. #define PTW32_INTERLOCKED_LONGPTR PTW32_INTERLOCKED_VOLATILE long*
  88. #define PTW32_INTERLOCKED_SIZEPTR PTW32_INTERLOCKED_VOLATILE size_t*
  89. #define PTW32_INTERLOCKED_PVOID_PTR PTW32_INTERLOCKED_VOLATILE PVOID*
  90. #if defined(__MINGW64__) || defined(__MINGW32__)
  91. # include <stdint.h>
  92. #elif defined(__BORLANDC__)
  93. # define int64_t ULONGLONG
  94. #else
  95. # define int64_t _int64
  96. # if defined(_MSC_VER) && _MSC_VER < 1300
  97. typedef long intptr_t;
  98. # endif
  99. #endif
  100. typedef enum
  101. {
  102. /*
  103. * This enumeration represents the state of the thread;
  104. * The thread is still "alive" if the numeric value of the
  105. * state is greater or equal "PThreadStateRunning".
  106. */
  107. PThreadStateInitial = 0, /* Thread not running */
  108. PThreadStateRunning, /* Thread alive & kicking */
  109. PThreadStateSuspended, /* Thread alive but suspended */
  110. PThreadStateCancelPending, /* Thread alive but */
  111. /* has cancelation pending. */
  112. PThreadStateCanceling, /* Thread alive but is */
  113. /* in the process of terminating */
  114. /* due to a cancellation request */
  115. PThreadStateExiting, /* Thread alive but exiting */
  116. /* due to an exception */
  117. PThreadStateLast, /* All handlers have been run and now */
  118. /* final cleanup can be done. */
  119. PThreadStateReuse /* In reuse pool. */
  120. }
  121. PThreadState;
  122. typedef struct ptw32_mcs_node_t_ ptw32_mcs_local_node_t;
  123. typedef struct ptw32_mcs_node_t_* ptw32_mcs_lock_t;
  124. typedef struct ptw32_robust_node_t_ ptw32_robust_node_t;
  125. typedef struct ptw32_thread_t_ ptw32_thread_t;
  126. struct ptw32_thread_t_
  127. {
  128. unsigned __int64 seqNumber; /* Process-unique thread sequence number */
  129. HANDLE threadH; /* Win32 thread handle - POSIX thread is invalid if threadH == 0 */
  130. pthread_t ptHandle; /* This thread's permanent pthread_t handle */
  131. ptw32_thread_t * prevReuse; /* Links threads on reuse stack */
  132. volatile PThreadState state;
  133. ptw32_mcs_lock_t threadLock; /* Used for serialised access to public thread state */
  134. ptw32_mcs_lock_t stateLock; /* Used for async-cancel safety */
  135. HANDLE cancelEvent;
  136. void *exitStatus;
  137. void *parms;
  138. void *keys;
  139. void *nextAssoc;
  140. #if defined(__CLEANUP_C)
  141. jmp_buf start_mark; /* Jump buffer follows void* so should be aligned */
  142. #endif /* __CLEANUP_C */
  143. #if defined(HAVE_SIGSET_T)
  144. sigset_t sigmask;
  145. #endif /* HAVE_SIGSET_T */
  146. ptw32_mcs_lock_t
  147. robustMxListLock; /* robustMxList lock */
  148. ptw32_robust_node_t*
  149. robustMxList; /* List of currenty held robust mutexes */
  150. int ptErrno;
  151. int detachState;
  152. int sched_priority; /* As set, not as currently is */
  153. int cancelState;
  154. int cancelType;
  155. int implicit:1;
  156. DWORD thread; /* Win32 thread ID */
  157. #if defined(_UWIN)
  158. DWORD dummy[5];
  159. #endif
  160. size_t align; /* Force alignment if this struct is packed */
  161. };
  162. /*
  163. * Special value to mark attribute objects as valid.
  164. */
  165. #define PTW32_ATTR_VALID ((unsigned long) 0xC4C0FFEE)
  166. struct pthread_attr_t_
  167. {
  168. unsigned long valid;
  169. void *stackaddr;
  170. size_t stacksize;
  171. int detachstate;
  172. struct sched_param param;
  173. int inheritsched;
  174. int contentionscope;
  175. #if defined(HAVE_SIGSET_T)
  176. sigset_t sigmask;
  177. #endif /* HAVE_SIGSET_T */
  178. };
  179. /*
  180. * ====================
  181. * ====================
  182. * Semaphores, Mutexes and Condition Variables
  183. * ====================
  184. * ====================
  185. */
  186. struct sem_t_
  187. {
  188. int value;
  189. pthread_mutex_t lock;
  190. HANDLE sem;
  191. #if defined(NEED_SEM)
  192. int leftToUnblock;
  193. #endif
  194. };
  195. #define PTW32_OBJECT_AUTO_INIT ((void *)(size_t) -1)
  196. #define PTW32_OBJECT_INVALID NULL
  197. struct pthread_mutex_t_
  198. {
  199. LONG lock_idx; /* Provides exclusive access to mutex state
  200. via the Interlocked* mechanism.
  201. 0: unlocked/free.
  202. 1: locked - no other waiters.
  203. -1: locked - with possible other waiters.
  204. */
  205. int recursive_count; /* Number of unlocks a thread needs to perform
  206. before the lock is released (recursive
  207. mutexes only). */
  208. int kind; /* Mutex type. */
  209. pthread_t ownerThread;
  210. HANDLE event; /* Mutex release notification to waiting
  211. threads. */
  212. ptw32_robust_node_t*
  213. robustNode; /* Extra state for robust mutexes */
  214. };
  215. enum ptw32_robust_state_t_
  216. {
  217. PTW32_ROBUST_CONSISTENT,
  218. PTW32_ROBUST_INCONSISTENT,
  219. PTW32_ROBUST_NOTRECOVERABLE
  220. };
  221. typedef enum ptw32_robust_state_t_ ptw32_robust_state_t;
  222. /*
  223. * Node used to manage per-thread lists of currently-held robust mutexes.
  224. */
  225. struct ptw32_robust_node_t_
  226. {
  227. pthread_mutex_t mx;
  228. ptw32_robust_state_t stateInconsistent;
  229. ptw32_robust_node_t* prev;
  230. ptw32_robust_node_t* next;
  231. };
  232. struct pthread_mutexattr_t_
  233. {
  234. int pshared;
  235. int kind;
  236. int robustness;
  237. };
  238. /*
  239. * Possible values, other than PTW32_OBJECT_INVALID,
  240. * for the "interlock" element in a spinlock.
  241. *
  242. * In this implementation, when a spinlock is initialised,
  243. * the number of cpus available to the process is checked.
  244. * If there is only one cpu then "interlock" is set equal to
  245. * PTW32_SPIN_USE_MUTEX and u.mutex is an initialised mutex.
  246. * If the number of cpus is greater than 1 then "interlock"
  247. * is set equal to PTW32_SPIN_UNLOCKED and the number is
  248. * stored in u.cpus. This arrangement allows the spinlock
  249. * routines to attempt an InterlockedCompareExchange on "interlock"
  250. * immediately and, if that fails, to try the inferior mutex.
  251. *
  252. * "u.cpus" isn't used for anything yet, but could be used at
  253. * some point to optimise spinlock behaviour.
  254. */
  255. #define PTW32_SPIN_INVALID (0)
  256. #define PTW32_SPIN_UNLOCKED (1)
  257. #define PTW32_SPIN_LOCKED (2)
  258. #define PTW32_SPIN_USE_MUTEX (3)
  259. struct pthread_spinlock_t_
  260. {
  261. long interlock; /* Locking element for multi-cpus. */
  262. union
  263. {
  264. int cpus; /* No. of cpus if multi cpus, or */
  265. pthread_mutex_t mutex; /* mutex if single cpu. */
  266. } u;
  267. };
  268. /*
  269. * MCS lock queue node - see ptw32_MCS_lock.c
  270. */
  271. struct ptw32_mcs_node_t_
  272. {
  273. struct ptw32_mcs_node_t_ **lock; /* ptr to tail of queue */
  274. struct ptw32_mcs_node_t_ *next; /* ptr to successor in queue */
  275. HANDLE readyFlag; /* set after lock is released by
  276. predecessor */
  277. HANDLE nextFlag; /* set after 'next' ptr is set by
  278. successor */
  279. };
  280. struct pthread_barrier_t_
  281. {
  282. unsigned int nCurrentBarrierHeight;
  283. unsigned int nInitialBarrierHeight;
  284. int pshared;
  285. sem_t semBarrierBreeched;
  286. ptw32_mcs_lock_t lock;
  287. ptw32_mcs_local_node_t proxynode;
  288. };
  289. struct pthread_barrierattr_t_
  290. {
  291. int pshared;
  292. };
  293. struct pthread_key_t_
  294. {
  295. DWORD key;
  296. void (PTW32_CDECL *destructor) (void *);
  297. ptw32_mcs_lock_t keyLock;
  298. void *threads;
  299. };
  300. typedef struct ThreadParms ThreadParms;
  301. struct ThreadParms
  302. {
  303. pthread_t tid;
  304. void *(PTW32_CDECL *start) (void *);
  305. void *arg;
  306. };
  307. struct pthread_cond_t_
  308. {
  309. long nWaitersBlocked; /* Number of threads blocked */
  310. long nWaitersGone; /* Number of threads timed out */
  311. long nWaitersToUnblock; /* Number of threads to unblock */
  312. sem_t semBlockQueue; /* Queue up threads waiting for the */
  313. /* condition to become signalled */
  314. sem_t semBlockLock; /* Semaphore that guards access to */
  315. /* | waiters blocked count/block queue */
  316. /* +-> Mandatory Sync.LEVEL-1 */
  317. pthread_mutex_t mtxUnblockLock; /* Mutex that guards access to */
  318. /* | waiters (to)unblock(ed) counts */
  319. /* +-> Optional* Sync.LEVEL-2 */
  320. pthread_cond_t next; /* Doubly linked list */
  321. pthread_cond_t prev;
  322. };
  323. struct pthread_condattr_t_
  324. {
  325. int pshared;
  326. };
  327. #define PTW32_RWLOCK_MAGIC 0xfacade2
  328. struct pthread_rwlock_t_
  329. {
  330. pthread_mutex_t mtxExclusiveAccess;
  331. pthread_mutex_t mtxSharedAccessCompleted;
  332. pthread_cond_t cndSharedAccessCompleted;
  333. int nSharedAccessCount;
  334. int nExclusiveAccessCount;
  335. int nCompletedSharedAccessCount;
  336. int nMagic;
  337. };
  338. struct pthread_rwlockattr_t_
  339. {
  340. int pshared;
  341. };
  342. typedef struct ThreadKeyAssoc ThreadKeyAssoc;
  343. struct ThreadKeyAssoc
  344. {
  345. /*
  346. * Purpose:
  347. * This structure creates an association between a thread and a key.
  348. * It is used to implement the implicit invocation of a user defined
  349. * destroy routine for thread specific data registered by a user upon
  350. * exiting a thread.
  351. *
  352. * Graphically, the arrangement is as follows, where:
  353. *
  354. * K - Key with destructor
  355. * (head of chain is key->threads)
  356. * T - Thread that has called pthread_setspecific(Kn)
  357. * (head of chain is thread->keys)
  358. * A - Association. Each association is a node at the
  359. * intersection of two doubly-linked lists.
  360. *
  361. * T1 T2 T3
  362. * | | |
  363. * | | |
  364. * K1 -----+-----A-----A----->
  365. * | | |
  366. * | | |
  367. * K2 -----A-----A-----+----->
  368. * | | |
  369. * | | |
  370. * K3 -----A-----+-----A----->
  371. * | | |
  372. * | | |
  373. * V V V
  374. *
  375. * Access to the association is guarded by two locks: the key's
  376. * general lock (guarding the row) and the thread's general
  377. * lock (guarding the column). This avoids the need for a
  378. * dedicated lock for each association, which not only consumes
  379. * more handles but requires that the lock resources persist
  380. * until both the key is deleted and the thread has called the
  381. * destructor. The two-lock arrangement allows those resources
  382. * to be freed as soon as either thread or key is concluded.
  383. *
  384. * To avoid deadlock, whenever both locks are required both the
  385. * key and thread locks are acquired consistently in the order
  386. * "key lock then thread lock". An exception to this exists
  387. * when a thread calls the destructors, however, this is done
  388. * carefully (but inelegantly) to avoid deadlock.
  389. *
  390. * An association is created when a thread first calls
  391. * pthread_setspecific() on a key that has a specified
  392. * destructor.
  393. *
  394. * An association is destroyed either immediately after the
  395. * thread calls the key destructor function on thread exit, or
  396. * when the key is deleted.
  397. *
  398. * Attributes:
  399. * thread
  400. * reference to the thread that owns the
  401. * association. This is actually the pointer to the
  402. * thread struct itself. Since the association is
  403. * destroyed before the thread exits, this can never
  404. * point to a different logical thread to the one that
  405. * created the assoc, i.e. after thread struct reuse.
  406. *
  407. * key
  408. * reference to the key that owns the association.
  409. *
  410. * nextKey
  411. * The pthread_t->keys attribute is the head of a
  412. * chain of associations that runs through the nextKey
  413. * link. This chain provides the 1 to many relationship
  414. * between a pthread_t and all pthread_key_t on which
  415. * it called pthread_setspecific.
  416. *
  417. * prevKey
  418. * Similarly.
  419. *
  420. * nextThread
  421. * The pthread_key_t->threads attribute is the head of
  422. * a chain of associations that runs through the
  423. * nextThreads link. This chain provides the 1 to many
  424. * relationship between a pthread_key_t and all the
  425. * PThreads that have called pthread_setspecific for
  426. * this pthread_key_t.
  427. *
  428. * prevThread
  429. * Similarly.
  430. *
  431. * Notes:
  432. * 1) As soon as either the key or the thread is no longer
  433. * referencing the association, it can be destroyed. The
  434. * association will be removed from both chains.
  435. *
  436. * 2) Under WIN32, an association is only created by
  437. * pthread_setspecific if the user provided a
  438. * destroyRoutine when they created the key.
  439. *
  440. *
  441. */
  442. ptw32_thread_t * thread;
  443. pthread_key_t key;
  444. ThreadKeyAssoc *nextKey;
  445. ThreadKeyAssoc *nextThread;
  446. ThreadKeyAssoc *prevKey;
  447. ThreadKeyAssoc *prevThread;
  448. };
  449. #if defined(__CLEANUP_SEH)
  450. /*
  451. * --------------------------------------------------------------
  452. * MAKE_SOFTWARE_EXCEPTION
  453. * This macro constructs a software exception code following
  454. * the same format as the standard Win32 error codes as defined
  455. * in WINERROR.H
  456. * Values are 32 bit values laid out as follows:
  457. *
  458. * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  459. * +---+-+-+-----------------------+-------------------------------+
  460. * |Sev|C|R| Facility | Code |
  461. * +---+-+-+-----------------------+-------------------------------+
  462. *
  463. * Severity Values:
  464. */
  465. #define SE_SUCCESS 0x00
  466. #define SE_INFORMATION 0x01
  467. #define SE_WARNING 0x02
  468. #define SE_ERROR 0x03
  469. #define MAKE_SOFTWARE_EXCEPTION( _severity, _facility, _exception ) \
  470. ( (DWORD) ( ( (_severity) << 30 ) | /* Severity code */ \
  471. ( 1 << 29 ) | /* MS=0, User=1 */ \
  472. ( 0 << 28 ) | /* Reserved */ \
  473. ( (_facility) << 16 ) | /* Facility Code */ \
  474. ( (_exception) << 0 ) /* Exception Code */ \
  475. ) )
  476. /*
  477. * We choose one specific Facility/Error code combination to
  478. * identify our software exceptions vs. WIN32 exceptions.
  479. * We store our actual component and error code within
  480. * the optional information array.
  481. */
  482. #define EXCEPTION_PTW32_SERVICES \
  483. MAKE_SOFTWARE_EXCEPTION( SE_ERROR, \
  484. PTW32_SERVICES_FACILITY, \
  485. PTW32_SERVICES_ERROR )
  486. #define PTW32_SERVICES_FACILITY 0xBAD
  487. #define PTW32_SERVICES_ERROR 0xDEED
  488. #endif /* __CLEANUP_SEH */
  489. /*
  490. * Services available through EXCEPTION_PTW32_SERVICES
  491. * and also used [as parameters to ptw32_throw()] as
  492. * generic exception selectors.
  493. */
  494. #define PTW32_EPS_EXIT (1)
  495. #define PTW32_EPS_CANCEL (2)
  496. /* Useful macros */
  497. #define PTW32_MAX(a,b) ((a)<(b)?(b):(a))
  498. #define PTW32_MIN(a,b) ((a)>(b)?(b):(a))
  499. /* Declared in pthread_cancel.c */
  500. extern DWORD (*ptw32_register_cancelation) (PAPCFUNC, HANDLE, DWORD);
  501. /* Thread Reuse stack bottom marker. Must not be NULL or any valid pointer to memory. */
  502. #define PTW32_THREAD_REUSE_EMPTY ((ptw32_thread_t *)(size_t) 1)
  503. extern int ptw32_processInitialized;
  504. extern ptw32_thread_t * ptw32_threadReuseTop;
  505. extern ptw32_thread_t * ptw32_threadReuseBottom;
  506. extern pthread_key_t ptw32_selfThreadKey;
  507. extern pthread_key_t ptw32_cleanupKey;
  508. extern pthread_cond_t ptw32_cond_list_head;
  509. extern pthread_cond_t ptw32_cond_list_tail;
  510. extern int ptw32_mutex_default_kind;
  511. extern unsigned __int64 ptw32_threadSeqNumber;
  512. extern int ptw32_concurrency;
  513. extern int ptw32_features;
  514. extern ptw32_mcs_lock_t ptw32_thread_reuse_lock;
  515. extern ptw32_mcs_lock_t ptw32_mutex_test_init_lock;
  516. extern ptw32_mcs_lock_t ptw32_cond_list_lock;
  517. extern ptw32_mcs_lock_t ptw32_cond_test_init_lock;
  518. extern ptw32_mcs_lock_t ptw32_rwlock_test_init_lock;
  519. extern ptw32_mcs_lock_t ptw32_spinlock_test_init_lock;
  520. #if defined(_UWIN)
  521. extern int pthread_count;
  522. #endif
  523. #if defined(__cplusplus)
  524. extern "C"
  525. {
  526. #endif /* __cplusplus */
  527. /*
  528. * =====================
  529. * =====================
  530. * Forward Declarations
  531. * =====================
  532. * =====================
  533. */
  534. int ptw32_is_attr (const pthread_attr_t * attr);
  535. int ptw32_cond_check_need_init (pthread_cond_t * cond);
  536. int ptw32_mutex_check_need_init (pthread_mutex_t * mutex);
  537. int ptw32_rwlock_check_need_init (pthread_rwlock_t * rwlock);
  538. int ptw32_robust_mutex_inherit(pthread_mutex_t * mutex);
  539. void ptw32_robust_mutex_add(pthread_mutex_t* mutex, pthread_t self);
  540. void ptw32_robust_mutex_remove(pthread_mutex_t* mutex, ptw32_thread_t* otp);
  541. DWORD
  542. ptw32_RegisterCancelation (PAPCFUNC callback,
  543. HANDLE threadH, DWORD callback_arg);
  544. int ptw32_processInitialize (void);
  545. void ptw32_processTerminate (void);
  546. void ptw32_threadDestroy (pthread_t tid);
  547. void ptw32_pop_cleanup_all (int execute);
  548. pthread_t ptw32_new (void);
  549. pthread_t ptw32_threadReusePop (void);
  550. void ptw32_threadReusePush (pthread_t thread);
  551. int ptw32_getprocessors (int *count);
  552. int ptw32_setthreadpriority (pthread_t thread, int policy, int priority);
  553. void ptw32_rwlock_cancelwrwait (void *arg);
  554. #if ! (defined (__MINGW64__) || defined(__MINGW32__)) || (defined(__MSVCRT__) && ! defined(__DMC__))
  555. unsigned __stdcall
  556. #else
  557. void
  558. #endif
  559. ptw32_threadStart (void *vthreadParms);
  560. void ptw32_callUserDestroyRoutines (pthread_t thread);
  561. int ptw32_tkAssocCreate (ptw32_thread_t * thread, pthread_key_t key);
  562. void ptw32_tkAssocDestroy (ThreadKeyAssoc * assoc);
  563. int ptw32_semwait (sem_t * sem);
  564. DWORD ptw32_relmillisecs (const struct timespec * abstime);
  565. void ptw32_mcs_lock_acquire (ptw32_mcs_lock_t * lock, ptw32_mcs_local_node_t * node);
  566. int ptw32_mcs_lock_try_acquire (ptw32_mcs_lock_t * lock, ptw32_mcs_local_node_t * node);
  567. void ptw32_mcs_lock_release (ptw32_mcs_local_node_t * node);
  568. void ptw32_mcs_node_transfer (ptw32_mcs_local_node_t * new_node, ptw32_mcs_local_node_t * old_node);
  569. #if defined(NEED_FTIME)
  570. void ptw32_timespec_to_filetime (const struct timespec *ts, FILETIME * ft);
  571. void ptw32_filetime_to_timespec (const FILETIME * ft, struct timespec *ts);
  572. #endif
  573. /* Declared in misc.c */
  574. #if defined(NEED_CALLOC)
  575. #define calloc(n, s) ptw32_calloc(n, s)
  576. void *ptw32_calloc (size_t n, size_t s);
  577. #endif
  578. /* Declared in private.c */
  579. #if defined(_MSC_VER)
  580. /*
  581. * Ignore the warning:
  582. * "C++ exception specification ignored except to indicate that
  583. * the function is not __declspec(nothrow)."
  584. */
  585. #pragma warning(disable:4290)
  586. #endif
  587. void ptw32_throw (DWORD exception)
  588. #if defined(__CLEANUP_CXX)
  589. throw(ptw32_exception_cancel,ptw32_exception_exit)
  590. #endif
  591. ;
  592. #if defined(__cplusplus)
  593. }
  594. #endif /* __cplusplus */
  595. #if defined(_UWIN_)
  596. # if defined(_MT)
  597. # if defined(__cplusplus)
  598. extern "C"
  599. {
  600. # endif
  601. _CRTIMP unsigned long __cdecl _beginthread (void (__cdecl *) (void *),
  602. unsigned, void *);
  603. _CRTIMP void __cdecl _endthread (void);
  604. _CRTIMP unsigned long __cdecl _beginthreadex (void *, unsigned,
  605. unsigned (__stdcall *) (void *),
  606. void *, unsigned, unsigned *);
  607. _CRTIMP void __cdecl _endthreadex (unsigned);
  608. # if defined(__cplusplus)
  609. }
  610. # endif
  611. # endif
  612. #else
  613. # include <process.h>
  614. # endif
  615. /*
  616. * Use intrinsic versions wherever possible. VC will do this
  617. * automatically where possible and GCC define these if available:
  618. * __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1
  619. * __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2
  620. * __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
  621. * __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
  622. * __GCC_HAVE_SYNC_COMPARE_AND_SWAP_16
  623. *
  624. * The full set of Interlocked intrinsics in GCC are (check versions):
  625. * type __sync_fetch_and_add (type *ptr, type value, ...)
  626. * type __sync_fetch_and_sub (type *ptr, type value, ...)
  627. * type __sync_fetch_and_or (type *ptr, type value, ...)
  628. * type __sync_fetch_and_and (type *ptr, type value, ...)
  629. * type __sync_fetch_and_xor (type *ptr, type value, ...)
  630. * type __sync_fetch_and_nand (type *ptr, type value, ...)
  631. * type __sync_add_and_fetch (type *ptr, type value, ...)
  632. * type __sync_sub_and_fetch (type *ptr, type value, ...)
  633. * type __sync_or_and_fetch (type *ptr, type value, ...)
  634. * type __sync_and_and_fetch (type *ptr, type value, ...)
  635. * type __sync_xor_and_fetch (type *ptr, type value, ...)
  636. * type __sync_nand_and_fetch (type *ptr, type value, ...)
  637. * bool __sync_bool_compare_and_swap (type *ptr, type oldval type newval, ...)
  638. * type __sync_val_compare_and_swap (type *ptr, type oldval type newval, ...)
  639. * __sync_synchronize (...) // Full memory barrier
  640. * type __sync_lock_test_and_set (type *ptr, type value, ...) // Acquire barrier
  641. * void __sync_lock_release (type *ptr, ...) // Release barrier
  642. *
  643. * These are all overloaded and take 1,2,4,8 byte scalar or pointer types.
  644. *
  645. * The above aren't available in Mingw32 as of gcc 4.5.2 so define our own.
  646. */
  647. #if defined(__GNUC__)
  648. # if defined(_WIN64)
  649. # define PTW32_INTERLOCKED_COMPARE_EXCHANGE_64(location, value, comparand) \
  650. ({ \
  651. __typeof (value) _result; \
  652. __asm__ __volatile__ \
  653. ( \
  654. "lock\n\t" \
  655. "cmpxchgq %2,(%1)" \
  656. :"=a" (_result) \
  657. :"r" (location), "r" (value), "a" (comparand) \
  658. :"memory", "cc"); \
  659. _result; \
  660. })
  661. # define PTW32_INTERLOCKED_EXCHANGE_64(location, value) \
  662. ({ \
  663. __typeof (value) _result; \
  664. __asm__ __volatile__ \
  665. ( \
  666. "xchgq %0,(%1)" \
  667. :"=r" (_result) \
  668. :"r" (location), "0" (value) \
  669. :"memory", "cc"); \
  670. _result; \
  671. })
  672. # define PTW32_INTERLOCKED_EXCHANGE_ADD_64(location, value) \
  673. ({ \
  674. __typeof (value) _result; \
  675. __asm__ __volatile__ \
  676. ( \
  677. "lock\n\t" \
  678. "xaddq %0,(%1)" \
  679. :"=r" (_result) \
  680. :"r" (location), "0" (value) \
  681. :"memory", "cc"); \
  682. _result; \
  683. })
  684. # define PTW32_INTERLOCKED_INCREMENT_64(location) \
  685. ({ \
  686. PTW32_INTERLOCKED_LONG _temp = 1; \
  687. __asm__ __volatile__ \
  688. ( \
  689. "lock\n\t" \
  690. "xaddq %0,(%1)" \
  691. :"+r" (_temp) \
  692. :"r" (location) \
  693. :"memory", "cc"); \
  694. ++_temp; \
  695. })
  696. # define PTW32_INTERLOCKED_DECREMENT_64(location) \
  697. ({ \
  698. PTW32_INTERLOCKED_LONG _temp = -1; \
  699. __asm__ __volatile__ \
  700. ( \
  701. "lock\n\t" \
  702. "xaddq %2,(%1)" \
  703. :"+r" (_temp) \
  704. :"r" (location) \
  705. :"memory", "cc"); \
  706. --_temp; \
  707. })
  708. #endif
  709. # define PTW32_INTERLOCKED_COMPARE_EXCHANGE_LONG(location, value, comparand) \
  710. ({ \
  711. __typeof (value) _result; \
  712. __asm__ __volatile__ \
  713. ( \
  714. "lock\n\t" \
  715. "cmpxchgl %2,(%1)" \
  716. :"=a" (_result) \
  717. :"r" (location), "r" (value), "a" (comparand) \
  718. :"memory", "cc"); \
  719. _result; \
  720. })
  721. # define PTW32_INTERLOCKED_EXCHANGE_LONG(location, value) \
  722. ({ \
  723. __typeof (value) _result; \
  724. __asm__ __volatile__ \
  725. ( \
  726. "xchgl %0,(%1)" \
  727. :"=r" (_result) \
  728. :"r" (location), "0" (value) \
  729. :"memory", "cc"); \
  730. _result; \
  731. })
  732. # define PTW32_INTERLOCKED_EXCHANGE_ADD_LONG(location, value) \
  733. ({ \
  734. __typeof (value) _result; \
  735. __asm__ __volatile__ \
  736. ( \
  737. "lock\n\t" \
  738. "xaddl %0,(%1)" \
  739. :"=r" (_result) \
  740. :"r" (location), "0" (value) \
  741. :"memory", "cc"); \
  742. _result; \
  743. })
  744. # define PTW32_INTERLOCKED_INCREMENT_LONG(location) \
  745. ({ \
  746. PTW32_INTERLOCKED_LONG _temp = 1; \
  747. __asm__ __volatile__ \
  748. ( \
  749. "lock\n\t" \
  750. "xaddl %0,(%1)" \
  751. :"+r" (_temp) \
  752. :"r" (location) \
  753. :"memory", "cc"); \
  754. ++_temp; \
  755. })
  756. # define PTW32_INTERLOCKED_DECREMENT_LONG(location) \
  757. ({ \
  758. PTW32_INTERLOCKED_LONG _temp = -1; \
  759. __asm__ __volatile__ \
  760. ( \
  761. "lock\n\t" \
  762. "xaddl %0,(%1)" \
  763. :"+r" (_temp) \
  764. :"r" (location) \
  765. :"memory", "cc"); \
  766. --_temp; \
  767. })
  768. # define PTW32_INTERLOCKED_COMPARE_EXCHANGE_PTR(location, value, comparand) \
  769. PTW32_INTERLOCKED_COMPARE_EXCHANGE_SIZE((PTW32_INTERLOCKED_SIZEPTR)location, \
  770. (PTW32_INTERLOCKED_SIZE)value, \
  771. (PTW32_INTERLOCKED_SIZE)comparand)
  772. # define PTW32_INTERLOCKED_EXCHANGE_PTR(location, value) \
  773. PTW32_INTERLOCKED_EXCHANGE_SIZE((PTW32_INTERLOCKED_SIZEPTR)location, \
  774. (PTW32_INTERLOCKED_SIZE)value)
  775. #else
  776. # if defined(_WIN64)
  777. # define PTW32_INTERLOCKED_COMPARE_EXCHANGE_64 InterlockedCompareExchange64
  778. # define PTW32_INTERLOCKED_EXCHANGE_64 InterlockedExchange64
  779. # define PTW32_INTERLOCKED_EXCHANGE_ADD_64 InterlockedExchangeAdd64
  780. # define PTW32_INTERLOCKED_INCREMENT_64 InterlockedIncrement64
  781. # define PTW32_INTERLOCKED_DECREMENT_64 InterlockedDecrement64
  782. # endif
  783. # if defined(_MSC_VER) && _MSC_VER < 1300 && !defined(_WIN64) /* MSVC 6 */
  784. # define PTW32_INTERLOCKED_COMPARE_EXCHANGE_LONG(location, value, comparand) \
  785. ((LONG)InterlockedCompareExchange((PVOID *)(location), (PVOID)(value), (PVOID)(comparand)))
  786. # else
  787. # define PTW32_INTERLOCKED_COMPARE_EXCHANGE_LONG InterlockedCompareExchange
  788. # endif
  789. # define PTW32_INTERLOCKED_EXCHANGE_LONG InterlockedExchange
  790. # define PTW32_INTERLOCKED_EXCHANGE_ADD_LONG InterlockedExchangeAdd
  791. # define PTW32_INTERLOCKED_INCREMENT_LONG InterlockedIncrement
  792. # define PTW32_INTERLOCKED_DECREMENT_LONG InterlockedDecrement
  793. # if defined(_MSC_VER) && _MSC_VER < 1300 && !defined(_WIN64) /* MSVC 6 */
  794. # define PTW32_INTERLOCKED_COMPARE_EXCHANGE_PTR InterlockedCompareExchange
  795. # define PTW32_INTERLOCKED_EXCHANGE_PTR(location, value) \
  796. ((PVOID)InterlockedExchange((LPLONG)(location), (LONG)(value)))
  797. # else
  798. # define PTW32_INTERLOCKED_COMPARE_EXCHANGE_PTR InterlockedCompareExchangePointer
  799. # define PTW32_INTERLOCKED_EXCHANGE_PTR InterlockedExchangePointer
  800. # endif
  801. #endif
  802. #if defined(_WIN64)
  803. # define PTW32_INTERLOCKED_COMPARE_EXCHANGE_SIZE PTW32_INTERLOCKED_COMPARE_EXCHANGE_64
  804. # define PTW32_INTERLOCKED_EXCHANGE_SIZE PTW32_INTERLOCKED_EXCHANGE_64
  805. # define PTW32_INTERLOCKED_EXCHANGE_ADD_SIZE PTW32_INTERLOCKED_EXCHANGE_ADD_64
  806. # define PTW32_INTERLOCKED_INCREMENT_SIZE PTW32_INTERLOCKED_INCREMENT_64
  807. # define PTW32_INTERLOCKED_DECREMENT_SIZE PTW32_INTERLOCKED_DECREMENT_64
  808. #else
  809. # define PTW32_INTERLOCKED_COMPARE_EXCHANGE_SIZE PTW32_INTERLOCKED_COMPARE_EXCHANGE_LONG
  810. # define PTW32_INTERLOCKED_EXCHANGE_SIZE PTW32_INTERLOCKED_EXCHANGE_LONG
  811. # define PTW32_INTERLOCKED_EXCHANGE_ADD_SIZE PTW32_INTERLOCKED_EXCHANGE_ADD_LONG
  812. # define PTW32_INTERLOCKED_INCREMENT_SIZE PTW32_INTERLOCKED_INCREMENT_LONG
  813. # define PTW32_INTERLOCKED_DECREMENT_SIZE PTW32_INTERLOCKED_DECREMENT_LONG
  814. #endif
  815. #if defined(NEED_CREATETHREAD)
  816. /*
  817. * Macro uses args so we can cast start_proc to LPTHREAD_START_ROUTINE
  818. * in order to avoid warnings because of return type
  819. */
  820. #define _beginthreadex(security, \
  821. stack_size, \
  822. start_proc, \
  823. arg, \
  824. flags, \
  825. pid) \
  826. CreateThread(security, \
  827. stack_size, \
  828. (LPTHREAD_START_ROUTINE) start_proc, \
  829. arg, \
  830. flags, \
  831. pid)
  832. #define _endthreadex ExitThread
  833. #endif /* NEED_CREATETHREAD */
  834. #endif /* _IMPLEMENT_H */