Avoid potential negative array index access to cached text.
[LibreOffice.git] / include / comphelper / doublecheckedinit.hxx
blob200a9c88c42bbd529971735ff68c44b18bdef2bd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_COMPHELPER_DOUBLECHECKEDINIT_HXX
21 #define INCLUDED_COMPHELPER_DOUBLECHECKEDINIT_HXX
23 #include <osl/getglobalmutex.hxx>
25 #include <atomic>
26 // HACK: <atomic> includes <stdbool.h>, which in some Clang versions does '#define bool bool',
27 // which confuses clang plugins.
28 #undef bool
29 #include <functional>
31 namespace comphelper
33 /**
34 * Thread-safe singleton creation.
36 * It is normally sufficient to create singletons using static variables in a function.
37 * This function is only for use cases that have a more complex lifetime of the object,
38 * such as when the object may require specific cleanup or may be created more times
39 * (e.g. when there is a "singleton" per each instance of another object).
41 template <typename Type, typename Function = std::function<Type*()>,
42 typename Guard = osl::MutexGuard, typename GuardCtor = osl::GetGlobalMutex>
43 static inline Type* doubleCheckedInit(std::atomic<Type*>& pointer, Function function,
44 GuardCtor guardCtor = osl::GetGlobalMutex())
46 Type* p = pointer.load(std::memory_order_acquire);
47 if (!p)
49 Guard guard(guardCtor());
50 p = pointer.load(std::memory_order_relaxed);
51 if (!p)
53 p = function();
54 pointer.store(p, std::memory_order_release);
57 return p;
60 } // namespace
62 #endif // INCLUDED_COMPHELPER_DOUBLECHECKEDINIT_HXX
64 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */