• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * osal_workqueue.c
3  *
4  * osal driver
5  *
6  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
7  *
8  * This software is licensed under the terms of the GNU General Public
9  * License version 2, as published by the Free Software Foundation, and
10  * may be copied, distributed, and modified under those terms.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  */
18 
19 #include "hdf_workqueue.h"
20 #include <linux/workqueue.h>
21 #include "hdf_log.h"
22 #include "osal_mem.h"
23 
24 #define HDF_LOG_TAG hdf_workqueue
25 
26 struct WorkWrapper {
27 	struct delayed_work work;
28 	HdfWorkFunc workFunc;
29 	void *para;
30 };
31 
HdfWorkQueueInit(HdfWorkQueue * queue,char * name)32 int32_t HdfWorkQueueInit(HdfWorkQueue *queue, char *name)
33 {
34 	HDF_LOGD("%s  entry", __func__);
35 
36 	if (queue == NULL || name == NULL) {
37 		HDF_LOGE("%s invalid para", __func__);
38 		return HDF_ERR_INVALID_PARAM;
39 	}
40 
41 	queue->realWorkQueue = create_singlethread_workqueue(name);
42 	if (queue->realWorkQueue == NULL) {
43 		HDF_LOGE("%s create queue fail", __func__);
44 		return HDF_FAILURE;
45 	}
46 
47 	return HDF_SUCCESS;
48 }
49 EXPORT_SYMBOL(HdfWorkQueueInit);
50 
WorkEntry(struct work_struct * work)51 static void WorkEntry(struct work_struct *work)
52 {
53 	struct WorkWrapper *wrapper = NULL;
54 	if (work != NULL) {
55 		wrapper = (struct WorkWrapper *)work;
56 		if (wrapper->workFunc != NULL)
57 			wrapper->workFunc(wrapper->para);
58 		else
59 			HDF_LOGE("%s routine null", __func__);
60 	} else {
61 		HDF_LOGE("%s work null", __func__);
62 	}
63 }
64 
HdfWorkInit(HdfWork * work,HdfWorkFunc func,void * para)65 int32_t HdfWorkInit(HdfWork *work, HdfWorkFunc func, void *para)
66 {
67 	struct work_struct *realWork = NULL;
68 	struct WorkWrapper *wrapper = NULL;
69 
70 	if (work == NULL || func == NULL) {
71 		HDF_LOGE("%s invalid para", __func__);
72 		return HDF_ERR_INVALID_PARAM;
73 	}
74 	work->realWork = NULL;
75 
76 	wrapper = (struct WorkWrapper *)OsalMemCalloc(sizeof(*wrapper));
77 	if (wrapper == NULL) {
78 		HDF_LOGE("%s malloc fail", __func__);
79 		return HDF_ERR_MALLOC_FAIL;
80 	}
81 	realWork = &(wrapper->work.work);
82 	wrapper->workFunc = func;
83 	wrapper->para = para;
84 
85 	INIT_WORK(realWork, WorkEntry);
86 	work->realWork = wrapper;
87 
88 	return HDF_SUCCESS;
89 }
90 EXPORT_SYMBOL(HdfWorkInit);
91 
HdfDelayedWorkInit(HdfWork * work,HdfWorkFunc func,void * para)92 int32_t HdfDelayedWorkInit(HdfWork *work, HdfWorkFunc func, void *para)
93 {
94 	struct delayed_work *realWork = NULL;
95 	struct WorkWrapper *wrapper = NULL;
96 
97 	if (work == NULL || func == NULL) {
98 		HDF_LOGE("%s invalid para", __func__);
99 		return HDF_ERR_INVALID_PARAM;
100 	}
101 
102 	work->realWork = NULL;
103 
104 	wrapper = (struct WorkWrapper *)OsalMemCalloc(sizeof(*wrapper));
105 	if (wrapper == NULL) {
106 		HDF_LOGE("%s malloc fail", __func__);
107 		return HDF_ERR_MALLOC_FAIL;
108 	}
109 	realWork = &(wrapper->work);
110 	wrapper->workFunc = func;
111 	wrapper->para = para;
112 
113 	INIT_DELAYED_WORK(realWork, WorkEntry);
114 	work->realWork = wrapper;
115 
116 	return HDF_SUCCESS;
117 }
118 EXPORT_SYMBOL(HdfDelayedWorkInit);
119 
HdfWorkDestroy(HdfWork * work)120 void HdfWorkDestroy(HdfWork *work)
121 {
122 	if (work == NULL || work->realWork == NULL) {
123 		HDF_LOGE("%s invalid para", __func__);
124 		return;
125 	}
126 
127 	OsalMemFree(work->realWork);
128 	work->realWork = NULL;
129 
130 	return;
131 }
132 EXPORT_SYMBOL(HdfWorkDestroy);
133 
HdfDelayedWorkDestroy(HdfWork * work)134 void HdfDelayedWorkDestroy(HdfWork *work)
135 {
136 	if (work == NULL || work->realWork == NULL) {
137 		HDF_LOGE("%s invalid para", __func__);
138 		return;
139 	}
140 
141 	return HdfWorkDestroy(work);
142 }
143 EXPORT_SYMBOL(HdfDelayedWorkDestroy);
144 
HdfWorkQueueDestroy(HdfWorkQueue * queue)145 void HdfWorkQueueDestroy(HdfWorkQueue *queue)
146 {
147 	if (queue == NULL || queue->realWorkQueue == NULL) {
148 		HDF_LOGE("%s invalid para", __func__);
149 		return;
150 	}
151 
152 	destroy_workqueue(queue->realWorkQueue);
153 
154 	return;
155 }
156 EXPORT_SYMBOL(HdfWorkQueueDestroy);
157 
HdfAddWork(HdfWorkQueue * queue,HdfWork * work)158 bool HdfAddWork(HdfWorkQueue *queue, HdfWork *work)
159 {
160 	if (queue == NULL || queue->realWorkQueue == NULL || work == NULL || work->realWork == NULL) {
161 		HDF_LOGE("%s invalid para", __func__);
162 		return false;
163 	}
164 
165 	return queue_work(queue->realWorkQueue, &((struct WorkWrapper *)work->realWork)->work.work);
166 }
167 EXPORT_SYMBOL(HdfAddWork);
168 
HdfAddDelayedWork(HdfWorkQueue * queue,HdfWork * work,unsigned long ms)169 bool HdfAddDelayedWork(HdfWorkQueue *queue, HdfWork *work, unsigned long ms)
170 {
171 	if (queue == NULL || queue->realWorkQueue == NULL || work == NULL || work->realWork == NULL) {
172 		HDF_LOGE("%s invalid para", __func__);
173 		return false;
174 	}
175 
176 	return queue_delayed_work(queue->realWorkQueue, &((struct WorkWrapper *)work->realWork)->work,
177 		msecs_to_jiffies(ms));
178 }
179 EXPORT_SYMBOL(HdfAddDelayedWork);
180 
HdfWorkBusy(HdfWork * work)181 unsigned int HdfWorkBusy(HdfWork *work)
182 {
183 	if (work == NULL || work->realWork == NULL) {
184 		HDF_LOGE("%s invalid para", __func__);
185 		return 0;
186 	}
187 
188 	return work_busy(&((struct WorkWrapper *)work->realWork)->work.work);
189 }
190 EXPORT_SYMBOL(HdfWorkBusy);
191 
HdfCancelWorkSync(HdfWork * work)192 bool HdfCancelWorkSync(HdfWork *work)
193 {
194 	if (work == NULL || work->realWork == NULL) {
195 		HDF_LOGE("%s invalid para", __func__);
196 		return false;
197 	}
198 
199 	return cancel_work_sync(&((struct WorkWrapper *)work->realWork)->work.work);
200 }
201 EXPORT_SYMBOL(HdfCancelWorkSync);
202 
HdfCancelDelayedWorkSync(HdfWork * work)203 bool HdfCancelDelayedWorkSync(HdfWork *work)
204 {
205 	if (work == NULL || work->realWork == NULL) {
206 		HDF_LOGE("%s invalid para", __func__);
207 		return false;
208 	}
209 
210 	return cancel_delayed_work_sync(&((struct WorkWrapper *)work->realWork)->work);
211 }
212 EXPORT_SYMBOL(HdfCancelDelayedWorkSync);
213 
214