add ring queue/message queue/mail queue, binary heap/priority queue/priority message queue/priority mail queue

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)
This commit is contained in:
daishengdong
2019-10-28 15:50:46 +08:00
parent f35b725a59
commit d0b8d0675e
127 changed files with 26661 additions and 14013 deletions

View File

@@ -82,7 +82,7 @@ typedef enum at_channel_status_en {
typedef struct at_data_channel_st {
uint8_t is_free;
k_fifo_t rx_fifo;
k_chr_fifo_t rx_fifo;
uint8_t *rx_fifo_buffer;
at_channel_status_t status;
@@ -115,21 +115,21 @@ typedef struct at_agent_st {
at_data_channel_t data_channel[AT_DATA_CHANNEL_NUM];
at_event_t *event_table;
size_t event_table_size;
at_event_t *event_table;
size_t event_table_size;
at_echo_t *echo;
at_echo_t *echo;
at_cache_t recv_cache;
at_cache_t recv_cache;
at_timer_t timer;
at_timer_t timer;
char *cmd_buf;
char *cmd_buf;
hal_uart_t uart;
hal_uart_t uart;
k_fifo_t uart_rx_fifo;
uint8_t *uart_rx_fifo_buffer;
k_chr_fifo_t uart_rx_fifo;
uint8_t *uart_rx_fifo_buffer;
} at_agent_t;
#define AT_AGENT ((at_agent_t *)(&at_agent))

View File

@@ -8,7 +8,7 @@ __STATIC__ int at_uart_getchar(uint8_t *data, k_tick_t timeout)
at_delay(1);
err = tos_fifo_pop(&AT_AGENT->uart_rx_fifo, data);
err = tos_chr_fifo_pop(&AT_AGENT->uart_rx_fifo, data);
return err == K_ERR_NONE ? 0 : -1;
}
@@ -315,7 +315,7 @@ __API__ int tos_at_channel_read(int channel_id, uint8_t *buffer, size_t buffer_l
}
while (K_TRUE) {
read_len = tos_fifo_pop_stream(&data_channel->rx_fifo, buffer, buffer_len);
read_len = tos_chr_fifo_pop_stream(&data_channel->rx_fifo, buffer, buffer_len);
total_read_len += read_len;
if (total_read_len < buffer_len) {
@@ -347,7 +347,7 @@ __API__ int tos_at_channel_read_timed(int channel_id, uint8_t *buffer, size_t bu
return total_read_len;
}
read_len = tos_fifo_pop_stream(&data_channel->rx_fifo, buffer + read_len, buffer_len - total_read_len);
read_len = tos_chr_fifo_pop_stream(&data_channel->rx_fifo, buffer + read_len, buffer_len - total_read_len);
total_read_len += read_len;
if (total_read_len < buffer_len) {
@@ -369,7 +369,7 @@ __API__ int tos_at_channel_write(int channel_id, uint8_t *buffer, size_t buffer_
return -1;
}
return tos_fifo_push_stream(&data_channel->rx_fifo, buffer, buffer_len);
return tos_chr_fifo_push_stream(&data_channel->rx_fifo, buffer, buffer_len);
}
__STATIC_INLINE__ int at_channel_construct(at_data_channel_t *data_channel, const char *ip, const char *port)
@@ -382,7 +382,7 @@ __STATIC_INLINE__ int at_channel_construct(at_data_channel_t *data_channel, cons
}
data_channel->rx_fifo_buffer = fifo_buffer;
tos_fifo_create(&data_channel->rx_fifo, fifo_buffer, AT_DATA_CHANNEL_FIFO_BUFFER_SIZE);
tos_chr_fifo_create(&data_channel->rx_fifo, fifo_buffer, AT_DATA_CHANNEL_FIFO_BUFFER_SIZE);
data_channel->remote_ip = ip;
data_channel->remote_port = port;
@@ -441,7 +441,7 @@ __API__ int tos_at_channel_free(int channel_id)
}
tos_mmheap_free(data_channel->rx_fifo_buffer);
tos_fifo_destroy(&data_channel->rx_fifo);
tos_chr_fifo_destroy(&data_channel->rx_fifo);
memset(data_channel, 0, sizeof(at_data_channel_t));
@@ -542,7 +542,7 @@ __API__ int tos_at_init(hal_uart_port_t uart_port, evtdrv_task_id_t at_task_id,
}
AT_AGENT->uart_rx_fifo_buffer = (uint8_t *)buffer;
tos_fifo_create(&AT_AGENT->uart_rx_fifo, (uint8_t *)buffer, AT_UART_RX_FIFO_BUFFER_SIZE);
tos_chr_fifo_create(&AT_AGENT->uart_rx_fifo, (uint8_t *)buffer, AT_UART_RX_FIFO_BUFFER_SIZE);
buffer = tos_mmheap_alloc(AT_CMD_BUFFER_SIZE);
if (!buffer) {
@@ -570,7 +570,7 @@ errout1:
errout0:
tos_mmheap_free(AT_AGENT->uart_rx_fifo_buffer);
AT_AGENT->uart_rx_fifo_buffer = K_NULL;
tos_fifo_destroy(&AT_AGENT->uart_rx_fifo);
tos_chr_fifo_destroy(&AT_AGENT->uart_rx_fifo);
return -1;
}
@@ -587,7 +587,7 @@ __API__ void tos_at_deinit(void)
tos_mmheap_free(AT_AGENT->uart_rx_fifo_buffer);
AT_AGENT->uart_rx_fifo_buffer = K_NULL;
tos_fifo_destroy(&AT_AGENT->uart_rx_fifo);
tos_chr_fifo_destroy(&AT_AGENT->uart_rx_fifo);
at_channel_deinit();
}
@@ -596,9 +596,9 @@ __API__ void tos_at_deinit(void)
hal(driver framework), that would be a huge work, we place it in future plans. */
__API__ void tos_at_uart_write_byte(uint8_t data)
{
if (tos_fifo_push(&AT_AGENT->uart_rx_fifo, data) == K_ERR_NONE) {
if (tos_chr_fifo_push(&AT_AGENT->uart_rx_fifo, data) == K_ERR_NONE) {
tos_evtdrv_event_set(AT_AGENT->at_task_id, EVENT_AT_UART_INCOMING);
}
}
}
__STATIC__ void at_echo_event_emit(at_echo_t *echo)

View File

@@ -8,34 +8,222 @@ osThreadDef(task1, osPriorityNormal, 1, TASK1_STK_SIZE);
void task2(void *arg);
osThreadDef(task2, osPriorityNormal, 1, TASK2_STK_SIZE);
k_ring_q_t ring_q;
k_chr_fifo_t chr_fifo;
k_msg_q_t msg_q;
k_mail_q_t mail_q;
k_bin_heap_t bin_heap;
k_prio_q_t prio_q;
typedef struct fuck {
int a;
char *name;
} fuck_t;
char ring_q_pool[sizeof(fuck_t) * 4];
uint8_t chr_fifo_pool[sizeof(uint8_t) * 4];
uint8_t msg_q_pool[sizeof(void *) * 4];
uint8_t mail_q_pool[sizeof(fuck_t) * 4];
int int_pool[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int int_seq[] = { 0, 1, 2, 3, 4, 8, 9, 3, 5 };
fuck_t fuck_pool[9];
fuck_t fuck_seq[9] = {
{ 0, "000" },
{ 1, "111" },
{ 2, "222" },
{ 3, "333" },
{ 4, "444" },
{ 8, "888" },
{ 9, "999" },
{ 3, "333" },
{ 5, "555" },
};
uint8_t mgr_entry_array[TOS_PRIO_Q_ARRAY_SIZE(3)];
int int_cmp(void *first, void *second)
{
int f = *(int *)first;
int s = *(int *)second;
if (f > s) {
return 1;
}
return 0;
}
int fuck_cmp(void *first, void *second)
{
fuck_t *f = (fuck_t *)first;
fuck_t *s = (fuck_t *)second;
if (f->a > s->a) {
return 1;
}
return 0;
}
int fuck_int(void)
{
int i;
for (i = 0; i < bin_heap.total; ++i) {
printf("%d\n", int_pool[i]);
}
}
int fuck_fuck(void)
{
int i;
for (i = 0; i < bin_heap.total; ++i) {
printf("%d %s\n", fuck_pool[i].a, fuck_pool[i].name);
}
}
int fuck(void)
{
fuck_fuck();
}
void task1(void *arg)
{
size_t item_size;
int fuckx = 19;
k_err_t err;
int i = 0;
fuck_t dummy = {
10, "dummy"
};
#if 0
for (i = 0; i < sizeof(int_seq) / sizeof(int); ++i) {
err = tos_bin_heap_push(&bin_heap, &int_seq[i]);
printf("%d %d\n", int_seq[i], err);
}
err = tos_bin_heap_push(&bin_heap, &fuckx);
printf("%d %d\n", fuckx, err);
printf("=============\n\n");
fuck();
while (!tos_bin_heap_is_empty(&bin_heap)) {
err = tos_bin_heap_pop(&bin_heap, &fuckx, &item_size);
printf("!!!!!! %d %d %d\n", err, fuckx, item_size);
}
#elif 0
for (i = 0; i < 9; ++i) {
err = tos_bin_heap_push(&bin_heap, &fuck_seq[i]);
printf("%d %s %d\n", fuck_seq[i].a, fuck_seq[i].name, err);
}
err = tos_bin_heap_push(&bin_heap, &dummy);
printf("%d %d\n", dummy.a, err);
printf("=============\n\n");
fuck();
while (!tos_bin_heap_is_empty(&bin_heap)) {
err = tos_bin_heap_pop(&bin_heap, &dummy, &item_size);
printf("!!!!!! %d %d %s %d\n", err, dummy.a, dummy.name, item_size);
}
#else
for (i = 0; i < 9; ++i) {
err = tos_prio_q_enqueue(&prio_q, &fuck_seq[i], sizeof(fuck_t), fuck_seq[i].a);
printf("%d %s %d\n", fuck_seq[i].a, fuck_seq[i].name, err);
}
err = tos_prio_q_enqueue(&prio_q, &dummy, sizeof(fuck_t), dummy.a);
printf("%d %d\n", dummy.a, err);
printf("=============\n\n");
// fuck();
while (!tos_prio_q_is_empty(&prio_q)) {
err = tos_prio_q_dequeue(&prio_q, &dummy, &item_size);
printf("!!!!!! %d %d %s %d\n", err, dummy.a, dummy.name, item_size);
}
#endif
return;
fuck_t fuck1 = { 1, "111" };
fuck_t fuck2 = { 2, "222" };
fuck_t fuck3 = { 3, "333" };
fuck_t fuck4 = { 4, "444" };
fuck_t fuck5 = { 5, "555" };
int count = 1;
while (1) {
printf("###This is task1, %d\r\n", count++);
osDelay(2000);
osDelay(1000);
if (count == 2) {
err = tos_mail_q_post(&mail_q, &fuck1, sizeof(fuck1));
printf("task1: %d\n", err);
} else if (count == 3) {
err = tos_mail_q_post(&mail_q, &fuck2, sizeof(fuck2));
printf("task1: %d\n", err);
} else if (count == 4) {
err = tos_mail_q_post(&mail_q, &fuck3, sizeof(fuck3));
printf("task1: %d\n", err);
} else if (count == 5) {
err = tos_mail_q_post(&mail_q, &fuck4, sizeof(fuck4));
printf("task1: %d\n", err);
}
}
}
void task2(void *arg)
{
return;
k_err_t err;
void *msg;
size_t msg_size;
int count = 1;
while (1) {
#if TOS_CFG_TASK_STACK_DRAUGHT_DEPTH_DETACT_EN > 0u
k_err_t rc;
int depth;
rc = tos_task_stack_draught_depth(K_NULL, &depth);
printf("%d %d\n", rc, depth);
#endif
printf("***This is task2, %d\r\n", count++);
osDelay(1000);
err = tos_mail_q_pend(&mail_q, &msg, &msg_size, TOS_TIME_FOREVER);
printf("task2: %d %d %x %d %s\n", err, msg_size, msg, ((fuck_t *)msg)->a, ((fuck_t *)msg)->name);
}
}
void application_entry(void *arg)
{
tos_prio_q_create(&prio_q, mgr_entry_array, fuck_pool, sizeof(fuck_t), sizeof(fuck_pool) / sizeof(fuck_t));
// tos_bin_heap_create(&bin_heap, int_pool, sizeof(int), sizeof(int_pool) / sizeof(int), int_cmp);
// tos_bin_heap_create(&bin_heap, fuck_pool, sizeof(fuck_t), sizeof(fuck_pool) / sizeof(fuck_t), fuck_cmp);
#if 0
tos_ring_q_create(&ring_q, ring_q_pool, sizeof(fuck_t), 4);
tos_chr_fifo_create(&chr_fifo, chr_fifo_pool, sizeof(chr_fifo_pool));
tos_msg_q_create(&msg_q, msg_q_pool, 4);
tos_mail_q_create(&mail_q, mail_q_pool, sizeof(fuck_t), 4);
#endif
osThreadCreate(osThread(task1), NULL); // Create task1
osThreadCreate(osThread(task2), NULL); // Create task2
}