OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WThreadedRunner.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <iostream>
26 
27 #include "WConditionOneShot.h"
28 #include "WCondition.h"
29 #include "WLogger.h"
30 
31 #include "WThreadedRunner.h"
32 
34  m_shutdownFlag( new WConditionOneShot(), false )
35 {
36  // initialize members
37 }
38 
40 {
41  // cleanup
42  // XXX is this working if thread already has finished?
43  // wait( true ); <-- no
44 }
45 
47 {
48  run( boost::bind( &WThreadedRunner::threadMain, this ) );
49 }
50 
52 {
53  m_thread = boost::thread( f );
54 }
55 
56 void WThreadedRunner::wait( bool requestFinish )
57 {
58  if( requestFinish )
59  {
60  requestStop();
61  }
62  m_thread.join();
63 }
64 
66 {
67  // first notify
68  notifyStop();
69 
70  // then signal it
71  m_shutdownFlag( true );
72 }
73 
75 {
77 }
78 
80 {
81  WLogger::getLogger()->addLogMessage( "This should never be called. Implement a thread function here.", "WThreadedRunner", LL_WARNING );
82 }
83 
85 {
86 }
87 
89 {
90  m_thread.yield();
91 }
92 
93 void WThreadedRunner::sleep( const int32_t t ) const
94 {
95  boost::this_thread::sleep( boost::posix_time::seconds( t ) );
96 }
97 
98 void WThreadedRunner::msleep( const int32_t t ) const
99 {
100  boost::this_thread::sleep( boost::posix_time::microseconds( t ) );
101 }
102