100600bfbSopenharmony_ci/*
200600bfbSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
300600bfbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
400600bfbSopenharmony_ci * you may not use this file except in compliance with the License.
500600bfbSopenharmony_ci * You may obtain a copy of the License at
600600bfbSopenharmony_ci *
700600bfbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
800600bfbSopenharmony_ci *
900600bfbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1000600bfbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1100600bfbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1200600bfbSopenharmony_ci * See the License for the specific language governing permissions and
1300600bfbSopenharmony_ci * limitations under the License.
1400600bfbSopenharmony_ci */
1500600bfbSopenharmony_ci#ifndef HIDUMPER_UTILS_SP_SINGLETON_H
1600600bfbSopenharmony_ci#define HIDUMPER_UTILS_SP_SINGLETON_H
1700600bfbSopenharmony_ci#include <mutex>
1800600bfbSopenharmony_ci#include <refbase.h>
1900600bfbSopenharmony_ci#include "nocopyable.h"
2000600bfbSopenharmony_cinamespace OHOS {
2100600bfbSopenharmony_cinamespace HiviewDFX {
2200600bfbSopenharmony_citemplate<typename T>
2300600bfbSopenharmony_ciclass DumpDelayedSpSingleton : public NoCopyable {
2400600bfbSopenharmony_cipublic:
2500600bfbSopenharmony_ci    static sptr<T> GetInstance();
2600600bfbSopenharmony_ci    static void DestroyInstance();
2700600bfbSopenharmony_ciprivate:
2800600bfbSopenharmony_ci    static sptr<T> instance_;
2900600bfbSopenharmony_ci    static std::mutex mutex_;
3000600bfbSopenharmony_ci};
3100600bfbSopenharmony_ci
3200600bfbSopenharmony_citemplate<typename T>
3300600bfbSopenharmony_cisptr<T> DumpDelayedSpSingleton<T>::instance_ = nullptr;
3400600bfbSopenharmony_ci
3500600bfbSopenharmony_citemplate<typename T>
3600600bfbSopenharmony_cistd::mutex DumpDelayedSpSingleton<T>::mutex_;
3700600bfbSopenharmony_ci
3800600bfbSopenharmony_citemplate<typename T>
3900600bfbSopenharmony_civoid DumpDelayedSpSingleton<T>::DestroyInstance()
4000600bfbSopenharmony_ci{
4100600bfbSopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
4200600bfbSopenharmony_ci    if (instance_) {
4300600bfbSopenharmony_ci        instance_.clear();
4400600bfbSopenharmony_ci        instance_ = nullptr;
4500600bfbSopenharmony_ci    }
4600600bfbSopenharmony_ci}
4700600bfbSopenharmony_ci
4800600bfbSopenharmony_citemplate<typename T>
4900600bfbSopenharmony_cisptr<T> DumpDelayedSpSingleton<T>::GetInstance()
5000600bfbSopenharmony_ci{
5100600bfbSopenharmony_ci    if (!instance_) {
5200600bfbSopenharmony_ci        std::lock_guard<std::mutex> lock(mutex_);
5300600bfbSopenharmony_ci        if (instance_ == nullptr) {
5400600bfbSopenharmony_ci            instance_ = new T();
5500600bfbSopenharmony_ci        }
5600600bfbSopenharmony_ci    }
5700600bfbSopenharmony_ci    return instance_;
5800600bfbSopenharmony_ci}
5900600bfbSopenharmony_ci} // namespace HiviewDFX
6000600bfbSopenharmony_ci} // namespace OHOS
6100600bfbSopenharmony_ci#endif // HIDUMPER_UTILS_SP_SINGLETON_H
62