pthread.h 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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_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 <[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. 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. int pthread_condattr_getclock (const pthread_condattr_t * attr,
  702. clockid_t *clock_id);
  703. int pthread_condattr_setclock (pthread_condattr_t * attr,
  704. clockid_t clock_id);
  705. /*
  706. * Condition Variable Functions
  707. */
  708. int pthread_cond_init (pthread_cond_t * cond,
  709. const pthread_condattr_t * attr);
  710. int pthread_cond_destroy (pthread_cond_t * cond);
  711. int pthread_cond_wait (pthread_cond_t * cond,
  712. pthread_mutex_t * mutex);
  713. int pthread_cond_timedwait (pthread_cond_t * cond,
  714. pthread_mutex_t * mutex,
  715. const struct timespec *abstime);
  716. int pthread_cond_signal (pthread_cond_t * cond);
  717. int pthread_cond_broadcast (pthread_cond_t * cond);
  718. /*
  719. * Scheduling
  720. */
  721. int pthread_setschedparam (pthread_t thread,
  722. int policy,
  723. const struct sched_param *param);
  724. int pthread_getschedparam (pthread_t thread,
  725. int *policy,
  726. struct sched_param *param);
  727. int pthread_setconcurrency (int);
  728. int pthread_getconcurrency (void);
  729. /*
  730. * Read-Write Lock Functions
  731. */
  732. int pthread_rwlock_init(pthread_rwlock_t *lock,
  733. const pthread_rwlockattr_t *attr);
  734. int pthread_rwlock_destroy(pthread_rwlock_t *lock);
  735. int pthread_rwlock_tryrdlock(pthread_rwlock_t *);
  736. int pthread_rwlock_trywrlock(pthread_rwlock_t *);
  737. int pthread_rwlock_rdlock(pthread_rwlock_t *lock);
  738. int pthread_rwlock_timedrdlock(pthread_rwlock_t *lock,
  739. const struct timespec *abstime);
  740. int pthread_rwlock_wrlock(pthread_rwlock_t *lock);
  741. int pthread_rwlock_timedwrlock(pthread_rwlock_t *lock,
  742. const struct timespec *abstime);
  743. int pthread_rwlock_unlock(pthread_rwlock_t *lock);
  744. int pthread_rwlockattr_init (pthread_rwlockattr_t * attr);
  745. int pthread_rwlockattr_destroy (pthread_rwlockattr_t * attr);
  746. int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t * attr,
  747. int *pshared);
  748. int pthread_rwlockattr_setpshared (pthread_rwlockattr_t * attr,
  749. int pshared);
  750. #if PTE_LEVEL >= PTE_LEVEL_MAX - 1
  751. /*
  752. * Signal Functions. Should be defined in <signal.h> but we might
  753. * already have signal.h that don't define these.
  754. */
  755. int pthread_kill(pthread_t thread, int sig);
  756. /*
  757. * Non-portable functions
  758. */
  759. /*
  760. * Compatibility with Linux.
  761. */
  762. int pthread_mutexattr_setkind_np(pthread_mutexattr_t * attr,
  763. int kind);
  764. int pthread_mutexattr_getkind_np(pthread_mutexattr_t * attr,
  765. int *kind);
  766. /*
  767. * Possibly supported by other POSIX threads implementations
  768. */
  769. int pthread_delay_np (struct timespec * interval);
  770. int pthread_num_processors_np(void);
  771. /*
  772. * Register a system time change with the library.
  773. * Causes the library to perform various functions
  774. * in response to the change. Should be called whenever
  775. * the application's top level window receives a
  776. * WM_TIMECHANGE message. It can be passed directly to
  777. * pthread_create() as a new thread if desired.
  778. */
  779. void * pthread_timechange_handler_np(void *);
  780. #endif /*PTE_LEVEL >= PTE_LEVEL_MAX - 1 */
  781. #ifdef __cplusplus
  782. }
  783. #endif /* cplusplus */
  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. #ifdef PTE_CXX_EXCEPTIONS
  800. /*
  801. * Redefine the C++ catch keyword to ensure that applications
  802. * propagate our internal exceptions up to the library's internal handlers.
  803. */
  804. #define catch( E ) \
  805. catch( pte_exception & ) { throw; } \
  806. catch( E )
  807. #endif /* ! PTE_CXX_EXCEPTIONS */
  808. #undef PTE_LEVEL
  809. #undef PTE_LEVEL_MAX
  810. #endif /* PTHREAD_H */