Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usage of FreeRTOS timers from asyncFreeRtos #44

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Remove usage of FreeRTOS timers from asyncFreeRtos
Until now for implementing async schedule functions
FreeRTOS timers were in use. This led to a huge
overhead because most (re)setting of a timer caused
two additional task switches (first to high prio timer
task and then back to the active task).
This new implementation uses only the task notification
mechanism for both executing events and scheduling by
specifying the next required timeout of the task using
the parameter xTicksToWait of function xTaskNotifyWait.

Change-Id: Ieaa3ef298c39f2b11eca9c1ed9c2b8bde9dc2290
marcin-wierzbicki authored and SuhashiniNaik committed Jan 15, 2025
commit c171695235a9e7239f48c4c15a0b0b86971ca1f0
2 changes: 1 addition & 1 deletion libs/3rdparty/freeRtos/mock/include/os/FreeRtosMock.h
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ namespace os
MOCK_METHOD0(xTaskGetIdleTaskHandle, TaskHandle_t());
MOCK_METHOD3(xTaskNotify, void(TaskHandle_t taskHandle, uint32_t ulValue, eNotifyAction eAction));
MOCK_METHOD4(xTaskNotifyFromISR, void(TaskHandle_t taskHandle, uint32_t ulValue, eNotifyAction eAction, BaseType_t *pxHigherPriorityTaskWoken));
MOCK_METHOD4(xTaskNotifyWait, void(uint32_t ulBitsToClearOnEntry, uint32_t ulBitsToClearOnExit, uint32_t *pulNotificationValue, TickType_t xTicksToWait));
MOCK_METHOD4(xTaskNotifyWait, BaseType_t(uint32_t ulBitsToClearOnEntry, uint32_t ulBitsToClearOnExit, uint32_t *pulNotificationValue, TickType_t xTicksToWait));
MOCK_METHOD0(vTaskStartScheduler, void());
MOCK_METHOD1(uxTaskGetTaskNumber, UBaseType_t(TaskHandle_t xTask));
MOCK_METHOD1(uxTaskGetStackHighWaterMark, BaseType_t(TaskHandle_t taskHandle));
2 changes: 1 addition & 1 deletion libs/3rdparty/freeRtos/mock/include/task.h
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ TaskHandle_t xTaskCreateStatic(TaskFunction_t pxTaskCode, const char * pcName, c
void * pvParameters, UBaseType_t uxPriority, StackType_t * puxStackBuffer, StaticTask_t * pxTaskBuffer);
void xTaskNotify(TaskHandle_t taskHandle, uint32_t ulValue, eNotifyAction eAction);
void xTaskNotifyFromISR(TaskHandle_t taskHandle, uint32_t ulValue, eNotifyAction eAction, BaseType_t *pxHigherPriorityTaskWoken);
void xTaskNotifyWait(uint32_t ulBitsToClearOnEntry, uint32_t ulBitsToClearOnExit, uint32_t *pulNotificationValue, TickType_t xTicksToWait);
BaseType_t xTaskNotifyWait(uint32_t ulBitsToClearOnEntry, uint32_t ulBitsToClearOnExit, uint32_t *pulNotificationValue, TickType_t xTicksToWait);
const char* pcTaskGetName(TaskHandle_t taskHandle);
BaseType_t uxTaskGetStackHighWaterMark(TaskHandle_t taskHandle);
void vTaskStartScheduler();
4 changes: 2 additions & 2 deletions libs/3rdparty/freeRtos/mock/src/os/FreeRtosMock.cpp
Original file line number Diff line number Diff line change
@@ -45,14 +45,14 @@ xTaskNotifyFromISR(
taskHandle, ulValue, eAction, pxHigherPriorityTaskWoken);
}

void
BaseType_t
xTaskNotifyWait(
uint32_t ulBitsToClearOnEntry,
uint32_t ulBitsToClearOnExit,
uint32_t* pulNotificationValue,
TickType_t xTicksToWait)
{
FreeRtosMock::instance().xTaskNotifyWait(
return FreeRtosMock::instance().xTaskNotifyWait(
ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait);
}

11 changes: 4 additions & 7 deletions libs/bsw/asyncFreeRtos/include/async/FreeRtosAdapter.h
Original file line number Diff line number Diff line change
@@ -237,7 +237,7 @@ class FreeRtosAdapter

static void initTask(TaskInitializer& initializer);

static void initTask(ContextType context, char const* name, StaticTimer_t& timer);
static void initTask(ContextType context);

static void TaskFunction(void* param);

@@ -353,12 +353,12 @@ void FreeRtosAdapter<Binding>::initTask(TaskInitializer& initializer)
}
else
{
initTask(context, initializer._name, initializer._timer);
initTask(context);
TaskContextType& taskContext = _taskContexts[static_cast<size_t>(context)];
if (context == TASK_IDLE)
{
_idleTaskInitializer = &initializer;
taskContext.initTask(TASK_IDLE, initializer._taskFunction);
taskContext.initTask(TASK_IDLE, initializer._name, initializer._taskFunction);
}
else
{
@@ -374,11 +374,8 @@ void FreeRtosAdapter<Binding>::initTask(TaskInitializer& initializer)
}

template<class Binding>
void FreeRtosAdapter<Binding>::initTask(
ContextType const context, char const* const name, StaticTimer_t& timer)
void FreeRtosAdapter<Binding>::initTask(ContextType const context)
{
TaskContextType& taskContext = _taskContexts[static_cast<size_t>(context)];
taskContext.createTimer(timer, name);
#ifndef ASYNC_TIMEOUTMANAGER2_DISABLE
_timeoutManagers[static_cast<size_t>(context)].init(context);
#endif // ASYNC_TIMEOUTMANAGER2_DISABLE
107 changes: 39 additions & 68 deletions libs/bsw/asyncFreeRtos/include/async/TaskContext.h
Original file line number Diff line number Diff line change
@@ -19,7 +19,6 @@

#include <FreeRTOS.h>
#include <task.h>
#include <timers.h>

namespace async
{
@@ -46,9 +45,10 @@ class TaskContext : public EventDispatcher<2U, LockType>
/**
* Initializes a task with a specified context and task function.
* \param context The context associated with this task.
* \param name The name of this task.
* \param taskFunction The function to execute in this task.
*/
void initTask(ContextType context, TaskFunctionType taskFunction);
void initTask(ContextType context, char const* const name, TaskFunctionType taskFunction);

/**
* Sets the FreeRTOS task handle for this context.
@@ -73,13 +73,6 @@ class TaskContext : public EventDispatcher<2U, LockType>
StackType const& stack,
TaskFunctionType taskFunction);

/**
* Creates a FreeRTOS timer.
* \param timer The static timer storage structure.
* \param name The name of the timer.
*/
void createTimer(StaticTimer_t& timer, char const* name);

char const* getName() const;

TaskHandle_t getTaskHandle() const;
@@ -129,12 +122,6 @@ class TaskContext : public EventDispatcher<2U, LockType>
/// Dispatches events while runnable is executing.
void dispatchWhileWork();

/**
* Sets a timeout value in microseconds.
* \param timeInUs The timeout duration in microseconds.
*/
void setTimeout(uint32_t timeInUs);

/**
* Retrieves the amount of unused stack space for a specified task.
* \param taskHandle The handle of the task.
@@ -152,29 +139,28 @@ class TaskContext : public EventDispatcher<2U, LockType>
friend class EventPolicy<TaskContext<Binding>, 0U>;
friend class EventPolicy<TaskContext<Binding>, 1U>;

void setEvents(EventMaskType eventMask);
EventMaskType waitEvents();
EventMaskType peekEvents();

private:
using TimerType = ::timer::Timer<LockType>;
using ExecuteEventPolicyType = EventPolicy<TaskContext<Binding>, 0U>;
using TimerEventPolicyType = EventPolicy<TaskContext<Binding>, 1U>;
using TimerType = ::timer::Timer<LockType>;

static EventMaskType const STOP_EVENT_MASK = static_cast<EventMaskType>(
static_cast<EventMaskType>(1U) << static_cast<EventMaskType>(EVENT_COUNT));
static EventMaskType const WAIT_EVENT_MASK = (STOP_EVENT_MASK << 1U) - 1U;

void setEvents(EventMaskType eventMask);
EventMaskType waitEvents();
EventMaskType peekEvents();

void handleTimeout();

static void staticTaskFunction(void* param);
static void staticTimerFunction(TimerHandle_t handle);

RunnableExecutor<RunnableType, EventPolicy<TaskContext<Binding>, 0U>, LockType>
_runnableExecutor;
RunnableExecutor<RunnableType, ExecuteEventPolicyType, LockType> _runnableExecutor;
TimerType _timer;
EventPolicy<TaskContext<Binding>, 1U> _timerEventPolicy;
TimerEventPolicyType _timerEventPolicy;
TaskFunctionType _taskFunction;
TaskHandle_t _taskHandle;
TimerHandle_t _timerHandle;
char const* _name;
ContextType _context;
};

@@ -184,11 +170,10 @@ class TaskContext : public EventDispatcher<2U, LockType>
template<class Binding>
inline TaskContext<Binding>::TaskContext()
: _runnableExecutor(*this)
, _timer()
, _timerEventPolicy(*this)
, _taskFunction()
, _taskHandle(nullptr)
, _timerHandle(nullptr)
, _name(nullptr)
, _context(CONTEXT_INVALID)
{
_timerEventPolicy.setEventHandler(
@@ -197,9 +182,11 @@ inline TaskContext<Binding>::TaskContext()
}

template<class Binding>
void TaskContext<Binding>::initTask(ContextType const context, TaskFunctionType const taskFunction)
void TaskContext<Binding>::initTask(
ContextType const context, char const* const name, TaskFunctionType const taskFunction)
{
_context = context;
_name = name;
_taskFunction = taskFunction;
}

@@ -219,6 +206,7 @@ void TaskContext<Binding>::createTask(
TaskFunctionType const taskFunction)
{
_context = context;
_name = name;
_taskFunction = taskFunction.has_value()
? taskFunction
: TaskFunctionType::template create<&TaskContext::defaultTaskFunction>();
@@ -232,17 +220,10 @@ void TaskContext<Binding>::createTask(
&task);
}

template<class Binding>
void TaskContext<Binding>::createTimer(StaticTimer_t& timer, char const* const name)
{
_timerHandle = xTimerCreateStatic(
name, 1U, static_cast<UBaseType_t>(pdFALSE), this, &staticTimerFunction, &timer);
}

template<class Binding>
inline char const* TaskContext<Binding>::getName() const
{
return (_timerHandle != nullptr) ? pcTimerGetName(_timerHandle) : nullptr;
return _name;
}

template<class Binding>
@@ -317,33 +298,35 @@ template<class Binding>
inline EventMaskType TaskContext<Binding>::waitEvents()
{
EventMaskType eventMask = 0U;
xTaskNotifyWait(0U, WAIT_EVENT_MASK, &eventMask, Binding::WAIT_EVENTS_TICK_COUNT);
return eventMask;
uint32_t ticks = Binding::WAIT_EVENTS_TICK_COUNT;
uint32_t nextDelta;
bool const hasDelta = _timer.getNextDelta(getSystemTimeUs32Bit(), nextDelta);
if (hasDelta)
{
ticks = static_cast<uint32_t>((nextDelta + (Config::TICK_IN_US - 1U)) / Config::TICK_IN_US);
}
if (xTaskNotifyWait(0U, WAIT_EVENT_MASK, &eventMask, ticks) != 0)
{
return eventMask;
}
else if (hasDelta)
{
return TimerEventPolicyType::EVENT_MASK;
}
else
{
return 0U;
}
}

template<class Binding>
inline EventMaskType TaskContext<Binding>::peekEvents()
{
EventMaskType eventMask = 0U;
xTaskNotifyWait(0U, WAIT_EVENT_MASK, &eventMask, 0U);
(void)xTaskNotifyWait(0U, WAIT_EVENT_MASK, &eventMask, 0U);
return eventMask;
}

template<class Binding>
void TaskContext<Binding>::setTimeout(uint32_t const timeInUs)
{
if (timeInUs > 0U)
{
uint32_t const ticks
= static_cast<uint32_t>((timeInUs + (Config::TICK_IN_US - 1U)) / Config::TICK_IN_US);
(void)xTimerChangePeriod(_timerHandle, ticks, 0U);
}
else
{
(void)_timerEventPolicy.setEvent();
}
}

template<class Binding>
void TaskContext<Binding>::callTaskFunction()
{
@@ -364,7 +347,6 @@ void TaskContext<Binding>::dispatch()
template<class Binding>
inline void TaskContext<Binding>::stopDispatch()
{
_runnableExecutor.shutdown();
setEvents(STOP_EVENT_MASK);
}

@@ -373,6 +355,7 @@ void TaskContext<Binding>::dispatchWhileWork()
{
while (true)
{
handleTimeout();
EventMaskType const eventMask = peekEvents();
if (eventMask != 0U)
{
@@ -402,11 +385,6 @@ template<class Binding>
void TaskContext<Binding>::handleTimeout()
{
while (_timer.processNextTimeout(getSystemTimeUs32Bit())) {}
uint32_t nextDelta;
if (_timer.getNextDelta(getSystemTimeUs32Bit(), nextDelta))
{
setTimeout(nextDelta);
}
}

template<class Binding>
@@ -416,13 +394,6 @@ void TaskContext<Binding>::staticTaskFunction(void* const param)
taskContext.callTaskFunction();
}

template<class Binding>
void TaskContext<Binding>::staticTimerFunction(TimerHandle_t const handle)
{
TaskContext& taskContext = *reinterpret_cast<TaskContext*>(pvTimerGetTimerID(handle));
taskContext._timerEventPolicy.setEvent();
}

} // namespace async

#endif // GUARD_60674D25_51E0_4A73_8CD1_9C6C6138C877
Loading