1 /*
2  * Copyright (C) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "hc_thread.h"
17 #include "hal_error.h"
18 #include "hc_log.h"
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #define MAX_THREAD_STACK_SIZE (8 * 1024 * 1024)
25 
StaticThreadFunc(void * args)26 void *StaticThreadFunc(void *args)
27 {
28     HcThread* thread = (HcThread*)args;
29     if (thread == NULL) {
30         return NULL;
31     }
32 
33 #if defined(SET_THREAD_NAME)
34     int res = pthread_setname_np(pthread_self(), StringGet(&thread->name));
35     if (res != 0) {
36         LOGW("[OS]: pthread_setname_np fail. [Res]: %d", res);
37     } else {
38         LOGI("[OS]: pthread_setname_np success. [StackSize]: %zu, [Name]: %s",
39             thread->stackSize, StringGet(&thread->name));
40     }
41 #endif
42 
43     if (thread->threadFunc) {
44         thread->threadFunc(args);
45     }
46     thread->threadLock.lock(&thread->threadLock);
47     thread->running = HC_FALSE;
48     thread->threadWaitObj.notifyWithoutLock(&thread->threadWaitObj);
49     thread->threadLock.unlock(&thread->threadLock);
50     return NULL;
51 }
52 
Start(struct HcThreadT * thread)53 int Start(struct HcThreadT *thread)
54 {
55     if (thread == NULL) {
56         return HAL_ERR_NULL_PTR;
57     }
58     thread->threadLock.lock(&thread->threadLock);
59     if (thread->running) {
60         thread->threadLock.unlock(&thread->threadLock);
61         return 0;
62     }
63     thread->running = HC_TRUE;
64 
65     pthread_attr_t attr;
66     pthread_attr_init(&attr);
67     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
68 
69     if (thread->stackSize > 0 && thread->stackSize <= MAX_THREAD_STACK_SIZE) {
70         pthread_attr_setstacksize(&attr, thread->stackSize);
71     }
72 
73     LOGI("[OS]: pthread_create enter.");
74     int res = pthread_create(&thread->thread, &attr, StaticThreadFunc, thread);
75     LOGI("[OS]: pthread_create quit. [Res]: %d", res);
76     pthread_attr_destroy(&attr);
77     if (res != 0) {
78         LOGE("[OS]: pthread_create fail. [Res]: %d", res);
79         thread->running = HC_FALSE;
80     }
81     thread->threadLock.unlock(&thread->threadLock);
82     return res;
83 }
84 
Join(struct HcThreadT * thread)85 void Join(struct HcThreadT *thread)
86 {
87     if (thread == NULL) {
88         return;
89     }
90     thread->threadLock.lock(&thread->threadLock);
91     if (thread->running) {
92         thread->threadWaitObj.waitWithoutLock(&thread->threadWaitObj);
93     }
94     thread->threadLock.unlock(&thread->threadLock);
95 }
96 
BizWait(struct HcThreadT * thread)97 void BizWait(struct HcThreadT *thread)
98 {
99     if (thread == NULL) {
100         return;
101     }
102     thread->bizWaitObj.wait(&thread->bizWaitObj);
103 }
104 
BizNotify(struct HcThreadT * thread)105 void BizNotify(struct HcThreadT *thread)
106 {
107     if (thread == NULL) {
108         return;
109     }
110     thread->bizWaitObj.notify(&thread->bizWaitObj);
111 }
112 
InitThread(HcThread * thread,ThreadFunc func,size_t stackSize,const char * threadName)113 int32_t InitThread(HcThread *thread, ThreadFunc func, size_t stackSize, const char *threadName)
114 {
115     if (thread == NULL) {
116         return -1;
117     }
118 
119     thread->threadFunc = func;
120     thread->start = Start;
121     thread->wait = BizWait;
122     thread->notify = BizNotify;
123     thread->join = Join;
124     thread->stackSize = stackSize;
125     thread->running = HC_FALSE;
126     thread->name = CreateString();
127     if (StringSetPointer(&thread->name, threadName) != HC_TRUE) {
128         return -1;
129     }
130 
131     int32_t res = InitHcMutex(&thread->threadLock);
132     if (res != 0) {
133         DeleteString(&thread->name);
134         return res;
135     }
136     res = InitHcCond(&thread->threadWaitObj, &thread->threadLock);
137     if (res != 0) {
138         DeleteString(&thread->name);
139         DestroyHcMutex(&thread->threadLock);
140         return res;
141     }
142     res = InitHcCond(&thread->bizWaitObj, NULL);
143     if (res != 0) {
144         DeleteString(&thread->name);
145         DestroyHcMutex(&thread->threadLock);
146         DestroyHcCond(&thread->threadWaitObj);
147     }
148     return res;
149 }
150 
DestroyThread(HcThread * thread)151 void DestroyThread(HcThread *thread)
152 {
153     if (thread == NULL) {
154         return;
155     }
156 
157     DestroyHcCond(&thread->bizWaitObj);
158     DestroyHcCond(&thread->threadWaitObj);
159     DestroyHcMutex(&thread->threadLock);
160     DeleteString(&thread->name);
161 }
162 
163 #ifdef __cplusplus
164 }
165 #endif