📄 os.h
字号:
/* $Id: os.h 1124 2007-04-02 11:28:41Z 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);
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -