Orocos Real-Time Toolkit  2.6.0
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Friends
RTT::extras::FileDescriptorActivity Class Reference

An activity which is triggered by the availability of data on a set of file descriptors. More...

#include <rtt/extras/FileDescriptorActivity.hpp>

Inheritance diagram for RTT::extras::FileDescriptorActivity:
RTT::Activity RTT::base::ActivityInterface RTT::os::Thread RTT::os::ThreadInterface

List of all members.

Public Types

typedef boost::shared_ptr
< ActivityInterface
shared_ptr

Public Member Functions

 FileDescriptorActivity (int priority, base::RunnableInterface *_r=0, const std::string &name="FileDescriptorActivity")
 Create a FileDescriptorActivity with a given priority and base::RunnableInterface instance.
 FileDescriptorActivity (int scheduler, int priority, base::RunnableInterface *_r=0, const std::string &name="FileDescriptorActivity")
 Create a FileDescriptorActivity with a given scheduler type, priority and base::RunnableInterface instance.
bool isRunning () const
 Query if the activity is initialized and executing.
void watch (int fd)
 Sets the file descriptor the activity should be listening to.
void unwatch (int fd)
 Removes a file descriptor from the set of watched FDs.
void clearAllWatches ()
 Remove all FDs that are currently being watched.
bool isWatched (int fd) const
 True if this specific FD is being watched by the activity.
bool isUpdated (int fd) const
 True if this specific FD has new data.
bool hasTimeout () const
 True if the base::RunnableInterface has been triggered because of a timeout, instead of because of new data is available.
bool hasError () const
 True if one of the file descriptors has a problem (for instance it has been closed)
void setTimeout (int timeout)
 Sets the timeout, in milliseconds, for waiting on the IO.
int getTimeout () const
 Get the timeout, in milliseconds, for waiting on the IO.
virtual bool start ()
 Start the activity.
virtual void loop ()
virtual bool breakLoop ()
virtual bool stop ()
 Stop the activity This will stop the activity by removing it from the 'run-queue' of a thread or call RunnableInterface::breakLoop().
virtual void step ()
 Called by loop() when data is available on the file descriptor.
virtual bool trigger ()
 Force calling step() even if no data is available on the file descriptor, and returns true if the signalling was successful.
virtual bool execute ()
 Execute this activity such that it executes a step or loop of the RunnableInterface.
virtual bool isActive () const
 Query if the activity is started.
virtual bool isPeriodic () const
 Inspect if this activity is periodic.
virtual Seconds getPeriod () const
 Get the periodicity of this activity in Seconds.
void getPeriod (secs &s, nsecs &ns) const
 Get the periodicity of this thread (seconds, nanoseconds)
virtual bool setPeriod (Seconds period)
 Set the periodicity of this activity 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.
virtual unsigned getCpuAffinity () const
 Get the cpu affinity of this activity.
virtual bool setCpuAffinity (unsigned cpu)
 Set the cpu affinity of this activity.
virtual os::ThreadInterfacethread ()
 Returns a pointer to the thread which will run this activity.
virtual bool initialize ()
virtual void finalize ()
virtual bool run (RunnableInterface *r)
 Run exclusively this RunnableInterface.
virtual nsecs getPeriodNS () const
 Get the periodicity in nanoseconds.
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 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 disableRun (RunnableInterface *caller)
 This method is only meant for RunnableInterface (ie runner) in order to inform the ActivityInterface it should no longer be used.
void terminate ()
 Exit and destroy the thread.
void emergencyStop ()

Protected Attributes

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

Friends

class RunnableInterface

Detailed Description

An activity which is triggered by the availability of data on a set of file descriptors.

step() (and hence the base::RunnableInterface's step() method) is called when data is available or when an error is encountered on the file descriptor.

To use it, one must add the file descriptors to watch in the task's configureHook()

FileDescriptorActivity* fd_activity = dynamic_cast<FileDescriptorActivity*>(getActivity().get()); if (fd_activity) { fd_activity->watch(device_fd); // optional, set a timeout in milliseconds fd_activity->setTimeout(1000); }

Then, updateHook() and -- when in ERROR state -- errorHook() will be called when one of these three events happen:

The different cases can be tested in updateHook() as follows:

FileDescriptorActivity* fd_activity = dynamic_cast<FileDescriptorActivity*>(getActivity().get()); if (fd_activity) { if (fd_activity->hasError()) { } else if (fd_activity->hasTimeout()) { } else { // If there is more than one FD, discriminate. Otherwise, // we don't need to use isUpdated if (fd_activity->isUpdated(device_fd)) { } else if (fd_activity->isUpdated(another_fd)) { } } }

Definition at line 101 of file FileDescriptorActivity.hpp.


Constructor & Destructor Documentation

FileDescriptorActivity::FileDescriptorActivity ( int  priority,
base::RunnableInterface _r = 0,
const std::string &  name = "FileDescriptorActivity" 
)

Create a FileDescriptorActivity with a given priority and base::RunnableInterface instance.

Create a FileDescriptorActivity with a given priority and RunnableInterface instance.

The default scheduler for NonPeriodicActivity objects is ORO_SCHED_RT.

Parameters:
priorityThe priority of the underlying thread.
_rThe optional runner, if none, this->loop() is called.
nameThe name of the underlying thread.

The default scheduler for NonPeriodicActivity objects is ORO_SCHED_RT.

Parameters:
priorityThe priority of the underlying thread.
_rThe optional runner, if none, this->loop() is called.

Definition at line 79 of file FileDescriptorActivity.cpp.

FileDescriptorActivity::FileDescriptorActivity ( int  scheduler,
int  priority,
base::RunnableInterface _r = 0,
const std::string &  name = "FileDescriptorActivity" 
)

Create a FileDescriptorActivity with a given scheduler type, priority and base::RunnableInterface instance.

Create a FileDescriptorActivity with a given scheduler type, priority and RunnableInterface instance.

Parameters:
schedulerThe scheduler in which the activitie's thread must run. Use ORO_SCHED_OTHER or ORO_SCHED_RT.
priorityThe priority of the underlying thread.
_rThe optional runner, if none, this->loop() is called.
nameThe name of the underlying thread.
schedulerThe scheduler in which the activitie's thread must run. Use ORO_SCHED_OTHER or ORO_SCHED_RT.
priorityThe priority of the underlying thread.
_rThe optional runner, if none, this->loop() is called.

Definition at line 97 of file FileDescriptorActivity.cpp.


Member Function Documentation

See also:
base::RunnableInterface::breakLoop()

Reimplemented from RTT::Activity.

Definition at line 285 of file FileDescriptorActivity.cpp.

void RTT::base::ActivityInterface::disableRun ( RunnableInterface caller) [inline, protected, inherited]

This method is only meant for RunnableInterface (ie 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.

Definition at line 73 of file ActivityInterface.hpp.

Referenced by RTT::base::RunnableInterface::setActivity(), and RTT::base::RunnableInterface::~RunnableInterface().

bool RTT::Activity::execute ( ) [virtual, inherited]

Execute this activity such that it 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().

Return values:
trueWhen this->isActive() and the implementation allows external executes.
falseWhen !this->isActive() or the implementation does not allow external updating.
See also:
trigger() for use in callbacks which want execute() to be executed.

Implements RTT::base::ActivityInterface.

Definition at line 131 of file Activity.cpp.

void RTT::Activity::finalize ( ) [virtual, inherited]
See also:
base::RunnableInterface::finalize()

Reimplemented from RTT::os::Thread.

Reimplemented in RTT::corba::OrbRunner.

Definition at line 113 of file Activity.cpp.

References RTT::base::RunnableInterface::finalize().

unsigned RTT::Activity::getCpuAffinity ( ) const [virtual, inherited]

Get the cpu affinity of this activity.

Returns:
The cpu affinity of this activity.

Implements RTT::base::ActivityInterface.

Definition at line 158 of file Activity.cpp.

Seconds RTT::Activity::getPeriod ( ) const [virtual, inherited]

Get the periodicity of this activity in Seconds.

Returns:
The execution period of this activity (zero if !this->isPeriodic() ).

Implements RTT::base::ActivityInterface.

Definition at line 143 of file Activity.cpp.

nsecs RTT::os::Thread::getPeriodNS ( ) const [virtual, inherited]

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, inherited]

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, inherited]

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 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, inherited]

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

References RTT::os::rtos_task_get_scheduler().

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

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

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

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

Get the timeout, in milliseconds, for waiting on the IO.

Set to 0 for blocking behaviour (no timeout).

Definition at line 113 of file FileDescriptorActivity.cpp.

True if one of the file descriptors has a problem (for instance it has been closed)

This should only be used from within the base::RunnableInterface this activity is driving, i.e. in TaskContext::updateHook() or TaskContext::errorHook().

Definition at line 149 of file FileDescriptorActivity.cpp.

True if the base::RunnableInterface has been triggered because of a timeout, instead of because of new data is available.

This should only be used from within the base::RunnableInterface this activity is driving, i.e. in TaskContext::updateHook() or TaskContext::errorHook().

Definition at line 151 of file FileDescriptorActivity.cpp.

bool RTT::Activity::initialize ( ) [virtual, inherited]
bool RTT::Activity::isActive ( ) const [virtual, inherited]

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.

Returns:
true if it is active, false otherwise

Implements RTT::base::ActivityInterface.

Definition at line 139 of file Activity.cpp.

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

bool RTT::Activity::isPeriodic ( ) const [virtual, inherited]

Inspect if this activity is periodic.

If so, it will call RunnableInterface::step(). If the activity is not periodic, it will call RunnableInterface::loop().

Returns:
true if periodic.

Implements RTT::base::ActivityInterface.

Definition at line 154 of file Activity.cpp.

bool FileDescriptorActivity::isRunning ( ) const [virtual]

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), waiting for a signal to proceed. If this->isActive() and !this->isRunning() then the Activity is in a waiting state.

Returns:
true if it is running, false otherwise

Reimplemented from RTT::Activity.

Definition at line 111 of file FileDescriptorActivity.cpp.

bool FileDescriptorActivity::isUpdated ( int  fd) const

True if this specific FD has new data.

This should only be used from within the base::RunnableInterface this activity is driving, i.e. in TaskContext::updateHook() or TaskContext::errorHook().

Definition at line 147 of file FileDescriptorActivity.cpp.

void FileDescriptorActivity::loop ( ) [virtual]
See also:
base::RunnableInterface::loop()

Reimplemented from RTT::Activity.

Definition at line 188 of file FileDescriptorActivity.cpp.

References step().

bool ActivityInterface::run ( RunnableInterface r) [virtual, inherited]

Run exclusively this RunnableInterface.

Precondition:
this->isRunning() == false
Parameters:
rThe RunnableInterface to run exclusively.
Returns:
true if succeeded, false otherwise

Definition at line 57 of file ActivityInterface.cpp.

References RTT::base::ActivityInterface::isActive(), and RTT::base::RunnableInterface::setActivity().

Referenced by RTT::TaskContext::setActivity().

bool RTT::Activity::setCpuAffinity ( unsigned  cpu) [virtual, inherited]

Set the cpu affinity of this activity.

Returns:
true if it could be updated, false otherwise.

Implements RTT::base::ActivityInterface.

Definition at line 163 of file Activity.cpp.

bool RTT::Activity::setPeriod ( Seconds  s) [virtual, inherited]

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.

Returns:
true if it could be updated, false otherwise.

Implements RTT::base::ActivityInterface.

Definition at line 148 of file Activity.cpp.

bool RTT::os::Thread::setPriority ( int  priority) [virtual, inherited]

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

References RTT::os::rtos_task_set_priority().

bool RTT::os::Thread::setScheduler ( int  sched_type) [virtual, inherited]

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

References RTT::os::CheckScheduler(), RTT::os::Thread::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, inherited]

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

