PeriodicActivity.cpp

00001 /***************************************************************************
00002   tag: Peter Soetens  Mon May 10 19:10:29 CEST 2004  PeriodicActivity.cxx 
00003 
00004                         PeriodicActivity.cxx -  description
00005                            -------------------
00006     begin                : Mon May 10 2004
00007     copyright            : (C) 2004 Peter Soetens
00008     email                : peter.soetens@mech.kuleuven.ac.be
00009  
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
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             //Logger::log() << Logger::Error << "PeriodicActivity : already active or thread not running." << Logger::endl;
00117             return false;
00118         }
00119         // If thread is not yet running, try to start it.
00120         if ( !thread_->isRunning() && thread_->start() == false )
00121             return false;
00122         
00123         active = true;
00124         bool inError = !this->initialize();
00125         if ( inError ) {
00126             //Logger::log() << Logger::Error << "PeriodicActivity : initialize() returned false " << Logger::endl;
00127             active = false;
00128             return false;
00129         }
00130 
00131         bool res;
00132         res = thread_->addActivity( this );
00133         if ( res == false ) {
00134             //Logger::log() << Logger::Error << "PeriodicActivity : addActivity() returned false " << Logger::endl;
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         // since removeActivity synchronises, we do not need to mutex-lock
00149         // stop()
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         // override this method to avoid running runner.
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 }

Generated on Tue Mar 25 17:41:48 2008 for OrocosReal-TimeToolkit by  doxygen 1.5.3