00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifdef ORO_PRAGMA_INTERFACE
00039 #pragma implementation
00040 #endif
00041 #include "Time.hpp"
00042 #include "PeriodicActivity.hpp"
00043 #include "os/MutexLock.hpp"
00044 #include "Logger.hpp"
00045 #include "TimerThread.hpp"
00046 #include <cmath>
00047
00048 namespace RTT
00049 {
00050
00051 PeriodicActivity::PeriodicActivity(int priority, Seconds period, RunnableInterface* r )
00052 : runner(r), running(false), active(false),
00053 thread_( TimerThread::Instance(priority,period) )
00054 {
00055 this->init();
00056 }
00057
00058 PeriodicActivity::PeriodicActivity(int scheduler, int priority, Seconds period, RunnableInterface* r )
00059 : runner(r), running(false), active(false),
00060 thread_( TimerThread::Instance(scheduler, priority,period) )
00061 {
00062 this->init();
00063 }
00064
00065 PeriodicActivity::PeriodicActivity(TimerThreadPtr thread, RunnableInterface* r )
00066 : runner(r), running(false), active(false),
00067 thread_( thread )
00068 {
00069 this->init();
00070 }
00071
00072 PeriodicActivity::PeriodicActivity(Seconds period, TimerThreadPtr thread, RunnableInterface* r )
00073 : runner(r), running(false), active(false),
00074 thread_(thread)
00075 {
00076 this->init();
00077 }
00078
00079 PeriodicActivity::PeriodicActivity(secs s, nsecs ns, TimerThreadPtr thread, RunnableInterface* r )
00080 : runner(r),
00081 running(false), active(false),
00082 thread_(thread)
00083 {
00084 this->init();
00085 }
00086
00087 PeriodicActivity::~PeriodicActivity()
00088 {
00089 stop();
00090 if (runner)
00091 runner->setActivity(0);
00092 }
00093
00094 void PeriodicActivity::init() {
00095 if (runner)
00096 runner->setActivity(this);
00097 thread_->start();
00098 }
00099
00100
00101 bool PeriodicActivity::run( RunnableInterface* r )
00102 {
00103 if ( isRunning() )
00104 return false;
00105 if (runner)
00106 runner->setActivity(0);
00107 runner = r;
00108 if (runner)
00109 runner->setActivity(this);
00110 return true;
00111 }
00112
00113 bool PeriodicActivity::start()
00114 {
00115 if ( isActive() || !thread_ ) {
00116
00117 return false;
00118 }
00119
00120 if ( !thread_->isRunning() && thread_->start() == false )
00121 return false;
00122
00123 active = true;
00124 bool inError = !this->initialize();
00125 if ( inError ) {
00126
00127 active = false;
00128 return false;
00129 }
00130
00131 bool res;
00132 res = thread_->addActivity( this );
00133 if ( res == false ) {
00134
00135 this->finalize();
00136 active = false;
00137 return false;
00138 }
00139
00140 running = true;
00141 return true;
00142 }
00143
00144 bool PeriodicActivity::stop()
00145 {
00146 if ( !isActive() ) return false;
00147
00148
00149
00150 if ( thread_->removeActivity( this ) ) {
00151 running = false;
00152 this->finalize();
00153 active = false;
00154 return true;
00155 }
00156 return false;
00157 }
00158
00159 bool PeriodicActivity::isRunning() const
00160 {
00161 return running;
00162 }
00163
00164 bool PeriodicActivity::isActive() const
00165 {
00166 return active;
00167 }
00168
00169 Seconds PeriodicActivity::getPeriod() const
00170 {
00171 return thread_->getPeriod();
00172 }
00173
00174 bool PeriodicActivity::initialize() {
00175 if (runner != 0)
00176 return runner->initialize();
00177 else
00178 return true;
00179 }
00180
00181 bool PeriodicActivity::execute()
00182 {
00183 return false;
00184 }
00185
00186 bool PeriodicActivity::trigger()
00187 {
00188 return false;
00189 }
00190
00191 void PeriodicActivity::step()
00192 {
00193
00194 if (runner != 0)
00195 runner->step();
00196 }
00197
00198 void PeriodicActivity::finalize() {
00199 if (runner != 0)
00200 runner->finalize();
00201 }
00202
00203 OS::ThreadInterface* PeriodicActivity::thread() { return thread_.get(); }
00204
00205 bool PeriodicActivity::isPeriodic() const {
00206 return true;
00207 }
00208 }