RTT::OS::Thread Class Reference

A Thread object executes user code in its own thread. More...

#include <rtt/os/Thread.hpp>

Inheritance diagram for RTT::OS::Thread:
RTT::OS::ThreadInterface RTT::Activity

List of all members.

Public Member Functions

 Thread (int scheduler, int priority, double period, const std::string &name, OS::RunnableInterface *r=0)
 Create a Thread with a given scheduler type, priority and a name.
virtual bool run (OS::RunnableInterface *r)
 Run the functionality of one RunnableInterface object.
virtual bool start ()
 Start the Thread.
virtual bool stop ()
 Stop the Thread.
bool setPeriod (Seconds s)
 Set the periodicity in Seconds.
bool setPeriod (secs s, nsecs ns)
 Set the periodicity of this thread (seconds, nanoseconds).
bool setPeriod (TIME_SPEC p)
 Set the periodicity of this thread.
void getPeriod (secs &s, nsecs &ns) const
 Get the periodicity of this thread (seconds, nanoseconds).
virtual Seconds getPeriod () const
 Get the periodicity in Seconds.
virtual nsecs getPeriodNS () const
 Get the periodicity in nanoseconds.
virtual bool isPeriodic () const
virtual bool isRunning () const
 Returns whether the thread is running.
virtual bool isActive () const
 Returns whether the thread is active.
virtual const char * getName () const
 Read the name of this task.
virtual RTOS_TASK * getTask ()
 Get the RTOS_TASK pointer.
virtual bool setScheduler (int sched_type)
 Change the scheduler policy in which this thread runs.
virtual int getScheduler () const
 Get the scheduler policy in which this thread runs.
virtual bool setPriority (int priority)
 Set the priority of this Thread.
virtual int getPriority () const
 The priority of this Thread.
virtual void yield ()
 Yields (put to the back of the scheduler queue) the calling thread.
void setMaxOverrun (int m)
int getMaxOverrun () const
unsigned int threadNumber () const
 The unique thread number (within the same process).

Static Public Member Functions

static void setStackSize (unsigned int ssize)
 Sets the stack size of the threads to be created.

Protected Member Functions

void terminate ()
 Exit and destroy the thread.
void emergencyStop ()
virtual void step ()
virtual void loop ()
virtual bool breakLoop ()
virtual bool initialize ()
virtual void finalize ()

Protected Attributes

int threadnb
 Threads are given an unique number, which follows thread creation order.

Friends

void * thread_function (void *t)

Detailed Description

A Thread object executes user code in its own thread.

If the underlying OS supports it, both period and priority can be changed after creation. When the period is 0, the loop() function is executed, when the period is greater than 0, the step() function is executed. Note: if the user does not implement loop(), it will call step() by default.

The main functions to implement are initialize(), step()/loop() and finalize(). initialize() is called when start() is called, and before the first step()/loop() and finalize() is called when stop() is called after the last step()/loop() returns.

behavior

When a period is set, step() is executed from the moment start() is called, and afterwards according to it's period. When stop() is called, it waits for the step() function to return and then stops the periodic execution.

Step() overruns are detected and the threshold to 'emergency stop' the thread can be set by setMaxOverrun(). Overruns must be accumulated 'on average' to trigger this behavior: one not overrunning step() compensates for one overrunning step().

periodic behaviour

The first invocation of start() invokes the initialize() function and runs the loop() method in the thread. When loop() returns the thread waits for another start() to execute loop() again. In that case, initialize() is not executed.

When stop() is called and the thread is still executing loop() and breakLoop() returns true (not the default), the stop() function succeeds and the finalize() method is called by stop(). If the thread was not executing loop(), stop will always call finalize() and return success.

The user must provide an implementation of breakLoop() returning true to make stop() work while loop() is being executed. stop() will fail and not execute finalize() if the thread executes loop() and breakLoop() is not reimplemented to return true.

When a RunnableInterface object is given, the above methods initialize(), loop(), breakLoop() and finalize() are called on that object instead of on the SingleThread's virtual functions.

, priorities, schedulers and stack sizes

These four parameters are the parameters that users wish to set for every thread created. All but stack size can be modified after creation, and if the OS permits it, modified even when the thread is using user code. All these parameters are passed to the underlying OS unmodified. In case an incorrect combination is made, the change may be rejected or adjusted to a close or safe value.

See also:
setPeriod, setScheduler, setPriority, setStackSize

Definition at line 115 of file Thread.hpp.


Constructor & Destructor Documentation

RTT::OS::Thread::Thread ( int  scheduler,
int  priority,
double  period,
const std::string &  name,
OS::RunnableInterface r = 0 
)

Create a Thread with a given scheduler type, priority and a name.

Parameters:
scheduler The scheduler, one of ORO_SCHED_RT or ORO_SCHED_OTHER.
priority The priority of the thread, this is interpreted by your RTOS.
period The period in seconds (eg 0.001) of the thread, or zero if not periodic.
name The name of the Thread. May be used by your OS to identify the thread.
r The optional RunnableInterface instance to run. If not present, the thread's own virtual functions are executed.

Member Function Documentation

virtual bool RTT::OS::Thread::breakLoop (  )  [protected, virtual]
virtual void RTT::OS::Thread::finalize (  )  [protected, virtual]
virtual Seconds RTT::OS::Thread::getPeriod (  )  const [virtual]

