param_osadp.h 6.3 KB
Newer Older
M
Mupceet 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef BASE_STARTUP_PARAM_OS_ADAPTER_H
#define BASE_STARTUP_PARAM_OS_ADAPTER_H
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#if !(defined __LITEOS_A__ || defined __LITEOS_M__)
M
Mupceet 已提交
24
#include <sys/syscall.h>
M
Mupceet 已提交
25 26 27 28 29 30 31
#include "loop_event.h"
#else
#include <time.h>
#endif

#ifndef __LITEOS_M__
#include <pthread.h>
M
Mupceet 已提交
32
#include <stdatomic.h>
M
Mupceet 已提交
33 34
#endif

M
Mupceet 已提交
35 36 37 38
#if defined FUTEX_WAIT || defined FUTEX_WAKE
#include <linux/futex.h>
#endif

M
Mupceet 已提交
39 40
#include "param_utils.h"

M
Mupceet 已提交
41 42 43 44 45 46
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif

M
Mupceet 已提交
47
#define PARAM_WORKSPACE_MIN (1024)
M
Mupceet 已提交
48 49 50 51 52 53 54 55 56
#if (defined __LITEOS_A__ || defined __LITEOS_M__)
#define DAC_DEFAULT_MODE 0777
#ifdef STARTUP_INIT_TEST
#define PARAM_WORKSPACE_MAX (1024 * 50)
#else
#define PARAM_WORKSPACE_MAX (1024 * 30)
#endif
#define PARAM_WORKSPACE_SMALL PARAM_WORKSPACE_MAX
#define PARAM_WORKSPACE_DEF PARAM_WORKSPACE_MAX
M
Mupceet 已提交
57 58
#define DAC_DEFAULT_GROUP 0
#define DAC_DEFAULT_USER 0
M
Mupceet 已提交
59 60
#else
#define PARAM_WORKSPACE_MAX (80 * 1024)
M
Mupceet 已提交
61
#define PARAM_WORKSPACE_SMALL (1024 * 20)
M
Mupceet 已提交
62 63 64 65 66 67 68
#ifdef STARTUP_INIT_TEST
#define DAC_DEFAULT_MODE 0777
#define PARAM_WORKSPACE_DEF (1024 * 50)
#else
#define DAC_DEFAULT_MODE 0774
#define PARAM_WORKSPACE_DEF (1024 * 30)
#endif
M
Mupceet 已提交
69 70
#define DAC_DEFAULT_GROUP 2000
#define DAC_DEFAULT_USER 0
M
Mupceet 已提交
71 72
#endif

M
Mupceet 已提交
73 74 75 76 77 78 79
// support futex
#ifndef __NR_futex
#define PARAM_NR_FUTEX 202 /* syscall number */
#else
#define PARAM_NR_FUTEX __NR_futex
#endif

M
Mupceet 已提交
80
#if !(defined FUTEX_WAIT || defined FUTEX_WAKE)
M
Mupceet 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93
#define FUTEX_WAIT 0
#define FUTEX_WAKE 1

#ifndef __LITEOS_M__
#define PARAM_FUTEX(ftx, op, value, timeout, bitset)                         \
    do {                                                                   \
        struct timespec d_timeout = { 0, 1000 * 1000 * (timeout) };        \
        syscall(PARAM_NR_FUTEX, ftx, op, value, &d_timeout, NULL, bitset); \
    } while (0)

#define futex_wake(ftx, count) PARAM_FUTEX(ftx, FUTEX_WAKE, count, 0, 0)
#define futex_wait(ftx, value) PARAM_FUTEX(ftx, FUTEX_WAIT, value, 100, 0)
#else
M
Mupceet 已提交
94 95
#define futex_wake(ftx, count) (void)(ftx)
#define futex_wait(ftx, value) (void)(ftx)
M
Mupceet 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
#endif
#endif

// support timer
#if defined __LITEOS_A__ || defined __LITEOS_M__
struct ParamTimer_;
typedef void (*ProcessTimer)(const struct ParamTimer_ *taskHandle, void *context);
typedef struct ParamTimer_ {
    timer_t timerId;
    uint64_t repeat;
    ProcessTimer timeProcessor;
    void *context;
} ParamTimer;

typedef ParamTimer *ParamTaskPtr;
#else
typedef LoopBase *ParamTaskPtr;
typedef void (*ProcessTimer)(const ParamTaskPtr taskHandle, void *context);
#endif

int ParamTimerCreate(ParamTaskPtr *timer, ProcessTimer process, void *context);
int ParamTimerStart(const ParamTaskPtr timer, uint64_t timeout, uint64_t repeat);
void ParamTimerClose(ParamTaskPtr timer);

// support mutex
#ifndef __LITEOS_M__
typedef struct {
    pthread_rwlock_t rwlock;
} ParamRWMutex;

typedef struct {
    pthread_mutex_t mutex;
} ParamMutex;
#else
typedef struct {
    uint32_t mutex;
} ParamRWMutex;

typedef struct {
    uint32_t mutex;
} ParamMutex;
#endif

M
Mupceet 已提交
139
void  paramMutexEnvInit(void);
M
Mupceet 已提交
140 141 142 143 144 145
int ParamRWMutexCreate(ParamRWMutex *lock);
int ParamRWMutexWRLock(ParamRWMutex *lock);
int ParamRWMutexRDLock(ParamRWMutex *lock);
int ParamRWMutexUnlock(ParamRWMutex *lock);
int ParamRWMutexDelete(ParamRWMutex *lock);

M
Mupceet 已提交
146
int ParamMutexCreate(ParamMutex *mutex);
M
Mupceet 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
int ParamMutexPend(ParamMutex *mutex);
int ParamMutexPost(ParamMutex *mutex);
int ParamMutexDelete(ParamMutex *mutex);

#ifdef WORKSPACE_AREA_NEED_MUTEX
#define PARAMSPACE_AREA_INIT_LOCK(workspace) ParamRWMutexCreate(&workspace->rwlock)
#define PARAMSPACE_AREA_RW_LOCK(workspace) ParamRWMutexWRLock(&workspace->rwlock)
#define PARAMSPACE_AREA_RD_LOCK(workspace) ParamRWMutexRDLock(&workspace->rwlock)
#define PARAMSPACE_AREA_RW_UNLOCK(workspace) ParamRWMutexUnlock(&workspace->rwlock)
#else
#define PARAMSPACE_AREA_INIT_LOCK(rwlock) (void)(rwlock)
#define PARAMSPACE_AREA_RW_LOCK(rwlock) (void)(rwlock)
#define PARAMSPACE_AREA_RD_LOCK(rwlock) (void)(rwlock)
#define PARAMSPACE_AREA_RW_UNLOCK(rwlock) (void)(rwlock)
#endif

#ifdef PARAMWORKSPACE_NEED_MUTEX
#define WORKSPACE_INIT_LOCK(workspace) ParamRWMutexCreate(&(workspace).rwlock)
#define WORKSPACE_RW_LOCK(workspace) ParamRWMutexWRLock(&(workspace).rwlock)
#define WORKSPACE_RD_LOCK(workspace) ParamRWMutexRDLock(&(workspace).rwlock)
#define WORKSPACE_RW_UNLOCK(workspace) ParamRWMutexUnlock(&(workspace).rwlock)
#else
#define WORKSPACE_INIT_LOCK(workspace) (void)(workspace)
#define WORKSPACE_RW_LOCK(workspace) (void)(workspace)
#define WORKSPACE_RD_LOCK(workspace) (void)(workspace)
#define WORKSPACE_RW_UNLOCK(workspace) (void)(workspace)
#endif

typedef struct {
    int shmid;
} MemHandle;
void *GetSharedMem(const char *fileName, MemHandle *handle, uint32_t spaceSize, int readOnly);
void FreeSharedMem(const MemHandle *handle, void *mem, uint32_t dataSize);

M
Mupceet 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194
// for atomic
#ifdef __LITEOS_M__
#define ATOMIC_UINT32 uint32_t
#define ATOMIC_LLONG  long long
#define ATOMIC_INIT(commitId, value) *(commitId) = (value)
#define ATOMIC_LOAD_EXPLICIT(commitId, order) *(commitId)
#define ATOMIC_STORE_EXPLICIT(commitId, value, order) *(commitId) = (value)
#else
#define ATOMIC_UINT32 atomic_uint
#define ATOMIC_LLONG atomic_llong
#define ATOMIC_INIT(commitId, value) atomic_init((commitId), (value))
#define ATOMIC_LOAD_EXPLICIT(commitId, order) atomic_load_explicit((commitId), (order))
#define ATOMIC_STORE_EXPLICIT(commitId, value, order) atomic_store_explicit((commitId), (value), (order))
#endif
M
Mupceet 已提交
195 196 197 198 199
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif
M
Mupceet 已提交
200 201

uint32_t Difftime(time_t curr, time_t base);
M
Mupceet 已提交
202
#endif // BASE_STARTUP_PARAM_MESSAGE_H