Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Description of TencentOS tiny message queue and introduction of related operations of message queue

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "the description of the TencentOS tiny message queue and the introduction of the related operation of the message queue". In the daily operation, I believe that many people have doubts about the description of the TencentOS tiny message queue and the introduction of the related operation of the message queue. The editor consulted all kinds of materials and sorted out the simple and useful operation methods. I hope it will be helpful for you to answer the doubts about "the description of TencentOS tiny message queue and the introduction of related operations of message queue". Next, please follow the editor to study!

Message queue

In fact, message queuing is a basic component of TencentOS tiny and serves as the underlying layer of the queue. So in tos_config.h, you define it with the following macros:

# if (TOS_CFG_QUEUE_EN > 0u) # define TOS_CFG_MSG_EN 1u#else#define TOS_CFG_MSG_EN 0u#endif system message pool initialization

When the system initializes (tos_knl_init ()), the system will initialize the message pool, where the msgpool_init () function is used to initialize the message pool. The definition of this function is located in the tos_msg.c file. The function is mainly implemented through a for loop to initialize the member variables of the message pool kaccounmsg _ pool [TOS _ CFG_MSG_POOL_SIZE] to initialize the corresponding list node. And mount it to the free message list k_msg_freelist initialization completion diagram: (assuming there are only 3 messages)

