1 /*
2 * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include "hi_osal.h"
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/printk.h>
23 #include <linux/kthread.h>
24 #include <linux/slab.h>
25 #include "securec.h"
26
osal_kthread_create(threadfn_t thread,void * data,const char * name)27 osal_task_t *osal_kthread_create(threadfn_t thread, void *data, const char *name)
28 {
29 struct task_struct *k = NULL;
30 osal_task_t *p = (osal_task_t *)kmalloc(sizeof(osal_task_t), GFP_KERNEL);
31 if (p == NULL) {
32 osal_trace("%s - kmalloc error!\n", __FUNCTION__);
33 return NULL;
34 }
35 (void)memset_s(p, sizeof(osal_task_t), 0, sizeof(osal_task_t));
36
37 k = kthread_run(thread, data, name);
38 if (IS_ERR(k)) {
39 osal_trace("%s - kthread create error!\n", __FUNCTION__);
40 kfree(p);
41 p = NULL;
42 return NULL;
43 }
44 p->task_struct = k;
45 return p;
46 }
47 EXPORT_SYMBOL(osal_kthread_create);
48
osal_kthread_destroy(osal_task_t * task,unsigned int stop_flag)49 void osal_kthread_destroy(osal_task_t *task, unsigned int stop_flag)
50 {
51 if (task == NULL) {
52 osal_trace("%s - parameter invalid!\n", __FUNCTION__);
53 return;
54 }
55 /* note: When you call the Kthread_stop function, the thread function cannot be finished, otherwise it will oops. */
56 if (stop_flag != 0) {
57 kthread_stop ((struct task_struct *)(task->task_struct));
58 }
59 task->task_struct = NULL;
60 kfree(task);
61 }
62 EXPORT_SYMBOL(osal_kthread_destroy);
63