
1. remove the old msg queue and queue: i. msg queue is not a common and reusable/flexible component(need user to config the msg pool size and this componet can only be used by tos_queue) ii. tos_queue can only deliver the pointer message(cannot do a memory buffer deliver) 2. add ring queue(tos_ring_q) componet rinq queue can be reused by tos_chr_fifi/tos_msg_q/tos_mail_q as the foundational data container 3. add message queue(tos_msg_q) a little like the old queue mechanism, supply the capability to deliver a pointer message 4. add mail queue(tos_mail_q) supply the capability to deliver a memory buffer 5. add binary heap(tos_bin_heap) the basement componet to implement priority queue 6. add priority queue(tos_prio_q) can be reused by the priority message/mail queue as the foundational data container. 7. add priority message queue(tos_prio_msg_q) a message(pointer) deliver mechanism, supply the capability of delivering the message with priority(message with higher priority comes faster to the pender than with lower) 8. add priority mail queue(tos_prio_mail_q) a mail(memory buffer) deliver mechanism, supply the capability of delivering the mail with priority(mail with higher priority comes faster to the pender than with lower)
214 lines
5.2 KiB
C
214 lines
5.2 KiB
C
/*----------------------------------------------------------------------------
|
|
* Tencent is pleased to support the open source community by making TencentOS
|
|
* available.
|
|
*
|
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
|
* If you have downloaded a copy of the TencentOS binary from Tencent, please
|
|
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
|
|
*
|
|
* If you have downloaded a copy of the TencentOS source code from Tencent,
|
|
* please note that TencentOS source code is licensed under the BSD 3-Clause
|
|
* License, except for the third-party components listed below which are
|
|
* subject to different license terms. Your integration of TencentOS into your
|
|
* own projects may require compliance with the BSD 3-Clause License, as well
|
|
* as the other licenses applicable to the third-party components included
|
|
* within TencentOS.
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
#include "tos.h"
|
|
|
|
__STATIC_INLINE__ void ring_q_item_copy_to(k_ring_q_t *ring_q, void *item_out, size_t *item_size)
|
|
{
|
|
memcpy(item_out, RING_HEAD_ITEM(ring_q), ring_q->item_size);
|
|
if (item_size) {
|
|
*item_size = ring_q->item_size;
|
|
}
|
|
}
|
|
|
|
__STATIC_INLINE__ void ring_q_item_copy_from(k_ring_q_t *ring_q, void *item_in)
|
|
{
|
|
memcpy(RING_TAIL_ITEM(ring_q), item_in, ring_q->item_size);
|
|
}
|
|
|
|
__STATIC_INLINE__ void ring_q_item_increase(k_ring_q_t *ring_q)
|
|
{
|
|
ring_q->tail = RING_NEXT(ring_q, ring_q->tail);
|
|
++ring_q->total;
|
|
}
|
|
|
|
__STATIC_INLINE__ void ring_q_item_decrease(k_ring_q_t *ring_q)
|
|
{
|
|
ring_q->head = RING_NEXT(ring_q, ring_q->head);
|
|
--ring_q->total;
|
|
}
|
|
|
|
__API__ k_err_t tos_ring_q_create(k_ring_q_t *ring_q, void *pool, size_t item_cnt, size_t item_size)
|
|
{
|
|
TOS_PTR_SANITY_CHECK(ring_q);
|
|
TOS_PTR_SANITY_CHECK(pool);
|
|
|
|
#if TOS_CFG_OBJECT_VERIFY_EN > 0u
|
|
knl_object_init(&ring_q->knl_obj, KNL_OBJ_TYPE_RING_QUEUE);
|
|
#endif
|
|
|
|
ring_q->head = 0u;
|
|
ring_q->tail = 0u;
|
|
ring_q->total = 0;
|
|
ring_q->pool = (uint8_t *)pool;
|
|
ring_q->item_size = item_size;
|
|
ring_q->item_cnt = item_cnt;
|
|
|
|
return K_ERR_NONE;
|
|
}
|
|
|
|
__API__ k_err_t tos_ring_q_destroy(k_ring_q_t *ring_q)
|
|
{
|
|
TOS_PTR_SANITY_CHECK(ring_q);
|
|
|
|
#if TOS_CFG_OBJECT_VERIFY_EN > 0u
|
|
if (!knl_object_verify(&ring_q->knl_obj, KNL_OBJ_TYPE_RING_QUEUE)) {
|
|
return K_ERR_OBJ_INVALID;
|
|
}
|
|
#endif
|
|
|
|
ring_q->head = 0u;
|
|
ring_q->tail = 0u;
|
|
ring_q->total = 0;
|
|
ring_q->pool = K_NULL;
|
|
ring_q->item_size = 0u;
|
|
ring_q->item_cnt = 0u;
|
|
|
|
#if TOS_CFG_OBJECT_VERIFY_EN > 0u
|
|
knl_object_deinit(&ring_q->knl_obj);
|
|
#endif
|
|
|
|
return K_ERR_NONE;
|
|
}
|
|
|
|
__API__ k_err_t tos_ring_q_enqueue(k_ring_q_t *ring_q, void *item, size_t item_size)
|
|
{
|
|
TOS_CPU_CPSR_ALLOC();
|
|
|
|
TOS_PTR_SANITY_CHECK(ring_q);
|
|
TOS_PTR_SANITY_CHECK(item);
|
|
|
|
#if TOS_CFG_OBJECT_VERIFY_EN > 0u
|
|
if (!knl_object_verify(&ring_q->knl_obj, KNL_OBJ_TYPE_RING_QUEUE)) {
|
|
return K_ERR_OBJ_INVALID;
|
|
}
|
|
#endif
|
|
|
|
if (item_size != ring_q->item_size) {
|
|
return K_ERR_RING_Q_ITEM_SIZE_NOT_MATCH;
|
|
}
|
|
|
|
TOS_CPU_INT_DISABLE();
|
|
|
|
if (tos_ring_q_is_full(ring_q)) {
|
|
TOS_CPU_INT_ENABLE();
|
|
return K_ERR_RING_Q_FULL;
|
|
}
|
|
|
|
ring_q_item_copy_from(ring_q, item);
|
|
ring_q_item_increase(ring_q);
|
|
|
|
TOS_CPU_INT_ENABLE();
|
|
return K_ERR_NONE;
|
|
}
|
|
|
|
__API__ k_err_t tos_ring_q_dequeue(k_ring_q_t *ring_q, void *item, size_t *item_size)
|
|
{
|
|
TOS_CPU_CPSR_ALLOC();
|
|
|
|
TOS_PTR_SANITY_CHECK(ring_q);
|
|
TOS_PTR_SANITY_CHECK(item);
|
|
|
|
#if TOS_CFG_OBJECT_VERIFY_EN > 0u
|
|
if (!knl_object_verify(&ring_q->knl_obj, KNL_OBJ_TYPE_RING_QUEUE)) {
|
|
return K_ERR_OBJ_INVALID;
|
|
}
|
|
#endif
|
|
|
|
TOS_CPU_INT_DISABLE();
|
|
|
|
if (tos_ring_q_is_empty(ring_q)) {
|
|
TOS_CPU_INT_ENABLE();
|
|
return K_ERR_RING_Q_EMPTY;
|
|
}
|
|
|
|
ring_q_item_copy_to(ring_q, item, item_size);
|
|
ring_q_item_decrease(ring_q);
|
|
|
|
TOS_CPU_INT_ENABLE();
|
|
|
|
return K_ERR_NONE;
|
|
}
|
|
|
|
__API__ k_err_t tos_ring_q_flush(k_ring_q_t *ring_q)
|
|
{
|
|
TOS_CPU_CPSR_ALLOC();
|
|
|
|
TOS_PTR_SANITY_CHECK(ring_q);
|
|
|
|
#if TOS_CFG_OBJECT_VERIFY_EN > 0u
|
|
if (!knl_object_verify(&ring_q->knl_obj, KNL_OBJ_TYPE_RING_QUEUE)) {
|
|
return K_ERR_OBJ_INVALID;
|
|
}
|
|
#endif
|
|
|
|
TOS_CPU_INT_DISABLE();
|
|
|
|
ring_q->head = 0u;
|
|
ring_q->tail = 0u;
|
|
ring_q->total = 0;
|
|
|
|
TOS_CPU_INT_ENABLE();
|
|
|
|
return K_ERR_NONE;
|
|
}
|
|
|
|
__API__ int tos_ring_q_is_empty(k_ring_q_t *ring_q)
|
|
{
|
|
TOS_CPU_CPSR_ALLOC();
|
|
int is_empty = 0;
|
|
|
|
if (!ring_q) {
|
|
return K_FALSE;
|
|
}
|
|
|
|
#if TOS_CFG_OBJECT_VERIFY_EN > 0u
|
|
if (!knl_object_verify(&ring_q->knl_obj, KNL_OBJ_TYPE_RING_QUEUE)) {
|
|
return K_FALSE;
|
|
}
|
|
#endif
|
|
|
|
TOS_CPU_INT_DISABLE();
|
|
is_empty = (ring_q->total == 0);
|
|
TOS_CPU_INT_ENABLE();
|
|
|
|
return is_empty;
|
|
}
|
|
|
|
__API__ int tos_ring_q_is_full(k_ring_q_t *ring_q)
|
|
{
|
|
TOS_CPU_CPSR_ALLOC();
|
|
int is_full = 0;
|
|
|
|
if (!ring_q) {
|
|
return K_FALSE;
|
|
}
|
|
|
|
#if TOS_CFG_OBJECT_VERIFY_EN > 0u
|
|
if (!knl_object_verify(&ring_q->knl_obj, KNL_OBJ_TYPE_RING_QUEUE)) {
|
|
return K_FALSE;
|
|
}
|
|
#endif
|
|
|
|
TOS_CPU_INT_DISABLE();
|
|
is_full = (ring_q->total == ring_q->item_cnt);
|
|
TOS_CPU_INT_ENABLE();
|
|
|
|
return is_full;
|
|
}
|
|
|