1 /*
2 * Copyright (c) 2023 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_INIT_SYS_PARAM_H
17 #define BASE_STARTUP_INIT_SYS_PARAM_H
18 #include "param_common.h"
19
20 #ifdef __cplusplus
21 #if __cplusplus
22 extern "C" {
23 #endif
24 #endif
25
26 /**
27 * parameter client init
28 */
29 void InitParameterClient(void);
30
31 /**
32 * by name and default value,save parameter info in handle。
33 *
34 */
35 CachedHandle CachedParameterCreate(const char *name, const char *defValue);
36
37 /**
38 * destroy handle
39 *
40 */
41 void CachedParameterDestroy(CachedHandle handle);
42
43 /**
44 * if name exist,return value else return default value
45 *
46 */
CachedParameterGet(CachedHandle handle)47 static inline const char *CachedParameterGet(CachedHandle handle)
48 {
49 struct CachedParameter_ *param = (struct CachedParameter_ *)handle;
50 if (param == NULL) {
51 return NULL;
52 }
53
54 // no change, do not to find
55 long long spaceCommitId = ATOMIC_UINT64_LOAD_EXPLICIT(¶m->workspace->area->commitId, MEMORY_ORDER_ACQUIRE);
56 if (param->spaceCommitId == spaceCommitId) {
57 return param->paramValue;
58 }
59 param->spaceCommitId = spaceCommitId;
60 int changed = 0;
61 if (param->cachedParameterCheck == NULL) {
62 return param->paramValue;
63 }
64 return param->cachedParameterCheck(param, &changed);
65 }
66
CachedParameterGetChanged(CachedHandle handle,int * changed)67 static inline const char *CachedParameterGetChanged(CachedHandle handle, int *changed)
68 {
69 struct CachedParameter_ *param = (struct CachedParameter_ *)handle;
70 if (param == NULL) {
71 return NULL;
72 }
73 // no change, do not to find
74 long long spaceCommitId = ATOMIC_UINT64_LOAD_EXPLICIT(¶m->workspace->area->commitId, MEMORY_ORDER_ACQUIRE);
75 if (param->spaceCommitId == spaceCommitId) {
76 return param->paramValue;
77 }
78 param->spaceCommitId = spaceCommitId;
79 if ((changed == NULL) || (param->cachedParameterCheck == NULL)) {
80 return param->paramValue;
81 }
82 return param->cachedParameterCheck(param, changed);
83 }
84
85 #ifdef __cplusplus
86 #if __cplusplus
87 }
88 #endif
89 #endif
90 #endif