1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 */
4
5 #include <aos/kernel.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #if AOS_COMP_CLI
11 #include "aos/cli.h"
12 #endif
13
14 /**
15 * 该示例用工作队列实现不同场景的任务执行。
16 * 示例说明如下:
17 * 1.
18 * t0时刻,任务T调用aos_sem_create()创建一个信号量,调用aos_workqueue_create()创建一工作队列wq1。
19 * 任务T然后调用aos_work_init()创建7个任务:work0、work1、work2、work3、work4、work5、work6,延迟参数分别为0、0、1、20、18、40、50。
20 * 2. t1时刻,任务T调用aos_work_run()依次:
21 * a、将work0加入工作队列wq1
22 * b、将work1加入工作队列wq1
23 * c、将work1再次工作队列wq1
24 * d、将work2加入工作队列wq1
25 * e、将work2加入工作队列wq1
26 * f、将work3加入工作队列wq1
27 * g、将work4加入系统默认工作队列
28 * h、将work5加入系统默认工作队列
29 * i、将work6加入系统默认工作队列
30 * 3. t2时刻,任务T调用aos_sem_wait()等待信号量进入阻塞状态,
31 * 4.
32 * t3时刻,work4被执行释放信号量,任务T解除阻塞,将work5再加入系统默认工作队列后因等待信号量进入阻塞状态
33 * 5. t4时刻,work5被执行释放信号量,任务T解除阻塞,释放资源。
34 */
35
36 /* module name used by log print */
37 #define MODULE_NAME "aos_workqueue_example"
38
39 #define RHINO_CONFIG_WORKQUEUE 1
40
41 #if (RHINO_CONFIG_WORKQUEUE > 0)
42
43 #define WORK_STACK_BUF 1024
44
45 #define TASK_WORKQUEUE_PRI 16
46
47 static size_t stack1_buf[WORK_STACK_BUF];
48
49 static aos_workqueue_t wq1;
50
51 static aos_work_t work0;
52 static aos_work_t work1;
53 static aos_work_t work2;
54 static aos_work_t work3;
55 static aos_work_t work4;
56 static aos_work_t work5;
57 static aos_work_t work6;
58
59 static aos_sem_t g_wq_test_sem;
60
work0_func(void * arg)61 static void work0_func(void *arg) { printf("--%s--\n", (char *)arg); }
62
work1_func(void * arg)63 static void work1_func(void *arg) { printf("--%s--\n", (char *)arg); }
64
work2_func(void * arg)65 static void work2_func(void *arg) { printf("--%s--\n", (char *)arg); }
66
work3_func(void * arg)67 static void work3_func(void *arg) { printf("--%s--\n", (char *)arg); }
68
work4_func(void * arg)69 static void work4_func(void *arg)
70 {
71 printf("--%s--\n", (char *)arg);
72 aos_sem_signal(&g_wq_test_sem);
73 }
74
work5_func(void * arg)75 static void work5_func(void *arg)
76 {
77 printf("--%s--\n", (char *)arg);
78 aos_sem_signal(&g_wq_test_sem);
79 }
80
work6_func(void * arg)81 static void work6_func(void *arg) { printf("--%s--\n", (char *)arg); }
82
aos_workqueue_example(int argc,char ** argv)83 static void aos_workqueue_example(int argc, char **argv)
84 {
85 aos_status_t ret;
86
87 size_t stack1_size = WORK_STACK_BUF;
88
89 aos_sem_create(&g_wq_test_sem, 0, 0);
90
91 /* creat workqueues */
92
93 ret = aos_workqueue_create(&wq1, "WORKQUEUE1-TEST", TASK_WORKQUEUE_PRI,
94 stack1_buf, stack1_size);
95 if (ret != 0) {
96 aos_sem_signal(&g_wq_test_sem);
97 return;
98 }
99
100 /* init works */
101 ret = aos_work_init(&work0, work0_func, "WORK 0", 0);
102 if (ret != 0) {
103 aos_sem_signal(&g_wq_test_sem);
104 return;
105 }
106
107 ret = aos_work_init(&work1, work1_func, "WORK 1", 0);
108 if (ret != 0) {
109 aos_sem_signal(&g_wq_test_sem);
110 return;
111 }
112
113 ret = aos_work_init(&work2, work2_func, "WORK 2", 1);
114 if (ret != 0) {
115 aos_sem_signal(&g_wq_test_sem);
116 return;
117 }
118
119 ret = aos_work_init(&work3, work3_func, "WORK 3", 20);
120 if (ret != 0) {
121 aos_sem_signal(&g_wq_test_sem);
122 return;
123 }
124
125 ret = aos_work_init(&work4, work4_func, "WORK 4", 18);
126 if (ret != 0) {
127 aos_sem_signal(&g_wq_test_sem);
128 return;
129 }
130
131 ret = aos_work_init(&work5, work5_func, "WORK 5", 40);
132 if (ret != 0) {
133 aos_sem_signal(&g_wq_test_sem);
134 return;
135 }
136
137 ret = aos_work_init(&work6, work6_func, "WORK 6", 50);
138 if (ret != 0) {
139 aos_sem_signal(&g_wq_test_sem);
140 return;
141 }
142
143 /* work run */
144 aos_work_run(&wq1, &work0);
145 aos_work_run(&wq1, &work1);
146 aos_work_run(&wq1, &work1);
147
148 aos_work_run(&wq1, &work2);
149 aos_work_run(&wq1, &work2);
150 aos_work_run(&wq1, &work3);
151
152 aos_work_sched(&work4);
153 aos_work_sched(&work5);
154 aos_work_sched(&work6);
155
156 /* wait for task4 */
157 aos_sem_wait(&g_wq_test_sem, AOS_WAIT_FOREVER);
158
159 aos_work_sched(&work5);
160
161 /* wait for task6 */
162 aos_sem_wait(&g_wq_test_sem, AOS_WAIT_FOREVER);
163
164 aos_work_cancel(&work0);
165 aos_work_cancel(&work1);
166 aos_work_cancel(&work2);
167 aos_work_cancel(&work3);
168 aos_work_cancel(&work4);
169 aos_work_cancel(&work5);
170 aos_work_cancel(&work6);
171
172 aos_work_destroy(&work0);
173 aos_work_destroy(&work1);
174 aos_work_destroy(&work2);
175 aos_work_destroy(&work3);
176 aos_work_destroy(&work4);
177 aos_work_destroy(&work5);
178 aos_work_destroy(&work6);
179
180 aos_workqueue_del(&wq1);
181
182 aos_sem_free(&g_wq_test_sem);
183
184 return;
185 }
186
187 #endif
188
189 #if AOS_COMP_CLI
190 /* reg args: fun, cmd, description*/
191 ALIOS_CLI_CMD_REGISTER(aos_workqueue_example, workqueue_example,
192 aos workqueue example)
193 #endif
194