1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "hdf_thread_ex.h"
10 #include "osal_mem.h"
11 #include "osal_thread.h"
12 
HdfThreadStart(struct HdfThread * thread)13 void HdfThreadStart(struct HdfThread *thread)
14 {
15     if (thread == NULL) {
16         return;
17     }
18     struct OsalThreadParam param = {
19         .priority = OSAL_THREAD_PRI_DEFAULT,
20         .stackSize = 0,
21         .policy = 0,
22     };
23     OsalThreadStart(&thread->adapter, &param);
24     thread->status = true;
25 }
26 
HdfThreadStop(struct HdfThread * thread)27 void HdfThreadStop(struct HdfThread *thread)
28 {
29     if (thread == NULL) {
30         return;
31     }
32     OsalThreadDestroy(&thread->adapter);
33     thread->status = false;
34 }
35 
HdfThreadIsRunning(struct HdfThread * thread)36 bool HdfThreadIsRunning(struct HdfThread *thread)
37 {
38     if (thread == NULL) {
39         return false;
40     }
41     return thread->status;
42 }
43 
HdfThreadMain(void * argv)44 void HdfThreadMain(void *argv)
45 {
46     struct HdfThread *thread = (struct HdfThread *)argv;
47     if (thread == NULL) {
48         return;
49     }
50     if (thread->ThreadEntry != NULL) {
51         thread->ThreadEntry(argv);
52     } else {
53         OsalThreadDestroy(&thread->adapter);
54     }
55 }
56 
HdfThreadConstruct(struct HdfThread * thread)57 void HdfThreadConstruct(struct HdfThread *thread)
58 {
59     if (thread == NULL) {
60         return;
61     }
62     thread->Start = HdfThreadStart;
63     thread->Stop = HdfThreadStop;
64     thread->IsRunning = HdfThreadIsRunning;
65     thread->status = false;
66     OsalThreadCreate(&thread->adapter, (OsalThreadEntry)HdfThreadMain, thread);
67 }
68 
HdfThreadDestruct(struct HdfThread * thread)69 void HdfThreadDestruct(struct HdfThread *thread)
70 {
71     if (thread != NULL && thread->IsRunning()) {
72         thread->Stop(thread);
73     }
74 }
75 
HdfThreadNewInstance(void)76 struct HdfThread *HdfThreadNewInstance(void)
77 {
78     struct HdfThread *thread = (struct HdfThread *)OsalMemCalloc(sizeof(struct HdfThread));
79     if (thread != NULL) {
80         HdfThreadConstruct(thread);
81     }
82     return thread;
83 }
84 
HdfThreadFreeInstance(struct HdfThread * thread)85 void HdfThreadFreeInstance(struct HdfThread *thread)
86 {
87     if (thread != NULL) {
88         HdfThreadDestruct(thread);
89         OsalMemFree(thread);
90     }
91 }
92