不管是C 还是 C++ 在面向复杂业务逻辑如果不对任务结构进行封装,你的精力将被淹没在代码细节中不能自拔。
建立线程控制标准
namespace RTT
{ namespace base {
/**
* @brief Interface to start/stop and query a Activity.
*
* A ActivityInterface provides the control methods
* for activities. An activity can be periodic, non periodic, event driven
* or any activity object which can be started, stopped
* and be queried for their state and (optional) period.
*
* It is complementary to the RunnableInterface, which
* defines the methods for the functionality that is executed.
* @see RunnableInterface
* @ingroup CoreLibActivities
*/
class RTT_API ActivityInterface
{
protected:
RunnableInterface* runner;
/**
* This method is only meant for RunnableInterface (ie \a runner)
* in order to inform the ActivityInterface it should
* no longer be used. run(0) can not be used in this case
* because it would recurse.
*/
void disableRun(RunnableInterface* caller) { if (caller == runner) runner = 0; }
public:
friend class RunnableInterface;
typedef boost::shared_ptr shared_ptr;
ActivityInterface() : runner(0) {}
ActivityInterface(RunnableInterface* run);
virtual ~ActivityInterface();
/**
* Run exclusively this RunnableInterface.
* @pre this->isRunning() == false
* @param r
* The RunnableInterface to run exclusively.
* @return true if succeeded, false otherwise
*/
virtual bool run( RunnableInterface* r );
/**
* Start the activity.
* This will call RunnableInterface::initialize() and upon
* success, effectively start the activity, by running the
* RunnableInterface::step() or RunnableInterface::loop() in
* a thread.
* @see isPeriodic()
*
* @return true if the activity is started, false otherwise
*/
virtual bool start() = 0;
/**
* Stop the activity
* This will stop the activity by removing it from the 'run-queue'
* of a thread or call RunnableInterface::breakLoop().
* If no errors occured, RunnableInterface::finalize() is called.
* @see isPeriodic()
*
* @return true if the activity is stopped, false otherwise
*/
virtual bool stop() = 0;
/**
* Query if the activity is initialized and executing.
* This is more strict than isActive(), it is only true
* after initialize() is executed and before finalize()
* is executed. More-over, an Activity may decide to be
* temporarily not running (not executing code), \a waiting for a signal
* to proceed. If this->isActive() and !this->isRunning()
* then the Activity is in a \a waiting state.
*
* @return true if it is running, false otherwise
*/
virtual bool isRunning() const = 0;
/**
* Query if the activity is started.
* This is less strict than isRunning(), it is true during
* initialize(), step() or loop() and finalize(). Use
* this method to check if an activity was start()ed.
*
* @return true if it is active, false otherwise
*/
virtual bool isActive() const = 0;
/**
* Get the periodicity of this activity in Seconds
*
* @return The execution period of this activity (zero if !this->isPeriodic() ).
*/
virtual Seconds getPeriod() const = 0;
/**
* Inspect if this activity is periodic. If so, it will call RunnableInterface::step().
* If the activity is not periodic, it will call RunnableInterface::loop().
*
* @return true if periodic.
*/
virtual bool isPeriodic() const = 0;
/**
* Set the periodicity of this activity in Seconds.
* Note that not all activity implementation support periods. A period
* of s == 0 indicates non periodic. A non supported setting returns false.
*
* @return true if it could be updated, false otherwise.
*/
virtual bool setPeriod(Seconds s) = 0;
/**
* Get the cpu affinity of this activity
*
* @return The cpu affinity of this activity.
*/
virtual unsigned getCpuAffinity() const = 0;
/**
* Set the cpu affinity of this activity.
* @return true if it could be updated, false otherwise.
*/
virtual bool setCpuAffinity(unsigned cpu) = 0;
/**
* Execute this activity such that it \a executes a step or loop of the RunnableInterface.
* When you invoke execute() you intend to call the step() or loop() methods.
* Some activity implementations allow a user controlled execute, others ignore it,
* in which case execute() returns false.
*
* Semantics: If execute() returns true, the activity has been executed exactly once
* during execute().
*
* @retval true When this->isActive() and the implementation allows external
* executes.
* @retval false When !this->isActive() or the implementation does not
* allow external updating.
* @see trigger() for use in callbacks which want execute() to be executed.
*/
virtual bool execute() = 0;
/**
* Trigger that work has to be done. When you invoke
* trigger(), you intend to notify the mechanism that calls
* execute(), that execute() should be called. This allows a
* separation between actually executing code (execute()) and
* notifying that code must be executed (trigger()). A trigger
* may be ignored by the implementation, in which case trigger
* returns false.
*
* Semantics: If trigger() returns true, the activity will be executed at least
* once from the moment trigger() is called.
*
* Requests this Activity to wakeup and call step() + work(Trigger).
* If the thread is periodic, it will continue sleeping for the remainder of the time
* after the work() has finished.
*
* @retval true When this->isActive() and the implementation allows external
* triggers.
* @retval false When !this->isActive() or the implementation does not
* allow external triggering.
*/
virtual bool trigger() = 0;
/**
* Requests this Activity to wakeup and call step() + work(Timeout).
* Will be ignored for periodic activities, since they use an internal
* timing mechanism, but can be used for non-periodic activities which
* want to emulate a timeout happening towards the base::RunnableInterface.
*/
virtual bool timeout() = 0;
/**
* Returns a pointer to the thread which will
* run this activity. Will not be null.
*/
virtual os::ThreadInterface* thread() = 0;
/**
* Returns a pointer to the RunnableInterface instance
*/
virtual RunnableInterface* getRunner() const;
};
}}
建立事务标准
class RTT_API RunnableInterface
{
/**
* The Activityobject which owns this RunnableInterface.
*/
ActivityInterface* owner_act;
public:
enum WorkReason { TimeOut = 0, Trigger, IOReady };
/**
* Create a runnable object. The optional constructor parameter
* allows the object to attach directly to a thread. Otherwise,
* os::ThreadInterface::run(RunnableInterface*) must be used to
* attach this object to a thread. A thread can only run one
* RunnableInterface object, use CoreLib tasks otherwise.
* @param t The thread this object must attach to.
*/
RunnableInterface();
/**
* Checks if this is still in a task and if so, issues a critical warning.
*/
virtual ~RunnableInterface();
/**
* The method that will be called before the first periodical
* execution of \a step() ( or non periodical execution of \a loop() ),
* when the thread is started.
*/
virtual bool initialize() = 0;
/**
* The method that will be (periodically) executed when this
* object is run in an Activity.
*/
virtual void step() = 0;
/**
* Identical to step() but gives a reason why the function was called.
* Both step() and work() will be called an equal amount of times,
* so you need to use only one, but work gives you the reason why.
*/
virtual void work(WorkReason reason);
/**
* The method that will be executed once when this
* class is run in a non periodic Activity. The default
* implementation calls step() once.
*/
virtual void loop();
/**
* This method is called by the framework to break out of the \a loop() method.
* Reimplement this method to signal \a loop() to return and return
* true on success. When this method is not reimplemented by you, it
* will always return \a false, denoting that the loop can not
* be broken. If breakLoop() returns \a true, the caller will wait
* until loop() returns.
* @return true if the loop could be notified to return.
*/
virtual bool breakLoop();
/**
* The method that will be called after the last periodical
* execution of \a step() ( or non periodical execution of \a loop() ),
* when the RunnableInterface is stopped.
*/
virtual void finalize() = 0;
/**
* Get the thread this object is run in.
* @return a pointer to the thread or 0 if not run by a thread.
*/
virtual os::ThreadInterface* getThread() const;
/**
* This method is for 'intelligent' activity implementations
* that wish to see if it is required to call step() (again).
* By default, \a false is returned. You should only return \a true in
* case there is a temporary reason to (re-)run step.
* @return \a true if this object should be run.
* @see extras::SequentialActivity implementation to see how this can be
* of use.
*/
virtual bool hasWork();
/**
* @brief Query for the task this interface is run in.
*
* Zero denotes that no task is present to run
* it, and hence no detailed information is available.
*
* @return The Activity which runs this RunnableInterface.
*/
inline ActivityInterface* getActivity() const;
/**
* @brief Set the task this interface is run in.
*
* A Zero means no task is running it.
*
* @param task The ActivityInterface running this interface.
*/
virtual void setActivity( ActivityInterface* task );
};
线程控制 ActivityInterface
事务运行标准 RunnableInterface