pthread.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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: [email protected]
  9. *
  10. *
  11. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  12. * Copyright(C) 2008 Jason Schmidlapp
  13. *
  14. * Contact Email: [email protected]
  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: [email protected]
  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-config.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 <[email protected]>
  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. /*
  349. * Generic handle type - intended to extend uniqueness beyond
  350. * that available with a simple pointer. It should scale for either
  351. * IA-32 or IA-64.
  352. */
  353. typedef struct
  354. {
  355. void * p; /* Pointer to actual object */
  356. unsigned int x; /* Extra information - reuse count etc */
  357. } pte_handle_t;
  358. typedef pte_handle_t pthread_t;
  359. typedef struct pthread_attr_t_ * pthread_attr_t;
  360. typedef struct pthread_once_t_ pthread_once_t;
  361. typedef struct pthread_key_t_ * pthread_key_t;
  362. typedef struct pthread_mutex_t_ * pthread_mutex_t;
  363. typedef struct pthread_mutexattr_t_ * pthread_mutexattr_t;
  364. typedef struct pthread_cond_t_ * pthread_cond_t;
  365. typedef struct pthread_condattr_t_ * pthread_condattr_t;
  366. typedef struct pthread_rwlock_t_ * pthread_rwlock_t;
  367. typedef struct pthread_rwlockattr_t_ * pthread_rwlockattr_t;
  368. typedef struct pthread_spinlock_t_ * pthread_spinlock_t;
  369. typedef struct pthread_barrier_t_ * pthread_barrier_t;
  370. typedef struct pthread_barrierattr_t_ * pthread_barrierattr_t;
  371. /*
  372. * ====================
  373. * ====================
  374. * POSIX Threads
  375. * ====================
  376. * ====================
  377. */
  378. enum
  379. {
  380. /*
  381. * pthread_attr_{get,set}detachstate
  382. */
  383. PTHREAD_CREATE_JOINABLE = 0, /* Default */
  384. PTHREAD_CREATE_DETACHED = 1,
  385. /*
  386. * pthread_attr_{get,set}inheritsched
  387. */
  388. PTHREAD_INHERIT_SCHED = 0,
  389. PTHREAD_EXPLICIT_SCHED = 1, /* Default */
  390. /*
  391. * pthread_{get,set}scope
  392. */
  393. PTHREAD_SCOPE_PROCESS = 0,
  394. PTHREAD_SCOPE_SYSTEM = 1, /* Default */
  395. PTHREAD_SCOPE_PROCESS_VFPU = 2, /* PSP specific */
  396. /*
  397. * pthread_setcancelstate paramters
  398. */
  399. PTHREAD_CANCEL_ENABLE = 0, /* Default */
  400. PTHREAD_CANCEL_DISABLE = 1,
  401. /*
  402. * pthread_setcanceltype parameters
  403. */
  404. PTHREAD_CANCEL_ASYNCHRONOUS = 0,
  405. PTHREAD_CANCEL_DEFERRED = 1, /* Default */
  406. /*
  407. * pthread_mutexattr_{get,set}pshared
  408. * pthread_condattr_{get,set}pshared
  409. */
  410. PTHREAD_PROCESS_PRIVATE = 0,
  411. PTHREAD_PROCESS_SHARED = 1,
  412. /*
  413. * pthread_barrier_wait
  414. */
  415. PTHREAD_BARRIER_SERIAL_THREAD = -1
  416. };
  417. /*
  418. * ====================
  419. * ====================
  420. * Cancelation
  421. * ====================
  422. * ====================
  423. */
  424. #define PTHREAD_CANCELED ((void *) -1)
  425. /*
  426. * ====================
  427. * ====================
  428. * Once Key
  429. * ====================
  430. * ====================
  431. */
  432. #define PTHREAD_ONCE_INIT { PTE_FALSE, 0, 0, 0}
  433. struct pthread_once_t_
  434. {
  435. int state;
  436. void * semaphore;
  437. int numSemaphoreUsers;
  438. int done; /* indicates if user function has been executed */
  439. // void * lock;
  440. // int reserved1;
  441. // int reserved2;
  442. };
  443. /*
  444. * ====================
  445. * ====================
  446. * Object initialisers
  447. * ====================
  448. * ====================
  449. */
  450. #define PTHREAD_MUTEX_INITIALIZER ((pthread_mutex_t) -1)
  451. #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER ((pthread_mutex_t) -2)
  452. #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER ((pthread_mutex_t) -3)
  453. /*
  454. * Compatibility with LinuxThreads
  455. */
  456. #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP PTHREAD_RECURSIVE_MUTEX_INITIALIZER
  457. #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP PTHREAD_ERRORCHECK_MUTEX_INITIALIZER
  458. #define PTHREAD_COND_INITIALIZER ((pthread_cond_t) -1)
  459. #define PTHREAD_RWLOCK_INITIALIZER ((pthread_rwlock_t) -1)
  460. #define PTHREAD_SPINLOCK_INITIALIZER ((pthread_spinlock_t) -1)
  461. /*
  462. * Mutex types.
  463. */
  464. enum
  465. {
  466. /* Compatibility with LinuxThreads */
  467. PTHREAD_MUTEX_FAST_NP,
  468. PTHREAD_MUTEX_RECURSIVE_NP,
  469. PTHREAD_MUTEX_ERRORCHECK_NP,
  470. PTHREAD_MUTEX_TIMED_NP = PTHREAD_MUTEX_FAST_NP,
  471. PTHREAD_MUTEX_ADAPTIVE_NP = PTHREAD_MUTEX_FAST_NP,
  472. /* For compatibility with POSIX */
  473. PTHREAD_MUTEX_NORMAL = PTHREAD_MUTEX_FAST_NP,
  474. PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP,
  475. PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP,
  476. PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
  477. };
  478. typedef struct pte_cleanup_t pte_cleanup_t;
  479. typedef void (* pte_cleanup_callback_t)(void *);
  480. struct pte_cleanup_t
  481. {
  482. pte_cleanup_callback_t routine;
  483. void *arg;
  484. struct pte_cleanup_t *prev;
  485. };
  486. #ifdef PTE_CLEANUP_C
  487. /*
  488. * C implementation of PThreads cancel cleanup
  489. */
  490. #define pthread_cleanup_push( _rout, _arg ) \
  491. { \
  492. pte_cleanup_t _cleanup; \
  493. \
  494. pte_push_cleanup( &_cleanup, (pte_cleanup_callback_t) (_rout), (_arg) ); \
  495. #define pthread_cleanup_pop( _execute ) \
  496. (void) pte_pop_cleanup( _execute ); \
  497. }
  498. #else /* PTE_CLEANUP_C */
  499. #ifdef PTE_CLEANUP_CXX
  500. /*
  501. * C++ version of cancel cleanup.
  502. * - John E. Bossom.
  503. */
  504. class PThreadCleanup
  505. {
  506. /*
  507. * PThreadCleanup
  508. *
  509. * Purpose
  510. * This class is a C++ helper class that is
  511. * used to implement pthread_cleanup_push/
  512. * pthread_cleanup_pop.
  513. * The destructor of this class automatically
  514. * pops the pushed cleanup routine regardless
  515. * of how the code exits the scope
  516. * (i.e. such as by an exception)
  517. */
  518. pte_cleanup_callback_t cleanUpRout;
  519. void * obj;
  520. int executeIt;
  521. public:
  522. PThreadCleanup() :
  523. cleanUpRout( 0 ),
  524. obj( 0 ),
  525. executeIt( 0 )
  526. /*
  527. * No cleanup performed
  528. */
  529. {
  530. }
  531. PThreadCleanup(
  532. pte_cleanup_callback_t routine,
  533. void * arg ) :
  534. cleanUpRout( routine ),
  535. obj( arg ),
  536. executeIt( 1 )
  537. /*
  538. * Registers a cleanup routine for 'arg'
  539. */
  540. {
  541. }
  542. ~PThreadCleanup()
  543. {
  544. if ( executeIt )
  545. {
  546. (void) (*cleanUpRout)( obj );
  547. }
  548. }
  549. void execute( int exec )
  550. {
  551. executeIt = exec;
  552. }
  553. };
  554. /*
  555. * C++ implementation of PThreads cancel cleanup;
  556. * This implementation takes advantage of a helper
  557. * class who's destructor automatically calls the
  558. * cleanup routine if we exit our scope weirdly
  559. */
  560. #define pthread_cleanup_push( _rout, _arg ) \
  561. { \
  562. PThreadCleanup cleanup((pte_cleanup_callback_t)(_rout), \
  563. (void *) (_arg) );
  564. #define pthread_cleanup_pop( _execute ) \
  565. cleanup.execute( _execute ); \
  566. }
  567. #else
  568. #error ERROR [__FILE__, line __LINE__]: Cleanup type undefined.
  569. #endif /* PTE_CLEANUP_CXX */
  570. #endif /* PTE_CLEANUP_C */
  571. /*
  572. * ===============
  573. * ===============
  574. * Methods
  575. * ===============
  576. * ===============
  577. */
  578. int pthread_init (void);
  579. void pthread_terminate (void);
  580. /*
  581. * PThread Attribute Functions
  582. */
  583. int pthread_attr_init (pthread_attr_t * attr);
  584. int pthread_attr_destroy (pthread_attr_t * attr);
  585. int pthread_attr_getdetachstate (const pthread_attr_t * attr,
  586. int *detachstate);
  587. int pthread_attr_getstackaddr (const pthread_attr_t * attr,
  588. void **stackaddr);
  589. int pthread_attr_getstacksize (const pthread_attr_t * attr,
  590. size_t * stacksize);
  591. int pthread_attr_setdetachstate (pthread_attr_t * attr,
  592. int detachstate);
  593. int pthread_attr_setstackaddr (pthread_attr_t * attr,
  594. void *stackaddr);
  595. int pthread_attr_setstacksize (pthread_attr_t * attr,
  596. size_t stacksize);
  597. int pthread_attr_getschedparam (const pthread_attr_t *attr,
  598. struct sched_param *param);
  599. int pthread_attr_setschedparam (pthread_attr_t *attr,
  600. const struct sched_param *param);
  601. int pthread_attr_setschedpolicy (pthread_attr_t *,
  602. int);
  603. int pthread_attr_getschedpolicy (pthread_attr_t *,
  604. int *);
  605. int pthread_attr_setinheritsched(pthread_attr_t * attr,
  606. int inheritsched);
  607. int pthread_attr_getinheritsched(pthread_attr_t * attr,
  608. int * inheritsched);
  609. int pthread_attr_setscope (pthread_attr_t *,
  610. int);
  611. int pthread_attr_getscope (const pthread_attr_t *,
  612. int *);
  613. /*
  614. * PThread Functions
  615. */
  616. int pthread_create (pthread_t * tid,
  617. const pthread_attr_t * attr,
  618. void *(*start) (void *),
  619. void *arg);
  620. int pthread_detach (pthread_t tid);
  621. int pthread_equal (pthread_t t1,
  622. pthread_t t2);
  623. void pthread_exit (void *value_ptr);
  624. int pthread_join (pthread_t thread,
  625. void **value_ptr);
  626. pthread_t pthread_self (void);
  627. int pthread_cancel (pthread_t thread);
  628. int pthread_setcancelstate (int state,
  629. int *oldstate);
  630. int pthread_setcanceltype (int type,
  631. int *oldtype);
  632. void pthread_testcancel (void);
  633. int pthread_once (pthread_once_t * once_control,
  634. void (*init_routine) (void));
  635. #if PTE_LEVEL >= PTE_LEVEL_MAX
  636. pte_cleanup_t * pte_pop_cleanup (int execute);
  637. void pte_push_cleanup (pte_cleanup_t * cleanup,
  638. void (*routine) (void *),
  639. void *arg);
  640. #endif /* PTE_LEVEL >= PTE_LEVEL_MAX */
  641. /*
  642. * Thread Specific Data Functions
  643. */
  644. int pthread_key_create (pthread_key_t * key,
  645. void (*destructor) (void *));
  646. int pthread_key_delete (pthread_key_t key);
  647. int pthread_setspecific (pthread_key_t key,
  648. const void *value);
  649. void * pthread_getspecific (pthread_key_t key);
  650. /*
  651. * Mutex Attribute Functions
  652. */
  653. int pthread_mutexattr_init (pthread_mutexattr_t * attr);
  654. int pthread_mutexattr_destroy (pthread_mutexattr_t * attr);
  655. int pthread_mutexattr_getpshared (const pthread_mutexattr_t
  656. * attr,
  657. int *pshared);
  658. int pthread_mutexattr_setpshared (pthread_mutexattr_t * attr,
  659. int pshared);
  660. int pthread_mutexattr_settype (pthread_mutexattr_t * attr, int kind);
  661. int pthread_mutexattr_gettype (pthread_mutexattr_t * attr, int *kind);
  662. /*
  663. * Barrier Attribute Functions
  664. */
  665. int pthread_barrierattr_init (pthread_barrierattr_t * attr);
  666. int pthread_barrierattr_destroy (pthread_barrierattr_t * attr);
  667. int pthread_barrierattr_getpshared (const pthread_barrierattr_t
  668. * attr,
  669. int *pshared);
  670. int pthread_barrierattr_setpshared (pthread_barrierattr_t * attr,
  671. int pshared);
  672. /*
  673. * Mutex Functions
  674. */
  675. int pthread_mutex_init (pthread_mutex_t * mutex,
  676. const pthread_mutexattr_t * attr);
  677. int pthread_mutex_destroy (pthread_mutex_t * mutex);
  678. int pthread_mutex_lock (pthread_mutex_t * mutex);
  679. int pthread_mutex_timedlock(pthread_mutex_t *mutex,
  680. const struct timespec *abstime);
  681. int pthread_mutex_trylock (pthread_mutex_t * mutex);
  682. int pthread_mutex_unlock (pthread_mutex_t * mutex);
  683. /*
  684. * Spinlock Functions
  685. */
  686. int pthread_spin_init (pthread_spinlock_t * lock, int pshared);
  687. int pthread_spin_destroy (pthread_spinlock_t * lock);
  688. int pthread_spin_lock (pthread_spinlock_t * lock);
  689. int pthread_spin_trylock (pthread_spinlock_t * lock);
  690. int pthread_spin_unlock (pthread_spinlock_t * lock);
  691. /*
  692. * Barrier Functions
  693. */
  694. int pthread_barrier_init (pthread_barrier_t * barrier,
  695. const pthread_barrierattr_t * attr,
  696. unsigned int count);
  697. int pthread_barrier_destroy (pthread_barrier_t * barrier);
  698. int pthread_barrier_wait (pthread_barrier_t * barrier);
  699. /*
  700. * Condition Variable Attribute Functions
  701. */
  702. int pthread_condattr_init (pthread_condattr_t * attr);
  703. int pthread_condattr_destroy (pthread_condattr_t * attr);
  704. int pthread_condattr_getpshared (const pthread_condattr_t * attr,
  705. int *pshared);
  706. int pthread_condattr_setpshared (pthread_condattr_t * attr,
  707. int pshared);
  708. /*
  709. * Condition Variable Functions
  710. */
  711. int pthread_cond_init (pthread_cond_t * cond,
  712. const pthread_condattr_t * attr);
  713. int pthread_cond_destroy (pthread_cond_t * cond);
  714. int pthread_cond_wait (pthread_cond_t * cond,
  715. pthread_mutex_t * mutex);
  716. int pthread_cond_timedwait (pthread_cond_t * cond,
  717. pthread_mutex_t * mutex,
  718. const struct timespec *abstime);
  719. int pthread_cond_signal (pthread_cond_t * cond);
  720. int pthread_cond_broadcast (pthread_cond_t * cond);
  721. /*
  722. * Scheduling
  723. */
  724. int pthread_setschedparam (pthread_t thread,
  725. int policy,
  726. const struct sched_param *param);
  727. int pthread_getschedparam (pthread_t thread,
  728. int *policy,
  729. struct sched_param *param);
  730. int pthread_setconcurrency (int);
  731. int pthread_getconcurrency (void);
  732. /*
  733. * Read-Write Lock Functions
  734. */
  735. int pthread_rwlock_init(pthread_rwlock_t *lock,
  736. const pthread_rwlockattr_t *attr);
  737. int pthread_rwlock_destroy(pthread_rwlock_t *lock);
  738. int pthread_rwlock_tryrdlock(pthread_rwlock_t *);
  739. int pthread_rwlock_trywrlock(pthread_rwlock_t *);
  740. int pthread_rwlock_rdlock(pthread_rwlock_t *lock);
  741. int pthread_rwlock_timedrdlock(pthread_rwlock_t *lock,
  742. const struct timespec *abstime);
  743. int pthread_rwlock_wrlock(pthread_rwlock_t *lock);
  744. int pthread_rwlock_timedwrlock(pthread_rwlock_t *lock,
  745. const struct timespec *abstime);
  746. int pthread_rwlock_unlock(pthread_rwlock_t *lock);
  747. int pthread_rwlockattr_init (pthread_rwlockattr_t * attr);
  748. int pthread_rwlockattr_destroy (pthread_rwlockattr_t * attr);
  749. int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t * attr,
  750. int *pshared);
  751. int pthread_rwlockattr_setpshared (pthread_rwlockattr_t * attr,
  752. int pshared);
  753. #if PTE_LEVEL >= PTE_LEVEL_MAX - 1
  754. /*
  755. * Signal Functions. Should be defined in <signal.h> but we might
  756. * already have signal.h that don't define these.
  757. */
  758. int pthread_kill(pthread_t thread, int sig);
  759. /*
  760. * Non-portable functions
  761. */
  762. /*
  763. * Compatibility with Linux.
  764. */
  765. int pthread_mutexattr_setkind_np(pthread_mutexattr_t * attr,
  766. int kind);
  767. int pthread_mutexattr_getkind_np(pthread_mutexattr_t * attr,
  768. int *kind);
  769. /*
  770. * Possibly supported by other POSIX threads implementations
  771. */
  772. int pthread_delay_np (struct timespec * interval);
  773. int pthread_num_processors_np(void);
  774. /*
  775. * Register a system time change with the library.
  776. * Causes the library to perform various functions
  777. * in response to the change. Should be called whenever
  778. * the application's top level window receives a
  779. * WM_TIMECHANGE message. It can be passed directly to
  780. * pthread_create() as a new thread if desired.
  781. */
  782. void * pthread_timechange_handler_np(void *);
  783. #endif /*PTE_LEVEL >= PTE_LEVEL_MAX - 1 */
  784. #if PTE_LEVEL >= PTE_LEVEL_MAX
  785. #endif /* PTE_LEVEL >= PTE_LEVEL_MAX */
  786. /*
  787. * Some compiler environments don't define some things.
  788. */
  789. # define _ftime ftime
  790. # define _timeb timeb
  791. #ifdef __cplusplus
  792. /*
  793. * Internal exceptions
  794. */
  795. class pte_exception {};
  796. class pte_exception_cancel : public pte_exception {};
  797. class pte_exception_exit : public pte_exception {};
  798. #endif
  799. #ifndef PTE_BUILD
  800. #ifdef PTE_CLEANUP_CXX
  801. /*
  802. * Redefine the C++ catch keyword to ensure that applications
  803. * propagate our internal exceptions up to the library's internal handlers.
  804. */
  805. #define catch( E ) \
  806. catch( pte_exception & ) { throw; } \
  807. catch( E )
  808. #endif /* PTE_CLEANUP_CXX */
  809. #endif /* ! PTE_BUILD */
  810. #undef PTE_LEVEL
  811. #undef PTE_LEVEL_MAX
  812. #endif /* PTHREAD_H */