Separate the task allocation and start operations into separate calls.
This allows applications greater control over when and how FreeRTOS tasks are created and executed. It's now possible to allocate the memory for the task without having it also start to execute.
This commit is contained in:
Родитель
b4d4a717f8
Коммит
001c5da1e4
|
@ -103,7 +103,7 @@ int event_task_freertos_notify (const struct event_task *task,
|
|||
|
||||
/**
|
||||
* Initialize an event handler task. The actual FreeRTOS task will not be allocated until a call to
|
||||
* {@link event_task_freertos_start}.
|
||||
* one of the task allocation functions is made.
|
||||
*
|
||||
* @param task The event handler task to initialize.
|
||||
* @param state Variable context for the task. This must be uninitialized.
|
||||
|
@ -139,7 +139,7 @@ int event_task_freertos_init (struct event_task_freertos *task,
|
|||
/**
|
||||
* Initialize only the variable state for an event handler task. The rest of the task instance is
|
||||
* assumed to have already been initialized. The actual FreeRTOS task will not be allocated until a
|
||||
* call to {@link event_task_freertos_start}.
|
||||
* call to one of the task allocation functions is made.
|
||||
*
|
||||
* This would generally be used with a statically initialized instance.
|
||||
*
|
||||
|
@ -187,6 +187,9 @@ static void event_task_freertos_process_notification (const struct event_task_fr
|
|||
{
|
||||
bool reset = false;
|
||||
|
||||
/* Wait for the task to be started before executing anything in the task context. */
|
||||
ulTaskNotifyTake (pdTRUE, portMAX_DELAY);
|
||||
|
||||
event_task_prepare_handlers (task->handlers, task->num_handlers);
|
||||
|
||||
/* Indicate that the handlers have been initialized and the task is ready to process
|
||||
|
@ -223,18 +226,18 @@ static void event_task_freertos_process_notification (const struct event_task_fr
|
|||
|
||||
#if configSUPPORT_DYNAMIC_ALLOCATION == 1
|
||||
/**
|
||||
* Allocate and start running the event handler task using dynamic allocation of task resources. No
|
||||
* events can be handled until the task has been started.
|
||||
* Allocate the event handler task using dynamic allocation of task resources. The task will not be
|
||||
* ready for processing events until {@link event_task_freertos_start} is called.
|
||||
*
|
||||
* @param task The event task to start.
|
||||
* @param task The event task to allocate.
|
||||
* @param stack_words The size of the task stack. The stack size is measured in words.
|
||||
* @param task_name An identifying name to assign to the task. The maximum length is determined by
|
||||
* the FreeRTOS configuration for the platform.
|
||||
* @param priority The priority to assign to this task.
|
||||
*
|
||||
* @return 0 if the task was started or an error code.
|
||||
* @return 0 if the task was allocated or an error code.
|
||||
*/
|
||||
int event_task_freertos_start (const struct event_task_freertos *task, uint16_t stack_words,
|
||||
int event_task_freertos_allocate (const struct event_task_freertos *task, uint16_t stack_words,
|
||||
const char *task_name, int priority)
|
||||
{
|
||||
int status;
|
||||
|
@ -256,10 +259,10 @@ int event_task_freertos_start (const struct event_task_freertos *task, uint16_t
|
|||
|
||||
#if configSUPPORT_STATIC_ALLOCATION == 1
|
||||
/**
|
||||
* Initialize and start running the event handler task using static allocation of task resources.
|
||||
* No events can be handled until the task has been started.
|
||||
* Allocate the event handler task using static allocation of task resources. The task will not be
|
||||
* ready for processing events until {@link event_task_freertos_start} is called.
|
||||
*
|
||||
* @param task The event task to start.
|
||||
* @param task The event task to allocate.
|
||||
* @param context The statically allocated FreeRTOS context for the task.
|
||||
* @param stack A buffer to use for the task's stack.
|
||||
* @param stack_words The number of words in the stack buffer.
|
||||
|
@ -267,10 +270,11 @@ int event_task_freertos_start (const struct event_task_freertos *task, uint16_t
|
|||
* the FreeRTOS configuration for the platform.
|
||||
* @param priority The priority to assign to this task.
|
||||
*
|
||||
* @return 0 if the task was started or an error code.
|
||||
* @return 0 if the task was allocated or an error code.
|
||||
*/
|
||||
int event_task_freertos_start_static (const struct event_task_freertos *task, StaticTask_t *context,
|
||||
StackType_t *stack, uint32_t stack_words, const char *task_name, int priority)
|
||||
int event_task_freertos_allocate_static (const struct event_task_freertos *task,
|
||||
StaticTask_t *context, StackType_t *stack, uint32_t stack_words, const char *task_name,
|
||||
int priority)
|
||||
{
|
||||
if (task == NULL) {
|
||||
return EVENT_TASK_INVALID_ARGUMENT;
|
||||
|
@ -286,3 +290,16 @@ int event_task_freertos_start_static (const struct event_task_freertos *task, St
|
|||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Start running an event handler task that was previously allocated. No events can be handled
|
||||
* until the task has been started.
|
||||
*
|
||||
* @param task The event task to start. If this is null, nothing will be done.
|
||||
*/
|
||||
void event_task_freertos_start (const struct event_task_freertos *task)
|
||||
{
|
||||
if (task != NULL) {
|
||||
xTaskNotifyGive (task->state->task);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,56 +1,59 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
#ifndef EVENT_TASK_FREERTOS_H_
|
||||
#define EVENT_TASK_FREERTOS_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "platform_api.h"
|
||||
#include "system/event_task.h"
|
||||
#include "system/system.h"
|
||||
|
||||
|
||||
/**
|
||||
* Variable context for the task.
|
||||
*/
|
||||
struct event_task_freertos_state {
|
||||
struct event_task_context context; /**< Context for handlers to use for event processing. */
|
||||
TaskHandle_t task; /**< The task that will execute event handlers. */
|
||||
platform_mutex lock; /**< Synchronization with the execution task. */
|
||||
bool notifying; /**< Flag to indicate when an event is being triggered. */
|
||||
int running; /**< Index of the active handler for event processing. */
|
||||
};
|
||||
|
||||
/**
|
||||
* FreeRTOS implementation for a task to handle event processing.
|
||||
*/
|
||||
struct event_task_freertos {
|
||||
struct event_task base; /**< Base interface to the task. */
|
||||
struct event_task_freertos_state *state; /**< Variable context for the task. */
|
||||
struct system *system; /**< The system manager. */
|
||||
const struct event_task_handler **handlers; /**< List of registered event handlers. */
|
||||
size_t num_handlers; /**< Number of registered handlers in the list. */
|
||||
};
|
||||
|
||||
|
||||
int event_task_freertos_init (struct event_task_freertos *task,
|
||||
struct event_task_freertos_state *state, struct system *system,
|
||||
const struct event_task_handler **handlers, size_t num_handlers);
|
||||
int event_task_freertos_init_state (const struct event_task_freertos *task);
|
||||
void event_task_freertos_release (const struct event_task_freertos *task);
|
||||
|
||||
#if configSUPPORT_DYNAMIC_ALLOCATION == 1
|
||||
int event_task_freertos_start (const struct event_task_freertos *task, uint16_t stack_words,
|
||||
const char *task_name, int priority);
|
||||
#endif
|
||||
|
||||
#if configSUPPORT_STATIC_ALLOCATION == 1
|
||||
int event_task_freertos_start_static (const struct event_task_freertos *task, StaticTask_t *context,
|
||||
StackType_t *stack, uint32_t stack_words, const char *task_name, int priority);
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* EVENT_TASK_FREERTOS_H_ */
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
#ifndef EVENT_TASK_FREERTOS_H_
|
||||
#define EVENT_TASK_FREERTOS_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "platform_api.h"
|
||||
#include "system/event_task.h"
|
||||
#include "system/system.h"
|
||||
|
||||
|
||||
/**
|
||||
* Variable context for the task.
|
||||
*/
|
||||
struct event_task_freertos_state {
|
||||
struct event_task_context context; /**< Context for handlers to use for event processing. */
|
||||
TaskHandle_t task; /**< The task that will execute event handlers. */
|
||||
platform_mutex lock; /**< Synchronization with the execution task. */
|
||||
bool notifying; /**< Flag to indicate when an event is being triggered. */
|
||||
int running; /**< Index of the active handler for event processing. */
|
||||
};
|
||||
|
||||
/**
|
||||
* FreeRTOS implementation for a task to handle event processing.
|
||||
*/
|
||||
struct event_task_freertos {
|
||||
struct event_task base; /**< Base interface to the task. */
|
||||
struct event_task_freertos_state *state; /**< Variable context for the task. */
|
||||
struct system *system; /**< The system manager. */
|
||||
const struct event_task_handler **handlers; /**< List of registered event handlers. */
|
||||
size_t num_handlers; /**< Number of registered handlers in the list. */
|
||||
};
|
||||
|
||||
|
||||
int event_task_freertos_init (struct event_task_freertos *task,
|
||||
struct event_task_freertos_state *state, struct system *system,
|
||||
const struct event_task_handler **handlers, size_t num_handlers);
|
||||
int event_task_freertos_init_state (const struct event_task_freertos *task);
|
||||
void event_task_freertos_release (const struct event_task_freertos *task);
|
||||
|
||||
#if configSUPPORT_DYNAMIC_ALLOCATION == 1
|
||||
int event_task_freertos_allocate (const struct event_task_freertos *task, uint16_t stack_words,
|
||||
const char *task_name, int priority);
|
||||
#endif
|
||||
|
||||
#if configSUPPORT_STATIC_ALLOCATION == 1
|
||||
int event_task_freertos_allocate_static (const struct event_task_freertos *task,
|
||||
StaticTask_t *context, StackType_t *stack, uint32_t stack_words, const char *task_name,
|
||||
int priority);
|
||||
#endif
|
||||
|
||||
void event_task_freertos_start (const struct event_task_freertos *task);
|
||||
|
||||
|
||||
#endif /* EVENT_TASK_FREERTOS_H_ */
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
/**
|
||||
* Initialize a periodic handler task. The actual FreeRTOS task will not be allocated until a call
|
||||
* to {@link periodic_task_freertos_start}.
|
||||
* to one of the task allocation functions is made.
|
||||
*
|
||||
* @param task The periodic handler task to initialize.
|
||||
* @param state Variable context for the task. This must be uninitialized.
|
||||
|
@ -41,7 +41,7 @@ int periodic_task_freertos_init (struct periodic_task_freertos *task,
|
|||
/**
|
||||
* Initialize only the variable state for a periodic handler task. The rest of the task instance is
|
||||
* assumed to have already been initialized. The actual FreeRTOS task will not be allocated until a
|
||||
* call to {@link periodic_task_freertos_start}.
|
||||
* call to one of the task allocation functions is made.
|
||||
*
|
||||
* This would generally be used with a statically initialized instance.
|
||||
*
|
||||
|
@ -86,6 +86,9 @@ static void periodic_task_freertos_loop (struct periodic_task_freertos *task)
|
|||
int status;
|
||||
int last_error = 0;
|
||||
|
||||
/* Wait for the task to be started before executing anything in the task context. */
|
||||
ulTaskNotifyTake (pdTRUE, portMAX_DELAY);
|
||||
|
||||
periodic_task_prepare_handlers (task->handlers, task->num_handlers);
|
||||
|
||||
while (1) {
|
||||
|
@ -101,19 +104,19 @@ static void periodic_task_freertos_loop (struct periodic_task_freertos *task)
|
|||
|
||||
#if configSUPPORT_DYNAMIC_ALLOCATION == 1
|
||||
/**
|
||||
* Allocate and start running the periodic handler task using dynamic allocation of task resources.
|
||||
* No handlers will be called until the task has been started.
|
||||
* Allocate the periodic handler task using dynamic allocation of task resources. No handlers will
|
||||
* be prepared or executed until {@link periodic_task_freertos_start} is called.
|
||||
*
|
||||
* @param task The periodic task to start.
|
||||
* @param task The periodic task to allocate.
|
||||
* @param stack_words The size of the task stack. The stack size is measured in words.
|
||||
* @param task_name An identifying name to assign to the task. The maximum length is determined by
|
||||
* the FreeRTOS configuration for the platform.
|
||||
* @param priority The priority to assign to this task.
|
||||
*
|
||||
* @return 0 if the task was started or an error code.
|
||||
* @return 0 if the task was allocated or an error code.
|
||||
*/
|
||||
int periodic_task_freertos_start (const struct periodic_task_freertos *task, uint16_t stack_words,
|
||||
const char *task_name, int priority)
|
||||
int periodic_task_freertos_allocate (const struct periodic_task_freertos *task,
|
||||
uint16_t stack_words, const char *task_name, int priority)
|
||||
{
|
||||
int status;
|
||||
|
||||
|
@ -134,10 +137,10 @@ int periodic_task_freertos_start (const struct periodic_task_freertos *task, uin
|
|||
|
||||
#if configSUPPORT_STATIC_ALLOCATION == 1
|
||||
/**
|
||||
* Initialize and start running the periodic handler task using static allocation of task resources.
|
||||
* No handlers will be called until the task has been started.
|
||||
* Allocate the periodic handler task using static allocation of task resources. No handlers will
|
||||
* be prepared or executed until {@link periodic_task_freertos_start} is called.
|
||||
*
|
||||
* @param task The periodic task to start.
|
||||
* @param task The periodic task to allocate.
|
||||
* @param context The statically allocated FreeRTOS context for the task.
|
||||
* @param stack A buffer to use for the task's stack.
|
||||
* @param stack_words The number of words in the stack buffer.
|
||||
|
@ -145,9 +148,9 @@ int periodic_task_freertos_start (const struct periodic_task_freertos *task, uin
|
|||
* the FreeRTOS configuration for the platform.
|
||||
* @param priority The priority to assign to this task.
|
||||
*
|
||||
* @return 0 if the task was started or an error code.
|
||||
* @return 0 if the task was allocated or an error code.
|
||||
*/
|
||||
int periodic_task_freertos_start_static (const struct periodic_task_freertos *task,
|
||||
int periodic_task_freertos_allocate_static (const struct periodic_task_freertos *task,
|
||||
StaticTask_t *context, StackType_t *stack, uint32_t stack_words, const char *task_name,
|
||||
int priority)
|
||||
{
|
||||
|
@ -164,3 +167,16 @@ int periodic_task_freertos_start_static (const struct periodic_task_freertos *ta
|
|||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Start running a periodic handler task that was previously allocated. No handlers will be called
|
||||
* until the task has been started.
|
||||
*
|
||||
* @param task The periodic task to start. If this is null, nothing will be done.
|
||||
*/
|
||||
void periodic_task_freertos_start (const struct periodic_task_freertos *task)
|
||||
{
|
||||
if (task != NULL) {
|
||||
xTaskNotifyGive (task->state->task);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,50 +1,52 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
#ifndef PERIODIC_TASK_FREERTOS_H_
|
||||
#define PERIODIC_TASK_FREERTOS_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "system/periodic_task.h"
|
||||
|
||||
|
||||
/**
|
||||
* Variable context for the task.
|
||||
*/
|
||||
struct periodic_task_freertos_state {
|
||||
TaskHandle_t task; /**< The task that will execute periodic operations. */
|
||||
};
|
||||
|
||||
/**
|
||||
* FreeRTOS implementation for a task to handle event processing.
|
||||
*/
|
||||
struct periodic_task_freertos {
|
||||
struct periodic_task_freertos_state *state; /**< Variable context for the task. */
|
||||
const struct periodic_task_handler **handlers; /**< List of registered handlers. */
|
||||
size_t num_handlers; /**< Number of registered handlers in the list. */
|
||||
int id; /**< Logging identifier. */
|
||||
};
|
||||
|
||||
|
||||
int periodic_task_freertos_init (struct periodic_task_freertos *task,
|
||||
struct periodic_task_freertos_state *state, const struct periodic_task_handler **handlers,
|
||||
size_t num_handlers, int log_id);
|
||||
int periodic_task_freertos_init_state (const struct periodic_task_freertos *task);
|
||||
void periodic_task_freertos_release (const struct periodic_task_freertos *task);
|
||||
|
||||
#if configSUPPORT_DYNAMIC_ALLOCATION == 1
|
||||
int periodic_task_freertos_start (const struct periodic_task_freertos *task, uint16_t stack_words,
|
||||
const char *task_name, int priority);
|
||||
#endif
|
||||
|
||||
#if configSUPPORT_STATIC_ALLOCATION == 1
|
||||
int periodic_task_freertos_start_static (const struct periodic_task_freertos *task,
|
||||
StaticTask_t *context, StackType_t *stack, uint32_t stack_words, const char *task_name,
|
||||
int priority);
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* PERIODIC_TASK_FREERTOS_H_ */
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
#ifndef PERIODIC_TASK_FREERTOS_H_
|
||||
#define PERIODIC_TASK_FREERTOS_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "system/periodic_task.h"
|
||||
|
||||
|
||||
/**
|
||||
* Variable context for the task.
|
||||
*/
|
||||
struct periodic_task_freertos_state {
|
||||
TaskHandle_t task; /**< The task that will execute periodic operations. */
|
||||
};
|
||||
|
||||
/**
|
||||
* FreeRTOS implementation for a task to handle event processing.
|
||||
*/
|
||||
struct periodic_task_freertos {
|
||||
struct periodic_task_freertos_state *state; /**< Variable context for the task. */
|
||||
const struct periodic_task_handler **handlers; /**< List of registered handlers. */
|
||||
size_t num_handlers; /**< Number of registered handlers in the list. */
|
||||
int id; /**< Logging identifier. */
|
||||
};
|
||||
|
||||
|
||||
int periodic_task_freertos_init (struct periodic_task_freertos *task,
|
||||
struct periodic_task_freertos_state *state, const struct periodic_task_handler **handlers,
|
||||
size_t num_handlers, int log_id);
|
||||
int periodic_task_freertos_init_state (const struct periodic_task_freertos *task);
|
||||
void periodic_task_freertos_release (const struct periodic_task_freertos *task);
|
||||
|
||||
#if configSUPPORT_DYNAMIC_ALLOCATION == 1
|
||||
int periodic_task_freertos_allocate (const struct periodic_task_freertos *task,
|
||||
uint16_t stack_words, const char *task_name, int priority);
|
||||
#endif
|
||||
|
||||
#if configSUPPORT_STATIC_ALLOCATION == 1
|
||||
int periodic_task_freertos_allocate_static (const struct periodic_task_freertos *task,
|
||||
StaticTask_t *context, StackType_t *stack, uint32_t stack_words, const char *task_name,
|
||||
int priority);
|
||||
#endif
|
||||
|
||||
void periodic_task_freertos_start (const struct periodic_task_freertos *task);
|
||||
|
||||
|
||||
#endif /* PERIODIC_TASK_FREERTOS_H_ */
|
||||
|
|
Загрузка…
Ссылка в новой задаче