Libbarrett  1.2.4
include/barrett/systems/abstract/execution_manager.h
00001 /*
00002         Copyright 2009, 2010 Barrett Technology <support@barrett.com>
00003 
00004         This file is part of libbarrett.
00005 
00006         This version of libbarrett is free software: you can redistribute it
00007         and/or modify it under the terms of the GNU General Public License as
00008         published by the Free Software Foundation, either version 3 of the
00009         License, or (at your option) any later version.
00010 
00011         This version of libbarrett is distributed in the hope that it will be
00012         useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00013         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014         GNU General Public License for more details.
00015 
00016         You should have received a copy of the GNU General Public License along
00017         with this version of libbarrett.  If not, see
00018         <http://www.gnu.org/licenses/>.
00019 
00020         Further, non-binding information about licensing is available at:
00021         <http://wiki.barrett.com/libbarrett/wiki/LicenseNotes>
00022 */
00023 
00024 /*
00025  * execution_manager.h
00026  *
00027  *  Created on: Dec 9, 2009
00028  *      Author: dc
00029  */
00030 
00031 
00032 // Include system.h before the include-guard to resolve include order dependency.
00033 #include <barrett/systems/abstract/system.h>
00034 
00035 
00036 #ifndef BARRETT_SYSTEMS_ABSTRACT_EXECUTION_MANAGER_H_
00037 #define BARRETT_SYSTEMS_ABSTRACT_EXECUTION_MANAGER_H_
00038 
00039 
00040 #include <boost/intrusive/list.hpp>
00041 
00042 #include <libconfig.h++>
00043 
00044 #include <barrett/detail/ca_macro.h>
00045 #include <barrett/detail/libconfig_utils.h>
00046 #include <barrett/thread/abstract/mutex.h>
00047 #include <barrett/thread/null_mutex.h>
00048 
00049 
00050 namespace barrett {
00051 namespace systems {
00052 
00053 
00054 // Thrown to indicate that the ExecutionManager should stop executing.
00055 class ExecutionManagerException : public std::runtime_error {
00056 public:
00057         explicit ExecutionManagerException(const std::string& str) : std::runtime_error(str) {}
00058 };
00059 
00060 
00061 
00062 // this isn't technically abstract, but neither does it have all the elements of a useful interface...
00063 class ExecutionManager {
00064 public:
00065         explicit ExecutionManager(double period_s = -1.0) :
00066                 mutex(new thread::NullMutex), period(period_s), ut(System::UT_NULL) {}
00067         explicit ExecutionManager(const libconfig::Setting& setting) :
00068                 mutex(new thread::NullMutex), period(), ut(System::UT_NULL)
00069         {
00070                 period = barrett::detail::numericToDouble(setting["control_loop_period"]);
00071         }
00072         ~ExecutionManager();
00073 
00074         void startManaging(System& sys);
00075         void stopManaging(System& sys);
00076 
00077         thread::Mutex& getMutex() const { return *mutex; }
00078         double getPeriod() const {  return period;  }
00079 
00080 protected:
00081         void runExecutionCycle();
00082 
00083         thread::Mutex* mutex;
00084         double period;
00085         System::update_token_type ut;
00086 
00087 private:
00088         typedef boost::intrusive::list<System, boost::intrusive::member_hook<System, System::managed_hook_type, &System::managedHook> > managed_system_list_type;
00089         managed_system_list_type managedSystems;
00090 
00091         DISALLOW_COPY_AND_ASSIGN(ExecutionManager);
00092 };
00093 
00094 
00095 }
00096 }
00097 
00098 
00099 #endif /* BARRETT_SYSTEMS_ABSTRACT_EXECUTION_MANAGER_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Defines