pthread.h 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. /*
  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. #ifdef __cplusplus
  572. extern "C" {
  573. #endif /* __cplusplus */
  574. /*
  575. * ===============
  576. * ===============
  577. * Methods
  578. * ===============
  579. * ===============
  580. */
  581. void pthread_init (void);
  582. void pthread_terminate (void);
  583. /*
  584. * PThread Attribute Functions
  585. */
  586. int pthread_attr_init (pthread_attr_t * attr);
  587. int pthread_attr_destroy (pthread_attr_t * attr);
  588. int pthread_attr_getdetachstate (const pthread_attr_t * attr,
  589. int *detachstate);
  590. int pthread_attr_getstackaddr (const pthread_attr_t * attr,
  591. void **stackaddr);
  592. int pthread_attr_getstacksize (const pthread_attr_t * attr,
  593. size_t * stacksize);
  594. int pthread_attr_setdetachstate (pthread_attr_t * attr,
  595. int detachstate);
  596. int pthread_attr_setstackaddr (pthread_attr_t * attr,
  597. void *stackaddr);
  598. int pthread_attr_setstacksize (pthread_attr_t * attr,
  599. size_t stacksize);
  600. int pthread_attr_getschedparam (const pthread_attr_t *attr,
  601. struct sched_param *param);
  602. int pthread_attr_setschedparam (pthread_attr_t *attr,
  603. const struct sched_param *param);
  604. int pthread_attr_setschedpolicy (pthread_attr_t *,
  605. int);
  606. int pthread_attr_getschedpolicy (pthread_attr_t *,
  607. int *);
  608. int pthread_attr_setinheritsched(pthread_attr_t * attr,
  609. int inheritsched);
  610. int pthread_attr_getinheritsched(pthread_attr_t * attr,
  611. int * inheritsched);
  612. int pthread_attr_setscope (pthread_attr_t *,
  613. int);
  614. int pthread_attr_getscope (const pthread_attr_t *,
  615. int *);
  616. /*
  617. * PThread Functions
  618. */
  619. int pthread_create (pthread_t * tid,
  620. const pthread_attr_t * attr,
  621. void *(*start) (void *),
  622. void *arg);
  623. int pthread_detach (pthread_t tid);
  624. int pthread_equal (pthread_t t1,
  625. pthread_t t2);
  626. void pthread_exit (void *value_ptr);
  627. int pthread_join (pthread_t thread,
  628. void **value_ptr);
  629. pthread_t pthread_self (void);
  630. int pthread_cancel (pthread_t thread);
  631. int pthread_setcancelstate (int state,
  632. int *oldstate);
  633. int pthread_setcanceltype (int type,
  634. int *oldtype);
  635. void pthread_testcancel (void);
  636. int pthread_once (pthread_once_t * once_control,
  637. void (*init_routine) (void));
  638. #if PTE_LEVEL >= PTE_LEVEL_MAX
  639. pte_cleanup_t * pte_pop_cleanup (int execute);
  640. void pte_push_cleanup (pte_cleanup_t * cleanup,
  641. void (*routine) (void *),
  642. void *arg);
  643. #endif /* PTE_LEVEL >= PTE_LEVEL_MAX */
  644. /*
  645. * Thread Specific Data Functions
  646. */
  647. int pthread_key_create (pthread_key_t * key,
  648. void (*destructor) (void *));
  649. int pthread_key_delete (pthread_key_t key);
  650. int pthread_setspecific (pthread_key_t key,
  651. const void *value);
  652. void * pthread_getspecific (pthread_key_t key);
  653. /*
  654. * Mutex Attribute Functions
  655. */
  656. int pthread_mutexattr_init (pthread_mutexattr_t * attr);
  657. int pthread_mutexattr_destroy (pthread_mutexattr_t * attr);
  658. int pthread_mutexattr_getpshared (const pthread_mutexattr_t
  659. * attr,
  660. int *pshared);
  661. int pthread_mutexattr_setpshared (pthread_mutexattr_t * attr,
  662. int pshared);
  663. int pthread_mutexattr_settype (pthread_mutexattr_t * attr, int kind);
  664. int pthread_mutexattr_gettype (pthread_mutexattr_t * attr, int *kind);
  665. /*
  666. * Barrier Attribute Functions
  667. */
  668. int pthread_barrierattr_init (pthread_barrierattr_t * attr);
  669. int pthread_barrierattr_destroy (pthread_barrierattr_t * attr);
  670. int pthread_barrierattr_getpshared (const pthread_barrierattr_t
  671. * attr,
  672. int *pshared);
  673. int pthread_barrierattr_setpshared (pthread_barrierattr_t * attr,
  674. int pshared);
  675. /*
  676. * Mutex Functions
  677. */
  678. int pthread_mutex_init (pthread_mutex_t * mutex,
  679. const pthread_mutexattr_t * attr);
  680. int pthread_mutex_destroy (pthread_mutex_t * mutex);
  681. int pthread_mutex_lock (pthread_mutex_t * mutex);
  682. int pthread_mutex_timedlock(pthread_mutex_t *mutex,
  683. const struct timespec *abstime);
  684. int pthread_mutex_trylock (pthread_mutex_t * mutex);
  685. int pthread_mutex_unlock (pthread_mutex_t * mutex);
  686. /*
  687. * Spinlock Functions
  688. */
  689. int pthread_spin_init (pthread_spinlock_t * lock, int pshared);
  690. int pthread_spin_destroy (pthread_spinlock_t * lock);
  691. int pthread_spin_lock (pthread_spinlock_t * lock);
  692. int pthread_spin_trylock (pthread_spinlock_t * lock);
  693. int pthread_spin_unlock (pthread_spinlock_t * lock);
  694. /*
  695. * Barrier Functions
  696. */
  697. int pthread_barrier_init (pthread_barrier_t * barrier,
  698. const pthread_barrierattr_t * attr,
  699. unsigned int count);
  700. int pthread_barrier_destroy (pthread_barrier_t * barrier);
  701. int pthread_barrier_wait (pthread_barrier_t * barrier);
  702. /*
  703. * Condition Variable Attribute Functions
  704. */
  705. int pthread_condattr_init (pthread_condattr_t * attr);
  706. int pthread_condattr_destroy (pthread_condattr_t * attr);
  707. int pthread_condattr_getpshared (const pthread_condattr_t * attr,
  708. int *pshared);
  709. int pthread_condattr_setpshared (pthread_condattr_t * attr,
  710. int pshared);
  711. /*
  712. * Condition Variable Functions
  713. */
  714. int pthread_cond_init (pthread_cond_t * cond,
  715. const pthread_condattr_t * attr);
  716. int pthread_cond_destroy (pthread_cond_t * cond);
  717. int pthread_cond_wait (pthread_cond_t * cond,
  718. pthread_mutex_t * mutex);
  719. int pthread_cond_timedwait (pthread_cond_t * cond,
  720. pthread_mutex_t * mutex,
  721. const struct timespec *abstime);
  722. int pthread_cond_signal (pthread_cond_t * cond);
  723. int pthread_cond_broadcast (pthread_cond_t * cond);
  724. /*
  725. * Scheduling
  726. */
  727. int pthread_setschedparam (pthread_t thread,
  728. int policy,
  729. const struct sched_param *param);
  730. int pthread_getschedparam (pthread_t thread,
  731. int *policy,
  732. struct sched_param *param);
  733. int pthread_setconcurrency (int);
  734. int pthread_getconcurrency (void);
  735. /*
  736. * Read-Write Lock Functions
  737. */
  738. int pthread_rwlock_init(pthread_rwlock_t *lock,
  739. const pthread_rwlockattr_t *attr);
  740. int pthread_rwlock_destroy(pthread_rwlock_t *lock);
  741. int pthread_rwlock_tryrdlock(pthread_rwlock_t *);
  742. int pthread_rwlock_trywrlock(pthread_rwlock_t *);
  743. int pthread_rwlock_rdlock(pthread_rwlock_t *lock);
  744. int pthread_rwlock_timedrdlock(pthread_rwlock_t *lock,
  745. const struct timespec *abstime);
  746. int pthread_rwlock_wrlock(pthread_rwlock_t *lock);
  747. int pthread_rwlock_timedwrlock(pthread_rwlock_t *lock,
  748. const struct timespec *abstime);
  749. int pthread_rwlock_unlock(pthread_rwlock_t *lock);
  750. int pthread_rwlockattr_init (pthread_rwlockattr_t * attr);
  751. int pthread_rwlockattr_destroy (pthread_rwlockattr_t * attr);
  752. int pthread_rwlockattr_getpshared (const pthread_rwlockattr_t * attr,
  753. int *pshared);
  754. int pthread_rwlockattr_setpshared (pthread_rwlockattr_t * attr,
  755. int pshared);
  756. #if PTE_LEVEL >= PTE_LEVEL_MAX - 1
  757. /*
  758. * Signal Functions. Should be defined in <signal.h> but we might
  759. * already have signal.h that don't define these.
  760. */
  761. int pthread_kill(pthread_t thread, int sig);
  762. /*
  763. * Non-portable functions
  764. */
  765. /*
  766. * Compatibility with Linux.
  767. */
  768. int pthread_mutexattr_setkind_np(pthread_mutexattr_t * attr,
  769. int kind);
  770. int pthread_mutexattr_getkind_np(pthread_mutexattr_t * attr,
  771. int *kind);
  772. /*
  773. * Possibly supported by other POSIX threads implementations
  774. */
  775. int pthread_delay_np (struct timespec * interval);
  776. int pthread_num_processors_np(void);
  777. /*
  778. * Register a system time change with the library.
  779. * Causes the library to perform various functions
  780. * in response to the change. Should be called whenever
  781. * the application's top level window receives a
  782. * WM_TIMECHANGE message. It can be passed directly to
  783. * pthread_create() as a new thread if desired.
  784. */
  785. void * pthread_timechange_handler_np(void *);
  786. #endif /*PTE_LEVEL >= PTE_LEVEL_MAX - 1 */
  787. #ifdef __cplusplus
  788. }
  789. #endif /* cplusplus */
  790. #if PTE_LEVEL >= PTE_LEVEL_MAX
  791. #endif /* PTE_LEVEL >= PTE_LEVEL_MAX */
  792. /*
  793. * Some compiler environments don't define some things.
  794. */
  795. # define _ftime ftime
  796. # define _timeb timeb
  797. #ifdef __cplusplus
  798. /*
  799. * Internal exceptions
  800. */
  801. class pte_exception {};
  802. class pte_exception_cancel : public pte_exception {};
  803. class pte_exception_exit : public pte_exception {};
  804. #endif
  805. #ifdef PTE_CXX_EXCEPTIONS
  806. /*
  807. * Redefine the C++ catch keyword to ensure that applications
  808. * propagate our internal exceptions up to the library's internal handlers.
  809. */
  810. #define catch( E ) \
  811. catch( pte_exception & ) { throw; } \
  812. catch( E )
  813. #endif /* ! PTE_CXX_EXCEPTIONS */
  814. #undef PTE_LEVEL
  815. #undef PTE_LEVEL_MAX
  816. #endif /* PTHREAD_H */