1 /*
2  * Copyright (C) 2015-2021 Alibaba Group Holding Limited
3  */
4 
5 #include <stdio.h>
6 #include <time.h>
7 
8 #include <k_api.h>
9 #include <aos/errno.h>
10 #include <aos/kernel.h>
11 
12 #include "rhino_p.h"
13 
14 #if (RHINO_CONFIG_KOBJ_DYN_ALLOC == 0)
15 #warning "RHINO_CONFIG_KOBJ_DYN_ALLOC is disabled!"
16 #endif
17 
18 #if (RHINO_CONFIG_WORKQUEUE > 0)
19 
aos_workqueue_create(aos_workqueue_t * workqueue,const char * name,int32_t prio,void * stack,size_t stack_size)20 aos_status_t aos_workqueue_create(aos_workqueue_t *workqueue, const char *name, int32_t prio, void *stack, size_t stack_size)
21 {
22     aos_status_t ret;
23 
24     cpu_stack_t *stk = NULL;
25     kworkqueue_t *wq;
26 
27     if (workqueue == NULL) {
28         return -EINVAL;
29     }
30 
31     if (stack_size < sizeof(cpu_stack_t)) {
32         return -EINVAL;
33     }
34 
35     wq = aos_malloc(sizeof(kworkqueue_t));
36     if (wq == NULL) {
37         return -ENOMEM;
38     }
39 
40     if (NULL == stack) {
41         stk = aos_malloc(stack_size);
42         if (stk == NULL) {
43             if (wq != NULL) {
44                 aos_free(wq);
45             }
46             return -ENOMEM;
47         }
48         workqueue->stk = stk;
49     } else {
50         workqueue->stk = NULL;
51         stk = stack;
52     }
53 
54     ret = krhino_workqueue_create(wq, name, prio, stk,
55                                   stack_size / sizeof(cpu_stack_t));
56     if (ret != RHINO_SUCCESS) {
57         aos_free(wq);
58         if (workqueue->stk != NULL) {
59             aos_free(workqueue->stk);
60         }
61         return ret;
62     }
63 
64     workqueue->hdl = wq;
65 
66     return 0;
67 }
68 
aos_workqueue_del(aos_workqueue_t * workqueue)69 void aos_workqueue_del(aos_workqueue_t *workqueue)
70 {
71     if (!workqueue || !(workqueue->hdl)) {
72         return;
73     }
74 
75     krhino_workqueue_del(workqueue->hdl);
76 
77     aos_free(workqueue->hdl);
78 
79     if (NULL != workqueue->stk) {
80         aos_free(workqueue->stk);
81     }
82 }
83 
aos_work_init(aos_work_t * work,void (* fn)(void *),void * arg,int dly)84 aos_status_t aos_work_init(aos_work_t *work, void (*fn)(void *), void *arg, int dly)
85 {
86     aos_status_t  ret;
87     kwork_t *w;
88 
89     if (work == NULL) {
90         return -EINVAL;
91     }
92 
93     w = aos_malloc(sizeof(kwork_t));
94     if (w == NULL) {
95         return -ENOMEM;
96     }
97 
98     ret = krhino_work_init(w, fn, arg, MS2TICK(dly));
99     if (ret != RHINO_SUCCESS) {
100         aos_free(w);
101         return ret;
102     }
103 
104     *work = w;
105 
106     return 0;
107 }
108 
aos_work_destroy(aos_work_t * work)109 void aos_work_destroy(aos_work_t *work)
110 {
111     kwork_t *w;
112 
113     if (work == NULL) {
114         return;
115     }
116 
117     w = *work;
118 
119     if (w->timer != NULL) {
120         krhino_timer_stop(w->timer);
121         krhino_timer_dyn_del(w->timer);
122     }
123 
124     aos_free(*work);
125     *work = NULL;
126 }
127 
aos_work_run(aos_workqueue_t * workqueue,aos_work_t * work)128 aos_status_t aos_work_run(aos_workqueue_t *workqueue, aos_work_t *work)
129 {
130     aos_status_t ret;
131 
132     if ((workqueue == NULL) || (work == NULL)) {
133         return -EINVAL;
134     }
135 
136     ret = krhino_work_run(workqueue->hdl, *work);
137     if (ret == RHINO_SUCCESS) {
138         return 0;
139     }
140 
141     return ret;
142 }
143 
aos_work_sched(aos_work_t * work)144 aos_status_t aos_work_sched(aos_work_t *work)
145 {
146     aos_status_t ret;
147 
148     if (work == NULL) {
149         return -EINVAL;
150     }
151 
152     ret = krhino_work_sched(*work);
153     if (ret == RHINO_SUCCESS) {
154         return 0;
155     }
156 
157     return ret;
158 }
159 
aos_work_cancel(aos_work_t * work)160 aos_status_t aos_work_cancel(aos_work_t *work)
161 {
162     aos_status_t ret;
163 
164     if (work == NULL) {
165         return -EINVAL;
166     }
167 
168     ret = krhino_work_cancel(*work);
169     if (ret != RHINO_SUCCESS) {
170         return -EBUSY;
171     }
172 
173     return 0;
174 }
175 #endif
176 
177 
178 
179