Get the periodicity in Seconds.

Return zero if non periodic.

Implements RTT::OS::ThreadInterface.

Reimplemented in RTT::Activity.

virtual nsecs RTT::OS::Thread::getPeriodNS (  )  const [virtual]

Get the periodicity in nanoseconds.

Return zero if non periodic.

Implements RTT::OS::ThreadInterface.

virtual int RTT::OS::Thread::getPriority (  )  const [virtual]

The priority of this Thread.

Returns:
The priority given upon construction of this thread or set with setPriority. The returned number has to be interpreted in the current OS scheduler.
See also:
setScheduler

Implements RTT::OS::ThreadInterface.

virtual int RTT::OS::Thread::getScheduler (  )  const [virtual]

Get the scheduler policy in which this thread runs.

Returns:
An OS-specific value which represents the used scheduler.
See also:
setScheduler

Implements RTT::OS::ThreadInterface.

virtual RTOS_TASK* RTT::OS::Thread::getTask (  )  [inline, virtual]

Get the RTOS_TASK pointer.

Note:
Using this function leads to non-portable code. It is here for users which wish to tweak OS specific thread settings.

Implements RTT::OS::ThreadInterface.

Definition at line 183 of file Thread.hpp.

virtual bool RTT::OS::Thread::initialize (  )  [protected, virtual]
virtual bool RTT::OS::Thread::isActive (  )  const [virtual]

Returns whether the thread is active.

A thread is active between the invocation of start() and the invocation of stop().

Implements RTT::OS::ThreadInterface.

Reimplemented in RTT::Activity.

virtual bool RTT::OS::Thread::isRunning (  )  const [virtual]

Returns whether the thread is running.

A thread is running if it is executing its loop() function or in the process of periodically calling step(). Hence for periodic threads, isRunning() == isActive() while for non-periodic threads, isRunning() is only true if isActive() and it is executing loop().

Implements RTT::OS::ThreadInterface.

Reimplemented in RTT::Activity.

virtual void RTT::OS::Thread::loop (  )  [protected, virtual]
virtual bool RTT::OS::Thread::run ( OS::RunnableInterface r  )  [virtual]

Run the functionality of one RunnableInterface object.

Only one RunnableInterface object can be run, the old one is disconnected.

Parameters:
r The object to run or zero to clear.
Returns:
true if accepted, false if the thread is running.

Implements RTT::OS::ThreadInterface.

bool RTT::OS::Thread::setPeriod ( Seconds  new_period  )  [virtual]

Set the periodicity in Seconds.

Parameters:
new_period A positive number expressing the period
Returns:
true if it was accepted, false otherwise.

Implements RTT::OS::ThreadInterface.

Reimplemented in RTT::Activity.

virtual bool RTT::OS::Thread::setPriority ( int  priority  )  [virtual]

Set the priority of this Thread.

Parameters:
priority The priority given upon construction of this thread. It has to be interpreted in the current OS scheduler.
See also:
setScheduler

Implements RTT::OS::ThreadInterface.

virtual bool RTT::OS::Thread::setScheduler ( int  sched_type  )  [virtual]

Change the scheduler policy in which this thread runs.

Parameters:
sched_type An OS-specific value which selects a scheduler. Orocos requires that these two values are available:

  • ORO_SCHED_RT: Hint the OS that this thread should be scheduled as a priority or real-time process.
  • ORO_SCHED_OTHER: Hint the OS that this thread should not be scheduled as a priority or real-time process.

Your OS can in addition provide other sched_type's which map more naturally to the schedulers present. If your OS does not make a distinction between real-time and other, both values may map to the same scheduler type.

Returns:
true if the change could be made.

Implements RTT::OS::ThreadInterface.

static void RTT::OS::Thread::setStackSize ( unsigned int  ssize  )  [static]

Sets the stack size of the threads to be created.

This value is suggestive and may be altered or ignored by your operating system. Use zero to use the system's default.

Parameters:
ssize the size of the stack in bytes
virtual bool RTT::OS::Thread::start (  )  [virtual]

Start the Thread.

Postcondition:
initialize() is called first
The Thread is running
Returns:
true if the function did succeed. false otherwise.

Implements RTT::OS::ThreadInterface.

Reimplemented in RTT::Activity.

virtual void RTT::OS::Thread::step (  )  [protected, virtual]
virtual bool RTT::OS::Thread::stop (  )  [virtual]

Stop the Thread.

Postcondition:
The Thread is no longer being run
finalize() is called when the Thread is stopped.
Returns:
true if the function did succeed. false otherwise.

Implements RTT::OS::ThreadInterface.

Reimplemented in RTT::Activity.

void RTT::OS::Thread::terminate (  )  [protected]

Exit and destroy the thread.

Precondition:
this is only called from within the destructor.
Postcondition:
the thread does no longer exist.

Member Data Documentation

int RTT::OS::ThreadInterface::threadnb [protected, inherited]

Threads are given an unique number, which follows thread creation order.

See also:
OS::threads

Definition at line 191 of file ThreadInterface.hpp.


The documentation for this class was generated from the following file:
Generated on Thu Dec 23 13:22:47 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3