1 /*
2 * Copyright (C) 2021 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 (thread->threadFunc) {
34 thread->threadFunc(args);
35 }
36 thread->threadLock.lock(&thread->threadLock);
37 thread->running = HC_FALSE;
38 thread->threadWaitObj.notifyWithoutLock(&thread->threadWaitObj);
39 thread->threadLock.unlock(&thread->threadLock);
40 return 0;
41 }
42
Start(struct HcThreadT * thread)43 int Start(struct HcThreadT* thread)
44 {
45 if (thread == NULL) {
46 return HAL_ERR_NULL_PTR;
47 }
48 thread->threadLock.lock(&thread->threadLock);
49 if (thread->running) {
50 thread->threadLock.unlock(&thread->threadLock);
51 return 0;
52 }
53 thread->running = HC_TRUE;
54
55 pthread_attr_t attr;
56 pthread_attr_init(&attr);
57 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
58
59 if (thread->stackSize > 0 && thread->stackSize <= MAX_THREAD_STACK_SIZE) {
60 pthread_attr_setstacksize(&attr, thread->stackSize);
61 }
62
63 int result = pthread_create(&thread->thread, &attr, StaticThreadFunc, thread);
64 pthread_attr_destroy(&attr);
65
66 if (result != 0) {
67 LOGE("pthread_create failed.");
68 thread->running = HC_FALSE;
69 }
70 thread->threadLock.unlock(&thread->threadLock);
71 return result;
72 }
73
Join(struct HcThreadT * thread)74 void Join(struct HcThreadT* thread)
75 {
76 if (thread == NULL) {
77 return;
78 }
79 thread->threadLock.lock(&thread->threadLock);
80 if (thread->running) {
81 thread->threadWaitObj.waitWithoutLock(&thread->threadWaitObj);
82 }
83 thread->threadLock.unlock(&thread->threadLock);
84 }
85
BizWait(struct HcThreadT * thread)86 void BizWait(struct HcThreadT* thread)
87 {
88 if (thread == NULL) {
89 return;
90 }
91 thread->bizWaitObj.wait(&thread->bizWaitObj);
92 }
93
BizNotify(struct HcThreadT * thread)94 void BizNotify(struct HcThreadT* thread)
95 {
96 if (thread == NULL) {
97 return;
98 }
99 thread->bizWaitObj.notify(&thread->bizWaitObj);
100 }
101
InitThread(HcThread * thread,ThreadFunc func,size_t stackSize,const char * threadName)102 int32_t InitThread(HcThread* thread, ThreadFunc func, size_t stackSize, const char* threadName)
103 {
104 if (thread == NULL) {
105 return -1;
106 }
107
108 thread->threadFunc = func;
109 thread->start = Start;
110 thread->wait = BizWait;
111 thread->notify = BizNotify;
112 thread->join = Join;
113 thread->stackSize = stackSize;
114 thread->running = HC_FALSE;
115 thread->name = CreateString();
116 if (StringSetPointer(&thread->name, threadName) != HC_TRUE) {
117 return -1;
118 }
119
120 int32_t res = InitHcMutex(&thread->threadLock);
121 if (res != 0) {
122 DeleteString(&thread->name);
123 return res;
124 }
125 res = InitHcCond(&thread->threadWaitObj, &thread->threadLock);
126 if (res != 0) {
127 DeleteString(&thread->name);
128 DestroyHcMutex(&thread->threadLock);
129 return res;
130 }
131 res = InitHcCond(&thread->bizWaitObj, NULL);
132 if (res != 0) {
133 DeleteString(&thread->name);
134 DestroyHcMutex(&thread->threadLock);
135 DestroyHcCond(&thread->threadWaitObj);
136 }
137 return res;
138 }
139
DestroyThread(HcThread * thread)140 void DestroyThread(HcThread* thread)
141 {
142 if (thread == NULL) {
143 return;
144 }
145
146 DestroyHcCond(&thread->bizWaitObj);
147 DestroyHcCond(&thread->threadWaitObj);
148 DestroyHcMutex(&thread->threadLock);
149 DeleteString(&thread->name);
150 }
151
152 #ifdef __cplusplus
153 }
154 #endif