Skip to content

Commit

Permalink
Merge pull request orocos-toolchain#215 from meyerj/fix-timerthread-c…
Browse files Browse the repository at this point in the history
…pu-affinity

TimerThread: do not return the same instance if CPU affinity differs
  • Loading branch information
meyerj committed May 16, 2017
2 parents 068214b + f65d958 commit 2dd754a
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 4 deletions.
4 changes: 3 additions & 1 deletion rtt/extras/TimerThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "../Logger.hpp"
#include <algorithm>
#include "../os/MutexLock.hpp"
#include "../os/MainThread.hpp"

namespace RTT {
using namespace extras;
Expand All @@ -60,14 +61,15 @@ namespace RTT {

TimerThreadPtr TimerThread::Instance(int scheduler, int pri, double per)
{
return Instance(scheduler, pri, per, ~0);
return Instance(scheduler, pri, per, 0);
}

TimerThreadPtr TimerThread::Instance(int scheduler, int pri, double per, unsigned cpu_affinity)
{
// Since the period is stored as nsecs, we convert per to NS in order
// to get a match.
os::CheckPriority(scheduler, pri);
if (cpu_affinity == 0) cpu_affinity = os::MainThread::Instance()->getCpuAffinity();
TimerThreadList::iterator it = TimerThreads.begin();
while ( it != TimerThreads.end() ) {
TimerThreadPtr tptr = it->lock();
Expand Down
5 changes: 5 additions & 0 deletions rtt/os/MainThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ namespace RTT
return rtos_task_get_pid(&main_task);
}

unsigned MainThread::getCpuAffinity() const
{
return rtos_task_get_cpu_affinity(&main_task);
}

bool MainThread::setPeriod(Seconds period)
{
return false;
Expand Down
2 changes: 2 additions & 0 deletions rtt/os/MainThread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ namespace RTT

virtual unsigned int getPid() const;

virtual unsigned getCpuAffinity() const;

virtual void setMaxOverrun(int m);

virtual int getMaxOverrun() const;
Expand Down
1 change: 1 addition & 0 deletions rtt/os/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "threads.hpp"
#include "../Logger.hpp"
#include "MutexLock.hpp"
#include "MainThread.hpp"

#include "../rtt-config.h"
#include "../internal/CatchConfig.hpp"
Expand Down
4 changes: 3 additions & 1 deletion rtt/os/Thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ namespace RTT
* @param priority The priority of the thread, this is interpreted by your RTOS.
* @param period The period in seconds (eg 0.001) of the thread, or zero if not periodic.
* @param cpu_affinity The cpu affinity of the thread, this is interpreted by your RTOS.
* If 0, the new thread inherits the affinity of its creator (the default).
* @param name The name of the Thread. May be used by your OS to identify the thread.
* the thread's own virtual functions are executed.
*/
Expand Down Expand Up @@ -220,9 +221,10 @@ namespace RTT
virtual int getPriority() const;

virtual unsigned int getPid() const;

/**
* Set cpu affinity for this thread
* @cpu_affinity The cpu affinity of the thread (@see rtos_task_set_cpu_affinity).
* @param cpu_affinity The cpu affinity of the thread (@see rtos_task_set_cpu_affinity).
* @return true if the mask has been applied
*/
virtual bool setCpuAffinity(unsigned cpu_affinity);
Expand Down
5 changes: 5 additions & 0 deletions rtt/os/ThreadInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ namespace RTT
*/
virtual unsigned int getPid() const = 0;

/**
* @return the cpu affinity
*/
virtual unsigned getCpuAffinity() const = 0;

virtual void setMaxOverrun(int m) = 0;

virtual int getMaxOverrun() const = 0;
Expand Down
2 changes: 1 addition & 1 deletion rtt/os/gnulinux/fosi_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ namespace RTT
}
}

if ( cpu_affinity != (unsigned)~0 ) {
if ( cpu_affinity != 0 ) {
log(Debug) << "Setting CPU affinity to " << cpu_affinity << endlog();
int result = rtos_task_set_cpu_affinity(task, cpu_affinity);
if (result != 0) {
Expand Down
2 changes: 1 addition & 1 deletion rtt/os/xenomai/fosi_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ namespace RTT

INTERNAL_QUAL unsigned rtos_task_get_cpu_affinity(const RTOS_TASK *task)
{
return 0;
return ~0;
}

INTERNAL_QUAL const char* rtos_task_get_name(const RTOS_TASK* mytask) {
Expand Down
8 changes: 8 additions & 0 deletions tests/taskthread_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ BOOST_AUTO_TEST_CASE(testPeriodic )
}
}

// Different CPU affinity
unsigned cpu_affinity = 1; // first CPU only
if ( mtask.thread()->getCpuAffinity() != cpu_affinity ) {
PeriodicActivity m4task(ORO_SCHED_OTHER, 15, 0.01, cpu_affinity);
BOOST_CHECK( mtask.thread() != m4task.thread() );
BOOST_CHECK_EQUAL( cpu_affinity, m4task.thread()->getCpuAffinity() );
}

// Starting thread if thread not running
BOOST_CHECK( mtask.thread()->stop() );
BOOST_CHECK( mtask.thread()->isRunning() == false );
Expand Down

0 comments on commit 2dd754a

Please sign in to comment.