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