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