pthread.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. /* This is an implementation of the threads API of POSIX 1003.1-2001.
  2. *
  3. * --------------------------------------------------------------------------
  4. *
  5. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  6. * Copyright(C) 2008 Jason Schmidlapp
  7. *
  8. * Contact Email: jschmidlapp@users.sourceforge.net
  9. *
  10. *
  11. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  12. * Copyright(C) 2008 Jason Schmidlapp
  13. *
  14. * Contact Email: jschmidlapp@users.sourceforge.net
  15. *
  16. *
  17. * Based upon Pthreads-win32 - POSIX Threads Library for Win32
  18. * Copyright(C) 1998 John E. Bossom
  19. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  20. *
  21. * Contact Email: rpj@callisto.canberra.edu.au
  22. *
  23. * The original list of contributors to the Pthreads-win32 project
  24. * is contained in the file CONTRIBUTORS.ptw32 included with the
  25. * source code distribution. The list can also be seen at the
  26. * following World Wide Web location:
  27. * http://sources.redhat.com/pthreads-win32/contributors.html
  28. *
  29. * This library is free software; you can redistribute it and/or
  30. * modify it under the terms of the GNU Lesser General Public
  31. * License as published by the Free Software Foundation; either
  32. * version 2 of the License, or (at your option) any later version.
  33. *
  34. * This library is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  37. * Lesser General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU Lesser General Public
  40. * License along with this library in the file COPYING.LIB;
  41. * if not, write to the Free Software Foundation, Inc.,
  42. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  43. */
  44. #if !defined( PTHREAD_H )
  45. #define PTHREAD_H
  46. #include <pte_types.h>
  47. #include <sched.h>
  48. #define PTE_VERSION 2,8,0,0
  49. #define PTE_VERSION_STRING "2, 8, 0, 0\0"
  50. /* There are two implementations of cancel cleanup.
  51. * Note that pthread.h is included in both application
  52. * compilation units and also internally for the library.
  53. * The code here and within the library aims to work
  54. * for all reasonable combinations of environments.
  55. *
  56. * The two implementations are:
  57. *
  58. * C
  59. * C++
  60. *
  61. */
  62. /*
  63. * Define defaults for cleanup code.
  64. * Note: Unless the build explicitly defines one of the following, then
  65. * we default to standard C style cleanup. This style uses setjmp/longjmp
  66. * in the cancelation and thread exit implementations and therefore won't
  67. * do stack unwinding if linked to applications that have it (e.g.
  68. * C++ apps). This is currently consistent with most/all commercial Unix
  69. * POSIX threads implementations.
  70. */
  71. #if !defined( PTE_CLEANUP_CXX ) && !defined( PTE_CLEANUP_C )
  72. # define PTE_CLEANUP_C
  73. #endif
  74. #undef PTE_LEVEL
  75. #if defined(_POSIX_SOURCE)
  76. #define PTE_LEVEL 0
  77. /* Early POSIX */
  78. #endif
  79. #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309
  80. #undef PTE_LEVEL
  81. #define PTE_LEVEL 1
  82. /* Include 1b, 1c and 1d */
  83. #endif
  84. #if defined(INCLUDE_NP)
  85. #undef PTE_LEVEL
  86. #define PTE_LEVEL 2
  87. /* Include Non-Portable extensions */
  88. #endif
  89. #define PTE_LEVEL_MAX 3
  90. #if !defined(PTE_LEVEL)
  91. #define PTE_LEVEL PTE_LEVEL_MAX
  92. /* Include everything */
  93. #endif
  94. /*
  95. * -------------------------------------------------------------
  96. *
  97. *
  98. * Module: pthread.h
  99. *
  100. * Purpose:
  101. * Provides an implementation of PThreads based upon the
  102. * standard:
  103. *
  104. * POSIX 1003.1-2001
  105. * and
  106. * The Single Unix Specification version 3
  107. *
  108. * (these two are equivalent)
  109. *
  110. * in order to enhance code portability between Windows,
  111. * various commercial Unix implementations, and Linux.
  112. *
  113. * See the ANNOUNCE file for a full list of conforming
  114. * routines and defined constants, and a list of missing
  115. * routines and constants not defined in this implementation.
  116. *
  117. * Authors:
  118. * There have been many contributors to this library.
  119. * The initial implementation was contributed by
  120. * John Bossom, and several others have provided major
  121. * sections or revisions of parts of the implementation.
  122. * Often significant effort has been contributed to
  123. * find and fix important bugs and other problems to
  124. * improve the reliability of the library, which sometimes
  125. * is not reflected in the amount of code which changed as
  126. * result.
  127. * As much as possible, the contributors are acknowledged
  128. * in the ChangeLog file in the source code distribution
  129. * where their changes are noted in detail.
  130. *
  131. * Contributors are listed in the CONTRIBUTORS file.
  132. *
  133. * As usual, all bouquets go to the contributors, and all
  134. * brickbats go to the project maintainer.
  135. *
  136. * Maintainer:
  137. * The code base for this project is coordinated and
  138. * eventually pre-tested, packaged, and made available by
  139. *
  140. * Ross Johnson <rpj@callisto.canberra.edu.au>
  141. *
  142. * QA Testers:
  143. * Ultimately, the library is tested in the real world by
  144. * a host of competent and demanding scientists and
  145. * engineers who report bugs and/or provide solutions
  146. * which are then fixed or incorporated into subsequent
  147. * versions of the library. Each time a bug is fixed, a
  148. * test case is written to prove the fix and ensure
  149. * that later changes to the code don't reintroduce the
  150. * same error. The number of test cases is slowly growing
  151. * and therefore so is the code reliability.
  152. *
  153. * Compliance:
  154. * See the file ANNOUNCE for the list of implemented
  155. * and not-implemented routines and defined options.
  156. * Of course, these are all defined is this file as well.
  157. *
  158. * Web site:
  159. * The source code and other information about this library
  160. * are available from
  161. *
  162. * http://sources.redhat.com/pthreads-win32/
  163. *
  164. * -------------------------------------------------------------
  165. */
  166. #include <time.h>
  167. #include <setjmp.h>
  168. #include <limits.h>
  169. /*
  170. * Boolean values to make us independent of system includes.
  171. */
  172. enum
  173. {
  174. PTE_FALSE = 0,
  175. PTE_TRUE = (! PTE_FALSE)
  176. };
  177. /*
  178. * -------------------------------------------------------------
  179. *
  180. * POSIX 1003.1-2001 Options
  181. * =========================
  182. *
  183. * Options are normally set in <unistd.h>, which is not provided
  184. * with pthreads-embedded.
  185. *
  186. * For conformance with the Single Unix Specification (version 3), all of the
  187. * options below are defined, and have a value of either -1 (not supported)
  188. * or 200112L (supported).
  189. *
  190. * These options can neither be left undefined nor have a value of 0, because
  191. * either indicates that sysconf(), which is not implemented, may be used at
  192. * runtime to check the status of the option.
  193. *
  194. * _POSIX_THREADS (== 200112L)
  195. * If == 200112L, you can use threads
  196. *
  197. * _POSIX_THREAD_ATTR_STACKSIZE (== 200112L)
  198. * If == 200112L, you can control the size of a thread's
  199. * stack
  200. * pthread_attr_getstacksize
  201. * pthread_attr_setstacksize
  202. *
  203. * _POSIX_THREAD_ATTR_STACKADDR (== -1)
  204. * If == 200112L, you can allocate and control a thread's
  205. * stack. If not supported, the following functions
  206. * will return ENOSYS, indicating they are not
  207. * supported:
  208. * pthread_attr_getstackaddr
  209. * pthread_attr_setstackaddr
  210. *
  211. * _POSIX_THREAD_PRIORITY_SCHEDULING (== -1)
  212. * If == 200112L, you can use realtime scheduling.
  213. * This option indicates that the behaviour of some
  214. * implemented functions conforms to the additional TPS
  215. * requirements in the standard. E.g. rwlocks favour
  216. * writers over readers when threads have equal priority.
  217. *
  218. * _POSIX_THREAD_PRIO_INHERIT (== -1)
  219. * If == 200112L, you can create priority inheritance
  220. * mutexes.
  221. * pthread_mutexattr_getprotocol +
  222. * pthread_mutexattr_setprotocol +
  223. *
  224. * _POSIX_THREAD_PRIO_PROTECT (== -1)
  225. * If == 200112L, you can create priority ceiling mutexes
  226. * Indicates the availability of:
  227. * pthread_mutex_getprioceiling
  228. * pthread_mutex_setprioceiling
  229. * pthread_mutexattr_getprioceiling
  230. * pthread_mutexattr_getprotocol +
  231. * pthread_mutexattr_setprioceiling
  232. * pthread_mutexattr_setprotocol +
  233. *
  234. * _POSIX_THREAD_PROCESS_SHARED (== -1)
  235. * If set, you can create mutexes and condition
  236. * variables that can be shared with another
  237. * process.If set, indicates the availability
  238. * of:
  239. * pthread_mutexattr_getpshared
  240. * pthread_mutexattr_setpshared
  241. * pthread_condattr_getpshared
  242. * pthread_condattr_setpshared
  243. *
  244. * _POSIX_THREAD_SAFE_FUNCTIONS (== 200112L)
  245. * If == 200112L you can use the special *_r library
  246. * functions that provide thread-safe behaviour
  247. *
  248. * _POSIX_READER_WRITER_LOCKS (== 200112L)
  249. * If == 200112L, you can use read/write locks
  250. *
  251. * _POSIX_SPIN_LOCKS (== 200112L)
  252. * If == 200112L, you can use spin locks
  253. *
  254. * _POSIX_BARRIERS (== 200112L)
  255. * If == 200112L, you can use barriers
  256. *
  257. * + These functions provide both 'inherit' and/or
  258. * 'protect' protocol, based upon these macro
  259. * settings.
  260. *
  261. * -------------------------------------------------------------
  262. */
  263. /*
  264. * POSIX Options
  265. */
  266. #undef _POSIX_THREADS
  267. #define _POSIX_THREADS 200112L
  268. #undef _POSIX_READER_WRITER_LOCKS
  269. #define _POSIX_READER_WRITER_LOCKS 200112L
  270. #undef _POSIX_SPIN_LOCKS
  271. #define _POSIX_SPIN_LOCKS 200112L
  272. #undef _POSIX_BARRIERS
  273. #define _POSIX_BARRIERS 200112L
  274. #undef _POSIX_THREAD_SAFE_FUNCTIONS
  275. #define _POSIX_THREAD_SAFE_FUNCTIONS 200112L
  276. #undef _POSIX_THREAD_ATTR_STACKSIZE
  277. #define _POSIX_THREAD_ATTR_STACKSIZE 200112L
  278. /*
  279. * The following options are not supported
  280. */
  281. #undef _POSIX_THREAD_ATTR_STACKADDR
  282. #define _POSIX_THREAD_ATTR_STACKADDR -1
  283. #undef _POSIX_THREAD_PRIO_INHERIT
  284. #define _POSIX_THREAD_PRIO_INHERIT -1
  285. #undef _POSIX_THREAD_PRIO_PROTECT
  286. #define _POSIX_THREAD_PRIO_PROTECT -1
  287. /* TPS is not fully supported. */
  288. #undef _POSIX_THREAD_PRIORITY_SCHEDULING
  289. #define _POSIX_THREAD_PRIORITY_SCHEDULING -1
  290. #undef _POSIX_THREAD_PROCESS_SHARED
  291. #define _POSIX_THREAD_PROCESS_SHARED -1
  292. /*
  293. * POSIX 1003.1-2001 Limits
  294. * ===========================
  295. *
  296. * These limits are normally set in <limits.h>, which is not provided with
  297. * pthreads-embedded.
  298. *
  299. * PTHREAD_DESTRUCTOR_ITERATIONS
  300. * Maximum number of attempts to destroy
  301. * a thread's thread-specific data on
  302. * termination (must be at least 4)
  303. *
  304. * PTHREAD_KEYS_MAX
  305. * Maximum number of thread-specific data keys
  306. * available per process (must be at least 128)
  307. *
  308. * PTHREAD_STACK_MIN
  309. * Minimum supported stack size for a thread
  310. *
  311. * PTHREAD_THREADS_MAX
  312. * Maximum number of threads supported per
  313. * process (must be at least 64).
  314. *
  315. * SEM_NSEMS_MAX
  316. * The maximum number of semaphores a process can have.
  317. * (must be at least 256)
  318. *
  319. * SEM_VALUE_MAX
  320. * The maximum value a semaphore can have.
  321. * (must be at least 32767)
  322. *
  323. */
  324. #undef _POSIX_THREAD_DESTRUCTOR_ITERATIONS
  325. #define _POSIX_THREAD_DESTRUCTOR_ITERATIONS 4
  326. #undef PTHREAD_DESTRUCTOR_ITERATIONS
  327. #define PTHREAD_DESTRUCTOR_ITERATIONS _POSIX_THREAD_DESTRUCTOR_ITERATIONS
  328. #undef _POSIX_THREAD_KEYS_MAX
  329. #define _POSIX_THREAD_KEYS_MAX 128
  330. #undef PTHREAD_KEYS_MAX
  331. #define PTHREAD_KEYS_MAX _POSIX_THREAD_KEYS_MAX
  332. #undef PTHREAD_STACK_MIN
  333. #define PTHREAD_STACK_MIN 0
  334. #undef _POSIX_THREAD_THREADS_MAX
  335. #define _POSIX_THREAD_THREADS_MAX 64
  336. /* Arbitrary value */
  337. #undef PTHREAD_THREADS_MAX
  338. #define PTHREAD_THREADS_MAX 2019
  339. #undef _POSIX_SEM_NSEMS_MAX
  340. #define _POSIX_SEM_NSEMS_MAX 256
  341. /* Arbitrary value */
  342. #undef SEM_NSEMS_MAX
  343. #define SEM_NSEMS_MAX 1024
  344. #undef _POSIX_SEM_VALUE_MAX
  345. #define _POSIX_SEM_VALUE_MAX 32767
  346. #undef SEM_VALUE_MAX
  347. #define SEM_VALUE_MAX INT_MAX
  348. typedef void * pthread_t;
  349. typedef struct pthread_attr_t_ * pthread_attr_t;
  350. typedef struct pthread_once_t_ pthread_once_t;
  351. typedef struct pthread_key_t_ * pthread_key_t;
  352. typedef struct pthread_mutex_t_ * pthread_mutex_t;
  353. typedef struct pthread_mutexattr_t_ * pthread_mutexattr_t;
  354. typedef struct pthread_cond_t_ * pthread_cond_t;
  355. typedef struct pthread_condattr_t_ * pthread_condattr_t;
  356. typedef struct pthread_rwlock_t_ * pthread_rwlock_t;
  357. typedef struct pthread_rwlockattr_t_ * pthread_rwlockattr_t;
  358. typedef struct pthread_spinlock_t_ * pthread_spinlock_t;
  359. typedef struct pthread_barrier_t_ * pthread_barrier_t;
  360. typedef struct pthread_barrierattr_t_ * pthread_barrierattr_t;
  361. /*
  362. * ====================
  363. * ====================
  364. * POSIX Threads
  365. * ====================
  366. * ====================
  367. */
  368. enum
  369. {
  370. /*
  371. * pthread_attr_{get,set}detachstate
  372. */
  373. PTHREAD_CREATE_JOINABLE = 0, /* Default */
  374. PTHREAD_CREATE_DETACHED = 1,
  375. /*
  376. * pthread_attr_{get,set}inheritsched
  377. */
  378. PTHREAD_INHERIT_SCHED = 0,
  379. PTHREAD_EXPLICIT_SCHED = 1, /* Default */
  380. /*
  381. * pthread_{get,set}scope
  382. */
  383. PTHREAD_SCOPE_PROCESS = 0,
  384. PTHREAD_SCOPE_SYSTEM = 1, /* Default */
  385. PTHREAD_SCOPE_PROCESS_VFPU = 2, /* PSP specific */
  386. /*
  387. * pthread_setcancelstate paramters
  388. */
  389. PTHREAD_CANCEL_ENABLE = 0, /* Default */
  390. PTHREAD_CANCEL_DISABLE = 1,
  391. /*
  392. * pthread_setcanceltype parameters
  393. */
  394. PTHREAD_CANCEL_ASYNCHRONOUS = 0,
  395. PTHREAD_CANCEL_DEFERRED = 1, /* Default */
  396. /*
  397. * pthread_mutexattr_{get,set}pshared
  398. * pthread_condattr_{get,set}pshared
  399. */
  400. PTHREAD_PROCESS_PRIVATE = 0,
  401. PTHREAD_PROCESS_SHARED = 1,
  402. /*
  403. * pthread_barrier_wait
  404. */
  405. PTHREAD_BARRIER_SERIAL_THREAD = -1
  406. };
  407. /*
  408. * ====================
  409. * ====================
  410. * Cancelation
  411. * ====================
  412. * ====================
  413. */
  414. #define PTHREAD_CANCELED ((void *) -1)
  415. /*
  416. * ====================
  417. * ====================
  418. * Once Key
  419. * ====================
  420. * ====================
  421. */
  422. #define PTHREAD_ONCE_INIT { PTE_FALSE, 0, 0, 0}
  423. struct pthread_once_t_
  424. {
  425. int state;
  426. void * semaphore;
  427. int numSemaphoreUsers;
  428. int done; /* indicates if user function has been executed */
  429. // void * lock;
  430. // int reserved1;
  431. // int reserved2;
  432. };
  433. /*
  434. * ====================
  435. * ====================
  436. * Object initialisers
  437. * ====================
  438. * ====================
  439. */
  440. #define PTHREAD_MUTEX_INITIALIZER ((pthread_mutex_t) -1)
  441. #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER ((pthread_mutex_t) -2)
  442. #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER ((pthread_mutex_t) -3)
  443. /*
  444. * Compatibility with LinuxThreads
  445. */
  446. #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP PTHREAD_RECURSIVE_MUTEX_INITIALIZER
  447. #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP PTHREAD_ERRORCHECK_MUTEX_INITIALIZER
  448. #define PTHREAD_COND_INITIALIZER ((pthread_cond_t) -1)
  449. #define PTHREAD_RWLOCK_INITIALIZER ((pthread_rwlock_t) -1)
  450. #define PTHREAD_SPINLOCK_INITIALIZER ((pthread_spinlock_t) -1)
  451. /*
  452. * Mutex types.
  453. */
  454. enum
  455. {
  456. /* Compatibility with LinuxThreads */
  457. PTHREAD_MUTEX_FAST_NP,
  458. PTHREAD_MUTEX_RECURSIVE_NP,
  459. PTHREAD_MUTEX_ERRORCHECK_NP,
  460. PTHREAD_MUTEX_TIMED_NP = PTHREAD_MUTEX_FAST_NP,
  461. PTHREAD_MUTEX_ADAPTIVE_NP = PTHREAD_MUTEX_FAST_NP,
  462. /* For compatibility with POSIX */
  463. PTHREAD_MUTEX_NORMAL = PTHREAD_MUTEX_FAST_NP,
  464. PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP,
  465. PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP,
  466. PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
  467. };
  468. typedef struct pte_cleanup_t pte_cleanup_t;
  469. typedef void (* pte_cleanup_callback_t)(void *);
  470. struct pte_cleanup_t
  471. {
  472. pte_cleanup_callback_t routine;
  473. void *arg;
  474. struct pte_cleanup_t *prev;
  475. };
  476. #ifdef PTE_CLEANUP_C
  477. /*
  478. * C implementation of PThreads cancel cleanup
  479. */
  480. #define pthread_cleanup_push( _rout, _arg ) \
  481. { \
  482. pte_cleanup_t _cleanup; \
  483. \
  484. pte_push_cleanup( &_cleanup, (pte_cleanup_callback_t) (_rout), (_arg) ); \
  485. #define pthread_cleanup_pop( _execute ) \
  486. (void) pte_pop_cleanup( _execute ); \
  487. }
  488. #else /* PTE_CLEANUP_C */
  489. #ifdef PTE_CLEANUP_CXX
  490. /*
  491. * C++ version of cancel cleanup.
  492. * - John E. Bossom.
  493. */
  494. class PThreadCleanup
  495. {
  496. /*
  497. * PThreadCleanup
  498. *
  499. * Purpose
  500. * This class is a C++ helper class that is
  501. * used to implement pthread_cleanup_push/
  502. * pthread_cleanup_pop.
  503. * The destructor of this class automatically
  504. * pops the pushed cleanup routine regardless
  505. * of how the code exits the scope
  506. * (i.e. such as by an exception)
  507. */
  508. pte_cleanup_callback_t cleanUpRout;
  509. void * obj;
  510. int executeIt;
  511. public:
  512. PThreadCleanup() :
  513. cleanUpRout( 0 ),
  514. obj( 0 ),
  515. executeIt( 0 )
  516. /*
  517. * No cleanup performed
  518. */
  519. {
  520. }
  521. PThreadCleanup(
  522. pte_cleanup_callback_t routine,
  523. void * arg ) :
  524. cleanUpRout( routine ),
  525. obj( arg ),
  526. executeIt( 1 )
  527. /*
  528. * Registers a cleanup routine for 'arg'
  529. */
  530. {
  531. }
  532. ~PThreadCleanup()
  533. {
  534. if ( executeIt )
  535. {
  536. (void) (*cleanUpRout)( obj );
  537. }
  538. }
  539. void execute( int exec )
  540. {
  541. executeIt = exec;
  542. }
  543. };
  544. /*
  545. * C++ implementation of PThreads cancel cleanup;
  546. * This implementation takes advantage of a helper
  547. * class who's destructor automatically calls the
  548. * cleanup routine if we exit our scope weirdly
  549. */
  550. #define pthread_cleanup_push( _rout, _arg ) \
  551. { \
  552. PThreadCleanup cleanup((pte_cleanup_callback_t)(_rout), \
  553. (void *) (_arg) );
  554. #define pthread_cleanup_pop( _execute ) \
  555. cleanup.execute( _execute ); \
  556. }
  557. #else
  558. #error ERROR [__FILE__, line __LINE__]: Cleanup type undefined.
  559. #endif /* PTE_CLEANUP_CXX */
  560. #endif /* PTE_CLEANUP_C */
  561. #ifdef __cplusplus
  562. extern "C" {
  563. #endif /* __cplusplus */
  564. /*
  565. * ===============
  566. * ===============
  567. * Methods
  568. * ===============
  569. * ===============
  570. */
  571. void pthread_init (void);
  572. void pthread_terminate (void);
  573. /*
  574. * PThread Attribute Functions
  575. */
  576. int pthread_attr_init (pthread_attr_t * attr);
  577. int pthread_attr_destroy (pthread_attr_t * attr);
  578. int pthread_attr_getdetachstate (const pthread_attr_t * attr,
  579. int *detachstate);
  580. int pthread_attr_getstackaddr (const pthread_attr_t * attr,
  581. void **stackaddr);
  582. int pthread_attr_getstacksize (const pthread_attr_t * attr,
  583. size_t * stacksize);
  584. int pthread_attr_setdetachstate (pthread_attr_t * attr,
  585. int detachstate);
  586. int pthread_attr_setstackaddr (pthread_attr_t * attr,
  587. void *stackaddr);
  588. int pthread_attr_setstacksize (pthread_attr_t * attr,
  589. size_t stacksize);
  590. int pthread_attr_getschedparam (const pthread_attr_t *attr,
  591. struct sched_param *param);
  592. int pthread_attr_setschedparam (pthread_attr_t *attr,
  593. const struct sched_param *param);
  594. int pthread_attr_setschedpolicy (pthread_attr_t *,
  595. int);
  596. int pthread_attr_getschedpolicy (pthread_attr_t *,
  597. int *);
  598. int pthread_attr_setinheritsched(pthread_attr_t * attr,
  599. int inheritsched);
  600. int pthread_attr_getinheritsched(pthread_attr_t * attr,
  601. int * inheritsched);
  602. int pthread_attr_setscope (pthread_attr_t *,
  603. int);
  604. int pthread_attr_getscope (const pthread_attr_t *,
  605. int *);
  606. /*
  607. * PThread Functions
  608. */
  609. int pthread_create (pthread_t * tid,
  610. const pthread_attr_t * attr,
  611. void *(*start) (void *),
  612. void *arg);
  613. int pthread_detach (pthread_t tid);
  614. int pthread_equal (pthread_t t1,
  615. pthread_t t2);
  616. void pthread_exit (void *value_ptr);
  617. int pthread_join (pthread_t thread,
  618. void **value_ptr);
  619. pthread_t pthread_self (void);
  620. int pthread_cancel (pthread_t thread);
  621. int pthread_setcancelstate (int state,
  622. int *oldstate);
  623. int pthread_setcanceltype (int type,
  624. int *oldtype);
  625. void pthread_testcancel (void);
  626. int pthread_once (pthread_once_t * once_control,
  627. void (*init_routine) (void));
  628. #if PTE_LEVEL >= PTE_LEVEL_MAX
  629. pte_cleanup_t * pte_pop_cleanup (int execute);
  630. void pte_push_cleanup (pte_cleanup_t * cleanup,
  631. void (*routine) (void *),
  632. void *arg);
  633. #endif /* PTE_LEVEL >= PTE_LEVEL_MAX */
  634. /*
  635. * Thread Specific Data Functions
  636. */
  637. int pthread_key_create (pthread_key_t * key,
  638. void (*destructor) (void *));
  639. int pthread_key_delete (pthread_key_t key);
  640. int pthread_setspecific (pthread_key_t key,
  641. const void *value);
  642. void * pthread_getspecific (pthread_key_t key);
  643. /*
  644. * Mutex Attribute Functions
  645. */
  646. int pthread_mutexattr_init (pthread_mutexattr_t * attr);
  647. int pthread_mutexattr_destroy (pthread_mutexattr_t * attr);
  648. int pthread_mutexattr_getpshared (const pthread_mutexattr_t
  649. * attr,
  650. int *pshared);
  651. int pthread_mutexattr_setpshared (pthread_mutexattr_t * attr,
  652. int pshared);
  653. int pthread_mutexattr_settype (pthread_mutexattr_t * attr, int kind);
  654. int pthread_mutexattr_gettype (pthread_mutexattr_t * attr, int *kind);
  655. /*
  656. * Barrier Attribute Functions
  657. */
  658. int pthread_barrierattr_init (pthread_barrierattr_t * attr);
  659. int pthread_barrierattr_destroy (pthread_barrierattr_t * attr);
  660. int pthread_barrierattr_getpshared (const pthread_barrierattr_t
  661. * attr,
  662. int *pshared);
  663. int pthread_barrierattr_setpshared (pthread_barrierattr_t * attr,
  664. int pshared);
  665. /*
  666. * Mutex Functions
  667. */
  668. int pthread_mutex_init (pthread_mutex_t * mutex,
  669. const pthread_mutexattr_t * attr);
  670. int pthread_mutex_destroy (pthread_mutex_t * mutex);
  671. int pthread_mutex_lock (pthread_mutex_t * mutex);
  672. int pthread_mutex_timedlock(pthread_mutex_t *mutex,
  673. const struct timespec *abstime);
  674. int pthread_mutex_trylock (pthread_mutex_t * mutex);
  675. int pthread_mutex_unlock (pthread_mutex_t * mutex);
  676. /*
  677. * Spinlock Functions
  678. */
  679. int pthread_spin_init (pthread_spinlock_t * lock, int pshared);
  680. int pthread_spin_destroy (pthread_spinlock_t * lock);
  681. int pthread_spin_lock (pthread_spinlock_t * lock);
  682. int pthread_spin_trylock (pthread_spinlock_t * lock);
  683. int pthread_spin_unlock (pthread_spinlock_t * lock);
  684. /*
  685. * Barrier Functions
  686. */
  687. int pthread_barrier_init (pthread_barrier_t * barrier,
  688. const pthread_barrierattr_t * attr,
  689. unsigned int count);
  690. int pthread_barrier_destroy (pthread_barrier_t * barrier);
  691. int pthread_barrier_wait (pthread_barrier_t * barrier);
  692. /*
  693. * Condition Variable Attribute Functions
  694. */
  695. int pthread_condattr_init (pthread_condattr_t * attr);
  696. int pthread_condattr_destroy (pthread_condattr_t * attr);
  697. int pthread_condattr_getpshared (const pthread_condattr_t * attr,
  698. int *pshared);
  699. int pthread_condattr_setpshared (pthread_condattr_t * attr,
  700. int pshared);
  701. /*
  702. * Condition Variable Functions
  703. */
  704. int pthread_cond_init (pthread_cond_t * cond,
  705. const pthread_condattr_t * attr);
  706. int pthread_cond_destroy (pthread_cond_t * cond);
  707. int pthread_cond_wait (pthread_cond_t * cond,
  708. pthread_mutex_t * mutex);
  709. int pthread_cond_timedwait (pthread_cond_t * cond,
  710. pthread_mutex_t * mutex,
  711. const struct timespec *abstime);
  712. int pthread_cond_signal (pthread_cond_t * cond);
  713. int pthread_cond_broadcast (pthread_cond_t * cond);
  714. /*
  715. * Scheduling
  716. */
  717. int pthread_setschedparam (pthread_t thread,
  718. int policy,
  719. const struct sched_param *param);
  720. int pthread_getschedparam (pthread_t thread,
  721. int *policy,
  722. struct sched_param *param);
  723. int pthread_setconcurrency (int);
  724. int pthread_getconcurrency (void);
  725. /*
  726. * Read-Write Lock Functions
  727. */
  728. int pthread_rwlock_init(pthread_rwlock_t *lock,
  729. const pthread_rwlockattr_t *attr);
  730. int pthread_rwlock_destroy(pthread_rwlock_t *lock);
  731. int pthread_rwlock_tryrdlock(pthread_rwlock_t *);
  732. int pthread_rwlock_trywrlock(pthread_rwlock_t *);
  733. int pthread_rwlock_rdlock(pthread_rwlock_t *lock);
  734. int pthread_rwlock_timedrdlock(pthread_rwlock_t *lock,
  735. const struct timespec *abstime);
  736. int pthread_rwlock_wrlock(pthread_rwlock_t *lock);
  737. int pthread_rwlock_timedwrlock(pthread_rwlock_t *lock,
  738. const struct timespec *abstime);
  739. int pthread_rwlock_unlock(pthread_rwlock_t *lock);
  740. int pthread_rwlockattr_init (pthread_rwlockattr_t * attr);
  741. int pthread_rwlockattr_destroy (pthread_rwlockattr_t * attr);
  742. int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t * attr,
  743. int *pshared);
  744. int pthread_rwlockattr_setpshared (pthread_rwlockattr_t * attr,
  745. int pshared);
  746. #if PTE_LEVEL >= PTE_LEVEL_MAX - 1
  747. /*
  748. * Signal Functions. Should be defined in <signal.h> but we might
  749. * already have signal.h that don't define these.
  750. */
  751. int pthread_kill(pthread_t thread, int sig);
  752. /*
  753. * Non-portable functions
  754. */
  755. /*
  756. * Compatibility with Linux.
  757. */
  758. int pthread_mutexattr_setkind_np(pthread_mutexattr_t * attr,
  759. int kind);
  760. int pthread_mutexattr_getkind_np(pthread_mutexattr_t * attr,
  761. int *kind);
  762. /*
  763. * Possibly supported by other POSIX threads implementations
  764. */
  765. int pthread_delay_np (struct timespec * interval);
  766. int pthread_num_processors_np(void);
  767. /*
  768. * Register a system time change with the library.
  769. * Causes the library to perform various functions
  770. * in response to the change. Should be called whenever
  771. * the application's top level window receives a
  772. * WM_TIMECHANGE message. It can be passed directly to
  773. * pthread_create() as a new thread if desired.
  774. */
  775. void * pthread_timechange_handler_np(void *);
  776. #endif /*PTE_LEVEL >= PTE_LEVEL_MAX - 1 */
  777. #ifdef __cplusplus
  778. }
  779. #endif /* cplusplus */
  780. #if PTE_LEVEL >= PTE_LEVEL_MAX
  781. #endif /* PTE_LEVEL >= PTE_LEVEL_MAX */
  782. /*
  783. * Some compiler environments don't define some things.
  784. */
  785. # define _ftime ftime
  786. # define _timeb timeb
  787. #ifdef __cplusplus
  788. /*
  789. * Internal exceptions
  790. */
  791. class pte_exception {};
  792. class pte_exception_cancel : public pte_exception {};
  793. class pte_exception_exit : public pte_exception {};
  794. #endif
  795. #ifdef PTE_CXX_EXCEPTIONS
  796. /*
  797. * Redefine the C++ catch keyword to ensure that applications
  798. * propagate our internal exceptions up to the library's internal handlers.
  799. */
  800. #define catch( E ) \
  801. catch( pte_exception & ) { throw; } \
  802. catch( E )
  803. #endif /* ! PTE_CXX_EXCEPTIONS */
  804. #undef PTE_LEVEL
  805. #undef PTE_LEVEL_MAX
  806. #endif /* PTHREAD_H */