📄 pthreadthread.cpp
字号:
// ___ ___ ___ ___ ___ // /\__\ ___ /\__\ /\ \ /\__\ /\ \. // /::| | /\ \ /::| | /::\ \ /:/ / /::\ \. // /:|:| | \:\ \ /:|:| | /:/\:\ \ /:/ / /:/\:\ \. // /:/|:|__|__ /::\__\ /:/|:| |__ /:/ \:\ \ /:/ / /::\~\:\ \. // /:/ |::::\__\ __/:/\/__/ /:/ |:| /\__\ /:/__/_\:\__\ /:/__/ /:/\:\ \:\__\.// \/__/~~/:/ / /\/:/ / \/__|:|/:/ / \:\ /\ \/__/ \:\ \ \:\~\:\ \/__/// /:/ / \::/__/ |:/:/ / \:\ \:\__\ \:\ \ \:\ \:\__\. // /:/ / \:\__\ |::/ / \:\/:/ / \:\ \ \:\ \/__/ // /:/ / \/__/ /:/ / \::/ / \:\__\ \:\__\. // \/__/ \/__/ \/__/ \/__/ \/__/ // // =============================================================================// Minimalist OpenGL Environment// =============================================================================//// This file is part of Minimalist OpenGL Environment (MinGLE)//// Version: 0.2.0// Author: Balazs Domonkos// Filename: src/threading/src/PthreadThread.cpp// Creation Date: December 13th 2007// Revision Date: December 13th 2007////// The Minimalist OpenGL Environment 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 3 of the License, or (at your// option) any later version.//// The Minimalist OpenGL Environment 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 Minimalist OpenGL Environment. If not, see // <http://www.gnu.org/licenses/>. See the COPYING file included // with this distribution file for license details.///// @file PthreadThread.cpp/// PTHREAD threading implementation// Header that contains the declaration#include <PthreadThread.h>// MinGLE headers#include <Exception.h>namespace MinGLE {// Dynamic library loader functionextern "C" void loadDynLib(void) { // Create and register pthread threading system implementation Threading::registerImplementation("pthread", new PthreadThreading());}// Dynamic library unloader functionextern "C" void unloadDynLib(void) { // Nothing to do}// =============================================================================PthreadMutex::PthreadMutex() { pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_FAST_NP); pthread_mutex_init(&mMutex, &attr); pthread_mutexattr_destroy(&attr);}PthreadMutex::~PthreadMutex() { pthread_mutex_destroy(&mMutex);}void PthreadMutex::lock() { pthread_mutex_lock(&mMutex);}void PthreadMutex::release() { pthread_mutex_unlock(&mMutex);}// =============================================================================PthreadThread::PthreadThread(Runnable *runnable, bool joinable) : Thread(runnable, joinable) {}void PthreadThread::_startThread() { pthread_attr_t attr; pthread_attr_init(&attr); if (mJoinable) // Create joinable thread Assert(!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE), "PthreadThread::_startThread"); else // Create detached thread Assert(!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED), "PthreadThread::_startThread"); // Create thread passing this pointer as thread argument // pthread_create call immediately starts the thread Assert(!pthread_create(&(mThread = 0), &attr, &(PthreadThread::_entryPoint), (void *) this), "PthreadThread::_startThread"); pthread_attr_destroy(&attr);}void PthreadThread::_joinThread() { // Wait for the thread // Note that the nonzero return value of the pthread_join function // is ignored since it it possible that the thread is not running. void *threadReturn; if (mRunning && !pthread_join(mThread, &threadReturn)) { // Check return value // On error it is zero Assert(threadReturn == &SUCCESS_RETURN_VALUE, "PthreadThread::_joinThread"); }}void PthreadThread::_killThread() { // TODO implement}void *PthreadThread::_entryPoint(void *arg) { PthreadThread *pthis = reinterpret_cast<PthreadThread *>(arg); Assert(pthis, "PthreadThread::_entryPoint"); // A simple Assert is not enought here, because the generated exception // cannot be handled. The error is reported using zero return value. if (!arg) return ((void *) &ERROR_RETURN_VALUE); //try { try { // Setting cancellation state //Assert(!pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL), "Thread::_entryPoint"); // Setting cancellation type //Assert(!pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL), "Thread::_entryPoint"); // Execute thread method pthis->mRunning = true, pthis->mRunnable->run(), pthis->mRunning = false; // Proper termination pthread_exit((void *) &SUCCESS_RETURN_VALUE); } catch(const Exception& e) { if(pthis && pthis->exceptionHandler(e)) // The exception is handled return((void *) &ERROR_RETURN_VALUE); else // Call unexpected unexpected(); } /*} catch(...) { unexpected(); }*/ return ((void *) &ERROR_RETURN_VALUE);}// =============================================================================Mutex* PthreadThreading::_createMutex() { return new PthreadMutex();}Thread* PthreadThreading::_createThread(Runnable *runnable, bool joinable) { return new PthreadThread(runnable, joinable);}} // namespace MinGLE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -