Orocos Real-Time Toolkit  2.8.3
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Types | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
RTT::extras::TimerThread Class Reference

This Periodic Thread is meant for executing a PeriodicActivity object periodically. More...

#include <rtt/extras/TimerThread.hpp>

Inheritance diagram for RTT::extras::TimerThread:
RTT::os::Thread RTT::os::ThreadInterface RTT::extras::SimulationThread

Public Member Functions

 TimerThread (int priority, const std::string &name, double periodicity, unsigned cpu_affinity=~0)
 Create a periodic Timer thread. More...
 
 TimerThread (int scheduler, int priority, const std::string &name, double periodicity, unsigned cpu_affinity=~0)
 Create a periodic Timer thread with a given scheduler type. More...
 
virtual ~TimerThread ()
 Destructor. More...
 
bool addActivity (PeriodicActivity *t)
 Add an Timer that will be ticked every execution period Once added, a timer can not be removed. More...
 
bool removeActivity (PeriodicActivity *t)
 
virtual bool start ()
 Start the Thread. More...
 
virtual bool stop ()
 Stop the Thread. More...
 
void setStopTimeout (Seconds s)
 Sets the timeout for stop(), in seconds. More...
 
Seconds getStopTimeout () const
 Returns the desired timeout for stop(), in seconds. More...
 
bool setPeriod (Seconds s)
 Set the periodicity in Seconds. More...
 
bool setPeriod (secs s, nsecs ns)
 Set the periodicity of this thread (seconds, nanoseconds) More...
 
bool setPeriod (TIME_SPEC p)
 Set the periodicity of this thread. More...
 
void getPeriod (secs &s, nsecs &ns) const
 Get the periodicity of this thread (seconds, nanoseconds) More...
 
virtual Seconds getPeriod () const
 Get the periodicity in Seconds. More...
 
virtual nsecs getPeriodNS () const
 Get the periodicity in nanoseconds. More...
 
virtual bool isPeriodic () const
 
virtual bool isRunning () const
 Returns whether the thread is running. More...
 
virtual bool isActive () const
 Returns whether the thread is active. More...
 
virtual const char * getName () const
 Read the name of this task. More...
 
virtual RTOS_TASKgetTask ()
 Get the RTOS_TASK pointer. More...
 
virtual const RTOS_TASKgetTask () const
 const version of the above. More...
 
virtual bool setScheduler (int sched_type)
 Change the scheduler policy in which this thread runs. More...
 
virtual int getScheduler () const
 Get the scheduler policy in which this thread runs. More...
 
virtual bool setPriority (int priority)
 Set the priority of this Thread. More...
 
virtual int getPriority () const
 The priority of this Thread. More...
 
virtual unsigned int getPid () const
 Returns the Process or Thread ID of this thread, as assigned by the Operating System. More...
 
virtual bool setCpuAffinity (unsigned cpu_affinity)
 Set cpu affinity for this thread. More...
 
virtual unsigned getCpuAffinity () const
 
virtual void yield ()
 Yields (put to the back of the scheduler queue) the calling thread. More...
 
virtual void setMaxOverrun (int m)
 
virtual int getMaxOverrun () const
 
virtual void setWaitPeriodPolicy (int p)
 Set the wait policy of a periodic thread. More...
 
unsigned int threadNumber () const
 The unique thread number (within the same process). More...
 
bool isSelf () const
 

Static Public Member Functions

static TimerThreadPtr Instance (int priority, double periodicity)
 Create a TimerThread with a given priority and periodicity, using the default scheduler, ORO_SCHED_RT. More...
 
static TimerThreadPtr Instance (int scheduler, int priority, double periodicity)
 Create a TimerThread with a given scheduler, priority and periodicity. More...
 
static TimerThreadPtr Instance (int scheduler, int priority, double periodicity, unsigned cpu_affinity)
 Create a TimerThread with a given scheduler, priority and periodicity. More...
 
static void setStackSize (unsigned int ssize)
 Sets the stack size of the threads to be created. More...
 
static void setLockTimeoutNoPeriod (double timeout_in_s)
 Sets the lock timeout for a thread which does not have a period The default is 1 second. More...
 
static void setLockTimeoutPeriodFactor (double factor)
 Set the lock timeout for a thread which has a period by a factor of the period The default is factor 10. More...
 

Static Public Attributes

static const unsigned int MAX_ACTIVITIES = 64
 

Protected Types

typedef std::vector< boost::weak_ptr< TimerThread > > TimerThreadList
 A Boost weak pointer is used to store non-owning pointers to shared objects. More...
 

Protected Member Functions

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

Protected Attributes

os::MutexRecursive mutex
 A Activity can not create a activity of same priority from step(). More...
 
int threadnb
 Threads are given an unique number, which follows thread creation order. More...
 

Static Protected Attributes

static TimerThreadList TimerThreads
 All timer threads. More...
 

Detailed Description

This Periodic Thread is meant for executing a PeriodicActivity object periodically.

See also
PeriodicActivity

Definition at line 69 of file TimerThread.hpp.

Member Typedef Documentation

typedef std::vector< boost::weak_ptr<TimerThread> > RTT::extras::TimerThread::TimerThreadList
protected

A Boost weak pointer is used to store non-owning pointers to shared objects.

Definition at line 140 of file TimerThread.hpp.

Constructor & Destructor Documentation

RTT::TimerThread::TimerThread ( int  priority,
const std::string &  name,
double  periodicity,
unsigned  cpu_affinity = ~0 
)

Create a periodic Timer thread.

Parameters
priorityThe priority of this thread
periodicityThe periodicity of this thread in seconds (e.g. 0.001 = 1000Hz )

Definition at line 95 of file TimerThread.cpp.

References MAX_ACTIVITIES.

RTT::TimerThread::TimerThread ( int  scheduler,
int  priority,
const std::string &  name,
double  periodicity,
unsigned  cpu_affinity = ~0 
)

Create a periodic Timer thread with a given scheduler type.

Parameters
schedulerThe scheduler in which this thread runs
priorityThe priority of this thread within scheduler
periodicityThe periodicity of this thread in seconds (e.g. 0.001 = 1000Hz )

Definition at line 101 of file TimerThread.cpp.

References MAX_ACTIVITIES.

RTT::TimerThread::~TimerThread ( )
virtual

Destructor.

Definition at line 107 of file TimerThread.cpp.

References RTT::os::Thread::stop().

Member Function Documentation

bool RTT::TimerThread::addActivity ( PeriodicActivity t)

Add an Timer that will be ticked every execution period Once added, a timer can not be removed.

Definition at line 113 of file TimerThread.cpp.

References MAX_ACTIVITIES, and mutex.

bool RTT::os::Thread::breakLoop ( )
protectedvirtualinherited
void RTT::os::Thread::emergencyStop ( )
protectedinherited

Definition at line 226 of file Thread.cpp.

References RTT::os::Thread::finalize().

Referenced by RTT::os::thread_function().

void RTT::TimerThread::finalize ( )
protectedvirtual
See also
base::RunnableInterface::finalize()

Reimplemented from RTT::os::Thread.

Reimplemented in RTT::extras::SimulationThread.

Definition at line 140 of file TimerThread.cpp.

References mutex, and reorderList().

unsigned RTT::os::Thread::getCpuAffinity ( ) const
virtualinherited
Returns
the cpu affinity (
See also
rtos_task_set_cpu_affinity)

Implements RTT::os::ThreadInterface.

Reimplemented in RTT::Activity.

Definition at line 606 of file Thread.cpp.

References RTT::os::rtos_task_get_cpu_affinity().

Referenced by RTT::Activity::getCpuAffinity(), and RTT::os::Thread::Thread().

int RTT::os::Thread::getMaxOverrun ( ) const
virtualinherited

Implements RTT::os::ThreadInterface.

Definition at line 642 of file Thread.cpp.

const char * RTT::os::Thread::getName ( ) const
virtualinherited
void RTT::os::Thread::getPeriod ( secs s,
nsecs ns 
) const
inherited

Get the periodicity of this thread (seconds, nanoseconds)

Definition at line 570 of file Thread.cpp.

double RTT::os::Thread::getPeriod ( ) const
virtualinherited
nsecs RTT::os::Thread::getPeriodNS ( ) const
virtualinherited

Get the periodicity in nanoseconds.

Return zero if non periodic.

Implements RTT::os::ThreadInterface.

Definition at line 596 of file Thread.cpp.

unsigned int RTT::os::Thread::getPid ( ) const
virtualinherited

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 611 of file Thread.cpp.

References RTT::os::rtos_task_get_pid().

Referenced by RTT::os::Thread::Thread().

int RTT::os::Thread::getPriority ( ) const
virtualinherited

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.

Definition at line 586 of file Thread.cpp.

References RTT::os::rtos_task_get_priority().

Referenced by RTT::extras::SimulationThread::SimulationThread(), and RTT::os::Thread::Thread().

int RTT::os::Thread::getScheduler ( ) const
virtualinherited

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.

Definition at line 488 of file Thread.cpp.

References RTT::os::rtos_task_get_scheduler(), RTT::os::rtos_task_set_period(), and RTT::os::rtos_task_set_scheduler().

Referenced by RTT::os::Thread::setScheduler(), and RTT::os::Thread::Thread().

Seconds RTT::os::Thread::getStopTimeout ( ) const
inherited

Returns the desired timeout for stop(), in seconds.

If not set with setStopTimeout, the timeout is deduced from the global values lock_timeout_period_factor and lock_timeout_no_period_in_s

Definition at line 408 of file Thread.cpp.

References RTT::os::Thread::getPeriod().

Referenced by RTT::os::Thread::stop().

virtual RTOS_TASK* RTT::os::Thread::getTask ( )
inlinevirtualinherited

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 205 of file Thread.hpp.

Referenced by RTT::os::thread_function().

virtual const RTOS_TASK* RTT::os::Thread::getTask ( ) const
inlinevirtualinherited

const version of the above.

Implements RTT::os::ThreadInterface.

Definition at line 210 of file Thread.hpp.

bool RTT::TimerThread::initialize ( )
protectedvirtual
See also
base::RunnableInterface::initialize()

Reimplemented from RTT::os::Thread.

Reimplemented in RTT::extras::SimulationThread.

Definition at line 136 of file TimerThread.cpp.

TimerThreadPtr RTT::TimerThread::Instance ( int  priority,
double  periodicity 
)
static

Create a TimerThread with a given priority and periodicity, using the default scheduler, ORO_SCHED_RT.

Definition at line 57 of file TimerThread.cpp.

References ORO_SCHED_RT.

TimerThreadPtr RTT::TimerThread::Instance ( int  scheduler,
int  priority,
double  periodicity 
)
static

Create a TimerThread with a given scheduler, priority and periodicity.

Definition at line 62 of file TimerThread.cpp.

TimerThreadPtr RTT::TimerThread::Instance ( int  scheduler,
int  priority,
double  periodicity,
unsigned  cpu_affinity 
)
static

Create a TimerThread with a given scheduler, priority and periodicity.

Definition at line 67 of file TimerThread.cpp.

References RTT::os::CheckPriority(), RTT::os::ThreadInterface::getCpuAffinity(), RTT::os::MainThread::Instance(), and RTT::Seconds_to_nsecs().

bool RTT::os::Thread::isActive ( ) const
virtualinherited

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 464 of file Thread.cpp.

Referenced by RTT::Activity::isActive(), RTT::os::Thread::start(), and RTT::Activity::trigger().

bool RTT::os::Thread::isPeriodic ( ) const
virtualinherited

Reimplemented in RTT::Activity.

Definition at line 581 of file Thread.cpp.

Referenced by RTT::Activity::isPeriodic().

bool RTT::os::Thread::isRunning ( ) const
virtualinherited

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::extras::FileDescriptorActivity, RTT::Activity, and RTT::extras::SimulationThread.

Definition at line 459 of file Thread.cpp.

Referenced by RTT::extras::SimulationThread::isRunning(), RTT::Activity::isRunning(), and RTT::os::Thread::~Thread().

bool ThreadInterface::isSelf ( ) const
inherited
void RTT::os::Thread::loop ( )
protectedvirtualinherited
bool RTT::TimerThread::removeActivity ( PeriodicActivity t)

Definition at line 124 of file TimerThread.cpp.

References mutex.

void RTT::TimerThread::reorderList ( )
protected

Definition at line 163 of file TimerThread.cpp.

Referenced by finalize(), and step().

bool RTT::os::Thread::setCpuAffinity ( unsigned  cpu_affinity)
virtualinherited

Set cpu affinity for this thread.

Parameters
cpu_affinityThe cpu affinity of the thread (
See also
rtos_task_set_cpu_affinity).
Returns
true if the mask has been applied

Reimplemented in RTT::Activity.

Definition at line 601 of file Thread.cpp.

References RTT::os::rtos_task_set_cpu_affinity().

Referenced by RTT::Activity::setCpuAffinity().

void RTT::os::Thread::setLockTimeoutNoPeriod ( double  timeout_in_s)
staticinherited

Sets the lock timeout for a thread which does not have a period The default is 1 second.

Parameters
timeout_in_sthe timeout is seconds

Definition at line 71 of file Thread.cpp.

void RTT::os::Thread::setLockTimeoutPeriodFactor ( double  factor)
staticinherited

Set the lock timeout for a thread which has a period by a factor of the period The default is factor 10.

Parameters
factorFactor of the period

Definition at line 73 of file Thread.cpp.

void RTT::os::Thread::setMaxOverrun ( int  m)
virtualinherited

Implements RTT::os::ThreadInterface.

Definition at line 637 of file Thread.cpp.

bool RTT::os::Thread::setPeriod ( Seconds  new_period)
virtualinherited

Set the periodicity in Seconds.

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

Implements RTT::os::ThreadInterface.

Definition at line 536 of file Thread.cpp.

References RTT::Seconds_to_nsecs().

Referenced by RTT::Activity::setPeriod(), and RTT::os::Thread::setPeriod().

bool RTT::os::Thread::setPeriod ( secs  s,
nsecs  ns 
)
inherited

Set the periodicity of this thread (seconds, nanoseconds)

Definition at line 542 of file Thread.cpp.

References rtos_sem_signal(), and RTT::os::rtos_task_make_periodic().

bool RTT::os::Thread::setPeriod ( TIME_SPEC  p)
inherited

Set the periodicity of this thread.

Definition at line 565 of file Thread.cpp.

References RTT::os::Thread::setPeriod().

bool RTT::os::Thread::setPriority ( int  priority)
virtualinherited

Set the priority of this Thread.

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

Implements RTT::os::ThreadInterface.

Definition at line 576 of file Thread.cpp.

References RTT::os::rtos_task_set_priority().

bool RTT::os::Thread::setScheduler ( int  sched_type)
virtualinherited

Change the scheduler policy in which this thread runs.

Parameters
sched_typeAn 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.

Definition at line 469 of file Thread.cpp.

References RTT::os::CheckScheduler(), RTT::os::Thread::getScheduler(), RTT::Info, rtos_sem_signal(), 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)
staticinherited

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
ssizethe size of the stack in bytes

Definition at line 69 of file Thread.cpp.

void RTT::os::Thread::setStopTimeout ( Seconds  s)
inherited

Sets the timeout for stop(), in seconds.

See also
getStopTimeout

Definition at line 403 of file Thread.cpp.

void RTT::os::Thread::setWaitPeriodPolicy ( int  p)
virtualinherited

Set the wait policy of a periodic thread.

Parameters
Thewait policy between ORO_WAIT_ABS (absolute wait) and ORO_WAIT_REL (relative wait)

Implements RTT::os::ThreadInterface.

Definition at line 647 of file Thread.cpp.

References RTT::os::rtos_task_set_wait_period_policy().

bool RTT::os::Thread::start ( )
virtualinherited
void RTT::TimerThread::step ( )
protectedvirtual
bool RTT::os::Thread::stop ( )
virtualinherited
void RTT::os::Thread::terminate ( )
protectedinherited

Exit and destroy the thread.

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

Definition at line 621 of file Thread.cpp.

References rtos_sem_signal(), and RTT::os::rtos_task_delete().

Referenced by RTT::os::Thread::~Thread().

unsigned int RTT::os::ThreadInterface::threadNumber ( ) const
inlineinherited

The unique thread number (within the same process).

Definition at line 216 of file ThreadInterface.hpp.

Referenced by RTT::os::Thread::Thread(), and RTT::os::thread_function().

void RTT::os::Thread::yield ( )
virtualinherited

Yields (put to the back of the scheduler queue) the calling thread.

Implements RTT::os::ThreadInterface.

Definition at line 616 of file Thread.cpp.

References RTT::os::rtos_task_yield().

Member Data Documentation

const unsigned int RTT::extras::TimerThread::MAX_ACTIVITIES = 64
static

Definition at line 76 of file TimerThread.hpp.

Referenced by addActivity(), and TimerThread().

os::MutexRecursive RTT::extras::TimerThread::mutex
mutableprotected

A Activity can not create a activity of same priority from step().

If so a deadlock will occur.

Definition at line 134 of file TimerThread.hpp.

Referenced by addActivity(), finalize(), removeActivity(), and step().

int RTT::os::ThreadInterface::threadnb
protectedinherited

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

See also
os::threads

Definition at line 228 of file ThreadInterface.hpp.

Referenced by RTT::os::ThreadInterface::ThreadInterface().

TimerThread::TimerThreadList RTT::TimerThread::TimerThreads
staticprotected

All timer threads.

Definition at line 145 of file TimerThread.hpp.


The documentation for this class was generated from the following files: