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 #ifndef BASE_STARTUP_PARAM_OS_ADAPTER_H 17 #define BASE_STARTUP_PARAM_OS_ADAPTER_H 18 #include <stdint.h> 19 #include <string.h> 20 #include <unistd.h> 21 #include <stdio.h> 22 #include <string.h> 23 #if !(defined __LITEOS_A__ || defined __LITEOS_M__) 24 #include <sys/syscall.h> 25 #include "loop_event.h" 26 #else 27 #include <time.h> 28 #endif 29 30 #ifndef __LITEOS_M__ 31 #include <pthread.h> 32 #include <stdatomic.h> 33 #endif 34 35 #if defined FUTEX_WAIT || defined FUTEX_WAKE 36 #include <linux/futex.h> 37 #endif 38 39 #include "param_utils.h" 40 41 #ifdef __cplusplus 42 #if __cplusplus 43 extern "C" { 44 #endif 45 #endif 46 47 #define PARAM_WORKSPACE_INVALID ((uint32_t)-1) 48 #define PARAM_WORKSPACE_MIN (1024) 49 /* 50 length for parameter = node size + data size 51 xxxx.xxxx.xxxx.xxxx 52 node size: 53 24 * (count(.) + 1) + strlen(xxxx.xxxx.xxxx.xxxx) 54 data size 55 strlen(xxxx.xxxx.xxxx.xxxx) + 96 56 */ 57 #if (defined __LITEOS_A__ || defined __LITEOS_M__) 58 #define DAC_DEFAULT_MODE 0777 59 #ifdef STARTUP_INIT_TEST 60 #define PARAM_WORKSPACE_MAX (1024 * 50) 61 #else 62 #ifndef PARAM_WORKSPACE_MAX 63 #define PARAM_WORKSPACE_MAX (1024 * 5) 64 #endif 65 #endif 66 #define PARAM_WORKSPACE_SMALL PARAM_WORKSPACE_MAX 67 #define PARAM_WORKSPACE_DEF PARAM_WORKSPACE_MAX 68 #define DAC_DEFAULT_GROUP 0 69 #define DAC_DEFAULT_USER 0 70 #else 71 #define PARAM_WORKSPACE_MAX (80 * 1024) 72 #define PARAM_WORKSPACE_SMALL (1024 * 20) 73 #ifdef STARTUP_INIT_TEST 74 #define DAC_DEFAULT_MODE 0777 75 #define PARAM_WORKSPACE_DEF (1024 * 50) 76 #else 77 #define DAC_DEFAULT_MODE 0774 78 #define PARAM_WORKSPACE_DEF (1024 * 30) 79 #endif 80 #define DAC_DEFAULT_GROUP 0 81 #define DAC_DEFAULT_USER 0 82 #endif 83 84 // support futex 85 #ifndef __NR_futex 86 #define PARAM_NR_FUTEX 202 /* syscall number */ 87 #else 88 #define PARAM_NR_FUTEX __NR_futex 89 #endif 90 91 #if !(defined FUTEX_WAIT || defined FUTEX_WAKE) 92 #define FUTEX_WAIT 0 93 #define FUTEX_WAKE 1 94 95 #ifndef __LITEOS_M__ 96 #define PARAM_FUTEX(ftx, op, value, timeout, bitset) \ 97 do { \ 98 struct timespec d_timeout = { 0, 1000 * 1000 * (timeout) }; \ 99 syscall(PARAM_NR_FUTEX, ftx, op, value, &d_timeout, NULL, bitset); \ 100 } while (0) 101 102 #define futex_wake(ftx, count) PARAM_FUTEX(ftx, FUTEX_WAKE, count, 0, 0) 103 #define futex_wait(ftx, value) PARAM_FUTEX(ftx, FUTEX_WAIT, value, 100, 0) 104 #else 105 #define futex_wake(ftx, count) (void)(ftx) 106 #define futex_wait(ftx, value) (void)(ftx) 107 #endif 108 #endif 109 110 // support timer 111 #if defined __LITEOS_A__ || defined __LITEOS_M__ 112 struct ParamTimer_; 113 typedef void (*ProcessTimer)(const struct ParamTimer_ *taskHandle, void *context); 114 typedef struct ParamTimer_ { 115 timer_t timerId; 116 uint64_t repeat; 117 ProcessTimer timeProcessor; 118 void *context; 119 } ParamTimer; 120 121 typedef ParamTimer *ParamTaskPtr; 122 #else 123 typedef LoopBase *ParamTaskPtr; 124 typedef void (*ProcessTimer)(const ParamTaskPtr taskHandle, void *context); 125 #endif 126 127 int ParamTimerCreate(ParamTaskPtr *timer, ProcessTimer process, void *context); 128 int ParamTimerStart(const ParamTaskPtr timer, uint64_t timeout, uint64_t repeat); 129 void ParamTimerClose(ParamTaskPtr timer); 130 131 // support mutex 132 #ifndef __LITEOS_M__ 133 typedef struct { 134 pthread_rwlock_t rwlock; 135 } ParamRWMutex; 136 137 typedef struct { 138 pthread_mutex_t mutex; 139 } ParamMutex; 140 #else 141 typedef struct { 142 uint32_t mutex; 143 } ParamRWMutex; 144 145 typedef struct { 146 uint32_t mutex; 147 } ParamMutex; 148 #endif 149 150 INIT_LOCAL_API void paramMutexEnvInit(void); 151 INIT_LOCAL_API int ParamRWMutexCreate(ParamRWMutex *lock); 152 INIT_LOCAL_API int ParamRWMutexWRLock(ParamRWMutex *lock); 153 INIT_LOCAL_API int ParamRWMutexRDLock(ParamRWMutex *lock); 154 INIT_LOCAL_API int ParamRWMutexUnlock(ParamRWMutex *lock); 155 INIT_LOCAL_API int ParamRWMutexDelete(ParamRWMutex *lock); 156 157 INIT_LOCAL_API int ParamMutexCreate(ParamMutex *mutex); 158 INIT_LOCAL_API int ParamMutexPend(ParamMutex *mutex); 159 INIT_LOCAL_API int ParamMutexPost(ParamMutex *mutex); 160 INIT_LOCAL_API int ParamMutexDelete(ParamMutex *mutex); 161 162 #ifdef WORKSPACE_AREA_NEED_MUTEX 163 #define PARAMSPACE_AREA_INIT_LOCK(workspace) ParamRWMutexCreate(&workspace->rwlock) 164 #define PARAMSPACE_AREA_RW_LOCK(workspace) ParamRWMutexWRLock(&workspace->rwlock) 165 #define PARAMSPACE_AREA_RD_LOCK(workspace) ParamRWMutexRDLock(&workspace->rwlock) 166 #define PARAMSPACE_AREA_RW_UNLOCK(workspace) ParamRWMutexUnlock(&workspace->rwlock) 167 #else 168 #define PARAMSPACE_AREA_INIT_LOCK(rwlock) (void)(rwlock) 169 #define PARAMSPACE_AREA_RW_LOCK(rwlock) (void)(rwlock) 170 #define PARAMSPACE_AREA_RD_LOCK(rwlock) (void)(rwlock) 171 #define PARAMSPACE_AREA_RW_UNLOCK(rwlock) (void)(rwlock) 172 #endif 173 174 #ifdef PARAMWORKSPACE_NEED_MUTEX 175 #define WORKSPACE_INIT_LOCK(workspace) ParamRWMutexCreate(&(workspace).rwlock) 176 #define WORKSPACE_RW_LOCK(workspace) ParamRWMutexWRLock(&(workspace).rwlock) 177 #define WORKSPACE_RD_LOCK(workspace) ParamRWMutexRDLock(&(workspace).rwlock) 178 #define WORKSPACE_RW_UNLOCK(workspace) ParamRWMutexUnlock(&(workspace).rwlock) 179 #else 180 #define WORKSPACE_INIT_LOCK(workspace) (void)(workspace) 181 #define WORKSPACE_RW_LOCK(workspace) (void)(workspace) 182 #define WORKSPACE_RD_LOCK(workspace) (void)(workspace) 183 #define WORKSPACE_RW_UNLOCK(workspace) (void)(workspace) 184 #endif 185 186 typedef struct { 187 int shmid; 188 } MemHandle; 189 INIT_LOCAL_API void *GetSharedMem(const char *fileName, MemHandle *handle, uint32_t spaceSize, int readOnly); 190 INIT_LOCAL_API void FreeSharedMem(const MemHandle *handle, void *mem, uint32_t dataSize); 191 192 // for atomic 193 #ifdef __LITEOS_M__ 194 #define ATOMIC_UINT32 uint32_t 195 #define ATOMIC_LLONG long long 196 #define ATOMIC_INIT(commitId, value) *(commitId) = (value) 197 #define ATOMIC_LOAD_EXPLICIT(commitId, order) *(commitId) 198 #define ATOMIC_STORE_EXPLICIT(commitId, value, order) *(commitId) = (value) 199 #else 200 #define ATOMIC_UINT32 atomic_uint 201 #define ATOMIC_LLONG atomic_llong 202 #define ATOMIC_INIT(commitId, value) atomic_init((commitId), (value)) 203 #define ATOMIC_LOAD_EXPLICIT(commitId, order) atomic_load_explicit((commitId), (order)) 204 #define ATOMIC_STORE_EXPLICIT(commitId, value, order) atomic_store_explicit((commitId), (value), (order)) 205 #endif 206 #ifdef __cplusplus 207 #if __cplusplus 208 } 209 #endif 210 #endif 211 212 INIT_LOCAL_API uint32_t Difftime(time_t curr, time_t base); 213 #endif // BASE_STARTUP_PARAM_MESSAGE_H