OpenWalnut 1.2.5
|
00001 //--------------------------------------------------------------------------- 00002 // 00003 // Project: OpenWalnut ( http://www.openwalnut.org ) 00004 // 00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS 00006 // For more information see http://www.openwalnut.org/copying 00007 // 00008 // This file is part of OpenWalnut. 00009 // 00010 // OpenWalnut is free software: you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as published by 00012 // the Free Software Foundation, either version 3 of the License, or 00013 // (at your option) any later version. 00014 // 00015 // OpenWalnut is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 // GNU Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public License 00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>. 00022 // 00023 //--------------------------------------------------------------------------- 00024 00025 #include <iostream> 00026 00027 #include "WConditionOneShot.h" 00028 #include "WCondition.h" 00029 #include "WLogger.h" 00030 00031 #include "WThreadedRunner.h" 00032 00033 WThreadedRunner::WThreadedRunner(): 00034 m_shutdownFlag( new WConditionOneShot(), false ) 00035 { 00036 // initialize members 00037 } 00038 00039 WThreadedRunner::~WThreadedRunner() 00040 { 00041 // cleanup 00042 // XXX is this working if thread already has finished? 00043 // wait( true ); <-- no 00044 } 00045 00046 void WThreadedRunner::run() 00047 { 00048 run( boost::bind( &WThreadedRunner::threadMain, this ) ); 00049 } 00050 00051 void WThreadedRunner::run( THREADFUNCTION f ) 00052 { 00053 m_thread = boost::thread( f ); 00054 } 00055 00056 void WThreadedRunner::wait( bool requestFinish ) 00057 { 00058 if( requestFinish ) 00059 { 00060 requestStop(); 00061 } 00062 m_thread.join(); 00063 } 00064 00065 void WThreadedRunner::requestStop() 00066 { 00067 // first notify 00068 notifyStop(); 00069 00070 // then signal it 00071 m_shutdownFlag( true ); 00072 } 00073 00074 void WThreadedRunner::waitForStop() 00075 { 00076 m_shutdownFlag.wait(); 00077 } 00078 00079 void WThreadedRunner::threadMain() 00080 { 00081 WLogger::getLogger()->addLogMessage( "This should never be called. Implement a thread function here.", "WThreadedRunner", LL_WARNING ); 00082 } 00083 00084 void WThreadedRunner::notifyStop() 00085 { 00086 } 00087 00088 void WThreadedRunner::yield() const 00089 { 00090 m_thread.yield(); 00091 } 00092 00093 void WThreadedRunner::sleep( const int32_t t ) const 00094 { 00095 boost::this_thread::sleep( boost::posix_time::seconds( t ) ); 00096 } 00097 00098 void WThreadedRunner::msleep( const int32_t t ) const 00099 { 00100 boost::this_thread::sleep( boost::posix_time::microseconds( t ) ); 00101 } 00102