Skip to content

Commit

Permalink
Remove usage of FreeRTOS timers from asyncFreeRtos
Browse files Browse the repository at this point in the history
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
  • Loading branch information
marcin-wierzbicki authored and SuhashiniNaik committed Jan 15, 2025
1 parent a43a155 commit c171695
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 317 deletions.
2 changes: 1 addition & 1 deletion libs/3rdparty/freeRtos/mock/include/os/FreeRtosMock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion libs/3rdparty/freeRtos/mock/include/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions libs/3rdparty/freeRtos/mock/src/os/FreeRtosMock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
11 changes: 4 additions & 7 deletions libs/bsw/asyncFreeRtos/include/async/FreeRtosAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
{
Expand All @@ -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
Expand Down
107 changes: 39 additions & 68 deletions libs/bsw/asyncFreeRtos/include/async/TaskContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

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

namespace async
{
Expand All @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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;
};

Expand All @@ -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(
Expand All @@ -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;
}

Expand All @@ -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>();
Expand All @@ -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>
Expand Down Expand Up @@ -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()
{
Expand All @@ -364,7 +347,6 @@ void TaskContext<Binding>::dispatch()
template<class Binding>
inline void TaskContext<Binding>::stopDispatch()
{
_runnableExecutor.shutdown();
setEvents(STOP_EVENT_MASK);
}

Expand All @@ -373,6 +355,7 @@ void TaskContext<Binding>::dispatchWhileWork()
{
while (true)
{
handleTimeout();
EventMaskType const eventMask = peekEvents();
if (eventMask != 0U)
{
Expand Down Expand Up @@ -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>
Expand All @@ -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

0 comments on commit c171695

Please sign in to comment.