📄 os.h
字号:
* @}
*/
/* **************************************************************************/
/**
* @defgroup PJ_MUTEX Mutexes.
* @ingroup PJ_OS
* @{
*
* Mutex manipulation. Alternatively, application can use higher abstraction
* for lock objects, which provides uniform API for all kinds of lock
* mechanisms, including mutex. See @ref PJ_LOCK for more information.
*/
/**
* Mutex types:
* - PJ_MUTEX_DEFAULT: default mutex type, which is system dependent.
* - PJ_MUTEX_SIMPLE: non-recursive mutex.
* - PJ_MUTEX_RECURSIVE: recursive mutex.
*/
typedef enum pj_mutex_type_e
{
PJ_MUTEX_DEFAULT,
PJ_MUTEX_SIMPLE,
PJ_MUTEX_RECURSE
} pj_mutex_type_e;
/**
* Create mutex of the specified type.
*
* @param pool The pool.
* @param name Name to be associated with the mutex (for debugging).
* @param type The type of the mutex, of type #pj_mutex_type_e.
* @param mutex Pointer to hold the returned mutex instance.
*
* @return PJ_SUCCESS on success, or the error code.
*/
PJ_DECL(pj_status_t) pj_mutex_create(pj_pool_t *pool,
const char *name,
int type,
pj_mutex_t **mutex);
/**
* Create simple, non-recursive mutex.
* This function is a simple wrapper for #pj_mutex_create to create
* non-recursive mutex.
*
* @param pool The pool.
* @param name Mutex name.
* @param mutex Pointer to hold the returned mutex instance.
*
* @return PJ_SUCCESS on success, or the error code.
*/
PJ_DECL(pj_status_t) pj_mutex_create_simple( pj_pool_t *pool, const char *name,
pj_mutex_t **mutex );
/**
* Create recursive mutex.
* This function is a simple wrapper for #pj_mutex_create to create
* recursive mutex.
*
* @param pool The pool.
* @param name Mutex name.
* @param mutex Pointer to hold the returned mutex instance.
*
* @return PJ_SUCCESS on success, or the error code.
*/
PJ_DECL(pj_status_t) pj_mutex_create_recursive( pj_pool_t *pool,
const char *name,
pj_mutex_t **mutex );
/**
* Acquire mutex lock.
*
* @param mutex The mutex.
* @return PJ_SUCCESS on success, or the error code.
*/
PJ_DECL(pj_status_t) pj_mutex_lock(pj_mutex_t *mutex);
/**
* Release mutex lock.
*
* @param mutex The mutex.
* @return PJ_SUCCESS on success, or the error code.
*/
PJ_DECL(pj_status_t) pj_mutex_unlock(pj_mutex_t *mutex);
/**
* Try to acquire mutex lock.
*
* @param mutex The mutex.
* @return PJ_SUCCESS on success, or the error code if the
* lock couldn't be acquired.
*/
PJ_DECL(pj_status_t) pj_mutex_trylock(pj_mutex_t *mutex);
/**
* Destroy mutex.
*
* @param mutex Te mutex.
* @return PJ_SUCCESS on success, or the error code.
*/
PJ_DECL(pj_status_t) pj_mutex_destroy(pj_mutex_t *mutex);
/**
* Determine whether calling thread is owning the mutex (only available when
* PJ_DEBUG is set).
* @param mutex The mutex.
* @return Non-zero if yes.
*/
#if defined(PJ_DEBUG) && PJ_DEBUG != 0
PJ_DECL(pj_bool_t) pj_mutex_is_locked(pj_mutex_t *mutex);
#else
# define pj_mutex_is_locked(mutex) 1
#endif
/**
* @}
*/
/* **************************************************************************/
/**
* @defgroup PJ_RW_MUTEX Reader/Writer Mutex
* @ingroup PJ_OS
* @{
* Reader/writer mutex is a classic synchronization object where multiple
* readers can acquire the mutex, but only a single writer can acquire the
* mutex.
*/
/**
* Opaque declaration for reader/writer mutex.
* Reader/writer mutex is a classic synchronization object where multiple
* readers can acquire the mutex, but only a single writer can acquire the
* mutex.
*/
typedef struct pj_rwmutex_t pj_rwmutex_t;
/**
* Create reader/writer mutex.
*
* @param pool Pool to allocate memory for the mutex.
* @param name Name to be assigned to the mutex.
* @param mutex Pointer to receive the newly created mutex.
*
* @return PJ_SUCCESS on success, or the error code.
*/
PJ_DECL(pj_status_t) pj_rwmutex_create(pj_pool_t *pool, const char *name,
pj_rwmutex_t **mutex);
/**
* Lock the mutex for reading.
*
* @param mutex The mutex.
* @return PJ_SUCCESS on success, or the error code.
*/
PJ_DECL(pj_status_t) pj_rwmutex_lock_read(pj_rwmutex_t *mutex);
/**
* Lock the mutex for writing.
*
* @param mutex The mutex.
* @return PJ_SUCCESS on success, or the error code.
*/
PJ_DECL(pj_status_t) pj_rwmutex_lock_write(pj_rwmutex_t *mutex);
/**
* Release read lock.
*
* @param mutex The mutex.
* @return PJ_SUCCESS on success, or the error code.
*/
PJ_DECL(pj_status_t) pj_rwmutex_unlock_read(pj_rwmutex_t *mutex);
/**
* Release write lock.
*
* @param mutex The mutex.
* @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);
/**
* @}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -