• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <atomic>
16 
17 #include "log_print.h"
18 #include "runtime_context_impl.h"
19 #include "version.h"
20 
21 namespace DistributedDB {
GetInstance()22 RuntimeContext *RuntimeContext::GetInstance()
23 {
24     static char instMemory[sizeof(RuntimeContextImpl)];
25     static std::mutex instLock_;
26     static std::atomic<RuntimeContext *> instPtr = nullptr;
27     // For Double-Checked Locking, we need check insPtr twice
28     if (instPtr == nullptr) {
29         std::lock_guard<std::mutex> lock(instLock_);
30         if (instPtr == nullptr) {
31             // Use instMemory to make sure this singleton not free before other object.
32             // This operation needn't to malloc memory, we needn't to check nullptr.
33             instPtr = new (instMemory) RuntimeContextImpl;
34             LOGI("DistributedDB Version : %s", SOFTWARE_VERSION_STRING.c_str());
35         }
36     }
37     return instPtr;
38 }
39 } // namespace DistributedDB
40 
41