1 /* 2 * Copyright (c) 2021 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)13void 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 }; 22 OsalThreadStart(&thread->adapter, ¶m); 23 thread->status = true; 24 } 25 HdfThreadStop(struct HdfThread * thread)26void HdfThreadStop(struct HdfThread *thread) 27 { 28 if (thread == NULL) { 29 return; 30 } 31 OsalThreadDestroy(&thread->adapter); 32 thread->status = false; 33 } 34 HdfThreadIsRunning(struct HdfThread * thread)35bool HdfThreadIsRunning(struct HdfThread *thread) 36 { 37 if (thread == NULL) { 38 return false; 39 } 40 return thread->status; 41 } 42 HdfThreadMain(void * argv)43void HdfThreadMain(void *argv) 44 { 45 struct HdfThread *thread = (struct HdfThread *)argv; 46 if (thread == NULL) { 47 return; 48 } 49 if (thread->ThreadEntry != NULL) { 50 thread->ThreadEntry(argv); 51 } else { 52 OsalThreadDestroy(&thread->adapter); 53 } 54 } 55 HdfThreadConstruct(struct HdfThread * thread)56void HdfThreadConstruct(struct HdfThread *thread) 57 { 58 if (thread == NULL) { 59 return; 60 } 61 thread->Start = HdfThreadStart; 62 thread->Stop = HdfThreadStop; 63 thread->IsRunning = HdfThreadIsRunning; 64 thread->status = false; 65 OsalThreadCreate(&thread->adapter, (OsalThreadEntry)HdfThreadMain, thread); 66 } 67 HdfThreadDestruct(struct HdfThread * thread)68void HdfThreadDestruct(struct HdfThread *thread) 69 { 70 if (thread != NULL && thread->IsRunning()) { 71 thread->Stop(thread); 72 } 73 } 74 HdfThreadNewInstance()75struct HdfThread *HdfThreadNewInstance() 76 { 77 struct HdfThread *thread = 78 (struct HdfThread *)OsalMemCalloc(sizeof(struct HdfThread)); 79 if (thread != NULL) { 80 HdfThreadConstruct(thread); 81 } 82 return thread; 83 } 84 HdfThreadFreeInstance(struct HdfThread * thread)85void HdfThreadFreeInstance(struct HdfThread *thread) 86 { 87 if (thread != NULL) { 88 HdfThreadDestruct(thread); 89 OsalMemFree(thread); 90 } 91 } 92 93