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