1cb7eb8c9Sopenharmony_ci/* 2cb7eb8c9Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3cb7eb8c9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4cb7eb8c9Sopenharmony_ci * you may not use this file except in compliance with the License. 5cb7eb8c9Sopenharmony_ci * You may obtain a copy of the License at 6cb7eb8c9Sopenharmony_ci * 7cb7eb8c9Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8cb7eb8c9Sopenharmony_ci * 9cb7eb8c9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10cb7eb8c9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11cb7eb8c9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12cb7eb8c9Sopenharmony_ci * See the License for the specific language governing permissions and 13cb7eb8c9Sopenharmony_ci * limitations under the License. 14cb7eb8c9Sopenharmony_ci */ 15cb7eb8c9Sopenharmony_ci 16cb7eb8c9Sopenharmony_ci#ifndef DFSU_SINGLETON_H 17cb7eb8c9Sopenharmony_ci#define DFSU_SINGLETON_H 18cb7eb8c9Sopenharmony_ci 19cb7eb8c9Sopenharmony_ci#include <memory> 20cb7eb8c9Sopenharmony_ci#include <mutex> 21cb7eb8c9Sopenharmony_ci#include <shared_mutex> 22cb7eb8c9Sopenharmony_ci#include "nocopyable.h" 23cb7eb8c9Sopenharmony_ci 24cb7eb8c9Sopenharmony_cinamespace OHOS { 25cb7eb8c9Sopenharmony_cinamespace Storage { 26cb7eb8c9Sopenharmony_cinamespace DistributedFile { 27cb7eb8c9Sopenharmony_cinamespace Utils { 28cb7eb8c9Sopenharmony_ci#define DECLARE_SINGLETON(MyClass) \ 29cb7eb8c9Sopenharmony_cipublic: \ 30cb7eb8c9Sopenharmony_ci ~MyClass(); \ 31cb7eb8c9Sopenharmony_ci MyClass(const MyClass&) = delete; \ 32cb7eb8c9Sopenharmony_ci MyClass& operator=(const MyClass&) = delete; \ 33cb7eb8c9Sopenharmony_ci \ 34cb7eb8c9Sopenharmony_ciprivate: \ 35cb7eb8c9Sopenharmony_ci friend DfsuSingleton<MyClass>; \ 36cb7eb8c9Sopenharmony_ci MyClass(); 37cb7eb8c9Sopenharmony_ci 38cb7eb8c9Sopenharmony_citemplate<typename T> 39cb7eb8c9Sopenharmony_ciclass DfsuSingleton : public NoCopyable { 40cb7eb8c9Sopenharmony_cipublic: 41cb7eb8c9Sopenharmony_ci static std::shared_ptr<T> GetInstance(); 42cb7eb8c9Sopenharmony_ci 43cb7eb8c9Sopenharmony_ciprotected: 44cb7eb8c9Sopenharmony_ci /** 45cb7eb8c9Sopenharmony_ci * @note We depend on the IPC manager to serialize the start and the stop procedure 46cb7eb8c9Sopenharmony_ci */ 47cb7eb8c9Sopenharmony_ci virtual void StartInstance() = 0; 48cb7eb8c9Sopenharmony_ci 49cb7eb8c9Sopenharmony_ci /** 50cb7eb8c9Sopenharmony_ci * @note Be very careful when freeing memory! Threads may call stop and other member functions simultaneously 51cb7eb8c9Sopenharmony_ci */ 52cb7eb8c9Sopenharmony_ci virtual void StopInstance() = 0; 53cb7eb8c9Sopenharmony_ci}; 54cb7eb8c9Sopenharmony_ci 55cb7eb8c9Sopenharmony_ci/** 56cb7eb8c9Sopenharmony_ci * @brief 57cb7eb8c9Sopenharmony_ci * 58cb7eb8c9Sopenharmony_ci * @tparam T 59cb7eb8c9Sopenharmony_ci * @return T& 60cb7eb8c9Sopenharmony_ci * 61cb7eb8c9Sopenharmony_ci * @note We use call_once to ensure the atomicity of new() and start() 62cb7eb8c9Sopenharmony_ci * @note Memory leaking of T is exactly what we want. Now T will be available along the program's life-time 63cb7eb8c9Sopenharmony_ci */ 64cb7eb8c9Sopenharmony_citemplate<typename T> 65cb7eb8c9Sopenharmony_cistd::shared_ptr<T> DfsuSingleton<T>::GetInstance() 66cb7eb8c9Sopenharmony_ci{ 67cb7eb8c9Sopenharmony_ci static std::shared_ptr<T> *dummy = nullptr; 68cb7eb8c9Sopenharmony_ci static std::once_flag once; 69cb7eb8c9Sopenharmony_ci std::call_once(once, []() mutable { 70cb7eb8c9Sopenharmony_ci dummy = new std::shared_ptr<T>(new T()); 71cb7eb8c9Sopenharmony_ci (*dummy)->StartInstance(); 72cb7eb8c9Sopenharmony_ci }); 73cb7eb8c9Sopenharmony_ci return *dummy; 74cb7eb8c9Sopenharmony_ci} 75cb7eb8c9Sopenharmony_ci} // namespace Utils 76cb7eb8c9Sopenharmony_ci} // namespace DistributedFile 77cb7eb8c9Sopenharmony_ci} // namespace Storage 78cb7eb8c9Sopenharmony_ci} // namespace OHOS 79cb7eb8c9Sopenharmony_ci#endif // DFSU_SINGLETON_H