1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #ifndef K_WORKQUEUE_H
6 #define K_WORKQUEUE_H
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 /** @addtogroup aos_rhino workqueue
13  *  workqueue
14  *
15  *  @{
16  */
17 
18 #if (RHINO_CONFIG_WORKQUEUE > 0)
19 #define WORKQUEUE_WORK_MAX 32
20 
21 typedef void (*work_handle_t)(void *arg);
22 
23 typedef struct {
24     klist_t        work_node;
25     work_handle_t  handle;
26     void          *arg;
27     tick_t         dly;
28     ktimer_t      *timer;
29     void          *wq;
30     uint8_t        work_exit;
31 } kwork_t;
32 
33 typedef struct {
34     klist_t       workqueue_node;
35     klist_t       work_list;
36     kwork_t      *work_current; /* current work */
37     const name_t *name;
38     ktask_t       worker;
39     ksem_t        sem;
40 } kworkqueue_t;
41 
42 /**
43  * Creat a workqueue.
44  *
45  * @param[in]  workqueue   the workqueue to be created
46  * @param[in]  name        the name of workqueue/worker, which should be unique
47  * @param[in]  pri         the priority of the worker
48  * @param[in]  stack_buf   the stack of the worker(task)
49  * @param[in]  stack_size  the size of the worker-stack
50  *
51  * @return  the operation status, RHINO_SUCCESS is OK, others is error
52  */
53 kstat_t krhino_workqueue_create(kworkqueue_t *workqueue, const name_t *name,
54                                 uint8_t pri, cpu_stack_t *stack_buf, size_t stack_size);
55 
56 /**
57  * This function will delete a workqueue
58  * @param[in]  workqueue  the workqueue to be deleted
59  * @return  the operation status, RHINO_SUCCESS is OK, others is error
60  */
61 kstat_t krhino_workqueue_del(kworkqueue_t *workqueue);
62 
63 /**
64  * Initialize a work.
65  *
66  * @param[in]  work    the work to be initialized
67  * @param[in]  handle  the call back function to run
68  * @param[in]  arg     the paraments of the function
69  * @param[in]  dly     the ticks to delay before run
70  *
71  * @return  the operation status, RHINO_SUCCESS is OK, others is error
72  */
73 kstat_t krhino_work_init(kwork_t *work, work_handle_t handle, void *arg, tick_t dly);
74 
75 /**
76  * Run a work on a workqueue.
77  *
78  * @param[in]  workqueue  the workqueue to run work
79  * @param[in]  work       the work to run
80  *
81  * @return  the operation status, RHINO_SUCCESS is OK, others is error
82  */
83 kstat_t krhino_work_run(kworkqueue_t *workqueue, kwork_t *work);
84 
85 /**
86  * Run a work on the default workqueue.
87  *
88  * @param[in]  work  the work to run
89  *
90  * @return  the operation status, RHINO_SUCCESS is OK, others is error
91  */
92 kstat_t krhino_work_sched(kwork_t *work);
93 
94 /**
95  * Cancel a work.
96  *
97  * @param[in]  work  the work to cancel
98  *
99  * @return  the operation status, RHINO_SUCCESS is OK, others is error
100  */
101 kstat_t krhino_work_cancel(kwork_t *work);
102 #endif
103 
104 /** @} */
105 
106 #ifdef __cplusplus
107 }
108 #endif
109 
110 #endif /* K_WORKQUEUE_H */
111 
112