Skip to content

Commit

Permalink
Wrap raw task routine with an platform specific wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua-Anderson committed Jun 17, 2021
1 parent 8d3b1ca commit 7088384
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
17 changes: 11 additions & 6 deletions Os/Posix/Task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@

typedef void* (*pthread_func_ptr)(void*);

//#define DEBUG_PRINT(x,...) Fw::Logger::logMsg(x,##__VA_ARGS__);
#define DEBUG_PRINT(x,...)
void* pthread_entry_wrapper(void* arg) {
FW_ASSERT(arg);
Os::Task::TaskRoutineWrapper *task = reinterpret_cast<Os::Task::TaskRoutineWrapper*>(arg);
FW_ASSERT(task->routine);
task->routine(task->arg);
return NULL;
}

namespace Os {
Task::Task() : m_handle(0), m_identifier(0), m_affinity(-1), m_started(false), m_suspendedOnPurpose(false) {
Task::Task() : m_handle(0), m_identifier(0), m_affinity(-1), m_started(false), m_suspendedOnPurpose(false), m_routineWrapper() {
}

Task::TaskStatus Task::start(const Fw::StringBase &name, NATIVE_INT_TYPE identifier, NATIVE_INT_TYPE priority, NATIVE_INT_TYPE stackSize, taskRoutine routine, void* arg, NATIVE_INT_TYPE cpuAffinity) {
FW_ASSERT(routine);

this->m_name = "TP_";
this->m_name += name;
Expand All @@ -37,7 +43,6 @@ namespace Os {
this->m_name += pid;
#endif
this->m_identifier = identifier;

Task::TaskStatus tStat = TASK_OK;

pthread_attr_t att;
Expand Down Expand Up @@ -116,7 +121,8 @@ namespace Os {
}

pthread_t* tid = new pthread_t;
stat = pthread_create(tid,&att,(pthread_func_ptr)routine,arg);
this->m_routineWrapper = {.routine = routine, .arg = arg};
stat = pthread_create(tid,&att,pthread_entry_wrapper,&this->m_routineWrapper);

switch (stat) {
case 0:
Expand Down Expand Up @@ -214,7 +220,6 @@ namespace Os {
stat = pthread_join(*((pthread_t*) this->m_handle), value_ptr);

if (stat != 0) {
DEBUG_PRINT("join: %s\n", strerror(errno));
return TASK_JOIN_ERROR;
}
else {
Expand Down
7 changes: 6 additions & 1 deletion Os/Task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
namespace Os {

class TaskRegistry; //!< forward declaration

class Task {
public:

Expand All @@ -27,6 +26,11 @@ namespace Os {

typedef void (*taskRoutine)(void* ptr); //!< prototype for task routine started in task context

struct TaskRoutineWrapper {
taskRoutine routine; //!< contains the task entrypoint
void* arg; //!< contains the task entrypoint pointer
};

Task(); //!< constructor
virtual ~Task(); //!< destructor
// Priority is based on Posix priorities - 0 lowest, 255 highest
Expand Down Expand Up @@ -61,6 +65,7 @@ namespace Os {
void toString(char* buf, NATIVE_INT_TYPE buffSize); //!< print a string of the state of the task
bool m_started; //!< set when task has reached entry point
bool m_suspendedOnPurpose; //!< set when task was suspended in purpose (i.e. simulation)
TaskRoutineWrapper m_routineWrapper; //! Contains task entrypoint and argument for task wrapper

static TaskRegistry* s_taskRegistry; //!< pointer to registered task
static NATIVE_INT_TYPE s_numTasks; //!< stores the number of tasks created.
Expand Down

0 comments on commit 7088384

Please sign in to comment.