📄 threads.h
字号:
/*
Copyright (c) 2008, Intel Corporation.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/#include <map>#include "threadsync.h"#ifndef THREADS_HEADER#define THREADS_HEADER#define thr_self pthread_self#define thread_t pthread_t// Mutex-functions and types#define mutex_t pthread_mutex_t#define mutex_destroy pthread_mutex_destroy#define mutex_unlock pthread_mutex_unlock#define mutex_trylock pthread_mutex_trylock#define mutex_lock pthread_mutex_lock// Local storage#define thread_key_t pthread_key_t// Try to map a NT-style PTHREAD_START_ROUTINE to a Solaris-style// start_func() (see "man -s 3T thr_create()"). typedef void * (*solaris_PTHREAD_START_ROUTINE)(void *);#define THREAD_RUNNING 0#define THREAD_SUSPENDED 1#define THREAD_TERMINATED 2struct ThreadInfo { ThreadInfo::ThreadInfo() { ThreadInfo::init(THREAD_RUNNING); } ThreadInfo::ThreadInfo(DWORD aState) { ThreadInfo::init(aState); } ThreadInfo::ThreadInfo(const ThreadInfo& other) { state=other.state; suspendCount=other.suspendCount; exitCode=other.exitCode; mutex=other.mutex; cond=other.cond; counter=other.counter; (*counter)++; } ThreadInfo::~ThreadInfo() { (*counter)--; if(*counter) return; if(cond) { pthread_cond_destroy(cond); delete cond; cond=0; } if(mutex) { //printf("ThreadInfo destructor: before pthread_mutex_destroy\n"); pthread_mutex_destroy(mutex); //printf("ThreadInfo destructor: after pthread_mutex_destroy\n"); delete mutex; mutex=0; } delete counter; counter=0; } private: void ThreadInfo::operator=(ThreadInfo& other);public: inline void ThreadInfo::init(DWORD aState) { state = aState; suspendCount=0; exitCode = 0; cond=new pthread_cond_t; mutex= new pthread_mutex_t; pthread_cond_init(cond,0); pthread_mutex_init(mutex,0); counter=new DWORD; *counter=1; } volatile DWORD suspendCount; // suspend count, see ThreadResume(), ThreadSuspend() volatile DWORD state; // state: THREAD_* DWORD exitCode; // the threads exit code pthread_cond_t *cond; pthread_mutex_t *mutex; DWORD *counter;};typedef std::map<HANDLE, ThreadInfo, std::less<HANDLE> > ThreadInfoMap;extern "C" ThreadInfoMap ThreadInfos;extern "C" CriticalSection ThreadInfoLock;#endif //THREADS_HEADER
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -