Skip to content

Commit

Permalink
lestarch: correcting macOS issues; using deprecate instead of logger …
Browse files Browse the repository at this point in the history
…warnings
  • Loading branch information
LeStarch committed Sep 1, 2021
1 parent 3079f39 commit 62a4103
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 232 deletions.
3 changes: 1 addition & 2 deletions Drv/LinuxSerialDriver/LinuxSerialDriverComponentImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,7 @@ namespace Drv {
startReadThread(NATIVE_INT_TYPE priority, NATIVE_INT_TYPE stackSize, NATIVE_INT_TYPE cpuAffinity) {

Os::TaskString task("SerReader");
Os::Task::TaskStatus stat = this->m_readTask.start(task, 0, priority, stackSize,
serialReadTaskEntry, this, cpuAffinity);
Os::Task::TaskStatus stat = this->m_readTask.start(task, serialReadTaskEntry, this, priority, stackSize, cpuAffinity);
FW_ASSERT(stat == Os::Task::TASK_OK, stat);
}

Expand Down
2 changes: 1 addition & 1 deletion Drv/LinuxSerialDriver/LinuxSerialDriverComponentImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace Drv {
//! start the serial poll thread.
//! buffSize is the max receive buffer size
//!
void startReadThread(NATIVE_INT_TYPE priority, NATIVE_INT_TYPE stackSize, NATIVE_INT_TYPE cpuAffinity = -1);
void startReadThread(NATIVE_INT_TYPE priority = -1, NATIVE_INT_TYPE stackSize = -1, NATIVE_INT_TYPE cpuAffinity = -1);

//! Quit thread
void quitReadThread(void);
Expand Down
3 changes: 1 addition & 2 deletions Drv/SocketIpDriver/SocketIpDriverComponentImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ namespace Drv {
Fw::Logger::logMsg("Unable to open socket: %d\n",stat);
}
}
Os::Task::TaskStatus stat = m_recvTask.start(name, 0, priority, stack,
SocketIpDriverComponentImpl::readTask, this, cpuAffinity);
Os::Task::TaskStatus stat = m_recvTask.start(name, SocketIpDriverComponentImpl::readTask, this, priority, stack, cpuAffinity);
FW_ASSERT(Os::Task::TASK_OK == stat, static_cast<NATIVE_INT_TYPE>(stat));
}
}
Expand Down
11 changes: 9 additions & 2 deletions Fw/Comp/ActiveComponentBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ namespace Fw {
#endif

void ActiveComponentBase::start(NATIVE_INT_TYPE identifier, NATIVE_INT_TYPE priority, NATIVE_INT_TYPE stackSize, NATIVE_INT_TYPE cpuAffinity) {
this->start(static_cast<NATIVE_UINT_TYPE>(priority), static_cast<NATIVE_UINT_TYPE>(stackSize),
((cpuAffinity == -1) ? Os::Task::TASK_DEFAULT : static_cast<NATIVE_UINT_TYPE>(cpuAffinity)),
static_cast<NATIVE_UINT_TYPE>(identifier));
}

void ActiveComponentBase::start(NATIVE_UINT_TYPE priority_in, NATIVE_UINT_TYPE stackSize, NATIVE_UINT_TYPE cpuAffinity, NATIVE_UINT_TYPE identifier) {
NATIVE_INT_TYPE priority = static_cast<NATIVE_INT_TYPE>(priority_in);
Os::TaskString taskName;

#if FW_OBJECT_NAMES == 1
Expand All @@ -63,10 +69,11 @@ namespace Fw {
// If running with the baremetal scheduler, use a variant of the task-loop that
// does not loop internal, but waits for an external iteration call.
#if FW_BAREMETAL_SCHEDULER == 1
Os::Task::TaskStatus status = this->m_task.start(taskName, identifier, priority, stackSize, this->s_baseBareTask, this, cpuAffinity);
Os::Task::taskRoutine routine = this->s_baseBareTask;
#else
Os::Task::TaskStatus status = this->m_task.start(taskName, this->s_baseTask, this, priority, stackSize, cpuAffinity, identifier);
Os::Task::taskRoutine routine = this->s_baseTask;
#endif
Os::Task::TaskStatus status = this->m_task.start(taskName, routine, this, priority, stackSize, cpuAffinity, identifier);
FW_ASSERT(status == Os::Task::TASK_OK,(NATIVE_INT_TYPE)status);
}

Expand Down
7 changes: 5 additions & 2 deletions Fw/Comp/ActiveComponentBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
#include <Fw/Comp/QueuedComponentBase.hpp>
#include <Os/Task.hpp>
#include <FpConfig.hpp>

#include <Fw/Deprecate.hpp>

namespace Fw {
class ActiveComponentBase : public QueuedComponentBase {
public:
void start(NATIVE_INT_TYPE identifier = 0, NATIVE_INT_TYPE priority = -1, NATIVE_INT_TYPE stackSize = -1, NATIVE_INT_TYPE cpuAffinity = -1); //!< called by instantiator when task is to be started
void start(NATIVE_UINT_TYPE priority = Os::Task::TASK_DEFAULT, NATIVE_UINT_TYPE stackSize = Os::Task::TASK_DEFAULT, NATIVE_UINT_TYPE cpuAffinity = Os::Task::TASK_DEFAULT, NATIVE_UINT_TYPE identifier = Os::Task::TASK_DEFAULT); //!< called by instantiator when task is to be started

DEPRECATED(void start(NATIVE_INT_TYPE identifier, NATIVE_INT_TYPE priority, NATIVE_INT_TYPE stackSize, NATIVE_INT_TYPE cpuAffinity = -1),
"Please switch to start(NATIVE_UINT_TYPE priority, NATIVE_UINT_TYPE stackSize, NATIVE_UINT_TYPE cpuAffinity, NATIVE_UINT_TYPE identifier)"); //!< called by instantiator when task is to be started
void exit(void); //!< exit task in active component
Os::Task::TaskStatus join(void **value_ptr); //!< provide return value of thread if value_ptr is not NULL

Expand Down
15 changes: 15 additions & 0 deletions Fw/Deprecate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// deprecate.hpp:
//
// An minor implementation of compile-time deprecation for the fprime framework.

#ifndef FW_DEPRECATE_HPP
#define FW_DEPRECATE_HPP

#ifdef __GNUC__
#define DEPRECATED(func, message) func __attribute__ ((deprecated(message)))
#else
#warning "No implementation of DEPRECATED for given compiler. Pleas check for use of DEPRECATED() functions"
#define DEPRECATED(func) func
#endif

#endif // REF_DEPRECATE_HPP
2 changes: 1 addition & 1 deletion Os/Baremetal/Task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Task::Task() :
m_suspendedOnPurpose(false)
{}

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, taskRoutine routine, void* arg, NATIVE_UINT_TYPE priority, NATIVE_UINT_TYPE stackSize, NATIVE_UINT_TYPE cpuAffinity, NATIVE_UINT_TYPE identifier) {
//Get a task handle, and set it up
BareTaskHandle* handle = new BareTaskHandle();
if (handle == NULL) {
Expand Down
40 changes: 27 additions & 13 deletions Os/Posix/Task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
#include <errno.h>

#include <pthread.h>
#include <sched.h>
#include <limits.h>
#include <string.h>
#include <time.h>
#include <Fw/Logger/Logger.hpp>
#include <unistd.h>

static const NATIVE_INT_TYPE SCHED_POLICY = SCHED_RR;

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

Expand All @@ -24,27 +28,33 @@ void* pthread_entry_wrapper(void* arg) {
namespace Os {

void validate_arguments(NATIVE_INT_TYPE& priority, NATIVE_INT_TYPE& stack, NATIVE_INT_TYPE& affinity, bool expect_perm) {
const NATIVE_INT_TYPE min_priority = sched_get_priority_min(SCHED_POLICY);
const NATIVE_INT_TYPE max_priority = sched_get_priority_max(SCHED_POLICY);
// Check to ensure that these calls worked
if (min_priority == -1 or max_priority == -1) {
Fw::Logger::logMsg("[WARNING] Unable to determine min/max priority with error %s. Discarding priority. ", reinterpret_cast<POINTER_CAST>(strerror(errno)));
}
// Check priority attributes
if (!expect_perm and priority != -1) {
Fw::Logger::logMsg("[WARNING] task priority set and permissions unavailable. Discarding priority.\n");
Fw::Logger::logMsg("[WARNING] Task priority set and permissions unavailable. Discarding priority.\n");
priority = -1;
}
if (priority != -1 and priority < 1) {
Fw::Logger::logMsg("[WARNING] low task priority of %d being clamped to 1\n", priority);
priority = 1;
if (priority != -1 and priority < min_priority) {
Fw::Logger::logMsg("[WARNING] Low task priority of %d being clamped to %d\n", priority, min_priority);
priority = min_priority;
}
if (priority != -1 and priority > 99) {
Fw::Logger::logMsg("[WARNING] high task priority of %d being clamped to 99\n", priority);
priority = 99;
if (priority != -1 and priority > max_priority) {
Fw::Logger::logMsg("[WARNING] High task priority of %d being clamped to %d\n", priority, max_priority);
priority = max_priority;
}
// Check the stack
if (stack != -1 and stack < PTHREAD_STACK_MIN) {
Fw::Logger::logMsg("[WARNING] stack size %d too small, setting to minimum of %d\n", stack, PTHREAD_STACK_MIN);
Fw::Logger::logMsg("[WARNING] Stack size %d too small, setting to minimum of %d\n", stack, PTHREAD_STACK_MIN);
stack = PTHREAD_STACK_MIN;
}
// Check CPU affinity
if (!expect_perm and affinity != -1) {
Fw::Logger::logMsg("[WARNING] cpu affinity set and permissions unavailable. Discarding affinity.\n");
Fw::Logger::logMsg("[WARNING] Cpu affinity set and permissions unavailable. Discarding affinity.\n");
affinity = -1;
}
}
Expand All @@ -63,7 +73,7 @@ namespace Os {

Task::TaskStatus set_priority_params(pthread_attr_t& att, NATIVE_INT_TYPE priority) {
if (priority != -1) {
I32 stat = pthread_attr_setschedpolicy(&att, SCHED_FIFO);
I32 stat = pthread_attr_setschedpolicy(&att, SCHED_POLICY);
if (stat != 0) {
Fw::Logger::logMsg("pthread_attr_setschedpolicy: %s\n", reinterpret_cast<POINTER_CAST>(strerror(stat)));
return Task::TASK_INVALID_PARAMS;
Expand All @@ -90,6 +100,7 @@ namespace Os {

Task::TaskStatus set_cpu_affinity(pthread_attr_t& att, NATIVE_INT_TYPE cpuAffinity) {
if (cpuAffinity != -1) {
#ifdef TGT_OS_TYPE_LINUX
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(cpuAffinity, &cpuset);
Expand All @@ -100,6 +111,9 @@ namespace Os {
reinterpret_cast<POINTER_CAST>(strerror(stat)));
return Task::TASK_INVALID_PARAMS;
}
#else
Fw::Logger::logMsg("[WARNING] Setting CPU affinity is only available on Linux\n");
#endif
}
return Task::TASK_OK;
}
Expand Down Expand Up @@ -174,7 +188,7 @@ namespace Os {
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, taskRoutine routine, void* arg, NATIVE_INT_TYPE priority, NATIVE_INT_TYPE stackSize, NATIVE_INT_TYPE cpuAffinity, NATIVE_INT_TYPE identifier) {
Task::TaskStatus Task::start(const Fw::StringBase &name, taskRoutine routine, void* arg, NATIVE_UINT_TYPE priority, NATIVE_UINT_TYPE stackSize, NATIVE_UINT_TYPE cpuAffinity, NATIVE_UINT_TYPE identifier) {
FW_ASSERT(routine);

this->m_name = "TP_";
Expand All @@ -186,13 +200,13 @@ namespace Os {
pthread_t* tid;

// Try to create a permissioned thread
TaskStatus status = create_pthread(priority, stackSize, cpuAffinity, tid, &this->m_routineWrapper, true);
TaskStatus status = create_pthread(static_cast<NATIVE_INT_TYPE>(priority), static_cast<NATIVE_INT_TYPE>(stackSize), static_cast<NATIVE_INT_TYPE>(cpuAffinity), tid, &this->m_routineWrapper, true);
// Failure dur to permission automatically retried
if (status == TASK_ERROR_PERMISSION) {
Fw::Logger::logMsg("[WARNING] Insufficient permissions to create a prioritized tasks or specify CPU affinities. Attempting to fallback to tasks without priority.\n");
Fw::Logger::logMsg("[WARNING] Please use no-argument <component>.start() calls or ensure executing user has correct permissions for your operating system.\n");
Fw::Logger::logMsg("[WARNING] Note: this fallback to tasks without priority will be removed and will fail in future fprime releases.\n");
status = create_pthread(priority, stackSize, cpuAffinity, tid, &this->m_routineWrapper, false); // Fallback with no permission
status = create_pthread(static_cast<NATIVE_INT_TYPE>(priority), static_cast<NATIVE_INT_TYPE>(stackSize), static_cast<NATIVE_INT_TYPE>(cpuAffinity), tid, &this->m_routineWrapper, false); // Fallback with no permission
}
// Check for non-zero error code
if (status != TASK_OK) {
Expand Down
Loading

0 comments on commit 62a4103

Please sign in to comment.