Wrap raw task routine with an platform specific wrapper

This commit is contained in:
Joshua Anderson 2021-06-10 11:23:48 -07:00 committed by M Starch
parent 8d3b1ca999
commit 48d355a0b1
2 changed files with 17 additions and 7 deletions

View File

@ -19,14 +19,20 @@
typedef void* (*pthread_func_ptr)(void*); typedef void* (*pthread_func_ptr)(void*);
//#define DEBUG_PRINT(x,...) Fw::Logger::logMsg(x,##__VA_ARGS__); void* pthread_entry_wrapper(void* arg) {
#define DEBUG_PRINT(x,...) 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 { 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) { 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 = "TP_";
this->m_name += name; this->m_name += name;
@ -37,7 +43,6 @@ namespace Os {
this->m_name += pid; this->m_name += pid;
#endif #endif
this->m_identifier = identifier; this->m_identifier = identifier;
Task::TaskStatus tStat = TASK_OK; Task::TaskStatus tStat = TASK_OK;
pthread_attr_t att; pthread_attr_t att;
@ -116,7 +121,8 @@ namespace Os {
} }
pthread_t* tid = new pthread_t; 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) { switch (stat) {
case 0: case 0:
@ -214,7 +220,6 @@ namespace Os {
stat = pthread_join(*((pthread_t*) this->m_handle), value_ptr); stat = pthread_join(*((pthread_t*) this->m_handle), value_ptr);
if (stat != 0) { if (stat != 0) {
DEBUG_PRINT("join: %s\n", strerror(errno));
return TASK_JOIN_ERROR; return TASK_JOIN_ERROR;
} }
else { else {

View File

@ -11,7 +11,6 @@
namespace Os { namespace Os {
class TaskRegistry; //!< forward declaration class TaskRegistry; //!< forward declaration
class Task { class Task {
public: public:
@ -27,6 +26,11 @@ namespace Os {
typedef void (*taskRoutine)(void* ptr); //!< prototype for task routine started in task context 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 Task(); //!< constructor
virtual ~Task(); //!< destructor virtual ~Task(); //!< destructor
// Priority is based on Posix priorities - 0 lowest, 255 highest // Priority is based on Posix priorities - 0 lowest, 255 highest
@ -61,6 +65,7 @@ namespace Os {
void toString(char* buf, NATIVE_INT_TYPE buffSize); //!< print a string of the state of the task 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_started; //!< set when task has reached entry point
bool m_suspendedOnPurpose; //!< set when task was suspended in purpose (i.e. simulation) 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 TaskRegistry* s_taskRegistry; //!< pointer to registered task
static NATIVE_INT_TYPE s_numTasks; //!< stores the number of tasks created. static NATIVE_INT_TYPE s_numTasks; //!< stores the number of tasks created.