📄 os.hpp
字号:
/* $Id: os.hpp 122 2006-01-18 23:32:27Z bennylp $ *//* * Copyright (C)2003-2006 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 __PJPP_OS_HPP__#define __PJPP_OS_HPP__#include <pj/os.h>#include <pj/string.h>#include <pj++/types.hpp>#include <pj++/pool.hpp>class Pj_Thread;//// Thread API.//class Pj_Thread_API{public: // // Create a thread. // static pj_status_t create( Pj_Pool *pool, pj_thread_t **thread, pj_thread_proc *proc, void *arg, unsigned flags = 0, const char *name = NULL, pj_size_t stack_size = 0 ) { return pj_thread_create(pool->pool_(), name, proc, arg, stack_size, flags, thread); } // // Register a thread. // static pj_status_t register_this_thread( pj_thread_desc desc, pj_thread_t **thread, const char *name = NULL ) { return pj_thread_register( name, desc, thread ); } // // Get current thread. // Will return pj_thread_t (sorry folks, not Pj_Thread). // static pj_thread_t *this_thread() { return pj_thread_this(); } // // Get thread name. // static const char *get_name(pj_thread_t *thread) { return pj_thread_get_name(thread); } // // Resume thread. // static pj_status_t resume(pj_thread_t *thread) { return pj_thread_resume(thread); } // // Sleep. // static pj_status_t sleep(unsigned msec) { return pj_thread_sleep(msec); } // // Join the specified thread. // static pj_status_t join(pj_thread_t *thread) { return pj_thread_join(thread); } // // Destroy thread // static pj_status_t destroy(pj_thread_t *thread) { return pj_thread_destroy(thread); }};//// Thread object.//// How to use:// Derive a class from this class, then override main().//class Pj_Thread : public Pj_Object{public: enum Flags { FLAG_SUSPENDED = PJ_THREAD_SUSPENDED }; // // Default constructor. // Pj_Thread() : thread_(NULL) { } // // Destroy thread. // ~Pj_Thread() { destroy(); } // // This is the main thread function. // virtual int main() = 0; // // Start a thread. // pj_status_t create( Pj_Pool *pool, unsigned flags = 0, const char *thread_name = NULL, pj_size_t stack_size = PJ_THREAD_DEFAULT_STACK_SIZE) { destroy(); return Pj_Thread_API::create( pool, &thread_, &thread_proc, this, flags, thread_name, stack_size); } // // Get pjlib compatible thread object. // pj_thread_t *pj_thread_t_() { return thread_; } // // Get thread name. // const char *get_name() { return Pj_Thread_API::get_name(thread_); } // // Resume a suspended thread. // pj_status_t resume() { return Pj_Thread_API::resume(thread_); } // // Join this thread. // pj_status_t join() { return Pj_Thread_API::join(thread_); } // // Destroy thread. // pj_status_t destroy() { if (thread_) { Pj_Thread_API::destroy(thread_); thread_ = NULL; } }protected: pj_thread_t *thread_; static int PJ_THREAD_FUNC thread_proc(void *obj) { Pj_Thread *thread_class = (Pj_Thread*)obj; return thread_class->main(); }};//// External Thread// (threads that were started by external means, i.e. not // with Pj_Thread::create).//// This class will normally be defined as local variable in// external thread's stack, normally inside thread's main proc.// But be aware that the handle will be destroyed on destructor!//class Pj_External_Thread : public Pj_Thread{public: Pj_External_Thread() { } // // Register external thread so that pjlib functions can work // in that thread. // pj_status_t register_this_thread( const char *name=NULL ) { return Pj_Thread_API::register_this_thread(desc_, &thread_,name); }private: pj_thread_desc desc_;};//// Thread specific data/thread local storage/TLS.//class Pj_Thread_Local_API{public: // // Allocate thread local storage (TLS) index. // static pj_status_t alloc(long *index) { return pj_thread_local_alloc(index); } // // Free TLS index. // static void free(long index) { pj_thread_local_free(index); } // // Set thread specific data. // static pj_status_t set(long index, void *value) { return pj_thread_local_set(index, value); } // // Get thread specific data. // static void *get(long index) { return pj_thread_local_get(index); }};//// Atomic variable//// How to use:// Pj_Atomic_Var var(pool, 0);// var.set(..);//class Pj_Atomic_Var : public Pj_Object{public: // // Default constructor, initialize variable with NULL. // Pj_Atomic_Var() : var_(NULL) { } // // Construct atomic variable. // Pj_Atomic_Var(Pj_Pool *pool, pj_atomic_value_t value) : var_(NULL) { create(pool, value); } // // Destructor. // ~Pj_Atomic_Var() { destroy(); } // // Create atomic variable. // pj_status_t create( Pj_Pool *pool, pj_atomic_value_t value) { destroy(); return pj_atomic_create(pool->pool_(), value, &var_); } // // Destroy. // void destroy() { if (var_) { pj_atomic_destroy(var_); var_ = NULL; } } // // Get pjlib compatible atomic variable. // pj_atomic_t *pj_atomic_t_() { return var_; } // // Set the value. // void set(pj_atomic_value_t val) { pj_atomic_set(var_, val); } // // Get the value. // pj_atomic_value_t get() { return pj_atomic_get(var_); } // // Increment. // void inc() { pj_atomic_inc(var_); } // // Increment and get the result. // pj_atomic_value_t inc_and_get() { return pj_atomic_inc_and_get(var_); } // // Decrement. // void dec() { pj_atomic_dec(var_); } // // Decrement and get the result. // pj_atomic_value_t dec_and_get() { return pj_atomic_dec_and_get(var_); } // // Add the variable. // void add(pj_atomic_value_t value) { pj_atomic_add(var_, value); } // // Add the variable and get the value. // pj_atomic_value_t add_and_get(pj_atomic_value_t value) { return pj_atomic_add_and_get(var_, value ); }private: pj_atomic_t *var_;};//// Mutex//class Pj_Mutex : public Pj_Object{public: // // Mutex type. // enum Type { DEFAULT = PJ_MUTEX_DEFAULT, SIMPLE = PJ_MUTEX_SIMPLE, RECURSE = PJ_MUTEX_RECURSE, }; // // Default constructor will create default mutex. // explicit Pj_Mutex(Pj_Pool *pool, Type type = DEFAULT, const char *name = NULL) : mutex_(NULL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -