队列
概述
队列提供了一种任务间实现同步和数据传递的机制。事件只能用于任务间传递某类“事件”是否发生的信号,无法传递更为复杂的数据,队列弥补了事件的这一不足,可以在任务间传递不定长度的消息。
API讲解
编程实例
1、在tos_config.h中,配置队列组件开关TOS_CFG_QUEUE_EN:
#define TOS_CFG_QUEUE_EN 1u
2、在tos_config.h中,配置消息队列组件开关TOS_CFG_MSG_EN:
#define TOS_CFG_MSG_EN 1u
3、编写main.c示例代码:
#include "tos.h" #include "mcu_init.h" #define STK_SIZE_TASK_RECEIVER 512 #define STK_SIZE_TASK_SENDER 512 #define PRIO_TASK_RECEIVER_HIGHER_PRIO 4 #define PRIO_TASK_RECEIVER_LOWER_PRIO (PRIO_TASK_RECEIVER_HIGHER_PRIO + 1) k_stack_t stack_task_receiver_higher_prio[STK_SIZE_TASK_RECEIVER]; k_stack_t stack_task_receiver_lower_prio[STK_SIZE_TASK_RECEIVER]; k_stack_t stack_task_sender[STK_SIZE_TASK_SENDER]; k_task_t task_receiver_higher_prio; k_task_t task_receiver_lower_prio; k_task_t task_sender; k_queue_t queue; extern void entry_task_receiver_higher_prio(void *arg); extern void entry_task_receiver_lower_prio(void *arg); extern void entry_task_sender(void *arg); void entry_task_receiver_higher_prio(void *arg) { k_err_t err; void *msg_received; size_t msg_size; while (K_TRUE) { err = tos_queue_pend(&queue, &msg_received, &msg_size, TOS_TIME_FOREVER); if (err == K_ERR_NONE) { printf("entry_task_receiver_higher_prio:\n"); printf("message body: %s\n", (char *)msg_received); printf("message size: %d\n", msg_size); } } } void entry_task_receiver_lower_prio(void *arg) { k_err_t err; void *msg_received; size_t msg_size; while (K_TRUE) { err = tos_queue_pend(&queue, &msg_received, &msg_size, TOS_TIME_FOREVER); if (err == K_ERR_NONE) { printf("entry_task_receiver_lower_prio:\n"); printf("message body: %s\n", (char *)msg_received); printf("message size: %d\n", msg_size); } } } void entry_task_sender(void *arg) { int i = 1; char *msg_to_one_receiver = "message for one receiver[with highest priority]"; char *msg_to_all_receiver = "message for all receivers"; // 此任务不断通过队列queue发送消息 while (K_TRUE) { if (i == 2) { printf("entry_task_sender:\n"); printf("send a message to one receiver, and shoud be the highest priority one\n"); // 发送消息并唤醒一个等待任务,唤醒的应该是等待任务中优先级最高的 tos_queue_post(&queue, msg_to_one_receiver, strlen(msg_to_one_receiver)); } if (i == 3) { printf("entry_task_sender:\n"); printf("send a message to all recevier\n"); // 发送消息并唤醒所有正在等待的任务 tos_queue_post_all(&queue, msg_to_all_receiver, strlen(msg_to_all_receiver)); } if (i == 4) { printf("entry_task_sender:\n"); printf("send a message to one receiver, and shoud be the highest priority one\n"); // 发送消息并唤醒一个等待任务,唤醒的应该是等待任务中优先级最高的 tos_queue_post(&queue, msg_to_one_receiver, strlen(msg_to_one_receiver)); } if (i == 5) { printf("entry_task_sender:\n"); printf("send a message to all recevier\n"); // 发送消息并唤醒所有正在等待的任务 tos_queue_post_all(&queue, msg_to_all_receiver, strlen(msg_to_all_receiver)); } tos_task_delay(1000); ++i; } } int main(void) { board_init(); tos_knl_init(); tos_queue_create(&queue); // task_receiver_higher_prio任务的优先级较高 (void)tos_task_create(&task_receiver_higher_prio, "receiver_higher_prio", entry_task_receiver_higher_prio, NULL, PRIO_TASK_RECEIVER_HIGHER_PRIO, stack_task_receiver_higher_prio, STK_SIZE_TASK_RECEIVER, 0); // task_receiver_lower_prio任务的优先级较低 (void)tos_task_create(&task_receiver_lower_prio, "receiver_lower_prio", entry_task_receiver_lower_prio, NULL, PRIO_TASK_RECEIVER_LOWER_PRIO, stack_task_receiver_lower_prio, STK_SIZE_TASK_RECEIVER, 0); (void)tos_task_create(&task_sender, "sender", entry_task_sender, NULL, 4, stack_task_sender, STK_SIZE_TASK_SENDER, 0); tos_knl_start(); }
运行效果
entry_task_trigger: send a message to one receiver, and shoud be the highest priority one entry_task_receiver_higher_prio: message body: message for one receiver[with highest priority] message size: 47 entry_task_trigger: send a message to all recevier entry_task_receiver_higher_prio: message body: message for all receivers message size: 25 entry_task_receiver_lower_prio: message body: message for all receivers message size: 25 entry_task_trigger: send a message to one receiver, and shoud be the highest priority one entry_task_receiver_higher_prio: message body: message for one receiver[with highest priority] message size: 47 entry_task_trigger: send a message to all recevier entry_task_receiver_higher_prio: message body: message for all receivers message size: 25 entry_task_receiver_lower_prio: message body: message for all receivers message size: 25
[实例代码](./code/2.3.4 queue/main.c)