⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os.h

📁 基于sip协议的网络电话源码
💻 H
📖 第 1 页 / 共 2 页
字号:
/* $Id: os.h 974 2007-02-19 01:13:53Z bennylp $ *//*  * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  */#ifndef __PJ_OS_H__#define __PJ_OS_H__/** * @file os.h * @brief OS dependent functions */#include <pj/types.h>PJ_BEGIN_DECL/** * @defgroup PJ_OS Operating System Dependent Functionality. * @ingroup PJ *//* **************************************************************************//** * @defgroup PJ_THREAD Threads * @ingroup PJ_OS * @{ * This module provides multithreading API. * * \section pj_thread_examples_sec Examples * * For examples, please see: *  - \ref page_pjlib_thread_test *  - \ref page_pjlib_sleep_test * *//** * Thread creation flags: * - PJ_THREAD_SUSPENDED: specify that the thread should be created suspended. */typedef enum pj_thread_create_flags{    PJ_THREAD_SUSPENDED = 1} pj_thread_create_flags;/** * Type of thread entry function. */typedef int (PJ_THREAD_FUNC pj_thread_proc)(void*);/** * Size of thread struct. */#if !defined(PJ_THREAD_DESC_SIZE)#   define PJ_THREAD_DESC_SIZE	    (64)#endif/** * Thread structure, to thread's state when the thread is created by external * or native API.  */typedef long pj_thread_desc[PJ_THREAD_DESC_SIZE];/** * Get process ID. * @return process ID. */PJ_DECL(pj_uint32_t) pj_getpid(void);/** * Create a new thread. * * @param pool          The memory pool from which the thread record  *                      will be allocated from. * @param thread_name   The optional name to be assigned to the thread. * @param proc          Thread entry function. * @param arg           Argument to be passed to the thread entry function. * @param stack_size    The size of the stack for the new thread, or ZERO or *                      PJ_THREAD_DEFAULT_STACK_SIZE to let the  *		        library choose the reasonable size for the stack.  *                      For some systems, the stack will be allocated from  *                      the pool, so the pool must have suitable capacity. * @param flags         Flags for thread creation, which is bitmask combination  *                      from enum pj_thread_create_flags. * @param thread        Pointer to hold the newly created thread. * * @return	        PJ_SUCCESS on success, or the error code. */PJ_DECL(pj_status_t) pj_thread_create(  pj_pool_t *pool,                                         const char *thread_name,				        pj_thread_proc *proc,                                         void *arg,				        pj_size_t stack_size,                                         unsigned flags,					pj_thread_t **thread );/** * Register a thread that was created by external or native API to PJLIB. * This function must be called in the context of the thread being registered. * When the thread is created by external function or API call, * it must be 'registered' to PJLIB using pj_thread_register(), so that it can * cooperate with PJLIB's framework. During registration, some data needs to * be maintained, and this data must remain available during the thread's  * lifetime. * * @param thread_name   The optional name to be assigned to the thread. * @param desc          Thread descriptor, which must be available throughout  *                      the lifetime of the thread. * @param thread        Pointer to hold the created thread handle. * * @return              PJ_SUCCESS on success, or the error code. */PJ_DECL(pj_status_t) pj_thread_register ( const char *thread_name,					  pj_thread_desc desc,					  pj_thread_t **thread);/** * Check if this thread has been registered to PJLIB. * * @return		Non-zero if it is registered. */PJ_DECL(pj_bool_t) pj_thread_is_registered(void);/** * Get thread name. * * @param thread    The thread handle. * * @return Thread name as null terminated string. */PJ_DECL(const char*) pj_thread_get_name(pj_thread_t *thread);/** * Resume a suspended thread. * * @param thread    The thread handle. * * @return zero on success. */PJ_DECL(pj_status_t) pj_thread_resume(pj_thread_t *thread);/** * Get the current thread. * * @return Thread handle of current thread. */PJ_DECL(pj_thread_t*) pj_thread_this(void);/** * Join thread, and block the caller thread until the specified thread exits. * If the specified thread has already been dead, or it does not exist, * the function will return immediately with successfull status. * * @param thread    The thread handle. * * @return PJ_SUCCESS on success. */PJ_DECL(pj_status_t) pj_thread_join(pj_thread_t *thread);/** * Destroy thread and release resources allocated for the thread. * However, the memory allocated for the pj_thread_t itself will only be released * when the pool used to create the thread is destroyed. * * @param thread    The thread handle. * * @return zero on success. */PJ_DECL(pj_status_t) pj_thread_destroy(pj_thread_t *thread);/** * Put the current thread to sleep for the specified miliseconds. * * @param msec Miliseconds delay. * * @return zero if successfull. */PJ_DECL(pj_status_t) pj_thread_sleep(unsigned msec);/** * @def PJ_CHECK_STACK() * PJ_CHECK_STACK() macro is used to check the sanity of the stack. * The OS implementation may check that no stack overflow occurs, and * it also may collect statistic about stack usage. */#if defined(PJ_OS_HAS_CHECK_STACK) && PJ_OS_HAS_CHECK_STACK!=0#  define PJ_CHECK_STACK() pj_thread_check_stack(__FILE__, __LINE__)/** @internal * The implementation of stack checking.  */PJ_DECL(void) pj_thread_check_stack(const char *file, int line);/** @internal * Get maximum stack usage statistic.  */PJ_DECL(pj_uint32_t) pj_thread_get_stack_max_usage(pj_thread_t *thread);/** @internal * Dump thread stack status.  */PJ_DECL(pj_status_t) pj_thread_get_stack_info(pj_thread_t *thread,					      const char **file,					      int *line);#else#  define PJ_CHECK_STACK()/** pj_thread_get_stack_max_usage() for the thread */#  define pj_thread_get_stack_max_usage(thread)	    0/** pj_thread_get_stack_info() for the thread */#  define pj_thread_get_stack_info(thread,f,l)	    (*(f)="",*(l)=0)#endif	/* PJ_OS_HAS_CHECK_STACK *//** * @} *//* **************************************************************************//** * @defgroup PJ_TLS Thread Local Storage. * @ingroup PJ_OS * @{ *//**  * Allocate thread local storage index. The initial value of the variable at * the index is zero. * * @param index	    Pointer to hold the return value. * @return	    PJ_SUCCESS on success, or the error code. */PJ_DECL(pj_status_t) pj_thread_local_alloc(long *index);/** * Deallocate thread local variable. * * @param index	    The variable index. */PJ_DECL(void) pj_thread_local_free(long index);/** * Set the value of thread local variable. * * @param index	    The index of the variable. * @param value	    The value. */PJ_DECL(pj_status_t) pj_thread_local_set(long index, void *value);/** * Get the value of thread local variable. * * @param index	    The index of the variable. * @return	    The value. */PJ_DECL(void*) pj_thread_local_get(long index);/** * @} *//* **************************************************************************//** * @defgroup PJ_ATOMIC Atomic Variables * @ingroup PJ_OS * @{ * * This module provides API to manipulate atomic variables. * * \section pj_atomic_examples_sec Examples * * For some example codes, please see: *  - @ref page_pjlib_atomic_test *//** * Create atomic variable. * * @param pool	    The pool. * @param initial   The initial value of the atomic variable. * @param atomic    Pointer to hold the atomic variable upon return. * * @return	    PJ_SUCCESS on success, or the error code. */PJ_DECL(pj_status_t) pj_atomic_create( pj_pool_t *pool, 				       pj_atomic_value_t initial,				       pj_atomic_t **atomic );/** * Destroy atomic variable. * * @param atomic_var	the atomic variable. * * @return PJ_SUCCESS if success. */PJ_DECL(pj_status_t) pj_atomic_destroy( pj_atomic_t *atomic_var );/** * Set the value of an atomic type, and return the previous value. * * @param atomic_var	the atomic variable. * @param value		value to be set to the variable. */PJ_DECL(void) pj_atomic_set( pj_atomic_t *atomic_var, 			     pj_atomic_value_t value);/** * Get the value of an atomic type. * * @param atomic_var	the atomic variable. * * @return the value of the atomic variable. */PJ_DECL(pj_atomic_value_t) pj_atomic_get(pj_atomic_t *atomic_var);/** * Increment the value of an atomic type. * * @param atomic_var	the atomic variable. */PJ_DECL(void) pj_atomic_inc(pj_atomic_t *atomic_var);/** * Increment the value of an atomic type and get the result. * * @param atomic_var	the atomic variable. * * @return              The incremented value. */PJ_DECL(pj_atomic_value_t) pj_atomic_inc_and_get(pj_atomic_t *atomic_var);/** * Decrement the value of an atomic type. * * @param atomic_var	the atomic variable. */PJ_DECL(void) pj_atomic_dec(pj_atomic_t *atomic_var);/** * Decrement the value of an atomic type and get the result. * * @param atomic_var	the atomic variable. * * @return              The decremented value. */PJ_DECL(pj_atomic_value_t) pj_atomic_dec_and_get(pj_atomic_t *atomic_var);/** * Add a value to an atomic type. * * @param atomic_var	The atomic variable. * @param value		Value to be added. */PJ_DECL(void) pj_atomic_add( pj_atomic_t *atomic_var,			     pj_atomic_value_t value);/** * Add a value to an atomic type and get the result. * * @param atomic_var	The atomic variable. * @param value		Value to be added. * * @return              The result after the addition. */PJ_DECL(pj_atomic_value_t) pj_atomic_add_and_get( pj_atomic_t *atomic_var,			                          pj_atomic_value_t value);/** * @} *//* **************************************************************************//** * @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.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -