os.h 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJ_OS_H__
  20. #define __PJ_OS_H__
  21. /**
  22. * @file os.h
  23. * @brief OS dependent functions
  24. */
  25. #include <pj/types.h>
  26. PJ_BEGIN_DECL
  27. /**
  28. * @defgroup PJ_OS Operating System Dependent Functionality.
  29. */
  30. /* **************************************************************************/
  31. /**
  32. * @defgroup PJ_SYS_INFO System Information
  33. * @ingroup PJ_OS
  34. * @{
  35. */
  36. /**
  37. * These enumeration contains constants to indicate support of miscellaneous
  38. * system features. These will go in "flags" field of #pj_sys_info structure.
  39. */
  40. typedef enum pj_sys_info_flag
  41. {
  42. /**
  43. * Support for Apple iOS background feature.
  44. */
  45. PJ_SYS_HAS_IOS_BG = 1
  46. } pj_sys_info_flag;
  47. /**
  48. * This structure contains information about the system. Use #pj_get_sys_info()
  49. * to obtain the system information.
  50. */
  51. typedef struct pj_sys_info
  52. {
  53. /**
  54. * Null terminated string containing processor information (e.g. "i386",
  55. * "x86_64"). It may contain empty string if the value cannot be obtained.
  56. */
  57. pj_str_t machine;
  58. /**
  59. * Null terminated string identifying the system operation (e.g. "Linux",
  60. * "win32", "wince"). It may contain empty string if the value cannot be
  61. * obtained.
  62. */
  63. pj_str_t os_name;
  64. /**
  65. * A number containing the operating system version number. By convention,
  66. * this field is divided into four bytes, where the highest order byte
  67. * contains the most major version of the OS, the next less significant
  68. * byte contains the less major version, and so on. How the OS version
  69. * number is mapped into these four bytes would be specific for each OS.
  70. * For example, Linux-2.6.32-28 would yield "os_ver" value of 0x0206201c,
  71. * while for Windows 7 it will be 0x06010000 (because dwMajorVersion is
  72. * 6 and dwMinorVersion is 1 for Windows 7).
  73. *
  74. * This field may contain zero if the OS version cannot be obtained.
  75. */
  76. pj_uint32_t os_ver;
  77. /**
  78. * Null terminated string identifying the SDK name that is used to build
  79. * the library (e.g. "glibc", "uclibc", "msvc", "wince"). It may contain
  80. * empty string if the value cannot eb obtained.
  81. */
  82. pj_str_t sdk_name;
  83. /**
  84. * A number containing the SDK version, using the numbering convention as
  85. * the "os_ver" field. The value will be zero if the version cannot be
  86. * obtained.
  87. */
  88. pj_uint32_t sdk_ver;
  89. /**
  90. * A longer null terminated string identifying the underlying system with
  91. * as much information as possible.
  92. */
  93. pj_str_t info;
  94. /**
  95. * Other flags containing system specific information. The value is
  96. * bitmask of #pj_sys_info_flag constants.
  97. */
  98. pj_uint32_t flags;
  99. } pj_sys_info;
  100. /**
  101. * Obtain the system information.
  102. *
  103. * @return System information structure.
  104. */
  105. PJ_DECL(const pj_sys_info*) pj_get_sys_info(void);
  106. /**
  107. * @}
  108. */
  109. /* **************************************************************************/
  110. /**
  111. * @defgroup PJ_THREAD Threads
  112. * @ingroup PJ_OS
  113. * @{
  114. * This module provides multithreading API.
  115. *
  116. * \section pj_thread_examples_sec Examples
  117. *
  118. * For examples, please see:
  119. * - Thread test: \src{pjlib/src/pjlib-test/thread.c}
  120. * - Sleep, Time, and Timestamp test: \src{pjlib/src/pjlib-test/sleep.c}
  121. *
  122. */
  123. /**
  124. * Thread creation flags:
  125. * - PJ_THREAD_SUSPENDED: specify that the thread should be created suspended.
  126. */
  127. typedef enum pj_thread_create_flags
  128. {
  129. PJ_THREAD_SUSPENDED = 1
  130. } pj_thread_create_flags;
  131. /**
  132. * Type of thread entry function.
  133. */
  134. typedef int (PJ_THREAD_FUNC pj_thread_proc)(void*);
  135. /**
  136. * Size of thread struct.
  137. */
  138. #if !defined(PJ_THREAD_DESC_SIZE)
  139. # define PJ_THREAD_DESC_SIZE (64)
  140. #endif
  141. /**
  142. * Thread structure, to thread's state when the thread is created by external
  143. * or native API.
  144. */
  145. typedef long pj_thread_desc[PJ_THREAD_DESC_SIZE];
  146. /**
  147. * Get process ID.
  148. * @return process ID.
  149. */
  150. PJ_DECL(pj_uint32_t) pj_getpid(void);
  151. /**
  152. * Create a new thread.
  153. *
  154. * @param pool The memory pool from which the thread record
  155. * will be allocated from.
  156. * @param thread_name The optional name to be assigned to the thread.
  157. * @param proc Thread entry function.
  158. * @param arg Argument to be passed to the thread entry function.
  159. * @param stack_size The size of the stack for the new thread, or ZERO or
  160. * PJ_THREAD_DEFAULT_STACK_SIZE to let the
  161. * library choose the reasonable size for the stack.
  162. * For some systems, the stack will be allocated from
  163. * the pool, so the pool must have suitable capacity.
  164. * @param flags Flags for thread creation, which is bitmask combination
  165. * from enum pj_thread_create_flags.
  166. * @param thread Pointer to hold the newly created thread.
  167. *
  168. * @return PJ_SUCCESS on success, or the error code.
  169. */
  170. PJ_DECL(pj_status_t) pj_thread_create( pj_pool_t *pool,
  171. const char *thread_name,
  172. pj_thread_proc *proc,
  173. void *arg,
  174. pj_size_t stack_size,
  175. unsigned flags,
  176. pj_thread_t **thread );
  177. /**
  178. * Register a thread that was created by external or native API to PJLIB.
  179. * This function must be called in the context of the thread being registered.
  180. * When the thread is created by external function or API call,
  181. * it must be 'registered' to PJLIB using pj_thread_register(), so that it can
  182. * cooperate with PJLIB's framework. During registration, some data needs to
  183. * be maintained, and this data must remain available during the thread's
  184. * lifetime.
  185. *
  186. * @param thread_name The optional name to be assigned to the thread.
  187. * @param desc Thread descriptor, which must be available throughout
  188. * the lifetime of the thread.
  189. * @param thread Pointer to hold the created thread handle.
  190. *
  191. * @return PJ_SUCCESS on success, or the error code.
  192. */
  193. PJ_DECL(pj_status_t) pj_thread_register ( const char *thread_name,
  194. pj_thread_desc desc,
  195. pj_thread_t **thread);
  196. /**
  197. * Check if this thread has been registered to PJLIB.
  198. *
  199. * @return Non-zero if it is registered.
  200. */
  201. PJ_DECL(pj_bool_t) pj_thread_is_registered(void);
  202. /**
  203. * Get thread priority value for the thread.
  204. *
  205. * @param thread Thread handle.
  206. *
  207. * @return Thread priority value, or -1 on error.
  208. */
  209. PJ_DECL(int) pj_thread_get_prio(pj_thread_t *thread);
  210. /**
  211. * Set the thread priority. The priority value must be in the priority
  212. * value range, which can be retrieved with #pj_thread_get_prio_min() and
  213. * #pj_thread_get_prio_max() functions.
  214. *
  215. * For Android, this function will only set the priority of the calling thread
  216. * (the thread param must be set to NULL or the calling thread handle).
  217. *
  218. * @param thread Thread handle.
  219. * @param prio New priority to be set to the thread.
  220. *
  221. * @return PJ_SUCCESS on success or the error code.
  222. */
  223. PJ_DECL(pj_status_t) pj_thread_set_prio(pj_thread_t *thread, int prio);
  224. /**
  225. * Get the lowest priority value available for this thread.
  226. *
  227. * @param thread Thread handle.
  228. * @return Minimum thread priority value, or -1 on error.
  229. */
  230. PJ_DECL(int) pj_thread_get_prio_min(pj_thread_t *thread);
  231. /**
  232. * Get the highest priority value available for this thread.
  233. *
  234. * @param thread Thread handle.
  235. * @return Minimum thread priority value, or -1 on error.
  236. */
  237. PJ_DECL(int) pj_thread_get_prio_max(pj_thread_t *thread);
  238. /**
  239. * Return native handle from pj_thread_t for manipulation using native
  240. * OS APIs.
  241. *
  242. * @param thread PJLIB thread descriptor.
  243. *
  244. * @return Native thread handle. For example, when the
  245. * backend thread uses pthread, this function will
  246. * return pointer to pthread_t, and on Windows,
  247. * this function will return HANDLE.
  248. */
  249. PJ_DECL(void*) pj_thread_get_os_handle(pj_thread_t *thread);
  250. /**
  251. * Get thread name.
  252. *
  253. * @param thread The thread handle.
  254. *
  255. * @return Thread name as null terminated string.
  256. */
  257. PJ_DECL(const char*) pj_thread_get_name(pj_thread_t *thread);
  258. /**
  259. * Resume a suspended thread.
  260. *
  261. * @param thread The thread handle.
  262. *
  263. * @return zero on success.
  264. */
  265. PJ_DECL(pj_status_t) pj_thread_resume(pj_thread_t *thread);
  266. /**
  267. * Get the current thread.
  268. *
  269. * @return Thread handle of current thread.
  270. */
  271. PJ_DECL(pj_thread_t*) pj_thread_this(void);
  272. /**
  273. * Join thread, and block the caller thread until the specified thread exits.
  274. * If it is called from within the thread itself, it will return immediately
  275. * with failure status.
  276. * If the specified thread has already been dead, or it does not exist,
  277. * the function will return immediately with successful status.
  278. *
  279. * @param thread The thread handle.
  280. *
  281. * @return PJ_SUCCESS on success.
  282. */
  283. PJ_DECL(pj_status_t) pj_thread_join(pj_thread_t *thread);
  284. /**
  285. * Destroy thread and release resources allocated for the thread.
  286. * However, the memory allocated for the pj_thread_t itself will only be released
  287. * when the pool used to create the thread is destroyed.
  288. *
  289. * @param thread The thread handle.
  290. *
  291. * @return zero on success.
  292. */
  293. PJ_DECL(pj_status_t) pj_thread_destroy(pj_thread_t *thread);
  294. /**
  295. * Put the current thread to sleep for the specified miliseconds.
  296. *
  297. * @param msec Miliseconds delay.
  298. *
  299. * @return zero if successfull.
  300. */
  301. PJ_DECL(pj_status_t) pj_thread_sleep(unsigned msec);
  302. /**
  303. * @def PJ_CHECK_STACK()
  304. * PJ_CHECK_STACK() macro is used to check the sanity of the stack.
  305. * The OS implementation may check that no stack overflow occurs, and
  306. * it also may collect statistic about stack usage.
  307. */
  308. #if defined(PJ_OS_HAS_CHECK_STACK) && PJ_OS_HAS_CHECK_STACK!=0
  309. # define PJ_CHECK_STACK() pj_thread_check_stack(__FILE__, __LINE__)
  310. /** @internal
  311. * The implementation of stack checking.
  312. */
  313. PJ_DECL(void) pj_thread_check_stack(const char *file, int line);
  314. /** @internal
  315. * Get maximum stack usage statistic.
  316. */
  317. PJ_DECL(pj_uint32_t) pj_thread_get_stack_max_usage(pj_thread_t *thread);
  318. /** @internal
  319. * Dump thread stack status.
  320. */
  321. PJ_DECL(pj_status_t) pj_thread_get_stack_info(pj_thread_t *thread,
  322. const char **file,
  323. int *line);
  324. #else
  325. # define PJ_CHECK_STACK()
  326. /** pj_thread_get_stack_max_usage() for the thread */
  327. # define pj_thread_get_stack_max_usage(thread) 0
  328. /** pj_thread_get_stack_info() for the thread */
  329. # define pj_thread_get_stack_info(thread,f,l) (*(f)="",*(l)=0)
  330. #endif /* PJ_OS_HAS_CHECK_STACK */
  331. /**
  332. * @}
  333. */
  334. /* **************************************************************************/
  335. /**
  336. * @defgroup PJ_JNI Java Native Interface specific
  337. * @ingroup PJ_OS
  338. * @{
  339. * Functionalities specific to JNI.
  340. * Currently only implemented on Android OS, but may be extended to other
  341. * platforms in the future.
  342. *
  343. */
  344. /**
  345. * Set the Java Virtual Machine environment variable.
  346. * Note that applications typically do not need to call this function unless
  347. * PJ_JNI_HAS_JNI_ONLOAD is disabled.
  348. *
  349. * @param jvm The Java Virtual Machine environment.
  350. */
  351. PJ_DECL(void) pj_jni_set_jvm(void *jvm);
  352. /**
  353. * Attach the current thread to a Java Virtual Machine.
  354. *
  355. * @param jni_env Output parameter to store the JNI interface pointer.
  356. *
  357. * @return PJ_TRUE if the attachment is successful,
  358. * PJ_FALSE if otherwise.
  359. */
  360. PJ_DECL(pj_bool_t) pj_jni_attach_jvm(void **jni_env);
  361. /**
  362. * Detach the current thread from a Java Virtual Machine.
  363. *
  364. * @param attached Specify whether the current thread is attached
  365. * to a JVM.
  366. */
  367. PJ_DECL(void) pj_jni_detach_jvm(pj_bool_t attached);
  368. /**
  369. * @}
  370. */
  371. /* **************************************************************************/
  372. /**
  373. * @defgroup PJ_SYMBIAN_OS Symbian OS Specific
  374. * @ingroup PJ_OS
  375. * @{
  376. * Functionalities specific to Symbian OS.
  377. *
  378. * Symbian OS strongly discourages the use of polling since this wastes
  379. * CPU power, and instead provides Active Object and Active Scheduler
  380. * pattern to allow application (in this case, PJLIB) to register asynchronous
  381. * tasks. PJLIB port for Symbian complies to this recommended behavior.
  382. * As the result, few things have been changed in PJLIB for Symbian:
  383. * - the timer heap (see @ref PJ_TIMER) is implemented with active
  384. * object framework, and each timer entry registered to the timer
  385. * heap will register an Active Object to the Active Scheduler.
  386. * Because of this, polling the timer heap with pj_timer_heap_poll()
  387. * is no longer necessary, and this function will just evaluate
  388. * to nothing.
  389. * - the ioqueue (see @ref PJ_IOQUEUE) is also implemented with
  390. * active object framework, with each asynchronous operation will
  391. * register an Active Object to the Active Scheduler. Because of
  392. * this, polling the ioqueue with pj_ioqueue_poll() is no longer
  393. * necessary, and this function will just evaluate to nothing.
  394. *
  395. * Since timer heap and ioqueue polling are no longer necessary, Symbian
  396. * application can now poll for all events by calling
  397. * \a User::WaitForAnyRequest() and \a CActiveScheduler::RunIfReady().
  398. * PJLIB provides a thin wrapper which calls these two functions,
  399. * called pj_symbianos_poll().
  400. */
  401. /**
  402. * Wait the completion of any Symbian active objects. When the timeout
  403. * value is not specified (the \a ms_timeout argument is -1), this
  404. * function is a thin wrapper which calls \a User::WaitForAnyRequest()
  405. * and \a CActiveScheduler::RunIfReady(). If the timeout value is
  406. * specified, this function will schedule a timer entry to the timer
  407. * heap (which is an Active Object), to limit the wait time for event
  408. * occurences. Scheduling a timer entry is an expensive operation,
  409. * therefore application should only specify a timeout value when it's
  410. * really necessary (for example, when it's not sure there are other
  411. * Active Objects currently running in the application).
  412. *
  413. * @param priority The minimum priority of the Active Objects to
  414. * poll, which values are from CActive::TPriority
  415. * constants. If -1 is given, CActive::EPriorityStandard.
  416. * priority will be used.
  417. * @param ms_timeout Optional timeout to wait. Application should
  418. * specify -1 to let the function wait indefinitely
  419. * for any events.
  420. *
  421. * @return PJ_TRUE if there have been any events executed
  422. * during the polling. This function will only return
  423. * PJ_FALSE if \a ms_timeout argument is specified
  424. * (i.e. the value is not -1) and there was no event
  425. * executed when the timeout timer elapsed.
  426. */
  427. PJ_DECL(pj_bool_t) pj_symbianos_poll(int priority, int ms_timeout);
  428. /**
  429. * This structure declares Symbian OS specific parameters that can be
  430. * specified when calling #pj_symbianos_set_params().
  431. */
  432. typedef struct pj_symbianos_params
  433. {
  434. /**
  435. * Optional RSocketServ instance to be used by PJLIB. If this
  436. * value is NULL, PJLIB will create a new RSocketServ instance
  437. * when pj_init() is called.
  438. */
  439. void *rsocketserv;
  440. /**
  441. * Optional RConnection instance to be used by PJLIB when creating
  442. * sockets. If this value is NULL, no RConnection will be
  443. * specified when creating sockets.
  444. */
  445. void *rconnection;
  446. /**
  447. * Optional RHostResolver instance to be used by PJLIB. If this value
  448. * is NULL, a new RHostResolver instance will be created when
  449. * pj_init() is called.
  450. */
  451. void *rhostresolver;
  452. /**
  453. * Optional RHostResolver for IPv6 instance to be used by PJLIB.
  454. * If this value is NULL, a new RHostResolver instance will be created
  455. * when pj_init() is called.
  456. */
  457. void *rhostresolver6;
  458. } pj_symbianos_params;
  459. /**
  460. * Specify Symbian OS parameters to be used by PJLIB. This function MUST
  461. * be called before #pj_init() is called.
  462. *
  463. * @param prm Symbian specific parameters.
  464. *
  465. * @return PJ_SUCCESS if the parameters can be applied
  466. * successfully.
  467. */
  468. PJ_DECL(pj_status_t) pj_symbianos_set_params(pj_symbianos_params *prm);
  469. /**
  470. * Notify PJLIB that the access point connection has been down or unusable
  471. * and PJLIB should not try to access the Symbian socket API (especially ones
  472. * that send packets). Sending packet when RConnection is reconnected to
  473. * different access point may cause the WaitForRequest() for the function to
  474. * block indefinitely.
  475. *
  476. * @param up If set to PJ_FALSE it will cause PJLIB to not try
  477. * to access socket API, and error will be returned
  478. * immediately instead.
  479. */
  480. PJ_DECL(void) pj_symbianos_set_connection_status(pj_bool_t up);
  481. /**
  482. * @}
  483. */
  484. /* **************************************************************************/
  485. /**
  486. * @defgroup PJ_TLS Thread Local Storage.
  487. * @ingroup PJ_OS
  488. * @{
  489. */
  490. /**
  491. * Allocate thread local storage index. The initial value of the variable at
  492. * the index is zero.
  493. *
  494. * @param index Pointer to hold the return value.
  495. * @return PJ_SUCCESS on success, or the error code.
  496. */
  497. PJ_DECL(pj_status_t) pj_thread_local_alloc(long *index);
  498. /**
  499. * Deallocate thread local variable.
  500. *
  501. * @param index The variable index.
  502. */
  503. PJ_DECL(void) pj_thread_local_free(long index);
  504. /**
  505. * Set the value of thread local variable.
  506. *
  507. * @param index The index of the variable.
  508. * @param value The value.
  509. */
  510. PJ_DECL(pj_status_t) pj_thread_local_set(long index, void *value);
  511. /**
  512. * Get the value of thread local variable.
  513. *
  514. * @param index The index of the variable.
  515. * @return The value.
  516. */
  517. PJ_DECL(void*) pj_thread_local_get(long index);
  518. /**
  519. * @}
  520. */
  521. /* **************************************************************************/
  522. /**
  523. * @defgroup PJ_ATOMIC Atomic Variables
  524. * @ingroup PJ_OS
  525. * @{
  526. *
  527. * This module provides API to manipulate atomic variables.
  528. *
  529. * \section pj_atomic_examples_sec Examples
  530. *
  531. * For some example codes, please see:
  532. * - Atomic Variable test: \src{pjlib/src/pjlib-test/atomic.c}
  533. */
  534. /**
  535. * Create atomic variable.
  536. *
  537. * @param pool The pool.
  538. * @param initial The initial value of the atomic variable.
  539. * @param atomic Pointer to hold the atomic variable upon return.
  540. *
  541. * @return PJ_SUCCESS on success, or the error code.
  542. */
  543. PJ_DECL(pj_status_t) pj_atomic_create( pj_pool_t *pool,
  544. pj_atomic_value_t initial,
  545. pj_atomic_t **atomic );
  546. /**
  547. * Destroy atomic variable.
  548. *
  549. * @param atomic_var the atomic variable.
  550. *
  551. * @return PJ_SUCCESS if success.
  552. */
  553. PJ_DECL(pj_status_t) pj_atomic_destroy( pj_atomic_t *atomic_var );
  554. /**
  555. * Set the value of an atomic type, and return the previous value.
  556. *
  557. * @param atomic_var the atomic variable.
  558. * @param value value to be set to the variable.
  559. */
  560. PJ_DECL(void) pj_atomic_set( pj_atomic_t *atomic_var,
  561. pj_atomic_value_t value);
  562. /**
  563. * Get the value of an atomic type.
  564. *
  565. * @param atomic_var the atomic variable.
  566. *
  567. * @return the value of the atomic variable.
  568. */
  569. PJ_DECL(pj_atomic_value_t) pj_atomic_get(pj_atomic_t *atomic_var);
  570. /**
  571. * Increment the value of an atomic type.
  572. *
  573. * @param atomic_var the atomic variable.
  574. */
  575. PJ_DECL(void) pj_atomic_inc(pj_atomic_t *atomic_var);
  576. /**
  577. * Increment the value of an atomic type and get the result.
  578. *
  579. * @param atomic_var the atomic variable.
  580. *
  581. * @return The incremented value.
  582. */
  583. PJ_DECL(pj_atomic_value_t) pj_atomic_inc_and_get(pj_atomic_t *atomic_var);
  584. /**
  585. * Decrement the value of an atomic type.
  586. *
  587. * @param atomic_var the atomic variable.
  588. */
  589. PJ_DECL(void) pj_atomic_dec(pj_atomic_t *atomic_var);
  590. /**
  591. * Decrement the value of an atomic type and get the result.
  592. *
  593. * @param atomic_var the atomic variable.
  594. *
  595. * @return The decremented value.
  596. */
  597. PJ_DECL(pj_atomic_value_t) pj_atomic_dec_and_get(pj_atomic_t *atomic_var);
  598. /**
  599. * Add a value to an atomic type.
  600. *
  601. * @param atomic_var The atomic variable.
  602. * @param value Value to be added.
  603. */
  604. PJ_DECL(void) pj_atomic_add( pj_atomic_t *atomic_var,
  605. pj_atomic_value_t value);
  606. /**
  607. * Add a value to an atomic type and get the result.
  608. *
  609. * @param atomic_var The atomic variable.
  610. * @param value Value to be added.
  611. *
  612. * @return The result after the addition.
  613. */
  614. PJ_DECL(pj_atomic_value_t) pj_atomic_add_and_get( pj_atomic_t *atomic_var,
  615. pj_atomic_value_t value);
  616. /**
  617. * @}
  618. */
  619. /* **************************************************************************/
  620. /**
  621. * @defgroup PJ_MUTEX Mutexes.
  622. * @ingroup PJ_OS
  623. * @{
  624. *
  625. * Mutex manipulation. Alternatively, application can use higher abstraction
  626. * for lock objects, which provides uniform API for all kinds of lock
  627. * mechanisms, including mutex. See @ref PJ_LOCK for more information.
  628. */
  629. /**
  630. * Mutex types:
  631. * - PJ_MUTEX_DEFAULT: default mutex type, which is system dependent.
  632. * - PJ_MUTEX_SIMPLE: non-recursive mutex.
  633. * - PJ_MUTEX_RECURSE: recursive mutex.
  634. */
  635. typedef enum pj_mutex_type_e
  636. {
  637. PJ_MUTEX_DEFAULT,
  638. PJ_MUTEX_SIMPLE,
  639. PJ_MUTEX_RECURSE
  640. } pj_mutex_type_e;
  641. /**
  642. * Create mutex of the specified type.
  643. *
  644. * @param pool The pool.
  645. * @param name Name to be associated with the mutex (for debugging).
  646. * @param type The type of the mutex, of type #pj_mutex_type_e.
  647. * @param mutex Pointer to hold the returned mutex instance.
  648. *
  649. * @return PJ_SUCCESS on success, or the error code.
  650. */
  651. PJ_DECL(pj_status_t) pj_mutex_create(pj_pool_t *pool,
  652. const char *name,
  653. int type,
  654. pj_mutex_t **mutex);
  655. /**
  656. * Create simple, non-recursive mutex.
  657. * This function is a simple wrapper for #pj_mutex_create to create
  658. * non-recursive mutex.
  659. *
  660. * @param pool The pool.
  661. * @param name Mutex name.
  662. * @param mutex Pointer to hold the returned mutex instance.
  663. *
  664. * @return PJ_SUCCESS on success, or the error code.
  665. */
  666. PJ_DECL(pj_status_t) pj_mutex_create_simple( pj_pool_t *pool, const char *name,
  667. pj_mutex_t **mutex );
  668. /**
  669. * Create recursive mutex.
  670. * This function is a simple wrapper for #pj_mutex_create to create
  671. * recursive mutex.
  672. *
  673. * @param pool The pool.
  674. * @param name Mutex name.
  675. * @param mutex Pointer to hold the returned mutex instance.
  676. *
  677. * @return PJ_SUCCESS on success, or the error code.
  678. */
  679. PJ_DECL(pj_status_t) pj_mutex_create_recursive( pj_pool_t *pool,
  680. const char *name,
  681. pj_mutex_t **mutex );
  682. /**
  683. * Acquire mutex lock.
  684. *
  685. * @param mutex The mutex.
  686. * @return PJ_SUCCESS on success, or the error code.
  687. */
  688. PJ_DECL(pj_status_t) pj_mutex_lock(pj_mutex_t *mutex);
  689. /**
  690. * Release mutex lock.
  691. *
  692. * @param mutex The mutex.
  693. * @return PJ_SUCCESS on success, or the error code.
  694. */
  695. PJ_DECL(pj_status_t) pj_mutex_unlock(pj_mutex_t *mutex);
  696. /**
  697. * Try to acquire mutex lock.
  698. *
  699. * @param mutex The mutex.
  700. * @return PJ_SUCCESS on success, or the error code if the
  701. * lock couldn't be acquired.
  702. */
  703. PJ_DECL(pj_status_t) pj_mutex_trylock(pj_mutex_t *mutex);
  704. /**
  705. * Destroy mutex.
  706. *
  707. * @param mutex Te mutex.
  708. * @return PJ_SUCCESS on success, or the error code.
  709. */
  710. PJ_DECL(pj_status_t) pj_mutex_destroy(pj_mutex_t *mutex);
  711. /**
  712. * Determine whether calling thread is owning the mutex (only available when
  713. * PJ_DEBUG is set).
  714. * @param mutex The mutex.
  715. * @return Non-zero if yes.
  716. */
  717. PJ_DECL(pj_bool_t) pj_mutex_is_locked(pj_mutex_t *mutex);
  718. /**
  719. * @}
  720. */
  721. /* **************************************************************************/
  722. /**
  723. * @defgroup PJ_RW_MUTEX Reader/Writer Mutex
  724. * @ingroup PJ_OS
  725. * @{
  726. * Reader/writer mutex is a classic synchronization object where multiple
  727. * readers can acquire the mutex, but only a single writer can acquire the
  728. * mutex.
  729. */
  730. /**
  731. * Opaque declaration for reader/writer mutex.
  732. * Reader/writer mutex is a classic synchronization object where multiple
  733. * readers can acquire the mutex, but only a single writer can acquire the
  734. * mutex.
  735. */
  736. typedef struct pj_rwmutex_t pj_rwmutex_t;
  737. /**
  738. * Create reader/writer mutex.
  739. *
  740. * @param pool Pool to allocate memory for the mutex.
  741. * @param name Name to be assigned to the mutex.
  742. * @param mutex Pointer to receive the newly created mutex.
  743. *
  744. * @return PJ_SUCCESS on success, or the error code.
  745. */
  746. PJ_DECL(pj_status_t) pj_rwmutex_create(pj_pool_t *pool, const char *name,
  747. pj_rwmutex_t **mutex);
  748. /**
  749. * Lock the mutex for reading.
  750. *
  751. * @param mutex The mutex.
  752. * @return PJ_SUCCESS on success, or the error code.
  753. */
  754. PJ_DECL(pj_status_t) pj_rwmutex_lock_read(pj_rwmutex_t *mutex);
  755. /**
  756. * Lock the mutex for writing.
  757. *
  758. * @param mutex The mutex.
  759. * @return PJ_SUCCESS on success, or the error code.
  760. */
  761. PJ_DECL(pj_status_t) pj_rwmutex_lock_write(pj_rwmutex_t *mutex);
  762. /**
  763. * Release read lock.
  764. *
  765. * @param mutex The mutex.
  766. * @return PJ_SUCCESS on success, or the error code.
  767. */
  768. PJ_DECL(pj_status_t) pj_rwmutex_unlock_read(pj_rwmutex_t *mutex);
  769. /**
  770. * Release write lock.
  771. *
  772. * @param mutex The mutex.
  773. * @return PJ_SUCCESS on success, or the error code.
  774. */
  775. PJ_DECL(pj_status_t) pj_rwmutex_unlock_write(pj_rwmutex_t *mutex);
  776. /**
  777. * Destroy reader/writer mutex.
  778. *
  779. * @param mutex The mutex.
  780. * @return PJ_SUCCESS on success, or the error code.
  781. */
  782. PJ_DECL(pj_status_t) pj_rwmutex_destroy(pj_rwmutex_t *mutex);
  783. /**
  784. * @}
  785. */
  786. /* **************************************************************************/
  787. /**
  788. * @defgroup PJ_CRIT_SEC Critical sections.
  789. * @ingroup PJ_OS
  790. * @{
  791. * Critical section protection can be used to protect regions where:
  792. * - mutual exclusion protection is needed.
  793. * - it's rather too expensive to create a mutex.
  794. * - the time spent in the region is very very brief.
  795. *
  796. * Critical section is a global object, and it prevents any threads from
  797. * entering any regions that are protected by critical section once a thread
  798. * is already in the section.
  799. *
  800. * Critial section is \a not recursive!
  801. *
  802. * Application <b>MUST NOT</b> call any functions that may cause current
  803. * thread to block (such as allocating memory, performing I/O, locking mutex,
  804. * etc.) while holding the critical section.
  805. */
  806. /**
  807. * Enter critical section.
  808. */
  809. PJ_DECL(void) pj_enter_critical_section(void);
  810. /**
  811. * Leave critical section.
  812. */
  813. PJ_DECL(void) pj_leave_critical_section(void);
  814. /**
  815. * @}
  816. */
  817. /* **************************************************************************/
  818. #if defined(PJ_HAS_SEMAPHORE) && PJ_HAS_SEMAPHORE != 0
  819. /**
  820. * @defgroup PJ_SEM Semaphores.
  821. * @ingroup PJ_OS
  822. * @{
  823. *
  824. * This module provides abstraction for semaphores, where available.
  825. */
  826. /**
  827. * Create semaphore.
  828. *
  829. * @param pool The pool.
  830. * @param name Name to be assigned to the semaphore (for logging purpose)
  831. * @param initial The initial count of the semaphore.
  832. * @param max The maximum count of the semaphore.
  833. * @param sem Pointer to hold the semaphore created.
  834. *
  835. * @return PJ_SUCCESS on success, or the error code.
  836. */
  837. PJ_DECL(pj_status_t) pj_sem_create( pj_pool_t *pool,
  838. const char *name,
  839. unsigned initial,
  840. unsigned max,
  841. pj_sem_t **sem);
  842. /**
  843. * Wait for semaphore.
  844. *
  845. * @param sem The semaphore.
  846. *
  847. * @return PJ_SUCCESS on success, or the error code.
  848. */
  849. PJ_DECL(pj_status_t) pj_sem_wait(pj_sem_t *sem);
  850. /**
  851. * Try wait for semaphore.
  852. *
  853. * @param sem The semaphore.
  854. *
  855. * @return PJ_SUCCESS on success, or the error code.
  856. */
  857. PJ_DECL(pj_status_t) pj_sem_trywait(pj_sem_t *sem);
  858. /**
  859. * Release semaphore.
  860. *
  861. * @param sem The semaphore.
  862. *
  863. * @return PJ_SUCCESS on success, or the error code.
  864. */
  865. PJ_DECL(pj_status_t) pj_sem_post(pj_sem_t *sem);
  866. /**
  867. * Destroy semaphore.
  868. *
  869. * @param sem The semaphore.
  870. *
  871. * @return PJ_SUCCESS on success, or the error code.
  872. */
  873. PJ_DECL(pj_status_t) pj_sem_destroy(pj_sem_t *sem);
  874. /**
  875. * @}
  876. */
  877. #endif /* PJ_HAS_SEMAPHORE */
  878. /* **************************************************************************/
  879. #if defined(PJ_HAS_EVENT_OBJ) && PJ_HAS_EVENT_OBJ != 0
  880. /**
  881. * @defgroup PJ_EVENT Event Object.
  882. * @ingroup PJ_OS
  883. * @{
  884. *
  885. * This module provides abstraction to event object (e.g. Win32 Event) where
  886. * available. Event objects can be used for synchronization among threads.
  887. */
  888. /**
  889. * Create event object.
  890. *
  891. * @param pool The pool.
  892. * @param name The name of the event object (for logging purpose).
  893. * @param manual_reset Specify whether the event is manual-reset
  894. * @param initial Specify the initial state of the event object.
  895. * @param event Pointer to hold the returned event object.
  896. *
  897. * @return event handle, or NULL if failed.
  898. */
  899. PJ_DECL(pj_status_t) pj_event_create(pj_pool_t *pool, const char *name,
  900. pj_bool_t manual_reset, pj_bool_t initial,
  901. pj_event_t **event);
  902. /**
  903. * Wait for event to be signaled.
  904. *
  905. * @param event The event object.
  906. *
  907. * @return zero if successfull.
  908. */
  909. PJ_DECL(pj_status_t) pj_event_wait(pj_event_t *event);
  910. /**
  911. * Try wait for event object to be signalled.
  912. *
  913. * @param event The event object.
  914. *
  915. * @return zero if successfull.
  916. */
  917. PJ_DECL(pj_status_t) pj_event_trywait(pj_event_t *event);
  918. /**
  919. * Set the event object state to signaled. For auto-reset event, this
  920. * will only release the first thread that are waiting on the event. For
  921. * manual reset event, the state remains signaled until the event is reset.
  922. * If there is no thread waiting on the event, the event object state
  923. * remains signaled.
  924. *
  925. * @param event The event object.
  926. *
  927. * @return zero if successfull.
  928. */
  929. PJ_DECL(pj_status_t) pj_event_set(pj_event_t *event);
  930. /**
  931. * Set the event object to signaled state to release appropriate number of
  932. * waiting threads and then reset the event object to non-signaled. For
  933. * manual-reset event, this function will release all waiting threads. For
  934. * auto-reset event, this function will only release one waiting thread.
  935. *
  936. * @param event The event object.
  937. *
  938. * @return zero if successfull.
  939. */
  940. PJ_DECL(pj_status_t) pj_event_pulse(pj_event_t *event);
  941. /**
  942. * Set the event object state to non-signaled.
  943. *
  944. * @param event The event object.
  945. *
  946. * @return zero if successfull.
  947. */
  948. PJ_DECL(pj_status_t) pj_event_reset(pj_event_t *event);
  949. /**
  950. * Destroy the event object.
  951. *
  952. * @param event The event object.
  953. *
  954. * @return zero if successfull.
  955. */
  956. PJ_DECL(pj_status_t) pj_event_destroy(pj_event_t *event);
  957. /**
  958. * @}
  959. */
  960. #endif /* PJ_HAS_EVENT_OBJ */
  961. /* **************************************************************************/
  962. /**
  963. * @addtogroup PJ_TIME Time Data Type and Manipulation.
  964. * @ingroup PJ_OS
  965. * @{
  966. * This module provides API for manipulating time.
  967. *
  968. * \section pj_time_examples_sec Examples
  969. *
  970. * For examples, please see:
  971. * - Sleep, Time, and Timestamp test: \src{pjlib/src/pjlib-test/sleep.c}
  972. */
  973. /**
  974. * Get current time of day in local representation.
  975. *
  976. * @param tv Variable to store the result.
  977. *
  978. * @return zero if successfull.
  979. */
  980. PJ_DECL(pj_status_t) pj_gettimeofday(pj_time_val *tv);
  981. /**
  982. * Parse time value into date/time representation.
  983. *
  984. * @param tv The time.
  985. * @param pt Variable to store the date time result.
  986. *
  987. * @return zero if successfull.
  988. */
  989. PJ_DECL(pj_status_t) pj_time_decode(const pj_time_val *tv, pj_parsed_time *pt);
  990. /**
  991. * Encode date/time to time value.
  992. *
  993. * @param pt The date/time.
  994. * @param tv Variable to store time value result.
  995. *
  996. * @return zero if successfull.
  997. */
  998. PJ_DECL(pj_status_t) pj_time_encode(const pj_parsed_time *pt, pj_time_val *tv);
  999. /**
  1000. * Convert local time to GMT.
  1001. *
  1002. * @param tv Time to convert.
  1003. *
  1004. * @return zero if successfull.
  1005. */
  1006. PJ_DECL(pj_status_t) pj_time_local_to_gmt(pj_time_val *tv);
  1007. /**
  1008. * Convert GMT to local time.
  1009. *
  1010. * @param tv Time to convert.
  1011. *
  1012. * @return zero if successfull.
  1013. */
  1014. PJ_DECL(pj_status_t) pj_time_gmt_to_local(pj_time_val *tv);
  1015. /**
  1016. * @}
  1017. */
  1018. /* **************************************************************************/
  1019. #if defined(PJ_TERM_HAS_COLOR) && PJ_TERM_HAS_COLOR != 0
  1020. /**
  1021. * @defgroup PJ_TERM Terminal
  1022. * @ingroup PJ_OS
  1023. * @{
  1024. */
  1025. /**
  1026. * Set current terminal color.
  1027. *
  1028. * @param color The RGB color.
  1029. *
  1030. * @return zero on success.
  1031. */
  1032. PJ_DECL(pj_status_t) pj_term_set_color(pj_color_t color);
  1033. /**
  1034. * Get current terminal foreground color.
  1035. *
  1036. * @return RGB color.
  1037. */
  1038. PJ_DECL(pj_color_t) pj_term_get_color(void);
  1039. /**
  1040. * @}
  1041. */
  1042. #endif /* PJ_TERM_HAS_COLOR */
  1043. /* **************************************************************************/
  1044. /**
  1045. * @defgroup PJ_TIMESTAMP High Resolution Timestamp
  1046. * @ingroup PJ_OS
  1047. * @{
  1048. *
  1049. * PJLIB provides <b>High Resolution Timestamp</b> API to access highest
  1050. * resolution timestamp value provided by the platform. The API is usefull
  1051. * to measure precise elapsed time, and can be used in applications such
  1052. * as profiling.
  1053. *
  1054. * The timestamp value is represented in cycles, and can be related to
  1055. * normal time (in seconds or sub-seconds) using various functions provided.
  1056. *
  1057. * \section pj_timestamp_examples_sec Examples
  1058. *
  1059. * For examples, please see:
  1060. * - Sleep, Time, and Timestamp test: \src{pjlib/src/pjlib-test/sleep.c}
  1061. * - Timestamp test: \src{pjlib/src/pjlib-test/timestamp.c}
  1062. */
  1063. /*
  1064. * High resolution timer.
  1065. */
  1066. #if defined(PJ_HAS_HIGH_RES_TIMER) && PJ_HAS_HIGH_RES_TIMER != 0
  1067. /**
  1068. * Get monotonic time since some unspecified starting point.
  1069. *
  1070. * @param tv Variable to store the result.
  1071. *
  1072. * @return PJ_SUCCESS if successful.
  1073. */
  1074. PJ_DECL(pj_status_t) pj_gettickcount(pj_time_val *tv);
  1075. /**
  1076. * Acquire high resolution timer value. The time value are stored
  1077. * in cycles.
  1078. *
  1079. * @param ts High resolution timer value.
  1080. * @return PJ_SUCCESS or the appropriate error code.
  1081. *
  1082. * @see pj_get_timestamp_freq().
  1083. */
  1084. PJ_DECL(pj_status_t) pj_get_timestamp(pj_timestamp *ts);
  1085. /**
  1086. * Get high resolution timer frequency, in cycles per second.
  1087. *
  1088. * @param freq Timer frequency, in cycles per second.
  1089. * @return PJ_SUCCESS or the appropriate error code.
  1090. */
  1091. PJ_DECL(pj_status_t) pj_get_timestamp_freq(pj_timestamp *freq);
  1092. /**
  1093. * Set timestamp from 32bit values.
  1094. * @param t The timestamp to be set.
  1095. * @param hi The high 32bit part.
  1096. * @param lo The low 32bit part.
  1097. */
  1098. PJ_INLINE(void) pj_set_timestamp32(pj_timestamp *t, pj_uint32_t hi,
  1099. pj_uint32_t lo)
  1100. {
  1101. t->u32.hi = hi;
  1102. t->u32.lo = lo;
  1103. }
  1104. /**
  1105. * Compare timestamp t1 and t2.
  1106. * @param t1 t1.
  1107. * @param t2 t2.
  1108. * @return -1 if (t1 < t2), 1 if (t1 > t2), or 0 if (t1 == t2)
  1109. */
  1110. PJ_INLINE(int) pj_cmp_timestamp(const pj_timestamp *t1, const pj_timestamp *t2)
  1111. {
  1112. #if PJ_HAS_INT64
  1113. if (t1->u64 < t2->u64)
  1114. return -1;
  1115. else if (t1->u64 > t2->u64)
  1116. return 1;
  1117. else
  1118. return 0;
  1119. #else
  1120. if (t1->u32.hi < t2->u32.hi ||
  1121. (t1->u32.hi == t2->u32.hi && t1->u32.lo < t2->u32.lo))
  1122. return -1;
  1123. else if (t1->u32.hi > t2->u32.hi ||
  1124. (t1->u32.hi == t2->u32.hi && t1->u32.lo > t2->u32.lo))
  1125. return 1;
  1126. else
  1127. return 0;
  1128. #endif
  1129. }
  1130. /**
  1131. * Add timestamp t2 to t1.
  1132. * @param t1 t1.
  1133. * @param t2 t2.
  1134. */
  1135. PJ_INLINE(void) pj_add_timestamp(pj_timestamp *t1, const pj_timestamp *t2)
  1136. {
  1137. #if PJ_HAS_INT64
  1138. t1->u64 += t2->u64;
  1139. #else
  1140. pj_uint32_t old = t1->u32.lo;
  1141. t1->u32.hi += t2->u32.hi;
  1142. t1->u32.lo += t2->u32.lo;
  1143. if (t1->u32.lo < old)
  1144. ++t1->u32.hi;
  1145. #endif
  1146. }
  1147. /**
  1148. * Add timestamp t2 to t1.
  1149. * @param t1 t1.
  1150. * @param t2 t2.
  1151. */
  1152. PJ_INLINE(void) pj_add_timestamp32(pj_timestamp *t1, pj_uint32_t t2)
  1153. {
  1154. #if PJ_HAS_INT64
  1155. t1->u64 += t2;
  1156. #else
  1157. pj_uint32_t old = t1->u32.lo;
  1158. t1->u32.lo += t2;
  1159. if (t1->u32.lo < old)
  1160. ++t1->u32.hi;
  1161. #endif
  1162. }
  1163. /**
  1164. * Substract timestamp t2 from t1.
  1165. * @param t1 t1.
  1166. * @param t2 t2.
  1167. */
  1168. PJ_INLINE(void) pj_sub_timestamp(pj_timestamp *t1, const pj_timestamp *t2)
  1169. {
  1170. #if PJ_HAS_INT64
  1171. t1->u64 -= t2->u64;
  1172. #else
  1173. t1->u32.hi -= t2->u32.hi;
  1174. if (t1->u32.lo >= t2->u32.lo)
  1175. t1->u32.lo -= t2->u32.lo;
  1176. else {
  1177. t1->u32.lo -= t2->u32.lo;
  1178. --t1->u32.hi;
  1179. }
  1180. #endif
  1181. }
  1182. /**
  1183. * Substract timestamp t2 from t1.
  1184. * @param t1 t1.
  1185. * @param t2 t2.
  1186. */
  1187. PJ_INLINE(void) pj_sub_timestamp32(pj_timestamp *t1, pj_uint32_t t2)
  1188. {
  1189. #if PJ_HAS_INT64
  1190. t1->u64 -= t2;
  1191. #else
  1192. if (t1->u32.lo >= t2)
  1193. t1->u32.lo -= t2;
  1194. else {
  1195. t1->u32.lo -= t2;
  1196. --t1->u32.hi;
  1197. }
  1198. #endif
  1199. }
  1200. /**
  1201. * Get the timestamp difference between t2 and t1 (that is t2 minus t1),
  1202. * and return a 32bit signed integer difference.
  1203. */
  1204. PJ_INLINE(pj_int32_t) pj_timestamp_diff32(const pj_timestamp *t1,
  1205. const pj_timestamp *t2)
  1206. {
  1207. /* Be careful with the signess (I think!) */
  1208. #if PJ_HAS_INT64
  1209. pj_int64_t diff = t2->u64 - t1->u64;
  1210. return (pj_int32_t) diff;
  1211. #else
  1212. pj_int32 diff = t2->u32.lo - t1->u32.lo;
  1213. return diff;
  1214. #endif
  1215. }
  1216. /**
  1217. * Calculate the elapsed time, and store it in pj_time_val.
  1218. * This function calculates the elapsed time using highest precision
  1219. * calculation that is available for current platform, considering
  1220. * whether floating point or 64-bit precision arithmetic is available.
  1221. * For maximum portability, application should prefer to use this function
  1222. * rather than calculating the elapsed time by itself.
  1223. *
  1224. * @param start The starting timestamp.
  1225. * @param stop The end timestamp.
  1226. *
  1227. * @return Elapsed time as #pj_time_val.
  1228. *
  1229. * @see pj_elapsed_usec(), pj_elapsed_cycle(), pj_elapsed_nanosec()
  1230. */
  1231. PJ_DECL(pj_time_val) pj_elapsed_time( const pj_timestamp *start,
  1232. const pj_timestamp *stop );
  1233. /**
  1234. * Calculate the elapsed time as 32-bit miliseconds.
  1235. * This function calculates the elapsed time using highest precision
  1236. * calculation that is available for current platform, considering
  1237. * whether floating point or 64-bit precision arithmetic is available.
  1238. * For maximum portability, application should prefer to use this function
  1239. * rather than calculating the elapsed time by itself.
  1240. *
  1241. * @param start The starting timestamp.
  1242. * @param stop The end timestamp.
  1243. *
  1244. * @return Elapsed time in milisecond.
  1245. *
  1246. * @see pj_elapsed_time(), pj_elapsed_cycle(), pj_elapsed_nanosec()
  1247. */
  1248. PJ_DECL(pj_uint32_t) pj_elapsed_msec( const pj_timestamp *start,
  1249. const pj_timestamp *stop );
  1250. /**
  1251. * Variant of #pj_elapsed_msec() which returns 64bit value.
  1252. */
  1253. PJ_DECL(pj_uint64_t) pj_elapsed_msec64(const pj_timestamp *start,
  1254. const pj_timestamp *stop );
  1255. /**
  1256. * Calculate the elapsed time in 32-bit microseconds.
  1257. * This function calculates the elapsed time using highest precision
  1258. * calculation that is available for current platform, considering
  1259. * whether floating point or 64-bit precision arithmetic is available.
  1260. * For maximum portability, application should prefer to use this function
  1261. * rather than calculating the elapsed time by itself.
  1262. *
  1263. * @param start The starting timestamp.
  1264. * @param stop The end timestamp.
  1265. *
  1266. * @return Elapsed time in microsecond.
  1267. *
  1268. * @see pj_elapsed_time(), pj_elapsed_cycle(), pj_elapsed_nanosec()
  1269. */
  1270. PJ_DECL(pj_uint32_t) pj_elapsed_usec( const pj_timestamp *start,
  1271. const pj_timestamp *stop );
  1272. /**
  1273. * Calculate the elapsed time in 32-bit nanoseconds.
  1274. * This function calculates the elapsed time using highest precision
  1275. * calculation that is available for current platform, considering
  1276. * whether floating point or 64-bit precision arithmetic is available.
  1277. * For maximum portability, application should prefer to use this function
  1278. * rather than calculating the elapsed time by itself.
  1279. *
  1280. * @param start The starting timestamp.
  1281. * @param stop The end timestamp.
  1282. *
  1283. * @return Elapsed time in nanoseconds.
  1284. *
  1285. * @see pj_elapsed_time(), pj_elapsed_cycle(), pj_elapsed_usec()
  1286. */
  1287. PJ_DECL(pj_uint32_t) pj_elapsed_nanosec( const pj_timestamp *start,
  1288. const pj_timestamp *stop );
  1289. /**
  1290. * Calculate the elapsed time in 32-bit cycles.
  1291. * This function calculates the elapsed time using highest precision
  1292. * calculation that is available for current platform, considering
  1293. * whether floating point or 64-bit precision arithmetic is available.
  1294. * For maximum portability, application should prefer to use this function
  1295. * rather than calculating the elapsed time by itself.
  1296. *
  1297. * @param start The starting timestamp.
  1298. * @param stop The end timestamp.
  1299. *
  1300. * @return Elapsed time in cycles.
  1301. *
  1302. * @see pj_elapsed_usec(), pj_elapsed_time(), pj_elapsed_nanosec()
  1303. */
  1304. PJ_DECL(pj_uint32_t) pj_elapsed_cycle( const pj_timestamp *start,
  1305. const pj_timestamp *stop );
  1306. #endif /* PJ_HAS_HIGH_RES_TIMER */
  1307. /** @} */
  1308. /* **************************************************************************/
  1309. /**
  1310. * @defgroup PJ_APP_OS Application execution
  1311. * @ingroup PJ_OS
  1312. * @{
  1313. */
  1314. /**
  1315. * Type for application main function.
  1316. */
  1317. typedef int (*pj_main_func_ptr)(int argc, char *argv[]);
  1318. /**
  1319. * Run the application. This function has to be called in the main thread
  1320. * and after doing the necessary initialization according to the flags
  1321. * provided, it will call main_func() function.
  1322. *
  1323. * @param main_func Application's main function.
  1324. * @param argc Number of arguments from the main() function, which
  1325. * will be passed to main_func() function.
  1326. * @param argv The arguments from the main() function, which will
  1327. * be passed to main_func() function.
  1328. * @param flags Flags for application execution, currently must be 0.
  1329. *
  1330. * @return main_func()'s return value.
  1331. */
  1332. PJ_DECL(int) pj_run_app(pj_main_func_ptr main_func, int argc, char *argv[],
  1333. unsigned flags);
  1334. /** @} */
  1335. /* **************************************************************************/
  1336. /**
  1337. * Internal PJLIB function to initialize the threading subsystem.
  1338. * @return PJ_SUCCESS or the appropriate error code.
  1339. */
  1340. pj_status_t pj_thread_init(void);
  1341. /* **************************************************************************/
  1342. /**
  1343. * Set file descriptor close-on-exec flag
  1344. *
  1345. * @param fd The file descriptor
  1346. * @return on success, PJ_SUCCESS
  1347. *
  1348. */
  1349. PJ_DECL(pj_status_t) pj_set_cloexec_flag(int fd);
  1350. PJ_END_DECL
  1351. #endif /* __PJ_OS_H__ */