1 /*
2 * Copyright (c) 2021-2024 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 #ifndef SOFTBUS_ADAPTER_THREAD_H
17 #define SOFTBUS_ADAPTER_THREAD_H
18
19 #include "comm_log.h"
20 #include "softbus_adapter_timer.h"
21 #include "softbus_error_code.h"
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 #define TASK_NAME_MAX_LEN (16)
28
29 typedef enum {
30 SOFTBUS_SCHED_OTHER,
31 SOFTBUS_SCHED_RR
32 } SoftBusSched;
33
34 typedef enum {
35 SOFTBUS_THREAD_JOINABLE,
36 SOFTBUS_THREAD_DETACH
37 } SoftBusDetachState;
38
39 typedef enum {
40 SOFTBUS_PRIORITY_LOWEST,
41 SOFTBUS_PRIORITY_LOW,
42 SOFTBUS_PRIORITY_DEFAULT,
43 SOFTBUS_PRIORITY_HIGH,
44 SOFTBUS_PRIORITY_HIGHEST
45 } SoftBusThreadPriority;
46
47 typedef struct {
48 int32_t policy;
49 int32_t detachState;
50 SoftBusThreadPriority prior;
51 const char *taskName;
52 uint64_t stackSize;
53 } SoftBusThreadAttr;
54
55 typedef enum {
56 SOFTBUS_MUTEX_NORMAL,
57 SOFTBUS_MUTEX_RECURSIVE
58 } SoftBusMutexType;
59
60 typedef struct {
61 SoftBusMutexType type;
62 } SoftBusMutexAttr;
63
64 typedef uintptr_t SoftBusThread;
65 typedef uintptr_t SoftBusMutex;
66 typedef uintptr_t SoftBusCond;
67
68 // mutex
69 int32_t SoftBusMutexAttrInit(SoftBusMutexAttr *mutexAttr);
70 int32_t SoftBusMutexInit(SoftBusMutex *mutex, SoftBusMutexAttr *mutexAttr);
71 int32_t SoftBusMutexLockInner(SoftBusMutex *mutex);
72 int32_t SoftBusMutexUnlockInner(SoftBusMutex *mutex);
73 int32_t SoftBusMutexDestroy(SoftBusMutex *mutex);
74
CheckMutexIsNull(const SoftBusMutex * mutex)75 static inline bool CheckMutexIsNull(const SoftBusMutex *mutex)
76 {
77 return (mutex == NULL) || ((void *)(*mutex) == NULL);
78 }
79
80 #define SoftBusMutexLock(mutex) \
81 ({ \
82 int32_t ret = SOFTBUS_OK; \
83 if (CheckMutexIsNull(mutex)) { \
84 COMM_LOGD(COMM_ADAPTER, "SoftBusMutexLock mutex is null"); \
85 ret = SOFTBUS_INVALID_PARAM; \
86 } else { \
87 ret = SoftBusMutexLockInner(mutex); \
88 if (ret != 0) { \
89 COMM_LOGE(COMM_ADAPTER, "SoftBusMutexLock failed, ret=%{public}d", ret); \
90 ret = SOFTBUS_LOCK_ERR; \
91 } \
92 } \
93 ret; \
94 })
95
96 #define SoftBusMutexUnlock(mutex) \
97 ({ \
98 int32_t ret = SOFTBUS_OK; \
99 if (CheckMutexIsNull(mutex)) { \
100 COMM_LOGE(COMM_ADAPTER, "SoftBusMutexUnlock mutex is null"); \
101 ret = SOFTBUS_INVALID_PARAM; \
102 } else { \
103 ret = SoftBusMutexUnlockInner(mutex); \
104 if (ret != 0) { \
105 COMM_LOGE(COMM_ADAPTER, "SoftBusMutexUnlock failed, ret=%{public}d", ret); \
106 ret = SOFTBUS_LOCK_ERR; \
107 } \
108 } \
109 ret; \
110 })
111
SoftBusMutexUnlockAuto(SoftBusMutex ** mutex)112 static inline void SoftBusMutexUnlockAuto(SoftBusMutex **mutex)
113 {
114 if (mutex) {
115 SoftBusMutexUnlock(*mutex);
116 }
117 }
118
119 #define SOFTBUS_LOCK_GUARD(mutex) \
120 __attribute__((cleanup(SoftBusMutexUnlockAuto), unused)) SoftBusMutex *lockGuard##mutex = &mutex
121
122 // pthread
123 int32_t SoftBusThreadAttrInit(SoftBusThreadAttr *threadAttr);
124 int32_t SoftBusThreadCreate(SoftBusThread *thread, SoftBusThreadAttr *threadAttr, void *(*threadEntry)(void *),
125 void *arg);
126 int32_t SoftBusThreadJoin(SoftBusThread thread, void **value);
127 int32_t SoftBusThreadSetName(SoftBusThread thread, const char *name);
128 SoftBusThread SoftBusThreadGetSelf(void);
129
130 // cond
131 int32_t SoftBusCondInit(SoftBusCond *cond);
132 int32_t SoftBusCondSignal(SoftBusCond *cond);
133 int32_t SoftBusCondBroadcast(SoftBusCond *cond);
134 int32_t SoftBusCondWait(SoftBusCond *cond, SoftBusMutex *mutex, SoftBusSysTime *time);
135 int32_t SoftBusCondDestroy(SoftBusCond *cond);
136
137 #ifdef __cplusplus
138 }
139 #endif /* __cplusplus */
140
141 #endif // SOFTBUS_ADAPTER_THREAD_H
142