ithread.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. #ifndef ITHREAD_H
  2. #define ITHREAD_H
  3. /*******************************************************************************
  4. *
  5. * Copyright (c) 2000-2003 Intel Corporation
  6. * All rights reserved.
  7. * Copyright (c) 2012 France Telecom All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * * Neither name of Intel Corporation nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
  25. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  29. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. ******************************************************************************/
  34. /*!
  35. * \file
  36. */
  37. #if !defined(WIN32)
  38. #include <sys/param.h>
  39. #endif
  40. #include "UpnpGlobal.h" /* For UPNP_INLINE, EXPORT_SPEC */
  41. #include "UpnpUniStd.h" /* for close() */
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. #include <pthread.h>
  46. #if defined(BSD) && !defined(__GNU__)
  47. #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE
  48. #endif
  49. #if defined(PTHREAD_MUTEX_RECURSIVE) || defined(__DragonFly__)
  50. /* This system has SuS2-compliant mutex attributes.
  51. * E.g. on Cygwin, where we don't have the old nonportable (NP) symbols
  52. */
  53. #define ITHREAD_MUTEX_FAST_NP PTHREAD_MUTEX_NORMAL
  54. #define ITHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE
  55. #define ITHREAD_MUTEX_ERRORCHECK_NP PTHREAD_MUTEX_ERRORCHECK
  56. #else /* PTHREAD_MUTEX_RECURSIVE */
  57. #define ITHREAD_MUTEX_FAST_NP PTHREAD_MUTEX_FAST_NP
  58. #define ITHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE_NP
  59. #define ITHREAD_MUTEX_ERRORCHECK_NP PTHREAD_MUTEX_ERRORCHECK_NP
  60. #endif /* PTHREAD_MUTEX_RECURSIVE */
  61. #define ITHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE
  62. #define ITHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED
  63. #define ITHREAD_CANCELED PTHREAD_CANCELED
  64. #define ITHREAD_STACK_MIN PTHREAD_STACK_MIN
  65. #define ITHREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED
  66. #define ITHREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE
  67. /***************************************************************************
  68. * Name: ithread_t
  69. *
  70. * Description:
  71. * Thread handle.
  72. * typedef to pthread_t.
  73. * Internal Use Only.
  74. ***************************************************************************/
  75. typedef pthread_t ithread_t;
  76. /****************************************************************************
  77. * Name: ithread_attr_t
  78. *
  79. * Description:
  80. * Thread attribute.
  81. * typedef to pthread_attr_t
  82. * Internal Use Only
  83. ***************************************************************************/
  84. typedef pthread_attr_t ithread_attr_t;
  85. /****************************************************************************
  86. * Name: start_routine
  87. *
  88. * Description:
  89. * Thread start routine
  90. * Internal Use Only.
  91. ***************************************************************************/
  92. typedef void *(*start_routine)(void *arg);
  93. /****************************************************************************
  94. * Name: ithread_cond_t
  95. *
  96. * Description:
  97. * condition variable.
  98. * typedef to pthread_cond_t
  99. * Internal Use Only.
  100. ***************************************************************************/
  101. typedef pthread_cond_t ithread_cond_t;
  102. /****************************************************************************
  103. * Name: ithread_mutexattr_t
  104. *
  105. * Description:
  106. * Mutex attribute.
  107. * typedef to pthread_mutexattr_t
  108. * Internal Use Only
  109. ***************************************************************************/
  110. typedef pthread_mutexattr_t ithread_mutexattr_t;
  111. /****************************************************************************
  112. * Name: ithread_mutex_t
  113. *
  114. * Description:
  115. * Mutex.
  116. * typedef to pthread_mutex_t
  117. * Internal Use Only.
  118. ***************************************************************************/
  119. typedef pthread_mutex_t ithread_mutex_t;
  120. /****************************************************************************
  121. * Name: ithread_condattr_t
  122. *
  123. * Description:
  124. * Condition attribute.
  125. * typedef to pthread_condattr_t
  126. * NOT USED
  127. * Internal Use Only
  128. ***************************************************************************/
  129. typedef pthread_condattr_t ithread_condattr_t;
  130. /****************************************************************************
  131. * Name: ithread_rwlockattr_t
  132. *
  133. * Description:
  134. * Mutex attribute.
  135. * typedef to pthread_rwlockattr_t
  136. * Internal Use Only
  137. ***************************************************************************/
  138. #if UPNP_USE_RWLOCK
  139. typedef pthread_rwlockattr_t ithread_rwlockattr_t;
  140. #endif /* UPNP_USE_RWLOCK */
  141. /****************************************************************************
  142. * Name: ithread_rwlock_t
  143. *
  144. * Description:
  145. * Condition attribute.
  146. * typedef to pthread_rwlock_t
  147. * Internal Use Only
  148. ***************************************************************************/
  149. #if UPNP_USE_RWLOCK
  150. typedef pthread_rwlock_t ithread_rwlock_t;
  151. #else
  152. /* Read-write locks aren't available: use mutex instead. */
  153. typedef ithread_mutex_t ithread_rwlock_t;
  154. #endif /* UPNP_USE_RWLOCK */
  155. /****************************************************************************
  156. * Function: ithread_initialize_library
  157. *
  158. * Description:
  159. * Initializes the library. Does nothing in all implementations, except
  160. * when statically linked for WIN32.
  161. * Parameters:
  162. * none.
  163. * Returns:
  164. * 0 on success, Nonzero on failure.
  165. ***************************************************************************/
  166. static UPNP_INLINE int ithread_initialize_library(void) {
  167. int ret = 0;
  168. return ret;
  169. }
  170. /****************************************************************************
  171. * Function: ithread_cleanup_library
  172. *
  173. * Description:
  174. * Clean up library resources. Does nothing in all implementations, except
  175. * when statically linked for WIN32.
  176. * Parameters:
  177. * none.
  178. * Returns:
  179. * 0 on success, Nonzero on failure.
  180. ***************************************************************************/
  181. static UPNP_INLINE int ithread_cleanup_library(void) {
  182. int ret = 0;
  183. return ret;
  184. }
  185. /****************************************************************************
  186. * Function: ithread_initialize_thread
  187. *
  188. * Description:
  189. * Initializes the thread. Does nothing in all implementations, except
  190. * when statically linked for WIN32.
  191. * Parameters:
  192. * none.
  193. * Returns:
  194. * 0 on success, Nonzero on failure.
  195. ***************************************************************************/
  196. static UPNP_INLINE int ithread_initialize_thread(void) {
  197. int ret = 0;
  198. #if defined(WIN32) && defined(PTW32_STATIC_LIB)
  199. ret = !pthread_win32_thread_attach_np();
  200. #endif
  201. return ret;
  202. }
  203. /****************************************************************************
  204. * Function: ithread_cleanup_thread
  205. *
  206. * Description:
  207. * Clean up thread resources. Does nothing in all implementations, except
  208. * when statically linked for WIN32.
  209. * Parameters:
  210. * none.
  211. * Returns:
  212. * 0 on success, Nonzero on failure.
  213. ***************************************************************************/
  214. static UPNP_INLINE int ithread_cleanup_thread(void) {
  215. int ret = 0;
  216. #if defined(WIN32) && defined(PTW32_STATIC_LIB)
  217. ret = !pthread_win32_thread_detach_np();
  218. #endif
  219. return ret;
  220. }
  221. /****************************************************************************
  222. * Function: ithread_mutexattr_init
  223. *
  224. * Description:
  225. * Initializes a mutex attribute variable.
  226. * Used to set the type of the mutex.
  227. * Parameters:
  228. * ithread_mutexattr_init * attr (must be valid non NULL pointer to
  229. * pthread_mutexattr_t)
  230. * Returns:
  231. * 0 on success, Nonzero on failure.
  232. * Always returns 0.
  233. * See man page for pthread_mutexattr_init
  234. ***************************************************************************/
  235. #define ithread_mutexattr_init pthread_mutexattr_init
  236. /****************************************************************************
  237. * Function: ithread_mutexattr_destroy
  238. *
  239. * Description:
  240. * Releases any resources held by the mutex attribute.
  241. * Currently there are no resources associated with the attribute
  242. * Parameters:
  243. * ithread_mutexattr_t * attr (must be valid non NULL pointer to
  244. * pthread_mutexattr_t)
  245. * Returns:
  246. * 0 on success, Nonzero on failure.
  247. * Always returns 0.
  248. * See man page for pthread_mutexattr_destroy
  249. ***************************************************************************/
  250. #define ithread_mutexattr_destroy pthread_mutexattr_destroy
  251. /****************************************************************************
  252. * Function: ithread_mutexattr_setkind_np
  253. *
  254. * Description:
  255. * Sets the mutex type in the attribute.
  256. * Valid types are: ITHREAD_MUTEX_FAST_NP
  257. * ITHREAD_MUTEX_RECURSIVE_NP
  258. * ITHREAD_MUTEX_ERRORCHECK_NP
  259. *
  260. * Parameters:
  261. * ithread_mutexattr_t * attr (must be valid non NULL pointer to
  262. * ithread_mutexattr_t)
  263. * int kind (one of ITHREAD_MUTEX_FAST_NP or ITHREAD_MUTEX_RECURSIVE_NP
  264. * or ITHREAD_MUTEX_ERRORCHECK_NP)
  265. * Returns:
  266. * 0 on success. Nonzero on failure.
  267. * Returns EINVAL if the kind is not supported.
  268. * See man page for pthread_mutexattr_setkind_np
  269. *****************************************************************************/
  270. #if defined(PTHREAD_MUTEX_RECURSIVE) || defined(__DragonFly__)
  271. #define ithread_mutexattr_setkind_np pthread_mutexattr_settype
  272. #else
  273. #define ithread_mutexattr_setkind_np pthread_mutexattr_setkind_np
  274. #endif /* UPNP_USE_RWLOCK */
  275. /****************************************************************************
  276. * Function: ithread_mutexattr_getkind_np
  277. *
  278. * Description:
  279. * Gets the mutex type in the attribute.
  280. * Valid types are: ITHREAD_MUTEX_FAST_NP
  281. * ITHREAD_MUTEX_RECURSIVE_NP
  282. * ITHREAD_MUTEX_ERRORCHECK_NP
  283. *
  284. * Parameters:
  285. * ithread_mutexattr_t * attr (must be valid non NULL pointer to
  286. * pthread_mutexattr_t)
  287. * int *kind (one of ITHREAD_MUTEX_FAST_NP or ITHREAD_MUTEX_RECURSIVE_NP
  288. * or ITHREAD_MUTEX_ERRORCHECK_NP)
  289. * Returns:
  290. * 0 on success. Nonzero on failure.
  291. * Always returns 0.
  292. * See man page for pthread_mutexattr_getkind_np
  293. *****************************************************************************/
  294. #if defined(PTHREAD_MUTEX_RECURSIVE) || defined(__DragonFly__)
  295. #define ithread_mutexattr_getkind_np pthread_mutexattr_gettype
  296. #else
  297. #define ithread_mutexattr_getkind_np pthread_mutexattr_getkind_np
  298. #endif /* UPNP_USE_RWLOCK */
  299. /****************************************************************************
  300. * Function: ithread_mutex_init
  301. *
  302. * Description:
  303. * Initializes mutex.
  304. * Must be called before use.
  305. *
  306. * Parameters:
  307. * ithread_mutex_t * mutex (must be valid non NULL pointer to pthread_mutex_t)
  308. * const ithread_mutexattr_t * mutex_attr
  309. * Returns:
  310. * 0 on success, Nonzero on failure.
  311. * Always returns 0.
  312. * See man page for pthread_mutex_init
  313. *****************************************************************************/
  314. #define ithread_mutex_init pthread_mutex_init
  315. /****************************************************************************
  316. * Function: ithread_mutex_lock
  317. *
  318. * Description:
  319. * Locks mutex.
  320. * Parameters:
  321. * ithread_mutex_t * mutex (must be valid non NULL pointer to pthread_mutex_t)
  322. * mutex must be initialized.
  323. *
  324. * Returns:
  325. * 0 on success, Nonzero on failure.
  326. * Always returns 0.
  327. * See man page for pthread_mutex_lock
  328. *****************************************************************************/
  329. #define ithread_mutex_lock pthread_mutex_lock
  330. /****************************************************************************
  331. * Function: ithread_mutex_unlock
  332. *
  333. * Description:
  334. * Unlocks mutex.
  335. *
  336. * Parameters:
  337. * ithread_mutex_t * mutex (must be valid non NULL pointer to pthread_mutex_t)
  338. * mutex must be initialized.
  339. *
  340. * Returns:
  341. * 0 on success, Nonzero on failure.
  342. * Always returns 0.
  343. * See man page for pthread_mutex_unlock
  344. *****************************************************************************/
  345. #define ithread_mutex_unlock pthread_mutex_unlock
  346. /****************************************************************************
  347. * Function: ithread_mutex_destroy
  348. *
  349. * Description:
  350. * Releases any resources held by the mutex.
  351. * Mutex can no longer be used after this call.
  352. * Mutex is only destroyed when there are no longer any threads waiting on it.
  353. * Mutex cannot be destroyed if it is locked.
  354. * Parameters:
  355. * ithread_mutex_t * mutex (must be valid non NULL pointer to pthread_mutex_t)
  356. * mutex must be initialized.
  357. * Returns:
  358. * 0 on success. Nonzero on failure.
  359. * Always returns 0.
  360. * See man page for pthread_mutex_destroy
  361. *****************************************************************************/
  362. #define ithread_mutex_destroy pthread_mutex_destroy
  363. /****************************************************************************
  364. * Function: ithread_rwlockattr_init
  365. *
  366. * Description:
  367. * Initializes a rwlock attribute variable to default values.
  368. * Parameters:
  369. * const ithread_rwlockattr_init *attr (must be valid non NULL pointer to
  370. * pthread_rwlockattr_t)
  371. * Returns:
  372. * 0 on success, Nonzero on failure.
  373. * Always returns 0.
  374. * See man page for pthread_rwlockattr_init
  375. ***************************************************************************/
  376. #if UPNP_USE_RWLOCK
  377. #define ithread_rwlockattr_init pthread_rwlockattr_init
  378. #endif /* UPNP_USE_RWLOCK */
  379. /****************************************************************************
  380. * Function: ithread_rwlockattr_destroy
  381. *
  382. * Description:
  383. * Releases any resources held by the rwlock attribute.
  384. * Parameters:
  385. * ithread_rwlockattr_t *attr (must be valid non NULL pointer to
  386. * pthread_rwlockattr_t)
  387. * Returns:
  388. * 0 on success, Nonzero on failure.
  389. * Always returns 0.
  390. * See man page for pthread_rwlockattr_destroy
  391. ***************************************************************************/
  392. #if UPNP_USE_RWLOCK
  393. #define ithread_rwlockattr_destroy pthread_rwlockattr_destroy
  394. #endif /* UPNP_USE_RWLOCK */
  395. /****************************************************************************
  396. * Function: ithread_rwlockatttr_setpshared
  397. *
  398. * Description:
  399. * Sets the rwlock type in the attribute.
  400. * Valid types are: ITHREAD_PROCESS_PRIVATE
  401. * ITHREAD_PROCESS_SHARED
  402. *
  403. * Parameters:
  404. * ithread_rwlockattr_t * attr (must be valid non NULL pointer to
  405. * ithread_rwlockattr_t)
  406. * int kind (one of ITHREAD_PROCESS_PRIVATE or ITHREAD_PROCESS_SHARED)
  407. *
  408. * Returns:
  409. * 0 on success. Nonzero on failure.
  410. * Returns EINVAL if the kind is not supported.
  411. * See man page for pthread_rwlockattr_setkind_np
  412. *****************************************************************************/
  413. #if UPNP_USE_RWLOCK
  414. #define ithread_rwlockatttr_setpshared pthread_rwlockatttr_setpshared
  415. #endif /* UPNP_USE_RWLOCK */
  416. /****************************************************************************
  417. * Function: ithread_rwlockatttr_getpshared
  418. *
  419. * Description:
  420. * Gets the rwlock type in the attribute.
  421. * Valid types are: ITHREAD_PROCESS_PRIVATE
  422. * ITHREAD_PROCESS_SHARED
  423. *
  424. * Parameters:
  425. * ithread_rwlockattr_t * attr (must be valid non NULL pointer to
  426. * pthread_rwlockattr_t)
  427. * int *kind (one of ITHREAD_PROCESS_PRIVATE or ITHREAD_PROCESS_SHARED)
  428. *
  429. * Returns:
  430. * 0 on success. Nonzero on failure.
  431. * Always returns 0.
  432. * See man page for pthread_rwlockatttr_getpshared
  433. *****************************************************************************/
  434. #if UPNP_USE_RWLOCK
  435. #define ithread_rwlockatttr_getpshared pthread_rwlockatttr_getpshared
  436. #endif /* UPNP_USE_RWLOCK */
  437. /****************************************************************************
  438. * Function: ithread_rwlock_init
  439. *
  440. * Description:
  441. * Initializes rwlock.
  442. * Must be called before use.
  443. *
  444. * Parameters:
  445. * ithread_rwlock_t *rwlock (must be valid non NULL pointer to pthread_rwlock_t)
  446. * const ithread_rwlockattr_t *rwlock_attr
  447. * Returns:
  448. * 0 on success, Nonzero on failure.
  449. * Always returns 0.
  450. * See man page for pthread_rwlock_init
  451. *****************************************************************************/
  452. #if UPNP_USE_RWLOCK
  453. #define ithread_rwlock_init pthread_rwlock_init
  454. #else
  455. /* Read-write locks aren't available: use mutex instead. */
  456. #define ithread_rwlock_init ithread_mutex_init
  457. #endif
  458. /****************************************************************************
  459. * Function: ithread_rwlock_rdlock
  460. *
  461. * Description:
  462. * Locks rwlock for reading.
  463. * Parameters:
  464. * ithread_rwlock_t *rwlock (must be valid non NULL pointer to pthread_rwlock_t)
  465. * rwlock must be initialized.
  466. *
  467. * Returns:
  468. * 0 on success, Nonzero on failure.
  469. * Always returns 0.
  470. * See man page for pthread_rwlock_rdlock
  471. *****************************************************************************/
  472. #if UPNP_USE_RWLOCK
  473. #define ithread_rwlock_rdlock pthread_rwlock_rdlock
  474. #else
  475. /* Read-write locks aren't available: use mutex instead. */
  476. #define ithread_rwlock_rdlock ithread_mutex_lock
  477. #endif /* UPNP_USE_RWLOCK */
  478. /****************************************************************************
  479. * Function: ithread_rwlock_wrlock
  480. *
  481. * Description:
  482. * Locks rwlock for writting.
  483. * Parameters:
  484. * ithread_rwlock_t *rwlock (must be valid non NULL pointer to pthread_rwlock_t)
  485. * rwlock must be initialized.
  486. *
  487. * Returns:
  488. * 0 on success, Nonzero on failure.
  489. * Always returns 0.
  490. * See man page for pthread_rwlock_wrlock
  491. *****************************************************************************/
  492. #if UPNP_USE_RWLOCK
  493. #define ithread_rwlock_wrlock pthread_rwlock_wrlock
  494. #else
  495. /* Read-write locks aren't available: use mutex instead. */
  496. #define ithread_rwlock_wrlock ithread_mutex_lock
  497. #endif /* UPNP_USE_RWLOCK */
  498. /****************************************************************************
  499. * Function: ithread_rwlock_unlock
  500. *
  501. * Description:
  502. * Unlocks rwlock.
  503. *
  504. * Parameters:
  505. * ithread_rwlock_t *rwlock (must be valid non NULL pointer to pthread_rwlock_t)
  506. * rwlock must be initialized.
  507. *
  508. * Returns:
  509. * 0 on success, Nonzero on failure.
  510. * Always returns 0.
  511. * See man page for pthread_rwlock_unlock
  512. *****************************************************************************/
  513. #if UPNP_USE_RWLOCK
  514. #define ithread_rwlock_unlock pthread_rwlock_unlock
  515. #else
  516. /* Read-write locks aren't available: use mutex instead. */
  517. #define ithread_rwlock_unlock ithread_mutex_unlock
  518. #endif /* UPNP_USE_RWLOCK */
  519. /****************************************************************************
  520. * Function: ithread_rwlock_destroy
  521. *
  522. * Description:
  523. * Releases any resources held by the rwlock.
  524. * rwlock can no longer be used after this call.
  525. * rwlock is only destroyed when there are no longer any threads waiting on it.
  526. * rwlock cannot be destroyed if it is locked.
  527. * Parameters:
  528. * ithread_rwlock_t *rwlock (must be valid non NULL pointer to pthread_rwlock_t)
  529. * rwlock must be initialized.
  530. * Returns:
  531. * 0 on success. Nonzero on failure.
  532. * Always returns 0.
  533. * See man page for pthread_rwlock_destroy
  534. *****************************************************************************/
  535. #if UPNP_USE_RWLOCK
  536. #define ithread_rwlock_destroy pthread_rwlock_destroy
  537. #else
  538. /* Read-write locks aren't available: use mutex instead. */
  539. #define ithread_rwlock_destroy ithread_mutex_destroy
  540. #endif /* UPNP_USE_RWLOCK */
  541. /****************************************************************************
  542. * Function: ithread_cond_init
  543. *
  544. * Description:
  545. * Initializes condition variable.
  546. * Must be called before use.
  547. * Parameters:
  548. * ithread_cond_t *cond (must be valid non NULL pointer to pthread_cond_t)
  549. * const ithread_condattr_t *cond_attr (ignored)
  550. * Returns:
  551. * 0 on success, Nonzero on failure.
  552. * See man page for pthread_cond_init
  553. *****************************************************************************/
  554. #define ithread_cond_init pthread_cond_init
  555. /****************************************************************************
  556. * Function: ithread_cond_signal
  557. *
  558. * Description:
  559. * Wakes up exactly one thread waiting on condition.
  560. * Associated mutex MUST be locked by thread before entering this call.
  561. * Parameters:
  562. * ithread_cond_t *cond (must be valid non NULL pointer to
  563. * ithread_cond_t)
  564. * cond must be initialized
  565. * Returns:
  566. * 0 on success, Nonzero on failure.
  567. * See man page for pthread_cond_signal
  568. *****************************************************************************/
  569. #define ithread_cond_signal pthread_cond_signal
  570. /****************************************************************************
  571. * Function: ithread_cond_broadcast
  572. *
  573. * Description:
  574. * Wakes up all threads waiting on condition.
  575. * Associated mutex MUST be locked by thread before entering this call.
  576. * Parameters:
  577. * ithread_cond_t *cond (must be valid non NULL pointer to
  578. * ithread_cond_t)
  579. * cond must be initialized
  580. * Returns:
  581. * 0 on success, Nonzero on failure.
  582. * See man page for pthread_cond_broadcast
  583. *****************************************************************************/
  584. #define ithread_cond_broadcast pthread_cond_broadcast
  585. /****************************************************************************
  586. * Function: ithread_cond_wait
  587. *
  588. * Description:
  589. * Atomically releases mutex and waits on condition.
  590. * Associated mutex MUST be locked by thread before entering this call.
  591. * Mutex is reacquired when call returns.
  592. * Parameters:
  593. * ithread_cond_t *cond (must be valid non NULL pointer to
  594. * ithread_cond_t)
  595. * cond must be initialized
  596. * ithread_mutex_t *mutex (must be valid non NULL pointer to
  597. * ithread_mutex_t)
  598. * Mutex must be locked.
  599. * Returns:
  600. * 0 on success, Nonzero on failure.
  601. * See man page for pthread_cond_wait
  602. *****************************************************************************/
  603. #define ithread_cond_wait pthread_cond_wait
  604. /****************************************************************************
  605. * Function: pthread_cond_timedwait
  606. *
  607. * Description:
  608. * Atomically releases the associated mutex and waits on the
  609. * condition.
  610. * If the condition is not signaled in the specified time than the
  611. * call times out and returns.
  612. * Associated mutex MUST be locked by thread before entering this call.
  613. * Mutex is reacquired when call returns.
  614. * Parameters:
  615. * ithread_cond_t *cond (must be valid non NULL pointer to ithread_cond_t)
  616. * cond must be initialized
  617. * ithread_mutex_t *mutex (must be valid non NULL pointer to ithread_mutex_t)
  618. * Mutex must be locked.
  619. * const struct timespec *abstime (absolute time, measured from Jan 1, 1970)
  620. * Returns:
  621. * 0 on success. ETIMEDOUT on timeout. Nonzero on failure.
  622. * See man page for pthread_cond_timedwait
  623. ***************************************************************************/
  624. #define ithread_cond_timedwait pthread_cond_timedwait
  625. /****************************************************************************
  626. * Function: ithread_cond_destroy
  627. *
  628. * Description:
  629. * Releases any resources held by the condition variable.
  630. * Condition variable can no longer be used after this call.
  631. * Parameters:
  632. * ithread_cond_t *cond (must be valid non NULL pointer to
  633. * ithread_cond_t)
  634. * cond must be initialized.
  635. * Returns:
  636. * 0 on success. Nonzero on failure.
  637. * See man page for pthread_cond_destroy
  638. ***************************************************************************/
  639. #define ithread_cond_destroy pthread_cond_destroy
  640. /****************************************************************************
  641. * Function: ithread_attr_init
  642. *
  643. * Description:
  644. * Initialises thread attribute object.
  645. * Parameters:
  646. * ithread_attr_t *attr (must be valid non NULL pointer to
  647. * ithread_attr_t)
  648. * Returns:
  649. * 0 on success. Nonzero on failure.
  650. * See man page for pthread_attr_init
  651. ***************************************************************************/
  652. #define ithread_attr_init pthread_attr_init
  653. /****************************************************************************
  654. * Function: ithread_attr_destroy
  655. *
  656. * Description:
  657. * Destroys thread attribute object.
  658. * Parameters:
  659. * ithread_attr_t *attr (must be valid non NULL pointer to
  660. * ithread_attr_t)
  661. * Returns:
  662. * 0 on success. Nonzero on failure.
  663. * See man page for pthread_attr_destroy
  664. ***************************************************************************/
  665. #define ithread_attr_destroy pthread_attr_destroy
  666. /****************************************************************************
  667. * Function: ithread_attr_setstacksize
  668. *
  669. * Description:
  670. * Sets stack size of a thread attribute object.
  671. * Parameters:
  672. * ithread_attr_t *attr (must be valid non NULL pointer to
  673. * ithread_attr_t)
  674. * size_t stacksize (value of stacksize must be greater than
  675. * ITHREAD_STACK_MIN and lower than system-imposed limits
  676. * Returns:
  677. * 0 on success. Nonzero on failure.
  678. * See man page for pthread_attr_setstacksize
  679. ***************************************************************************/
  680. #define ithread_attr_setstacksize pthread_attr_setstacksize
  681. /****************************************************************************
  682. * Function: ithread_attr_setdetachstate
  683. *
  684. * Description:
  685. * Sets detach state of a thread attribute object.
  686. * Parameters:
  687. * ithread_attr_t *attr (must be valid non NULL pointer to
  688. * ithread_attr_t)
  689. * int detachstate (value of detachstate must be ITHREAD_CREATE_DETACHED
  690. * or ITHREAD_CREATE_JOINABLE)
  691. * Returns:
  692. * 0 on success. Nonzero on failure.
  693. * See man page for pthread_attr_setdetachstate
  694. ***************************************************************************/
  695. #define ithread_attr_setdetachstate pthread_attr_setdetachstate
  696. /****************************************************************************
  697. * Function: ithread_create
  698. *
  699. * Description:
  700. * Creates a thread with the given start routine
  701. * and argument.
  702. * Parameters:
  703. * ithread_t * thread (must be valid non NULL pointer to pthread_t)
  704. * ithread_attr_t *attr
  705. * void * (start_routine) (void *arg) (start routine)
  706. * void * arg - argument.
  707. * Returns:
  708. * 0 on success. Nonzero on failure.
  709. * Returns EAGAIN if a new thread can not be created.
  710. * Returns EINVAL if there is a problem with the arguments.
  711. * See man page fore pthread_create
  712. ***************************************************************************/
  713. #define ithread_create pthread_create
  714. /****************************************************************************
  715. * Function: ithread_cancel
  716. *
  717. * Description:
  718. * Cancels a thread.
  719. * Parameters:
  720. * ithread_t * thread (must be valid non NULL pointer to ithread_t)
  721. * Returns:
  722. * 0 on success. Nonzero on failure.
  723. * See man page for pthread_cancel
  724. ***************************************************************************/
  725. #define ithread_cancel pthread_cancel
  726. /****************************************************************************
  727. * Function: ithread_exit
  728. *
  729. * Description:
  730. * Returns a return code from a thread.
  731. * Implicitly called when the start routine returns.
  732. * Parameters:
  733. * void * return_code return code to return
  734. * See man page for pthread_exit
  735. ***************************************************************************/
  736. #define ithread_exit pthread_exit
  737. /****************************************************************************
  738. * Function: ithread_get_current_thread_id
  739. *
  740. * Description:
  741. * Returns the handle of the currently running thread.
  742. * Returns:
  743. * The handle of the currently running thread.
  744. * See man page for pthread_self
  745. ***************************************************************************/
  746. #define ithread_get_current_thread_id pthread_self
  747. /****************************************************************************
  748. * Function: ithread_self
  749. *
  750. * Description:
  751. * Returns the handle of the currently running thread.
  752. * Returns:
  753. * The handle of the currently running thread.
  754. * See man page for pthread_self
  755. ***************************************************************************/
  756. #define ithread_self pthread_self
  757. /****************************************************************************
  758. * Function: ithread_detach
  759. *
  760. * Description:
  761. * Makes a thread's resources reclaimed immediately
  762. * after it finishes
  763. * execution.
  764. * Returns:
  765. * 0 on success, Nonzero on failure.
  766. * See man page for pthread_detach
  767. ***************************************************************************/
  768. #define ithread_detach pthread_detach
  769. /****************************************************************************
  770. * Function: ithread_join
  771. *
  772. * Description:
  773. * Suspends the currently running thread until the
  774. * specified thread
  775. * has finished.
  776. * Returns the return code of the thread, or ITHREAD_CANCELED
  777. * if the thread has been canceled.
  778. * Parameters:
  779. * ithread_t *thread (valid non null thread identifier)
  780. * void ** return (space for return code)
  781. * Returns:
  782. * 0 on success, Nonzero on failure.
  783. * See man page for pthread_join
  784. ***************************************************************************/
  785. #define ithread_join pthread_join
  786. /****************************************************************************
  787. * Function: isleep
  788. *
  789. * Description:
  790. * Suspends the currently running thread for the specified number
  791. * of seconds
  792. * Always returns 0.
  793. * Parameters:
  794. * unsigned int seconds - number of seconds to sleep.
  795. * Returns:
  796. * 0 on success, Nonzero on failure.
  797. * See man page for sleep (man 3 sleep)
  798. *****************************************************************************/
  799. #ifdef WIN32
  800. #define isleep(x) Sleep((x)*1000)
  801. #else
  802. #define isleep sleep
  803. #endif
  804. /****************************************************************************
  805. * Function: isleep
  806. *
  807. * Description:
  808. * Suspends the currently running thread for the specified number
  809. * of milliseconds
  810. * Always returns 0.
  811. * Parameters:
  812. * unsigned int milliseconds - number of milliseconds to sleep.
  813. * Returns:
  814. * 0 on success, Nonzero on failure.
  815. * See man page for sleep (man 3 sleep)
  816. *****************************************************************************/
  817. #ifdef WIN32
  818. #define imillisleep Sleep
  819. #else
  820. #define imillisleep(x) usleep(1000*x)
  821. #endif
  822. #if !defined(PTHREAD_MUTEX_RECURSIVE) && !defined(__DragonFly__) && !defined(UPNP_USE_MSVCPP)
  823. /* !defined(UPNP_USE_MSVCPP) should probably also have pthreads version check - but it's not clear if that is possible */
  824. /* NK: Added for satisfying the gcc compiler */
  825. EXPORT_SPEC int pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind);
  826. #endif
  827. #ifdef __cplusplus
  828. }
  829. #endif
  830. #endif /* ITHREAD_H */