📄 os.h
字号:
* @return PJ_SUCCESS on success, or the error code. */PJ_DECL(pj_status_t) pj_rwmutex_unlock_write(pj_rwmutex_t *mutex);/** * Destroy reader/writer mutex. * * @param mutex The mutex. * @return PJ_SUCCESS on success, or the error code. */PJ_DECL(pj_status_t) pj_rwmutex_destroy(pj_rwmutex_t *mutex);/** * @} *//* **************************************************************************//** * @defgroup PJ_CRIT_SEC Critical sections. * @ingroup PJ_OS * @{ * Critical section protection can be used to protect regions where: * - mutual exclusion protection is needed. * - it's rather too expensive to create a mutex. * - the time spent in the region is very very brief. * * Critical section is a global object, and it prevents any threads from * entering any regions that are protected by critical section once a thread * is already in the section. * * Critial section is \a not recursive! * * Application <b>MUST NOT</b> call any functions that may cause current * thread to block (such as allocating memory, performing I/O, locking mutex, * etc.) while holding the critical section. *//** * Enter critical section. */PJ_DECL(void) pj_enter_critical_section(void);/** * Leave critical section. */PJ_DECL(void) pj_leave_critical_section(void);/** * @} *//* **************************************************************************/#if defined(PJ_HAS_SEMAPHORE) && PJ_HAS_SEMAPHORE != 0/** * @defgroup PJ_SEM Semaphores. * @ingroup PJ_OS * @{ * * This module provides abstraction for semaphores, where available. *//** * Create semaphore. * * @param pool The pool. * @param name Name to be assigned to the semaphore (for logging purpose) * @param initial The initial count of the semaphore. * @param max The maximum count of the semaphore. * @param sem Pointer to hold the semaphore created. * * @return PJ_SUCCESS on success, or the error code. */PJ_DECL(pj_status_t) pj_sem_create( pj_pool_t *pool, const char *name, unsigned initial, unsigned max, pj_sem_t **sem);/** * Wait for semaphore. * * @param sem The semaphore. * * @return PJ_SUCCESS on success, or the error code. */PJ_DECL(pj_status_t) pj_sem_wait(pj_sem_t *sem);/** * Try wait for semaphore. * * @param sem The semaphore. * * @return PJ_SUCCESS on success, or the error code. */PJ_DECL(pj_status_t) pj_sem_trywait(pj_sem_t *sem);/** * Release semaphore. * * @param sem The semaphore. * * @return PJ_SUCCESS on success, or the error code. */PJ_DECL(pj_status_t) pj_sem_post(pj_sem_t *sem);/** * Destroy semaphore. * * @param sem The semaphore. * * @return PJ_SUCCESS on success, or the error code. */PJ_DECL(pj_status_t) pj_sem_destroy(pj_sem_t *sem);/** * @} */#endif /* PJ_HAS_SEMAPHORE *//* **************************************************************************/#if defined(PJ_HAS_EVENT_OBJ) && PJ_HAS_EVENT_OBJ != 0/** * @defgroup PJ_EVENT Event Object. * @ingroup PJ_OS * @{ * * This module provides abstraction to event object (e.g. Win32 Event) where * available. Event objects can be used for synchronization among threads. *//** * Create event object. * * @param pool The pool. * @param name The name of the event object (for logging purpose). * @param manual_reset Specify whether the event is manual-reset * @param initial Specify the initial state of the event object. * @param event Pointer to hold the returned event object. * * @return event handle, or NULL if failed. */PJ_DECL(pj_status_t) pj_event_create(pj_pool_t *pool, const char *name, pj_bool_t manual_reset, pj_bool_t initial, pj_event_t **event);/** * Wait for event to be signaled. * * @param event The event object. * * @return zero if successfull. */PJ_DECL(pj_status_t) pj_event_wait(pj_event_t *event);/** * Try wait for event object to be signalled. * * @param event The event object. * * @return zero if successfull. */PJ_DECL(pj_status_t) pj_event_trywait(pj_event_t *event);/** * Set the event object state to signaled. For auto-reset event, this * will only release the first thread that are waiting on the event. For * manual reset event, the state remains signaled until the event is reset. * If there is no thread waiting on the event, the event object state * remains signaled. * * @param event The event object. * * @return zero if successfull. */PJ_DECL(pj_status_t) pj_event_set(pj_event_t *event);/** * Set the event object to signaled state to release appropriate number of * waiting threads and then reset the event object to non-signaled. For * manual-reset event, this function will release all waiting threads. For * auto-reset event, this function will only release one waiting thread. * * @param event The event object. * * @return zero if successfull. */PJ_DECL(pj_status_t) pj_event_pulse(pj_event_t *event);/** * Set the event object state to non-signaled. * * @param event The event object. * * @return zero if successfull. */PJ_DECL(pj_status_t) pj_event_reset(pj_event_t *event);/** * Destroy the event object. * * @param event The event object. * * @return zero if successfull. */PJ_DECL(pj_status_t) pj_event_destroy(pj_event_t *event);/** * @} */#endif /* PJ_HAS_EVENT_OBJ *//* **************************************************************************//** * @addtogroup PJ_TIME Time Data Type and Manipulation. * @ingroup PJ_OS * @{ * This module provides API for manipulating time. * * \section pj_time_examples_sec Examples * * For examples, please see: * - \ref page_pjlib_sleep_test *//** * Get current time of day in local representation. * * @param tv Variable to store the result. * * @return zero if successfull. */PJ_DECL(pj_status_t) pj_gettimeofday(pj_time_val *tv);/** * Parse time value into date/time representation. * * @param tv The time. * @param pt Variable to store the date time result. * * @return zero if successfull. */PJ_DECL(pj_status_t) pj_time_decode(const pj_time_val *tv, pj_parsed_time *pt);/** * Encode date/time to time value. * * @param pt The date/time. * @param tv Variable to store time value result. * * @return zero if successfull. */PJ_DECL(pj_status_t) pj_time_encode(const pj_parsed_time *pt, pj_time_val *tv);/** * Convert local time to GMT. * * @param tv Time to convert. * * @return zero if successfull. */PJ_DECL(pj_status_t) pj_time_local_to_gmt(pj_time_val *tv);/** * Convert GMT to local time. * * @param tv Time to convert. * * @return zero if successfull. */PJ_DECL(pj_status_t) pj_time_gmt_to_local(pj_time_val *tv);/** * @} *//* **************************************************************************/#if defined(PJ_TERM_HAS_COLOR) && PJ_TERM_HAS_COLOR != 0/** * @defgroup PJ_TERM Terminal * @ingroup PJ_OS * @{ *//** * Set current terminal color. * * @param color The RGB color. * * @return zero on success. */PJ_DECL(pj_status_t) pj_term_set_color(pj_color_t color);/** * Get current terminal foreground color. * * @return RGB color. */PJ_DECL(pj_color_t) pj_term_get_color(void);/** * @} */#endif /* PJ_TERM_HAS_COLOR *//* **************************************************************************//** * @defgroup PJ_TIMESTAMP High Resolution Timestamp * @ingroup PJ_OS * @{ * * PJLIB provides <b>High Resolution Timestamp</b> API to access highest * resolution timestamp value provided by the platform. The API is usefull * to measure precise elapsed time, and can be used in applications such * as profiling. * * The timestamp value is represented in cycles, and can be related to * normal time (in seconds or sub-seconds) using various functions provided. * * \section pj_timestamp_examples_sec Examples * * For examples, please see: * - \ref page_pjlib_sleep_test * - \ref page_pjlib_timestamp_test *//* * High resolution timer. */#if defined(PJ_HAS_HIGH_RES_TIMER) && PJ_HAS_HIGH_RES_TIMER != 0/** * Acquire high resolution timer value. The time value are stored * in cycles. * * @param ts High resolution timer value. * @return PJ_SUCCESS or the appropriate error code. * * @see pj_get_timestamp_freq(). */PJ_DECL(pj_status_t) pj_get_timestamp(pj_timestamp *ts);/** * Get high resolution timer frequency, in cycles per second. * * @param freq Timer frequency, in cycles per second. * @return PJ_SUCCESS or the appropriate error code. */PJ_DECL(pj_status_t) pj_get_timestamp_freq(pj_timestamp *freq);/** * Set timestamp from 32bit values. * @param t The timestamp to be set. * @param hi The high 32bit part. * @param lo The low 32bit part. */PJ_INLINE(void) pj_set_timestamp32(pj_timestamp *t, pj_uint32_t hi, pj_uint32_t lo){ t->u32.hi = hi; t->u32.lo = lo;}/** * Add timestamp t2 to t1. * @param t1 t1. * @param t2 t2. */PJ_INLINE(void) pj_add_timestamp(pj_timestamp *t1, const pj_timestamp *t2){#if PJ_HAS_INT64 t1->u64 += t2->u64;#else pj_uint32_t old = t1->u32.lo; t1->u32.hi += t2->u32.hi; t1->u32.lo += t2->u32.lo; if (t1->u32.lo < old) ++t1->u32.hi;#endif}/** * Add timestamp t2 to t1. * @param t1 t1. * @param t2 t2. */PJ_INLINE(void) pj_add_timestamp32(pj_timestamp *t1, pj_uint32_t t2){#if PJ_HAS_INT64 t1->u64 += t2;#else pj_uint32_t old = t1->u32.lo; t1->u32.lo += t2; if (t1->u32.lo < old) ++t1->u32.hi;#endif}/** * Substract timestamp t2 from t1. * @param t1 t1. * @param t2 t2. */PJ_INLINE(void) pj_sub_timestamp(pj_timestamp *t1, const pj_timestamp *t2){#if PJ_HAS_INT64 t1->u64 -= t2->u64;#else t1->u32.hi -= t2->u32.hi; if (t1->u32.lo >= t2->u32.lo) t1->u32.lo -= t2->u32.lo; else { t1->u32.lo -= t2->u32.lo; --t1->u32.hi; }#endif}/** * Substract timestamp t2 from t1. * @param t1 t1. * @param t2 t2. */PJ_INLINE(void) pj_sub_timestamp32(pj_timestamp *t1, pj_uint32_t t2){#if PJ_HAS_INT64 t1->u64 -= t2;#else if (t1->u32.lo >= t2) t1->u32.lo -= t2; else { t1->u32.lo -= t2; --t1->u32.hi; }#endif}/** * Get the timestamp difference between t2 and t1 (that is t2 minus t1), * and return a 32bit signed integer difference. */PJ_INLINE(pj_int32_t) pj_timestamp_diff32(const pj_timestamp *t1, const pj_timestamp *t2){ /* Be careful with the signess (I think!) */#if PJ_HAS_INT64 pj_int64_t diff = t2->u64 - t1->u64; return (pj_int32_t) diff;#else pj_int32 diff = t2->u32.lo - t1->u32.lo; return diff;#endif}/** * Calculate the elapsed time, and store it in pj_time_val. * This function calculates the elapsed time using highest precision * calculation that is available for current platform, considering * whether floating point or 64-bit precision arithmetic is available. * For maximum portability, application should prefer to use this function * rather than calculating the elapsed time by itself. * * @param start The starting timestamp. * @param stop The end timestamp. * * @return Elapsed time as #pj_time_val. * * @see pj_elapsed_usec(), pj_elapsed_cycle(), pj_elapsed_nanosec() */PJ_DECL(pj_time_val) pj_elapsed_time( const pj_timestamp *start, const pj_timestamp *stop );/** * Calculate the elapsed time as 32-bit miliseconds. * This function calculates the elapsed time using highest precision * calculation that is available for current platform, considering * whether floating point or 64-bit precision arithmetic is available. * For maximum portability, application should prefer to use this function * rather than calculating the elapsed time by itself. * * @param start The starting timestamp. * @param stop The end timestamp. * * @return Elapsed time in milisecond. * * @see pj_elapsed_time(), pj_elapsed_cycle(), pj_elapsed_nanosec() */PJ_DECL(pj_uint32_t) pj_elapsed_msec( const pj_timestamp *start, const pj_timestamp *stop );/** * Calculate the elapsed time in 32-bit microseconds. * This function calculates the elapsed time using highest precision * calculation that is available for current platform, considering * whether floating point or 64-bit precision arithmetic is available. * For maximum portability, application should prefer to use this function * rather than calculating the elapsed time by itself. * * @param start The starting timestamp. * @param stop The end timestamp. * * @return Elapsed time in microsecond. * * @see pj_elapsed_time(), pj_elapsed_cycle(), pj_elapsed_nanosec() */PJ_DECL(pj_uint32_t) pj_elapsed_usec( const pj_timestamp *start, const pj_timestamp *stop );/** * Calculate the elapsed time in 32-bit nanoseconds. * This function calculates the elapsed time using highest precision * calculation that is available for current platform, considering * whether floating point or 64-bit precision arithmetic is available. * For maximum portability, application should prefer to use this function * rather than calculating the elapsed time by itself. * * @param start The starting timestamp. * @param stop The end timestamp. * * @return Elapsed time in nanoseconds. * * @see pj_elapsed_time(), pj_elapsed_cycle(), pj_elapsed_usec() */PJ_DECL(pj_uint32_t) pj_elapsed_nanosec( const pj_timestamp *start, const pj_timestamp *stop );/** * Calculate the elapsed time in 32-bit cycles. * This function calculates the elapsed time using highest precision * calculation that is available for current platform, considering * whether floating point or 64-bit precision arithmetic is available. * For maximum portability, application should prefer to use this function * rather than calculating the elapsed time by itself. * * @param start The starting timestamp. * @param stop The end timestamp. * * @return Elapsed time in cycles. * * @see pj_elapsed_usec(), pj_elapsed_time(), pj_elapsed_nanosec() */PJ_DECL(pj_uint32_t) pj_elapsed_cycle( const pj_timestamp *start, const pj_timestamp *stop );#endif /* PJ_HAS_HIGH_RES_TIMER *//** @} *//* **************************************************************************//** * Internal PJLIB function to initialize the threading subsystem. * @return PJ_SUCCESS or the appropriate error code. */pj_status_t pj_thread_init(void);PJ_END_DECL#endif /* __PJ_OS_H__ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -