Orocos Real-Time Toolkit
2.6.0
|
A Thread object executes user code in its own thread. More...
#include <rtt/os/Thread.hpp>
Public Member Functions | |
Thread (int scheduler, int priority, double period, unsigned cpu_affinity, const std::string &name) | |
Create a Thread with a given scheduler type, priority and a name. | |
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 const RTOS_TASK * | getTask () const |
const version of the above. | |
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 unsigned int | getPid () const |
Returns the Process or Thread ID of this thread, as assigned by the Operating System. | |
virtual bool | setCpuAffinity (unsigned cpu_affinity) |
Set cpu affinity for this thread The cpu affinity of the thread (. | |
virtual unsigned | getCpuAffinity () const |
virtual void | yield () |
Yields (put to the back of the scheduler queue) the calling thread. | |
virtual void | setMaxOverrun (int m) |
virtual int | getMaxOverrun () const |
virtual void | setWaitPeriodPolicy (int p) |
Set the wait policy of a periodic thread. | |
unsigned int | threadNumber () const |
The unique thread number (within the same process). | |
bool | isSelf () const |
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) |
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.
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().
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.
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.
Definition at line 109 of file Thread.hpp.
RTT::os::Thread::Thread | ( | int | scheduler, |
int | priority, | ||
double | period, | ||
unsigned | cpu_affinity, | ||
const std::string & | name | ||
) |
Create a Thread with a given scheduler type, priority and a name.
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. |
cpu_affinity | The cpu affinity of the thread, this is interpreted by your RTOS. |
name | The name of the Thread. May be used by your OS to identify the thread. the thread's own virtual functions are executed. |
Definition at line 238 of file Thread.cpp.
bool RTT::os::Thread::breakLoop | ( | ) | [protected, virtual] |
Reimplemented in RTT::corba::OrbRunner, RTT::extras::FileDescriptorActivity, RTT::mqueue::Dispatcher, RTT::Activity, and RTT::corba::CorbaDispatcher.
Definition at line 508 of file Thread.cpp.
Referenced by stop().
void RTT::os::Thread::finalize | ( | ) | [protected, virtual] |
Reimplemented in RTT::corba::OrbRunner, RTT::Activity, RTT::extras::TimerThread, and RTT::extras::SimulationThread.
Definition at line 519 of file Thread.cpp.
Referenced by stop().
unsigned RTT::os::Thread::getCpuAffinity | ( | ) | const [virtual] |
Reimplemented in RTT::Activity.
Definition at line 593 of file Thread.cpp.
References RTT::os::rtos_task_get_cpu_affinity().
double RTT::os::Thread::getPeriod | ( | ) | const [virtual] |
Get the periodicity in Seconds.
Return zero if non periodic.
Implements RTT::os::ThreadInterface.
Reimplemented in RTT::Activity.
Definition at line 578 of file Thread.cpp.
Referenced by RTT::extras::SimulationThread::run(), RTT::extras::SimulationThread::SimulationThread(), RTT::extras::SimulationThread::step(), and stop().
nsecs RTT::os::Thread::getPeriodNS | ( | ) | const [virtual] |
Get the periodicity in nanoseconds.
Return zero if non periodic.
Implements RTT::os::ThreadInterface.
Definition at line 583 of file Thread.cpp.
unsigned int RTT::os::Thread::getPid | ( | ) | const [virtual] |
Returns the Process or Thread ID of this thread, as assigned by the Operating System.
This function tries to return the Thread ID, if the OS supports it, otherwise it returns the process ID. If that is not available either, zero is returned. On Linux systems for example, the Thread ID of the main thread is equal to the Process ID. The Thread ID of every newly created thread is different from the Process ID (and unique).
Implements RTT::os::ThreadInterface.
Definition at line 598 of file Thread.cpp.
References RTT::os::rtos_task_get_pid().
int RTT::os::Thread::getPriority | ( | ) | const [virtual] |
The priority of this Thread.
Implements RTT::os::ThreadInterface.
Definition at line 573 of file Thread.cpp.
References RTT::os::rtos_task_get_priority().
Referenced by RTT::extras::SimulationThread::SimulationThread().
int RTT::os::Thread::getScheduler | ( | ) | const [virtual] |
Get the scheduler policy in which this thread runs.
Implements RTT::os::ThreadInterface.
Definition at line 475 of file Thread.cpp.
References RTT::os::rtos_task_get_scheduler().
Referenced by setScheduler().
virtual RTOS_TASK* RTT::os::Thread::getTask | ( | ) | [inline, virtual] |
Get the RTOS_TASK pointer.
Implements RTT::os::ThreadInterface.
Definition at line 174 of file Thread.hpp.
Referenced by RTT::os::thread_function().
bool RTT::os::Thread::initialize | ( | ) | [protected, virtual] |
Reimplemented in RTT::Activity, RTT::corba::CorbaDispatcher, RTT::mqueue::Dispatcher, RTT::extras::TimerThread, and RTT::extras::SimulationThread.
Definition at line 514 of file Thread.cpp.
Referenced by start().
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.
Definition at line 451 of file Thread.cpp.
Referenced by start().
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, RTT::extras::FileDescriptorActivity, and RTT::extras::SimulationThread.
Definition at line 446 of file Thread.cpp.
void RTT::os::Thread::loop | ( | ) | [protected, virtual] |
Reimplemented in RTT::corba::OrbRunner, RTT::extras::FileDescriptorActivity, RTT::Activity, RTT::corba::CorbaDispatcher, and RTT::mqueue::Dispatcher.
Definition at line 503 of file Thread.cpp.
References step().
Referenced by RTT::os::thread_function().
bool RTT::os::Thread::setCpuAffinity | ( | unsigned | cpu_affinity | ) | [virtual] |
Set cpu affinity for this thread The cpu affinity of the thread (.
Reimplemented in RTT::Activity.
Definition at line 588 of file Thread.cpp.
References RTT::os::rtos_task_set_cpu_affinity().
bool RTT::os::Thread::setPeriod | ( | Seconds | new_period | ) | [virtual] |
Set the periodicity in Seconds.
new_period | A positive number expressing the period |
Implements RTT::os::ThreadInterface.
Reimplemented in RTT::Activity.
Definition at line 523 of file Thread.cpp.
Referenced by setPeriod().
bool RTT::os::Thread::setPriority | ( | int | priority | ) | [virtual] |
Set the priority of this Thread.
priority | The priority given upon construction of this thread. It has to be interpreted in the current OS scheduler. |
Implements RTT::os::ThreadInterface.
Definition at line 563 of file Thread.cpp.
References RTT::os::rtos_task_set_priority().
bool RTT::os::Thread::setScheduler | ( | int | sched_type | ) | [virtual] |
Change the scheduler policy in which this thread runs.
sched_type | An OS-specific value which selects a scheduler. Orocos requires that these two values are available:
|
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.
Implements RTT::os::ThreadInterface.
Definition at line 456 of file Thread.cpp.
References RTT::os::CheckScheduler(), getScheduler(), RTT::os::rtos_task_get_name(), and RTT::os::rtos_task_set_scheduler().
Referenced by RTT::extras::SimulationThread::SimulationThread().
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.
ssize | the size of the stack in bytes |
Definition at line 73 of file Thread.cpp.
void RTT::os::Thread::setWaitPeriodPolicy | ( | int | p | ) | [virtual] |
Set the wait policy of a periodic thread.
The | wait policy between ORO_WAIT_ABS (absolute wait) and ORO_WAIT_REL (relative wait) |
Implements RTT::os::ThreadInterface.
Definition at line 634 of file Thread.cpp.
References RTT::os::rtos_task_set_wait_period_policy().
bool RTT::os::Thread::start | ( | ) | [virtual] |
Start the Thread.
Implements RTT::os::ThreadInterface.
Reimplemented in RTT::extras::FileDescriptorActivity, RTT::Activity, RTT::extras::IRQActivity, and RTT::extras::SimulationThread.
Definition at line 339 of file Thread.cpp.
References initialize(), isActive(), and RTT::os::rtos_task_make_periodic().
void RTT::os::Thread::step | ( | ) | [protected, virtual] |
Reimplemented in RTT::extras::FileDescriptorActivity, RTT::Activity, RTT::extras::TimerThread, and RTT::extras::SimulationThread.
Definition at line 499 of file Thread.cpp.
Referenced by loop(), and RTT::os::thread_function().
bool RTT::os::Thread::stop | ( | ) | [virtual] |
Stop the Thread.
Implements RTT::os::ThreadInterface.
Reimplemented in RTT::extras::FileDescriptorActivity, and RTT::Activity.
Definition at line 404 of file Thread.cpp.
References breakLoop(), finalize(), getName(), getPeriod(), RTT::os::MutexTimedLock::isSuccessful(), and RTT::os::rtos_task_make_periodic().
Referenced by RTT::extras::SimulationThread::step(), RTT::extras::SimulationThread::~SimulationThread(), and RTT::extras::TimerThread::~TimerThread().
void RTT::os::Thread::terminate | ( | ) | [protected] |
Exit and destroy the thread.
Definition at line 608 of file Thread.cpp.
References RTT::os::rtos_task_delete().
void* thread_function | ( | void * | t | ) | [friend] |
This is one time initialisation
The real task starts here.
Definition at line 75 of file Thread.cpp.
int RTT::os::ThreadInterface::threadnb [protected, inherited] |
Threads are given an unique number, which follows thread creation order.
Definition at line 223 of file ThreadInterface.hpp.