-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathusm.hpp
636 lines (542 loc) · 12.9 KB
/
usm.hpp
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
#ifndef _USM_H
#define _USM_H
namespace USMLIBRARY
{
using namespace std;
#ifndef _NO_MUTUAL
class mutual
{
private:
wstring ev1;
wstring str;
HANDLE h1 = 0;
HANDLE h2 = 0;
std::function<void(unsigned long long lp)> lpf;
unsigned long long lpa = 0;
mutual(const mutual&) = delete;
void operator =(const mutual&) = delete;
HANDLE CreateEventX(int i)
{
TCHAR n[1000] = { 0 };
swprintf_s(n, L"%s{401F3B1E-6090-4DF2-95C0-F11C10F9F285}_%u", str.c_str(), i);
HANDLE hX = CreateEvent(0, 0, 0, n);
return hX;
}
void WaitForRequestNoLoop()
{
WaitForSingleObject(h1, INFINITE);
if (lpf)
lpf(lpa);
SetEvent(h2);
}
void WaitForRequestLoop()
{
for (;;)
{
if (WaitForSingleObject(h1, INFINITE) != WAIT_OBJECT_0)
break;
if (lpf)
lpf(lpa);
SetEvent(h2);
}
}
public:
mutual(const wchar_t* strn, std::function<void(unsigned long long lp)> req, unsigned long long lp, bool LoopRequest)
{
str = strn;
lpf = req;
lpa = lp;
h1 = CreateEventX(1);
h2 = CreateEventX(2);
if (req)
{
if (LoopRequest)
{
std::thread t(&mutual::WaitForRequestLoop, this);
t.detach();
}
else
{
std::thread t(&mutual::WaitForRequestNoLoop, this);
t.detach();
}
}
}
void request(DWORD Wait = INFINITE)
{
ResetEvent(h2);
SetEvent(h1);
if (!Wait)
return;
WaitForSingleObject(h2, Wait);
}
~mutual()
{
CloseHandle(h2);
h2 = 0;
CloseHandle(h1);
h1 = 0;
}
};
#endif
template <typename T = char>
class usm
{
private:
struct USMHEADER
{
};
struct USMTHREAD
{
DWORD id;
int evidx;
};
// Strings of handle ids
wstring cwmn;
wstring fmn;
wstring evrn;
wstring evrn2;
wstring evwn;
wstring stringid;
bool WasFirst = false;
// Auto reset event that is set when reading thread finishes reading
HANDLE hEventRead = 0;
// Auto reset event that is set when writing thread finishes writing
HANDLE hEventWrote = 0;
// Locked when this thread is writing
// Or when a thread prepares for initializing or exiting
HANDLE hMutexWriting = 0;
// Set when this thread is not reading
// Unset when this thread is reading
HANDLE hEventMeReading = 0;
HANDLE hFM = 0;
unsigned long long ClientSZ = 0;
DWORD MaxThreads = 0;
PVOID Buff = 0;
bool Executable = false;
SECURITY_ATTRIBUTES sattr;
SECURITY_DESCRIPTOR SD;
void FillSA()
{
sattr.nLength = sizeof(sattr);
BOOL fx = InitializeSecurityDescriptor(&SD, SECURITY_DESCRIPTOR_REVISION);
fx = SetSecurityDescriptorDacl(&SD, TRUE, NULL, FALSE);
sattr.bInheritHandle = true;
sattr.lpSecurityDescriptor = &SD;
}
HANDLE CreateEvR(int idx)
{
TCHAR n[1000] = { 0 };
swprintf_s(n, L"%s%i", evrn.c_str(), idx);
HANDLE hX = CreateEvent(&sattr, TRUE, TRUE, n);
return hX;
}
HANDLE CreateEvR2(int idx)
{
TCHAR n[1000] = { 0 };
swprintf_s(n, L"%s%i", evrn2.c_str(), idx);
HANDLE hX = CreateEvent(&sattr, 0, 0, n);
return hX;
}
HANDLE CreateEvW()
{
TCHAR n[1000] = { 0 };
swprintf_s(n, L"%s", evwn.c_str());
HANDLE hX = CreateEvent(&sattr, 0, 0, n);
return hX;
}
HANDLE CreateCWM()
{
HANDLE hX = OpenMutex(MUTEX_MODIFY_STATE | SYNCHRONIZE, false, cwmn.c_str());
if (hX != 0)
return hX;
hX = CreateMutex(&sattr, 0, cwmn.c_str());
return hX;
}
HANDLE CreateFM()
{
// Try to open the map , or else create it
WasFirst = true;
HANDLE hX = OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE | (Executable ? FILE_MAP_EXECUTE : 0), false, fmn.c_str());
if (hX != 0)
{
WasFirst = false;
return hX;
}
unsigned long long FinalSize = ClientSZ * sizeof(T) + MaxThreads * sizeof(USMTHREAD) + sizeof(USMHEADER);
ULARGE_INTEGER ulx = { 0 };
ulx.QuadPart = FinalSize;
hX = CreateFileMapping(INVALID_HANDLE_VALUE, &sattr, (Executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE), ulx.HighPart, ulx.LowPart, fmn.c_str());
if (hX != 0)
{
LPVOID Buff4 = MapViewOfFile(hX, FILE_MAP_READ | FILE_MAP_WRITE | (Executable ? FILE_MAP_EXECUTE : 0), 0, 0, 0);
if (Buff4)
{
memset(Buff4, 0, (size_t)FinalSize);
UnmapViewOfFile(Buff4);
}
}
return hX;
}
public:
HANDLE fmh()
{
return hX;
}
HANDLE GetFM() { return hFM; }
wstring GetFMN() { return fmn; }
int GetMaxThreads() { return MaxThreads; }
void End()
{
// Remove the ID from the thread
if (Buff)
{
USMTHREAD* th = (USMTHREAD*)((char*)((char*)Buff + sizeof(USMHEADER)));
WaitForSingleObject(hMutexWriting, INFINITE);
// Find
for (unsigned int y = 0; y < MaxThreads; y++)
{
USMTHREAD& tt = th[y];
DWORD myid = GetCurrentThreadId();
if (tt.id == myid)
{
tt.id = 0;
tt.evidx = 0;
break;
}
}
ReleaseMutex(hMutexWriting);
}
if (hEventRead)
CloseHandle(hEventRead);
hEventRead = 0;
if (hEventWrote)
CloseHandle(hEventWrote);
hEventWrote = 0;
if (hFM)
CloseHandle(hFM);
hFM = 0;
if (hEventMeReading)
CloseHandle(hEventMeReading);
hEventMeReading = 0;
if (hMutexWriting)
CloseHandle(hMutexWriting);
hMutexWriting = 0;
}
bool IsFirst() { return WasFirst; }
usm(const wchar_t* string_id = 0, bool Init = false, unsigned long long csz = 1048576, DWORD MaxTh = 100)
{
if (!string_id)
return;
CreateInit(string_id, Init, csz, MaxTh);
}
void operator =(const usm &x)
{
// Terminate current
End();
// Recreate
CreateInit(x.stringid.c_str(), true, x.ClientSZ, x.MaxThreads);
}
usm(const usm& x)
{
operator=(x);
}
void CreateInit(const wchar_t* string_id, bool Init = false, unsigned long long csz = 1048576, DWORD MaxTh = 100, bool LocalOnly = true, bool Exex = false)
{
Executable = Exex;
if (!string_id)
return;
if (wcslen(string_id) == 0)
return;
TCHAR xup[1000] = { 0 };
stringid = string_id;
FillSA();
if (LocalOnly)
{
swprintf_s(xup, 1000, L"%s_cwmn", stringid.c_str());
cwmn = xup;
swprintf_s(xup, 1000, L"%s_evrn", stringid.c_str());
evrn = xup;
swprintf_s(xup, 1000, L"%s_evrn2", stringid.c_str());
evrn2 = xup;
swprintf_s(xup, 1000, L"%s_evwn", stringid.c_str());
evwn = xup;
swprintf_s(xup, 1000, L"%s_fmn", stringid.c_str());
fmn = xup;
}
else
{
swprintf_s(xup, 1000, L"Global\\%s_cwmn", stringid.c_str());
cwmn = xup;
swprintf_s(xup, 1000, L"Global\\%s_evrn", stringid.c_str());
evrn = xup;
swprintf_s(xup, 1000, L"Global\\%s_evrn2", stringid.c_str());
evrn2 = xup;
swprintf_s(xup, 1000, L"Global\\%s_evwn", stringid.c_str());
evwn = xup;
swprintf_s(xup, 1000, L"Global\\%s_fmn", stringid.c_str());
fmn = xup;
}
if (!csz)
csz = 1048576;
ClientSZ = csz;
if (!MaxTh)
MaxTh = 100;
MaxThreads = MaxTh;
if (Init)
{
int iv = Initialize();
if (iv <= 0)
{
End();
throw iv;
}
}
}
~usm()
{
End();
}
int Initialize()
{
hEventRead = 0;
hEventWrote = 0;
hMutexWriting = 0;
hFM = 0;
Buff = 0;
hEventMeReading = 0;
if (hMutexWriting == 0)
hMutexWriting = CreateCWM();
if (hMutexWriting == 0)
return -1;
if (hFM == 0)
hFM = CreateFM();
if (hFM == 0)
return -1;
if (hEventWrote == 0)
hEventWrote = CreateEvW();
if (hEventWrote == 0)
return -1;
if (Buff == 0)
Buff = MapViewOfFile(hFM, FILE_MAP_READ | FILE_MAP_WRITE | (Executable ? FILE_MAP_EXECUTE : 0), 0, 0, 0);
if (!Buff)
return -1;
// Acquire lock for Count variable
// USMHEADER* h = (USMHEADER*)Buff;
USMTHREAD* th = (USMTHREAD*)((char*)((char*)Buff + sizeof(USMHEADER)));
WaitForSingleObject(hMutexWriting, INFINITE);
// Find
for (unsigned int y = 0; y < MaxThreads; y++)
{
USMTHREAD& tt = th[y];
if (tt.id == 0)
{
tt.id = GetCurrentThreadId();
tt.evidx = (y + 1);
hEventMeReading = CreateEvR(y + 1);
hEventRead = CreateEvR2(y + 1);
break;
}
}
ReleaseMutex(hMutexWriting);
if (!hEventMeReading)
return -1;
return 1;
}
const T* BeginRead(bool FailOnNotReady = false)
{
if (!Buff)
return 0;
// Is someone writing
if (FailOnNotReady)
{
DWORD x = WaitForSingleObject(hMutexWriting, 0);
if (x != WAIT_OBJECT_0)
return 0;
}
else
WaitForSingleObject(hMutexWriting, INFINITE);
// Reset our reading event
ResetEvent(hEventMeReading);
// Release the mutex, but now any writing thread that locks it must wait for is
ReleaseMutex(hMutexWriting);
// Return the pointer
const char* a1 = (const char*)Buff;
a1 += sizeof(USMHEADER);
a1 += sizeof(USMTHREAD)*MaxThreads;
return (T*)a1;
}
void EndRead()
{
SetEvent(hEventMeReading);
SetEvent(hEventRead);
}
unsigned long long ReadData(T* b, size_t sz, size_t offset = 0, bool FailIfNotReady = false)
{
const T* ptr = BeginRead(FailIfNotReady);
if (!ptr)
return (unsigned long long) - 1;
memcpy(b, ptr + offset, sz);
EndRead();
return sz;
}
DWORD NotifyOnRead(bool Wait)
{
// See if any thread is reading
USMTHREAD* th = (USMTHREAD*)((char*)((char*)Buff + sizeof(USMHEADER)));
vector<HANDLE> evs;
// Find
bool S = true;
for (unsigned int y = 0; y < MaxThreads; y++)
{
USMTHREAD& tt = th[y];
if (tt.evidx > 0)
{
// Open the event
TCHAR n[1000] = { 0 };
swprintf_s(n, L"%s%i", evrn.c_str(), tt.evidx);
HANDLE hEv = OpenEvent(SYNCHRONIZE, 0, n);
if (hEv == 0) // duh
{
S = false;
break;
}
evs.push_back(hEv);
}
}
DWORD fi = 0;
if (!S)
return (DWORD)-1;
if (evs.empty())
return (DWORD)-2;
// Wait for any thread to terminate reading
fi = WaitForMultipleObjects((DWORD)evs.size(), &evs[0], FALSE, Wait ? INFINITE : 0);
// Cleanup
for (unsigned int i = 0; i < evs.size(); i++)
CloseHandle(evs[i]);
evs.clear();
return fi;
}
T* BeginWrite(bool FailOnNotReady = false)
{
// Lock the writing mutex
if (FailOnNotReady)
{
DWORD x = WaitForSingleObject(hMutexWriting, 0);
if (x != WAIT_OBJECT_0)
return 0;
}
else
WaitForSingleObject(hMutexWriting, INFINITE);
// Having locked the writing mutex, no reading thread can start now
// After that, no new threads can read
vector<HANDLE> evs;
evs.reserve(MaxThreads);
// Wait for threads that are already in read state
USMTHREAD* th = (USMTHREAD*)((char*)((char*)Buff + sizeof(USMHEADER)));
// Find
bool S = true;
for (unsigned int y = 0; y < MaxThreads; y++)
{
USMTHREAD& tt = th[y];
if (tt.evidx > 0)
{
// Open the event
TCHAR n[1000] = { 0 };
swprintf_s(n, L"%s%i", evrn.c_str(), tt.evidx);
HANDLE hEv = OpenEvent(SYNCHRONIZE, 0, n);
if (hEv == 0) // duh
{
S = false;
break;
}
evs.push_back(hEv);
}
}
DWORD fi = 0;
if (S)
{
// Wait for all these threads to terminate reading
fi = WaitForMultipleObjects((DWORD)evs.size(), &evs[0], TRUE, FailOnNotReady ? 0 : INFINITE);
if (fi == -1 || fi == WAIT_TIMEOUT)
S = false;
}
else
{
fi = (DWORD)-1;
}
// Cleanup
for (unsigned int i = 0; i < evs.size(); i++)
CloseHandle(evs[i]);
evs.clear();
if (!S)
{
ReleaseMutex(hMutexWriting);
return 0;
}
// Return the pointer
char* a1 = (char*)Buff;
a1 += sizeof(USMHEADER);
a1 += sizeof(USMTHREAD)*MaxThreads;
ResetEvent(hEventWrote);
return (T*)a1;
}
void EndWrite()
{
ReleaseMutex(hMutexWriting);
SetEvent(hEventWrote);
}
DWORD NotifyWrite(bool Wait)
{
// Wait for all these threads to terminate reading
return WaitForSingleObject(hEventWrote, Wait ? INFINITE : 0);
}
unsigned long long WriteData(const T* b, size_t sz, size_t offset = 0, bool FailIfNotReady = false)
{
T* ptr = BeginWrite(FailIfNotReady);
if (!ptr)
return (unsigned long long) - 1;
memcpy(ptr + offset, b, sz);
EndWrite();
return sz;
}
// Sends data, then waits until all threads have read that data
unsigned long long SendDataAndWait(const T*b, size_t sz, size_t offset = 0)
{
unsigned long long r = WriteData(b, sz, offset);
if (r != sz)
return r;
USMTHREAD* th = (USMTHREAD*)((char*)((char*)Buff + sizeof(USMHEADER)));
vector<HANDLE> evs;
// Find
bool S = true;
for (unsigned int y = 0; y < MaxThreads; y++)
{
USMTHREAD& tt = th[y];
if (tt.id == GetCurrentThreadId())
continue;
if (tt.evidx > 0)
{
// Open the event
TCHAR n[1000] = { 0 };
swprintf_s(n, L"%s%i", evrn2.c_str(), tt.evidx);
HANDLE hEv = OpenEvent(SYNCHRONIZE, 0, n);
if (hEv == 0) // duh
{
S = false;
break;
}
evs.push_back(hEv);
}
}
if (!S)
return (DWORD)-1;
if (evs.empty())
return (DWORD)-2;
// Wait for all thread to terminate reading
WaitForMultipleObjects((DWORD)evs.size(), &evs[0], TRUE, INFINITE);
return r;
}
};
}
#endif // USM_H