blob: 024147fa4bb06bcb0628874d79c91be0aa04b6f3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
// *****************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: https://www.gnu.org/licenses/gpl-3.0 *
// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
// *****************************************************************************
#ifndef GLOBALS_H_8013740213748021573485
#define GLOBALS_H_8013740213748021573485
#include <atomic>
#include <memory>
#include "scope_guard.h"
namespace zen
{
/*
Solve static destruction order fiasco by providing shared ownership and serialized access to global variables
=>there may be accesses to "Global<T>::get()" during process shutdown e.g. _("") used by message in debug_minidump.cpp or by some detached thread assembling an error message!
=> use trivially-destructible POD only!!!
ATTENTION: function-static globals have the compiler generate "magic statics" == compiler-genenerated locking code which will crash or leak memory when accessed after global is "dead"
=> "solved" by FunStatGlobal, but we can't have "too many" of these...
*/
template <class T>
class Global //don't use for function-scope statics!
{
public:
Global()
{
static_assert(std::is_trivially_constructible_v<Pod>&& std::is_trivially_destructible_v<Pod>, "this memory needs to live forever");
assert(!pod_.inst && !pod_.spinLock); //we depend on static zero-initialization!
}
explicit Global(std::unique_ptr<T>&& newInst) { set(std::move(newInst)); }
~Global() { set(nullptr); }
std::shared_ptr<T> get() //=> return std::shared_ptr to let instance life time be handled by caller (MT usage!)
{
while (pod_.spinLock.exchange(true)) ;
ZEN_ON_SCOPE_EXIT(pod_.spinLock = false);
if (pod_.inst)
return *pod_.inst;
return nullptr;
}
void set(std::unique_ptr<T>&& newInst)
{
std::shared_ptr<T>* tmpInst = nullptr;
if (newInst)
tmpInst = new std::shared_ptr<T>(std::move(newInst));
{
while (pod_.spinLock.exchange(true)) ;
ZEN_ON_SCOPE_EXIT(pod_.spinLock = false);
std::swap(pod_.inst, tmpInst);
}
delete tmpInst;
}
private:
struct Pod
{
std::atomic<bool> spinLock; // { false }; rely entirely on static zero-initialization! => avoid potential contention with worker thread during Global<> construction!
//serialize access; can't use std::mutex: has non-trival destructor
std::shared_ptr<T>* inst; // = nullptr;
} pod_;
};
//===================================================================================================================
//===================================================================================================================
struct CleanUpEntry
{
using CleanUpFunction = void (*)(void* callbackData);
CleanUpFunction cleanUpFun;
void* callbackData;
CleanUpEntry* prev;
};
void registerGlobalForDestruction(CleanUpEntry& entry);
template <class T>
class FunStatGlobal
{
public:
//No FunStatGlobal() or ~FunStatGlobal()!
std::shared_ptr<T> get()
{
static_assert(std::is_trivially_constructible_v<FunStatGlobal>&&
std::is_trivially_destructible_v<FunStatGlobal>, "this class must not generate code for magic statics!");
while (pod_.spinLock.exchange(true)) ;
ZEN_ON_SCOPE_EXIT(pod_.spinLock = false);
if (pod_.inst)
return *pod_.inst;
return nullptr;
}
void set(std::unique_ptr<T>&& newInst)
{
std::shared_ptr<T>* tmpInst = nullptr;
if (newInst)
tmpInst = new std::shared_ptr<T>(std::move(newInst));
{
while (pod_.spinLock.exchange(true)) ;
ZEN_ON_SCOPE_EXIT(pod_.spinLock = false);
std::swap(pod_.inst, tmpInst);
registerDestruction();
}
delete tmpInst;
}
void initOnce(std::unique_ptr<T> (*getInitialValue)())
{
while (pod_.spinLock.exchange(true)) ;
ZEN_ON_SCOPE_EXIT(pod_.spinLock = false);
if (!pod_.cleanUpEntry.cleanUpFun)
{
assert(!pod_.inst);
if (std::unique_ptr<T> newInst = (*getInitialValue)())
pod_.inst = new std::shared_ptr<T>(std::move(newInst));
registerDestruction();
}
}
private:
//call while holding pod_.spinLock
void registerDestruction()
{
assert(pod_.spinLock);
if (!pod_.cleanUpEntry.cleanUpFun)
{
pod_.cleanUpEntry.callbackData = this;
pod_.cleanUpEntry.cleanUpFun = [](void* callbackData)
{
auto thisPtr = static_cast<FunStatGlobal*>(callbackData);
thisPtr->set(nullptr);
};
registerGlobalForDestruction(pod_.cleanUpEntry);
}
}
struct Pod
{
std::atomic<bool> spinLock; // { false }; rely entirely on static zero-initialization! => avoid potential contention with worker thread during Global<> construction!
//serialize access; can't use std::mutex: has non-trival destructor
std::shared_ptr<T>* inst; // = nullptr;
CleanUpEntry cleanUpEntry;
} pod_;
};
inline
void registerGlobalForDestruction(CleanUpEntry& entry)
{
static struct
{
std::atomic<bool> spinLock;
CleanUpEntry* head;
} cleanUpList;
static_assert(std::is_trivially_constructible_v<decltype(cleanUpList)>&&
std::is_trivially_destructible_v<decltype(cleanUpList)>, "we must not generate code for magic statics!");
while (cleanUpList.spinLock.exchange(true)) ;
ZEN_ON_SCOPE_EXIT(cleanUpList.spinLock = false);
std::atexit([]
{
while (cleanUpList.spinLock.exchange(true)) ;
ZEN_ON_SCOPE_EXIT(cleanUpList.spinLock = false);
(*cleanUpList.head->cleanUpFun)(cleanUpList.head->callbackData);
cleanUpList.head = cleanUpList.head->prev; //nicely clean up in reverse order of construction
});
entry.prev = cleanUpList.head;
cleanUpList.head = &entry;
}
}
#endif //GLOBALS_H_8013740213748021573485
|