_ _ KERNEL__ void msgpool_init (void) {uint32_t I; for (I = 0; I

< TOS_CFG_MSG_POOL_SIZE; ++i) { tos_list_init(&k_msg_pool[i].list); tos_list_add(&k_msg_pool[i].list, &k_msg_freelist); }}__API__ k_err_t tos_knl_init(void){ ··· #if (TOS_CFG_MSG_EN) >

0 msgpool_init (); # endif} message queue creation

This function is called during queue creation, but it can also be provided to the user himself as a user API interface, not just the kernel API interface. The essence of this function is to initialize the message list queue_head in the message queue. Schematic diagram of initialization completion:

_ _ API__ k_err_t tos_msg_queue_create (k_msg_queue_t * msg_queue) {TOS_PTR_SANITY_CHECK (msg_queue); # if TOS_CFG_OBJECT_VERIFY_EN > 0u knl_object_init (& msg_queue- > knl_obj, KNL_OBJ_TYPE_MSG_QUEUE); # endif tos_list_init (& msg_queue- > queue_head); return message queue destruction

The tos_msg_queue_destroy () function is used to destroy a message queue. When the message queue is no longer in use, it can be destroyed. The essence of destruction is to clear the contents of the message queue control block. First, determine that the type of message queue control block is KNL_OBJ_TYPE_MSG_QUEUE. This function can only destroy the control block of the queue type. Then call the tos_msg_queue_flush () function to "empty" all the messages in the queue's message list, which means to release the message mounted on the queue back to the message pool (if there is a message in the message list in the message queue, use the msgpool_free () function to release the message). And the message list of the message queue is initialized by the tos_list_init () function, which ensures that the message queue has been destroyed, and the type attribute in the pend_obj member variable of the message queue control block is identified as KNL_OBJ_TYPE_NONE.

However, it should be noted that because the RAM of the queue control block is statically allocated by the compiler, even if the queue is destroyed, this memory cannot be freed.

_ _ API__ k_err_t tos_msg_queue_destroy (k_msg_queue_t * msg_queue) {TOS_PTR_SANITY_CHECK (msg_queue); # if TOS_CFG_OBJECT_VERIFY_EN > 0u if (! knl_object_verify (& msg_queue- > knl_obj, KNL_OBJ_TYPE_MSG_QUEUE)) {return KnowledgeOBJINVALID;} # endif tos_msg_queue_flush (msg_queue) Tos_list_init (& msg_queue- > queue_head); # if TOS_CFG_OBJECT_VERIFY_EN > 0u knl_object_deinit (& msg_queue- > knl_obj); # endif return KnowledgeNone;} _ API__ void tos_msg_queue_flush (k_msg_queue_t * msg_queue) {TOS_CPU_CPSR_ALLOC (); k_list_t * curr, * next; TOS_CPU_INT_DISABLE () TOS_LIST_FOR_EACH_SAFE (curr, next, & msg_queue- > queue_head) {msgpool_free (TOS_LIST_ENTRY (curr, k_msg_t, list));} TOS_CPU_INT_ENABLE ();} get messages from message queue

The tos_msg_queue_get () function is used to get a message from the message queue. The obtained message is returned through the msg_addr parameter, and the size of the obtained message is returned to the user through the msg_size parameter. If the message is successfully obtained, K_ERR_NONE is returned, otherwise the corresponding error code is returned. This function to get a message from the message queue will not cause blocking. If there is a message, it will succeed, otherwise it will fail. Its implementation process is as follows: when the TOS_CFG_OBJECT_VERIFY_EN macro definition is enabled, call the knl_object_verify () function to ensure that the message is obtained from the message queue, and then use TOS_LIST_FIRST_ENTRY_OR_NULL to determine whether there is a message in the message list of the message queue. If it does not exist, returning K_ERR_MSG_QUEUE_EMPTY means that the message queue is empty. It will be successful anyway. After success, you need to use the msgpool_free () function to release the message back to the message pool.

_ _ API__ k_err_t tos_msg_queue_get (k_msg_queue_t * msg_queue, void * * msg_addr, size_t * msg_size) {TOS_CPU_CPSR_ALLOC (); k_msg_t * msg;#if TOS_CFG_OBJECT_VERIFY_EN > 0u if (! knl_object_verify (& msg_queue- > knl_obj, KNL_OBJ_TYPE_MSG_QUEUE)) {return K_ERR_OBJ_INVALID } # endif TOS_CPU_INT_DISABLE (); msg = TOS_LIST_FIRST_ENTRY_OR_NULL (& msg_queue- > queue_head, k_msg_t, list); if (! msg) {TOS_CPU_INT_ENABLE (); return KnowledgeMSGroupQUEUEY;} * msg_addr = msg- > msg_addr; * msg_size = msg- > msg_size; msgpool_free (msg) TOS_CPU_INT_ENABLE (); return write message to message queue

When sending a message, TencentOS tiny will take an idle message from the message pool (idle message list) and mount it to the message list of the message queue. You can choose to mount to the end or header of the message list through the opt parameter. Therefore, the writing of the message queue supports FIFO and LIFO. Msg_queue is the message queue control block to write the message, and msg_addr and msg_size are the address and size of the message to be written.

The process of writing a message is very simple. It directly fetches an idle message from the message pool through the msgpool_alloc () function. If there is no idle message in the system, it directly returns the error code K_ERR_MSG_QUEUE_FULL indicating that the available messages have been used up. If the idle message is successfully fetched, the address and size of the message to be written are recorded in the msg_addr and msg_size member variables of the message pool, and then the message is mounted to the location of the message list (header or tail) through the opt parameter.

_ _ API__ k_err_t tos_msg_queue_put (k_msg_queue_t * msg_queue, void * msg_addr, size_t msg_size, k_opt_t opt) {TOS_CPU_CPSR_ALLOC (); k_msg_t * msg;#if TOS_CFG_OBJECT_VERIFY_EN > 0u if (! knl_object_verify (& msg_queue- > knl_obj, KNL_OBJ_TYPE_MSG_QUEUE)) {return K_ERR_OBJ_INVALID } # endif TOS_CPU_INT_DISABLE (); msg = msgpool_alloc (); if (! msg) {TOS_CPU_INT_ENABLE (); return Knowerr é MSGraph QUEUEFULL;} msg- > msg_addr = msg_addr; msg- > msg_size = msg_size If (opt & TOS_OPT_MSG_PUT_LIFO) {tos_list_add (& msg- > list, & msg_queue- > queue_head);} else {tos_list_add_tail (& msg- > list, & msg_queue- > queue_head);} TOS_CPU_INT_ENABLE (); return K_ERR_NONE } Experimental test code # include "stm32f10x.h" # include "bsp_usart.h" # include "tos.h" k_msg_queue_t test_msg_queue_00;k_task_t task1;k_task_t task2;k_stack_t task_stack1 [1024]; k_stack_t task_stack2 [1024]; void test_task1 (void * Parameter) {k_err_t err; int i = 0; int msg_received; size_t msg_size = 0 While (1) {printf ("queue pend\ r\ n"); for (I = 0; I

< 3; ++i) { err = tos_msg_queue_get(&test_msg_queue_00, (void **)&msg_received, &msg_size); if (err == K_ERR_NONE) printf("msg queue get is %d \r\n",msg_received); if (err == K_ERR_PEND_DESTROY) { printf("queue is destroy\r\n"); tos_task_delay(TOS_TIME_FOREVER - 1); } } tos_task_delay(1000); }}void test_task2(void *Parameter){ k_err_t err; int i = 0; uint32_t msgs[3] = { 1, 2, 3 }; printf("task2 running\r\n"); while(1) { for (i = 0; i < 3; ++i) { err = tos_msg_queue_put(&test_msg_queue_00, (void *)(msgs[i]), sizeof(uint32_t), TOS_OPT_MSG_PUT_FIFO); if (err != K_ERR_NONE) printf("msg queue put fail! code : %d \r\n",err); } tos_task_delay(1000); }}/** * @brief 主函数 * @param 无 * @retval 无 */int main(void){ k_err_t err; /*初始化USART 配置模式为 115200 8-N-1,中断接收*/ USART_Config(); printf("Welcome to TencentOS tiny\r\n"); tos_knl_init(); // TOS Tiny kernel initialize tos_robin_config(TOS_ROBIN_STATE_ENABLED, (k_timeslice_t)500u); printf("create test_queue_00\r\n"); err = tos_msg_queue_create(&test_msg_queue_00); if(err != K_ERR_NONE) printf("TencentOS Create test_msg_queue_00 fail! code : %d \r\n",err); printf("create task1\r\n"); err = tos_task_create(&task1, "task1", test_task1, NULL, 3, task_stack1, 1024, 20); if(err != K_ERR_NONE) printf("TencentOS Create task1 fail! code : %d \r\n",err); printf("create task2\r\n"); err = tos_task_create(&task2, "task2", test_task2, NULL, 4, task_stack2, 1024, 20); if(err != K_ERR_NONE) printf("TencentOS Create task2 fail! code : %d \r\n",err); tos_knl_start(); // Start TOS Tiny}现象

At this point, the study on "the description of TencentOS tiny message queue and the introduction of related operations of message queue" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report