📄 pththread.cpp
字号:
// ___ ___ ___ ___ ___ // /\__\ ___ /\__\ /\ \ /\__\ /\ \. // /::| | /\ \ /::| | /::\ \ /:/ / /::\ \. // /:|:| | \:\ \ /:|:| | /:/\:\ \ /:/ / /:/\:\ \. // /:/|:|__|__ /::\__\ /:/|:| |__ /:/ \:\ \ /:/ / /::\~\:\ \. // /:/ |::::\__\ __/:/\/__/ /:/ |:| /\__\ /:/__/_\:\__\ /:/__/ /:/\:\ \:\__\.// \/__/~~/:/ / /\/:/ / \/__|:|/:/ / \:\ /\ \/__/ \:\ \ \:\~\:\ \/__/// /:/ / \::/__/ |:/:/ / \:\ \:\__\ \:\ \ \:\ \:\__\. // /:/ / \:\__\ |::/ / \:\/:/ / \:\ \ \:\ \/__/ // /:/ / \/__/ /:/ / \::/ / \:\__\ \:\__\. // \/__/ \/__/ \/__/ \/__/ \/__/ // // =============================================================================// Minimalist OpenGL Environment// =============================================================================//// This file is part of Minimalist OpenGL Environment (MinGLE)//// Version: 0.2.0// Author: Balazs Domonkos// Filename: src/threading/src/PthThread.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 PthThread.cpp/// PTH threading implementation// Header that contains the declaration#include <PthThread.h>// MinGLE headers#include <Exception.h>namespace MinGLE {// Dynamic library loader functionextern "C" void loadDynLib(void) { // Create and register pth threading system implementation Threading::registerImplementation("pth", new PthThreading());}// Dynamic library unloader functionextern "C" void unloadDynLib(void) { // Nothing to do}// =============================================================================PthMutex::PthMutex() { pth_mutex_init(&mMutex);}PthMutex::~PthMutex() { // Nothing to free}void PthMutex::lock() { pth_mutex_acquire(&mMutex, 1, 0);}void PthMutex::release() { pth_mutex_release(&mMutex);}// =============================================================================PthThread::PthThread(Runnable *runnable, bool joinable) : Thread(runnable, joinable) {}void PthThread::_startThread() { pth_attr_t attr = pth_attr_new(); if (mJoinable) // Create joinable thread Assert(pth_attr_set(attr, PTH_ATTR_JOINABLE, TRUE), "PthThread::_startThread"); else // Create detached thread Assert(pth_attr_set(attr, PTH_ATTR_JOINABLE, FALSE), "PthThread::_startThread"); // Create thread passing this pointer as thread argument // pth_spawn call immediately starts the thread // TODO: not working, it seems no thread is generated Assert(mThread = pth_spawn(attr, PthThread::_entryPoint, (void *) this), "PthThread::_startThread"); pth_attr_destroy(attr);}void PthThread::_joinThread() { // Wait for the thread // Note that the nonzero return value of the pth_join function // is ignored since it it possible that the thread is not running. void *threadReturn; if (mRunning && !pth_join(mThread, &threadReturn)) { // Check return value // On error it is zero Assert(threadReturn == &SUCCESS_RETURN_VALUE, "PthThread::_joinThread"); }}void PthThread::_killThread() { // TODO implement}void *PthThread::_entryPoint(void *arg) { PthThread *pthis = reinterpret_cast<PthThread *>(arg); Assert(pthis, "PthThread::_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 { // Execute thread method pthis->mRunning = true, pthis->mRunnable->run(), pthis->mRunning = false; // Proper termination pth_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);}// =============================================================================PthThreading::PthThreading() { pth_init();}PthThreading::~PthThreading() { pth_kill();}Mutex* PthThreading::_createMutex() { return new PthMutex();}Thread* PthThreading::_createThread(Runnable *runnable, bool joinable) { return new PthThread(runnable, joinable);}} // namespace MinGLE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -