reuse more of Cursor to avoid future issues
[folly.git] / folly / SingletonThreadLocal.h
1 /*
2  * Copyright 2016-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <folly/Singleton.h>
20 #include <folly/ThreadLocal.h>
21
22 namespace folly {
23
24 // SingletonThreadLocal
25 //
26 // This class can help you implement a per-thread leaky-singleton model within
27 // your application. Please read the usage block at the top of Singleton.h as
28 // the recommendations there are also generally applicable to this class.
29 //
30 // When we say this is "leaky" we mean that the T instances held by a
31 // SingletonThreadLocal<T> will survive until their owning thread exits,
32 // regardless of the lifetime of the singleton object holding them.  That
33 // means that they can be safely used during process shutdown, and
34 // that they can also be safely used in an application that spawns many
35 // temporary threads throughout its life.
36 //
37 // Keywords to help people find this class in search:
38 // Thread Local Singleton ThreadLocalSingleton
39 template <typename T, typename Tag = detail::DefaultTag>
40 class SingletonThreadLocal {
41  public:
42   using CreateFunc = std::function<T*(void)>;
43
44   SingletonThreadLocal() : SingletonThreadLocal([]() { return new T(); }) {}
45
46   template <typename Create>
47   FOLLY_NOINLINE explicit SingletonThreadLocal(Create create)
48       : singleton_([create = std::move(create)]() mutable {
49           return new ThreadLocalT([create = std::move(create)]() mutable {
50             return new Wrapper(std::unique_ptr<T>(create()));
51           });
52         }) {}
53
54   FOLLY_ALWAYS_INLINE static T& get() {
55 #ifdef FOLLY_TLS
56     return *localPtr() ? **localPtr() : *(*localPtr() = &getSlow());
57 #else
58     return **SingletonT::get();
59 #endif
60   }
61
62  private:
63   FOLLY_NOINLINE static T& getSlow() {
64     return **SingletonT::get();
65   }
66
67 #ifdef FOLLY_TLS
68   FOLLY_ALWAYS_INLINE static T** localPtr() {
69     static FOLLY_TLS T* localPtr = nullptr;
70     return &localPtr;
71   }
72 #endif
73
74   class Wrapper {
75    public:
76     explicit Wrapper(std::unique_ptr<T> t) : t_(std::move(t)) {}
77
78     ~Wrapper() {
79 #ifdef FOLLY_TLS
80       *localPtr() = nullptr;
81 #endif
82     }
83
84     T& operator*() { return *t_; }
85
86    private:
87     std::unique_ptr<T> t_;
88   };
89
90   using ThreadLocalT = ThreadLocal<Wrapper>;
91   using SingletonT = LeakySingleton<ThreadLocalT, Tag>;
92
93   SingletonT singleton_;
94 };
95 } // namespace folly