1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #include "aos_cpp.h"
6 #include <stdio.h>
7 
8 using namespace AOS;
9 
10 #define STACK_BUF_BYTES 2048
11 #define BUF_QUEUE_BYTES 1024
12 static char myQueue_buff[BUF_QUEUE_BYTES];
13 
14 static thread *pa;
15 static thread *pb;
16 static Queue * myQueue;
17 
demo_task1(void * arg)18 static void demo_task1(void *arg)
19 {
20     int    count   = 0;
21     int    rx_data = 0;
22     size_t rx_size = 0;
23 
24     while (1) {
25         printf("demo_task1 count  %d\n", count++);
26 
27         myQueue->receive(&rx_data, &rx_size, 0xFFFFFFFF);
28 
29         if (rx_size == sizeof(int)) {
30             printf("demo_task2 received message: %d\n", rx_data);
31         }
32 
33         if (count == 10) {
34             pa->terminate();
35         }
36     };
37 }
38 
demo_task2(void * arg)39 static void demo_task2(void *arg)
40 {
41     int count = 0;
42 
43     while (1) {
44         printf("demo_task2 count  %d\n", count++);
45 
46         myQueue->send(&count, sizeof(int));
47         printf("demo_task2 send message : %d\n", count);
48 
49         if (count == 10) {
50             pb->terminate();
51         }
52 
53         pb->sleep((uint32_t)1000);
54     };
55 }
56 
test_queue(void)57 void test_queue(void)
58 {
59     pa      = new thread;
60     pb      = new thread;
61     myQueue = new Queue;
62 
63     myQueue->create("queue", myQueue_buff, BUF_QUEUE_BYTES, sizeof(int));
64 
65     pa->create("task", (void *)NULL, (uint8_t)20, (tick_t)50, STACK_BUF_BYTES,
66                demo_task1, (uint8_t)1);
67 
68     pb->create("task", (void *)NULL, (uint8_t)20, (tick_t)50, STACK_BUF_BYTES,
69                demo_task2, (uint8_t)1);
70 }
71