-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingletonGlobal.cpp
372 lines (304 loc) · 8.75 KB
/
SingletonGlobal.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "pch.h"
#ifdef WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif // !WIN32_LEAN_AND_MEAN
#include <Windows.h>
#else
#error "Not implemented!"
#endif // WIN32
#include <cassert>
#include <functional>
#include <mutex>
class UnMoveable
{
protected:
constexpr UnMoveable() noexcept = default;
constexpr virtual ~UnMoveable() noexcept = default;
private:
constexpr UnMoveable(UnMoveable &&) noexcept = delete;
constexpr UnMoveable &operator=(UnMoveable &&) noexcept = delete;
};
class UnCopyable
{
protected:
constexpr UnCopyable() noexcept = default;
constexpr virtual ~UnCopyable() noexcept = default;
private:
constexpr UnCopyable(const UnCopyable &) noexcept = delete;
constexpr UnCopyable &operator=(const UnCopyable &) noexcept = delete;
};
template <typename Type> class ContainerLazyPtrRaw final : public UnCopyable, public UnMoveable
{
static_assert(!std::is_pointer_v<Type>, "The Type cannot be a raw pointer itself!");
public:
constexpr ContainerLazyPtrRaw() noexcept = default;
~ContainerLazyPtrRaw()
{
if (mCallbackUninitialize)
{
mCallbackUninitialize();
}
}
void SetUninitialize(std::move_only_function<void()> aCallback) noexcept
{
mCallbackUninitialize = std::move(aCallback);
}
void SetPtr(Type *aPtr) noexcept
{
mPtr = aPtr;
}
Type *GetPtr() const noexcept
{
return mPtr;
}
private:
Type *mPtr{};
std::move_only_function<void()> mCallbackUninitialize;
};
class SharedMemory final
{
public:
constexpr SharedMemory() noexcept = default;
constexpr ~SharedMemory() noexcept
{
Delete();
}
// If the name matches an existing mapping object, the function returns that mapped object
void *Create(const uint32_t aSize, std::string_view aName)
{
if (mHandle)
{
return mMemory;
}
mHandle = CreateFileMappingA(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, aSize, aName.data());
if (!mHandle)
{
throw std::runtime_error("mHandle cannot be null!");
}
mMemory = MapViewOfFile(mHandle, SECTION_MAP_WRITE | SECTION_MAP_READ, 0, 0, 0);
if (!mMemory)
{
throw std::runtime_error("mMemory cannot be null!");
}
return mMemory;
}
void Delete() noexcept
{
if (mMemory)
{
UnmapViewOfFile(mMemory);
mMemory = nullptr;
}
if (mHandle)
{
CloseHandle(mHandle);
mHandle = nullptr;
}
}
template <typename Type> Type GetMemory(const size_t aOffset = 0) const noexcept
{
assert(mMemory);
uint8_t *memoryAsBytes = reinterpret_cast<uint8_t *>(mMemory);
return reinterpret_cast<Type>(memoryAsBytes + aOffset);
}
private:
void *mMemory{};
void *mHandle{};
};
class SharedMutex final
{
public:
constexpr SharedMutex() noexcept = default;
constexpr ~SharedMutex() noexcept
{
Delete();
}
// If the name matches an existing mutex, the function returns that mutex
void *Create(std::string_view aName)
{
if (mMutex)
{
return mMutex;
}
mMutex = CreateMutexA(nullptr, FALSE, aName.data());
if (!mMutex)
{
throw std::runtime_error("mMutex cannot be null!");
}
return mMutex;
}
void Delete() noexcept
{
if (mMutex)
{
CloseHandle(mMutex);
mMutex = nullptr;
}
}
void lock()
{
if (!mMutex)
{
throw std::runtime_error("mMutex cannot be null!");
}
if (mLocked)
{
throw std::runtime_error("Could not lock mutex twice!");
}
mLocked = WaitForSingleObject(mMutex, INFINITE) == WAIT_OBJECT_0;
if (!mLocked)
{
throw std::runtime_error("Could not lock mutex!");
}
}
void unlock() noexcept
{
if (!mMutex)
{
return;
}
assert(mLocked);
mLocked = !ReleaseMutex(mMutex);
}
private:
void *mMutex{};
bool mLocked{};
};
// A Global/Per-Process Singleton that uses shared memory and named mutexes
//
// !!! WARNING !!!
//
// Per-Process SingletonGlobalLazy does not support shared properties by default
// To share properties between processes they MUST be on "stack" so
// SingletonGlobalLazy will actually allocate them on the shared memory
//
// !!! INFORMATION !!!
//
// The shared memory layout is:
// 2 bytes + the size of the object
// ref count the object itself
//
// The File Mapping has the name format: SingletonGlobalLazy<Type Hash>([PID])::Memory
// The Named Mutex has the name format: SingletonGlobalLazy<Type Hash>([PID])::Mutex
template <typename Type, bool global = false> class SingletonGlobalLazy : public UnCopyable, public UnMoveable
{
using RefCountType = int16_t;
public:
constexpr SingletonGlobalLazy() noexcept = default;
constexpr virtual ~SingletonGlobalLazy() noexcept = default;
[[nodiscard]] static Type &GetInstance()
{
mSharedMutex.Create(MakeNameForMutex());
return CreateInstance();
}
// Event that is called when the first instance is created
virtual void OnInitialize([[maybe_unused]] Type &instance)
{
}
// Event that is called for every instance BUT the last one when they are destroyed
virtual void OnPerInstanceUninitialize([[maybe_unused]] Type &instance)
{
}
static void DeleteInstance()
{
std::scoped_lock lock(mSharedMutex);
if (!mInstance.GetPtr())
{
return;
}
Uninitialize();
}
private:
static inline SharedMutex mSharedMutex;
static inline SharedMemory mSharedMemory;
static inline ContainerLazyPtrRaw<Type> mInstance;
static RefCountType &GetCount() noexcept
{
return *mSharedMemory.GetMemory<RefCountType *>();
}
static Type *GetObjectPtr() noexcept
{
return mSharedMemory.GetMemory<Type *>(sizeof(RefCountType));
}
[[nodiscard]] static Type &CreateInstance()
{
std::scoped_lock lock(mSharedMutex);
if (mInstance.GetPtr())
{
return *mInstance.GetPtr();
}
return Initialize();
}
[[nodiscard]] static Type &Initialize()
{
// contains the instance count and the object itself
const uint32_t sharedMemorySize = sizeof(RefCountType) + sizeof(Type);
mSharedMemory.Create(sharedMemorySize, MakeNameForMemory());
if (GetCount()++)
{
// the object was already created
mInstance.SetPtr(GetObjectPtr());
}
else
{
// use the placement new operator to create our object
// inside the shared memory we allocated
mInstance.SetPtr(new (GetObjectPtr()) Type());
mInstance.GetPtr()->OnInitialize(*GetObjectPtr());
}
// when this instance of the class will deallocate
// it will call this callback to delete the instance
mInstance.SetUninitialize(DeleteInstance);
return *mInstance.GetPtr();
}
static void Uninitialize()
{
const auto currentCount = --GetCount();
if (currentCount < 0)
{
throw std::runtime_error("Ref count can't be negative!");
}
// we just DeleteInstance so clear callback for DeleteInstance
mInstance.SetUninitialize(nullptr);
if (currentCount)
{
mInstance.GetPtr()->OnPerInstanceUninitialize(*GetObjectPtr());
mInstance.SetPtr(nullptr);
return;
}
// we cannot call the delete operator on this object
// in conjunction with the placement new operator
// therefore we call it's dtor explicitly
mInstance.GetPtr()->~Type();
mInstance.SetPtr(nullptr);
mSharedMemory.Delete();
mSharedMutex.Delete();
}
[[nodiscard]] static std::string MakeNameForMemory() noexcept
{
if constexpr (global)
{
return std::format("SingletonGlobal<{}>()::Memory", typeid(Type).hash_code());
}
else
{
return std::format("SingletonGlobal<{}>({})::Memory", typeid(Type).hash_code(), _getpid());
}
}
[[nodiscard]] static std::string MakeNameForMutex() noexcept
{
if constexpr (global)
{
return std::format("SingletonGlobal<{}>()::Mutex", typeid(Type).hash_code());
}
else
{
return std::format("SingletonGlobal<{}>({})::Mutex", typeid(Type).hash_code(), _getpid());
}
}
};
int main()
{
return 0;
}