void FileDescriptorActivity::setTimeout ( int  timeout)

Sets the timeout, in milliseconds, for waiting on the IO.

Set to 0 for blocking behaviour (no timeout).

Definition at line 115 of file FileDescriptorActivity.cpp.

void RTT::os::Thread::setWaitPeriodPolicy ( int  p) [virtual, inherited]

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

References RTT::os::rtos_task_set_wait_period_policy().

bool FileDescriptorActivity::start ( ) [virtual]

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 also:
isPeriodic()
Returns:
true if the activity is started, false otherwise

Reimplemented from RTT::Activity.

Definition at line 157 of file FileDescriptorActivity.cpp.

References RTT::Activity::start().

void FileDescriptorActivity::step ( ) [virtual]

Called by loop() when data is available on the file descriptor.

By default, it calls step() on the associated runner interface (if any)

Reimplemented from RTT::Activity.

Definition at line 296 of file FileDescriptorActivity.cpp.

References RTT::base::RunnableInterface::step().

Referenced by loop().

bool FileDescriptorActivity::stop ( ) [virtual]

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 also:
isPeriodic()
Returns:
true if the activity is stopped, false otherwise

Reimplemented from RTT::Activity.

Definition at line 304 of file FileDescriptorActivity.cpp.

void RTT::os::Thread::terminate ( ) [protected, inherited]

Exit and destroy the thread.

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

Definition at line 608 of file Thread.cpp.

References RTT::os::rtos_task_delete().

os::ThreadInterface * RTT::Activity::thread ( ) [virtual, inherited]

Returns a pointer to the thread which will run this activity.

Will not be null.

Implements RTT::base::ActivityInterface.

Definition at line 85 of file Activity.cpp.

Removes a file descriptor from the set of watched FDs.

This method is thread-safe, i.e. it can be called from any thread

Definition at line 129 of file FileDescriptorActivity.cpp.

Sets the file descriptor the activity should be listening to.

  • close_on_stop { if true, the file descriptor will be closed by the activity when stop() is called. Otherwise, the file descriptor is left as-is.}

This method is thread-safe, i.e. it can be called from any thread

Parameters:
fdthe file descriptor
close_on_stopif true, the FD will be closed automatically when the activity is stopped

Definition at line 117 of file FileDescriptorActivity.cpp.


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 223 of file ThreadInterface.hpp.


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