forked from microsoft/Xbox-GDK-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXAsyncExamples.cpp
More file actions
1703 lines (1433 loc) · 65.7 KB
/
XAsyncExamples.cpp
File metadata and controls
1703 lines (1433 loc) · 65.7 KB
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//--------------------------------------------------------------------------------------
// XAsyncExamples.cpp
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "XAsyncExamples.h"
#include "AsynchronousProgramming.h"
using namespace ATG;
#ifdef __clang__
#pragma clang diagnostic ignored "-Wmicrosoft-cast"
#endif
XAsyncExamples* XAsyncExamples::s_singleton = nullptr;
Sample* XAsyncExamples::s_sample = nullptr;
RDTSCPStopWatch XAsyncExamples::s_stopWatch;
XAsyncExamples::XAsyncExamples(Sample* sample)
: m_testInProgress(false)
{
if (s_singleton)
{
throw new std::exception("Only create 1 instance of this class");
}
s_singleton = this;
s_sample = sample;
s_stopWatch.Start();
CreateTaskQueues();
CreateThreads();
}
XAsyncExamples::~XAsyncExamples()
{
ShutdownThreads();
ShutdownTaskQueues();
}
void XAsyncExamples::Update(float /*dt*/)
{
// This is called from the main thread
// Dispatch events from the completion port of the task queue used in the CustomGDKStyleAPIs test
XTaskQueueDispatch(m_taskQueue_CustomGDKStyleAPIs, XTaskQueuePort::Completion, 0);
}
void XAsyncExamples::Notify_TaskQueueMonitor(WPARAM /*wp*/, LPARAM /*lp*/)
{
// This function is invoked by the message pump of the main window on whatever thread is handling it.
XTaskQueueDispatch(m_taskQueue_ManualAndMessageLoop, XTaskQueuePort::Completion, 0);
}
void XAsyncExamples::CreateThreads()
{
char buffer[256] = {};
// Create shutdown event for all threads
m_shutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
m_numCores = std::thread::hardware_concurrency();
sprintf_s(buffer, 256, u8"Detected core count at [%d]", m_numCores);
s_sample->Log(buffer);
sprintf_s(buffer, 256, u8"Spinning up %d threads with their affinities set to a single core", m_numCores);
s_sample->Log(buffer);
m_threads_SingleCore.reserve(m_numCores);
for(unsigned int i = 0; i < m_numCores; ++i)
{
m_threads_SingleCore.push_back(std::thread(
[&, i]()
{
ThreadProc_SingleCore(i);
}));
}
unsigned int numManualThreadsToSpin = m_numCores >= 4 ? m_numCores - 2 : 2;
sprintf_s(buffer, 256, u8"Spinning up %d threads with their affinities set to all cores except 0 and 1", numManualThreadsToSpin);
s_sample->Log(buffer);
m_threads_ManualAndMessageLoop.reserve(numManualThreadsToSpin);
for (unsigned int i = 0; i < numManualThreadsToSpin; ++i)
{
m_threads_ManualAndMessageLoop.push_back(std::thread(
[&]()
{
ThreadProc_ManualAndMessageLoop();
}));
}
}
void XAsyncExamples::ShutdownThreads()
{
SetEvent(m_shutdownEvent);
for (auto& thread : m_threads_SingleCore)
{
thread.join();
}
for (auto& thread : m_threads_ManualAndMessageLoop)
{
thread.join();
}
}
void XAsyncExamples::ThreadProc_SingleCore(unsigned int index)
{
char buffer[256] = {};
sprintf_s(buffer, 256, u8"[ThreadProc_SingleCore] Thread Index %d Spawned!", index);
s_sample->Log(buffer);
// Set core affinity to this thread's index only
DWORD_PTR result = SetThreadAffinityMask(GetCurrentThread(), 1ull << index);
if (result == 0)
{
sprintf_s(buffer, 256, u8"[ThreadProc_SingleCore][%d]: Failed to set affinity mask", index);
s_sample->Log(buffer);
}
while (WaitForSingleObject(m_shutdownEvent, 0) == WAIT_TIMEOUT)
{
// XTaskQueueDispatch is thread-safe for manual ports. The different threads dispatching will pull
// work on a first-come, first-serve basis. Use a timeout so that the threads will sleep a little when there
// is no work and not steal all system performance.
XTaskQueueDispatch(m_taskQueue_ParallelFor, XTaskQueuePort::Work, 200);
}
sprintf_s(buffer, 256, u8"[ThreadProc_SingleCore][%d]: Exiting", index);
s_sample->Log(buffer);
}
void XAsyncExamples::ThreadProc_ManualAndMessageLoop()
{
char buffer[256] = {};
s_sample->Log(u8"[ThreadProc_ManualAndMessageLoop] Thread Index Spawned!");
// Set core affinity all threads except thread 0 and 1
DWORD_PTR affinityMask = ~0x3ull;
DWORD_PTR result = SetThreadAffinityMask(GetCurrentThread(), affinityMask);
if (result == 0)
{
sprintf_s(buffer, 256, u8"[ThreadProc_ManualAndMessageLoop]: Failed to set affinity mask");
s_sample->Log(buffer);
}
while (WaitForSingleObject(m_shutdownEvent, 0) == WAIT_TIMEOUT)
{
// XTaskQueueDispatch is thread-safe for manual ports. The different threads dispatching will pull
// work on a first-come, first-serve basis. Use a timeout so that the threads will sleep a little when there
// is no work and not steal all system performance.
XTaskQueueDispatch(m_taskQueue_ManualAndMessageLoop, XTaskQueuePort::Work, 200);
}
sprintf_s(buffer, 256, u8"[ThreadProc_ManualAndMessageLoop]: Exiting");
s_sample->Log(buffer);
}
void XAsyncExamples::Monitor_ManualAndMessageLoop(XTaskQueueHandle /*queue*/, XTaskQueuePort port)
{
char buffer[256] = {};
// Only run the completion work via the message pump. The work work will be run by the spawned threads for this test.
if (port == XTaskQueuePort::Work)
{
sprintf_s(buffer, 256, u8"Monitor Callback invoked for XTaskQueuePort::Work. Spawned threads will process manual port automatically.");
s_sample->Log(buffer);
}
else
{
sprintf_s(buffer, 256, u8"Monitor Callback invoked for XTaskQueuePort::Completion. Posting a WM_TASKQUEUEMONITOR for handling in the Windows message loop.");
s_sample->Log(buffer);
PostMessage(s_sample->GetHWND(), WM_TASKQUEUEMONITOR, 0, 0);
}
}
double XAsyncExamples::GetTime()
{
return s_stopWatch.GetCurrentSeconds();
}
void XAsyncExamples::GetFormattedTimingInfo(double seconds, const char** outLabel, double* outValue)
{
assert(outLabel && outValue);
// Microseconds:
if (seconds < 0.001)
{
(*outValue) = seconds * 1000.0 * 1000.0;
(*outLabel) = u8"us";
}
// Milliseconds
else if (seconds < 1.0)
{
(*outValue) = seconds * 1000.0;
(*outLabel) = u8"ms";
}
// Seconds
else
{
(*outValue) = seconds;
(*outLabel) = u8"s";
}
}
void XAsyncExamples::LogTiming(StopwatchProfiler<Overhead_Total>::TimingAccumulator* accumulator, const char* prefixLabel)
{
char buffer[512] = {};
double minTime = 0.0;
double maxTime = 0.0;
double avgTime = 0.0;
const char* minLabel = nullptr;
const char* maxLabel = nullptr;
const char* avgLabel = nullptr;
GetFormattedTimingInfo(accumulator->m_lowestVal, &minLabel, &minTime);
GetFormattedTimingInfo(accumulator->m_highestVal, &maxLabel, &maxTime);
GetFormattedTimingInfo(accumulator->GetAverage(), &avgLabel, &avgTime);
sprintf_s(buffer, 512, u8"%s: avg:%.3f%s min:%.3f%s max:%.3f%s", prefixLabel, avgTime, avgLabel, minTime, minLabel, maxTime, maxLabel);
s_sample->Log(buffer);
}
void XAsyncExamples::CreateTaskQueues()
{
// Cache the current process task queue. A ref to the retrieved task queue is automatically added.
XTaskQueueGetCurrentProcessTaskQueue(&m_cachedProcessDefaultTaskQueueHandle);
assert(m_cachedProcessDefaultTaskQueueHandle);
DX::ThrowIfFailed(XTaskQueueCreate(
XTaskQueueDispatchMode::ThreadPool,
XTaskQueueDispatchMode::Manual,
&m_taskQueue_CustomGDKStyleAPIs));
DX::ThrowIfFailed(XTaskQueueCreate(
XTaskQueueDispatchMode::Manual,
XTaskQueueDispatchMode::Immediate,
&m_taskQueue_ParallelFor));
DX::ThrowIfFailed(XTaskQueueCreate(
XTaskQueueDispatchMode::Immediate,
XTaskQueueDispatchMode::Immediate,
&m_taskQueue_SynchronousTaskQueue));
DX::ThrowIfFailed(XTaskQueueCreate(
XTaskQueueDispatchMode::SerializedThreadPool,
XTaskQueueDispatchMode::SerializedThreadPool,
&m_taskQueue_SerializedAsync));
DX::ThrowIfFailed(XTaskQueueCreate(
XTaskQueueDispatchMode::Manual,
XTaskQueueDispatchMode::Manual,
&m_taskQueue_ManualAndMessageLoop));
DX::ThrowIfFailed(XTaskQueueRegisterMonitor(
m_taskQueue_ManualAndMessageLoop,
this,
[](void* context, XTaskQueueHandle queue, XTaskQueuePort port)
{
XAsyncExamples* examples = static_cast<XAsyncExamples*>(context);
examples->Monitor_ManualAndMessageLoop(queue, port);
},
&m_taskQueueToken_ManualAndMessageloop));
}
void XAsyncExamples::ShutdownTaskQueues()
{
// XTaskQueueTerminate adds to the terminated queue a task marker to know when the queue is empty.
// As a result, you can't directly call XTaskQueueTerminate with wait=true on any task queue with a
// manual port. Instead, you need to optionally use the terminate callbacks and keep pumping the manual
// of the queue until the terminate finishes. When a queue is terminated, an infinite-dispatching call
// will return, so we can use the behavior.
XTaskQueueTerminate(m_taskQueue_CustomGDKStyleAPIs, false, nullptr, nullptr);
XTaskQueueDispatch(m_taskQueue_CustomGDKStyleAPIs, XTaskQueuePort::Completion, INFINITE);
XTaskQueueCloseHandle(m_taskQueue_CustomGDKStyleAPIs);
XTaskQueueTerminate(m_taskQueue_ParallelFor, false, nullptr, nullptr);
XTaskQueueDispatch(m_taskQueue_ParallelFor, XTaskQueuePort::Work, INFINITE);
XTaskQueueCloseHandle(m_taskQueue_ParallelFor);
XTaskQueueTerminate(m_taskQueue_SynchronousTaskQueue, true, nullptr, nullptr);
XTaskQueueCloseHandle(m_taskQueue_SynchronousTaskQueue);
XTaskQueueTerminate(m_taskQueue_SerializedAsync, true, nullptr, nullptr);
XTaskQueueCloseHandle(m_taskQueue_SerializedAsync);
XTaskQueueUnregisterMonitor(m_taskQueue_ManualAndMessageLoop, m_taskQueueToken_ManualAndMessageloop);
XTaskQueueTerminate(m_taskQueue_ManualAndMessageLoop, false, nullptr, nullptr);
XTaskQueueDispatch(m_taskQueue_ManualAndMessageLoop, XTaskQueuePort::Work, INFINITE);
XTaskQueueDispatch(m_taskQueue_ManualAndMessageLoop, XTaskQueuePort::Completion, INFINITE);
XTaskQueueCloseHandle(m_taskQueue_ManualAndMessageLoop);
// Release the handle we acquired to the default process task queue
XTaskQueueTerminate(m_cachedProcessDefaultTaskQueueHandle, true, nullptr, nullptr);
XTaskQueueCloseHandle(m_cachedProcessDefaultTaskQueueHandle);
m_cachedProcessDefaultTaskQueueHandle = nullptr;
}
// This test demonstrates how to create and use GDK-API-style async invocation methods. See NthPrimeAsync and NthPrimeAsyncResult for
// the async plumbing implementation. The test function calls into NthPrimeAsync several times in the same manner as GDK Async functions.
// The work of calculating the primes is done via the system thread pool which takes tasks as they can. The completion callbacks will be handled
// by the main thread via the Update call of this class. Callbacks are handled in the order that they are queued to the task queue.
void XAsyncExamples::StartTest_CustomGDKStyleAPIs()
{
if (m_testInProgress)
{
return;
}
m_testInProgress = true;
s_sample->Log(u8"==========================================================================");
s_sample->Log(u8"Beginning CustomGDKStyleAPIs test");
// Begin several async invocations of NthPrime. Note that the XAsyncBlock cannot be shared between the different calls as it has internal
// bookkeeping information. Using a lambda to reduce code duplication
auto StartAsyncCall = [&](uint64_t n, volatile LONG* testCounter, LONG numTests)
{
struct CallData
{
uint64_t n;
volatile LONG* testCounter;
LONG numTests;
XAsyncExamples* examples;
};
CallData* callData = new CallData{n, testCounter, numTests, this};
XAsyncBlock* async = new XAsyncBlock{};
ZeroMemory(async, sizeof(XAsyncBlock));
async->queue = m_taskQueue_CustomGDKStyleAPIs;
async->context = callData;
async->callback = [](XAsyncBlock* async)
{
CallData* callData = static_cast<CallData*>(async->context);
uint64_t nthPrime = 0;
HRESULT hr = NthPrimeAsyncResult(async, &nthPrime);
if (SUCCEEDED(hr))
{
char buffer[256] = {};
sprintf_s(buffer, 256, u8"Async call to calculate the nth prime %llu finished with result %llu", callData->n, nthPrime);
s_sample->Log(buffer);
}
else
{
char buffer[256] = {};
sprintf_s(buffer, 256, u8"Async call to calculate the nth prime %llu failed with hr=%08X", callData->n, hr);
s_sample->Log(buffer);
}
// Detect when test is done
LONG counterValue = InterlockedIncrement(callData->testCounter);
if (counterValue == callData->numTests)
{
s_sample->Log(u8"CustomGDKStyleAPIs test finished");
s_sample->Log(u8"==========================================================================");
callData->examples->m_testInProgress = false;
delete callData->testCounter;
}
delete callData;
delete async;
};
char buffer[256] = {};
sprintf_s(buffer, 256, u8"Firing async call to calculate the nth prime %llu", n);
s_sample->Log(buffer);
DX::ThrowIfFailed(NthPrimeAsync(n, async));
};
volatile LONG* testCounter = new volatile LONG(0);
const LONG numTests = 5;
StartAsyncCall(12000, testCounter, numTests);
StartAsyncCall(9000, testCounter, numTests);
StartAsyncCall(7500, testCounter, numTests);
StartAsyncCall(5000, testCounter, numTests);
StartAsyncCall(3000, testCounter, numTests);
}
// This test shows how you can use different task queues to run asynchronous code synchronously. The
// whole test is run on a task as well to prevent the main process thread from locking up during the
// test. The process default task queue the the task queue used when nullptr is passed to the queue
// member in an XAsyncBlock. That task queue can be acquired, changed, and cleared as desired for
// your application.
void XAsyncExamples::StartTest_SynchronousTaskQueue()
{
if (m_testInProgress)
{
return;
}
m_testInProgress = true;
s_sample->Log(u8"==========================================================================");
s_sample->Log(u8"Beginning SynchronousTaskQueue test");
// If the process task queue is overridden to nullptr, any attempts to use it with a nullptr in XAsyncBlock
// will fail.
//XTaskQueueSetCurrentProcessTaskQueue(nullptr);
// Override the process default task queue with the synchronous task queue
XTaskQueueSetCurrentProcessTaskQueue(m_taskQueue_SynchronousTaskQueue);
RunTask(m_cachedProcessDefaultTaskQueueHandle,
// Work callback
[&]()
{
static constexpr unsigned int taskCount = 20;
static constexpr uint64_t nthPrimeToCalculate = 3000;
// First perform some tasks in parallel and time it
{
double startTime = GetTime();
ParallelExecute(taskCount, m_cachedProcessDefaultTaskQueueHandle,
[=](unsigned int)
{
NthPrime(nthPrimeToCalculate);
});
double endTime = GetTime();
char buffer[256] = {};
sprintf_s(buffer, 256, u8"Asynchronously calculating NthPrime(%llu) %d times took %.3f seconds.", nthPrimeToCalculate, taskCount, endTime - startTime);
s_sample->Log(buffer);
}
// Next perform those tasks, but do them synchronously on this thread and time it
{
// The process default task queue was overridden to a queue with both ports set to Immediate,
// so these calculations will run synchronously.
double startTime = GetTime();
ParallelExecute(taskCount, nullptr,
[=](unsigned int)
{
NthPrime(nthPrimeToCalculate);
});
double endTime = GetTime();
char buffer[256] = {};
sprintf_s(buffer, 256, u8"Synchronously calculating NthPrime(%llu) %d times took %.3f seconds.", nthPrimeToCalculate, taskCount, endTime - startTime);
s_sample->Log(buffer);
}
},
// Completion callback
[&]()
{
// Restore the process default task queue to the original ThreadPool one.
XTaskQueueSetCurrentProcessTaskQueue(m_cachedProcessDefaultTaskQueueHandle);
s_sample->Log(u8"SynchronousTaskQueue test finished");
s_sample->Log(u8"==========================================================================");
m_testInProgress = false;
});
}
// This test executes some tasks that are handled via a fully manual task queue. The work is performed on some threads that have their
// affinity set off of the first two cores. The completion callbacks are handled via the Windows Message Loop. To get the handling done there,
// a monitor was registered for this test's task queue that invokes an event to be propagated to the message loop.
void XAsyncExamples::StartTest_ManualAndMessageLoop()
{
if (m_testInProgress)
{
return;
}
m_testInProgress = true;
s_sample->Log(u8"==========================================================================");
s_sample->Log(u8"Beginning ManualAndMessageLoop test");
volatile LONG* testCounter = new volatile LONG(0);
// Run a bunch of tasks using the manual queue. Log will report the monitor invocations.
static constexpr LONG numTasks = 20;
for (LONG i = 0; i < numTasks; ++i)
{
char buffer[256] = {};
sprintf_s(buffer, 256, u8"Queuing Task Index [%d]", i);
s_sample->Log(buffer);
RunTask(m_taskQueue_ManualAndMessageLoop,
// Work callback
[i]()
{
char buffer[256] = {};
sprintf_s(buffer, 256, u8"Work Callback Index [%d] Invoked. Sleeping to simulate work.", i);
s_sample->Log(buffer);
// Simulate some time-taking work
Sleep(2000);
sprintf_s(buffer, 256, u8"Work Callback Index [%d] sleep finished.", i);
s_sample->Log(buffer);
},
// Completions callback
[&, i, testCounter]()
{
char buffer[256] = {};
sprintf_s(buffer, 256, u8"Completion Callback Index [%d] Invoked", i);
s_sample->Log(buffer);
// Detect when test is done
LONG counterValue = InterlockedIncrement(testCounter);
if (counterValue == numTasks)
{
delete testCounter;
s_sample->Log(u8"ManualAndMessageLoop test finished");
s_sample->Log(u8"==========================================================================");
m_testInProgress = false;
}
});
}
}
// This test shows how to use the SerializedThreadPool setting for the ports of the task queue. A serialized thread pool port
// processes work asynchronously on the system thread pools in the same way as a ThreadPool port, but only processes 1 element
// from the queue at a time. This allows the work to have dependencies between steps, but removes parallelism.
void XAsyncExamples::StartTest_SerializedAsync()
{
if (m_testInProgress)
{
return;
}
m_testInProgress = true;
s_sample->Log(u8"==========================================================================");
s_sample->Log(u8"Beginning SerializedAsync test");
// The following tasks all have dependencies upon each other and are ensured to run in-order with the serialized thread pool task queue.
// However it's important to note that this only applies to callbacks within the same port. Each port of the task queue could be set differently,
// so the completion callback from one task will still run in parallel with a work callback from another task.
// A use-case might be web requests, database queries, or other type of work which runs in the background and depends on the results at each step.
std::vector<uint64_t>* workSet = new std::vector<uint64_t>();
// First initialize the work set with some randomized numbers (use srand to be deterministic)
RunTask(m_taskQueue_SerializedAsync,
[workSet]()
{
char buffer[512] = {};
size_t bytesWritten = static_cast<size_t>(sprintf_s(buffer, 512, u8"Initialized work set to { "));
srand(0);
for (unsigned int i = 0; i < 20; ++i)
{
uint64_t randVal = static_cast<uint64_t>(rand() % 3000);
workSet->push_back(randVal);
bytesWritten += sprintf_s(buffer + bytesWritten, 512 - bytesWritten, u8"%llu ", randVal);
}
bytesWritten += sprintf_s(buffer + bytesWritten, 512 - bytesWritten, u8"}");
s_sample->Log(buffer);
});
// Next sort the numbers
RunTask(m_taskQueue_SerializedAsync,
[workSet]()
{
std::sort(workSet->begin(), workSet->end());
char buffer[512] = {};
size_t bytesWritten = static_cast<size_t>(sprintf_s(buffer, 512, u8"Sorted work set to { "));
for (unsigned int i = 0; i < workSet->size(); ++i)
{
bytesWritten += sprintf_s(buffer + bytesWritten, 512 - bytesWritten, u8"%llu ", (*workSet)[i]);
}
bytesWritten += sprintf_s(buffer + bytesWritten, 512 - bytesWritten, u8"}");
s_sample->Log(buffer);
});
// Use the numbers in the work set to calculate the nth primes
RunTask(m_taskQueue_SerializedAsync,
[workSet]()
{
char buffer[512] = {};
size_t bytesWritten = static_cast<size_t>(sprintf_s(buffer, 512, u8"NthPrimes for work set calculated to { "));
for (unsigned int i = 0; i < workSet->size(); ++i)
{
(*workSet)[i] = NthPrime((*workSet)[i]);
bytesWritten += sprintf_s(buffer + bytesWritten, 512 - bytesWritten, u8"%llu ", (*workSet)[i]);
}
bytesWritten += sprintf_s(buffer + bytesWritten, 512 - bytesWritten, u8"}");
s_sample->Log(buffer);
});
// Update all the numbers to be the prefix sum of itself
RunTask(m_taskQueue_SerializedAsync,
[workSet]()
{
char buffer[512] = {};
size_t bytesWritten = static_cast<size_t>(sprintf_s(buffer, 512, u8"Prefix sum for work set calculated to { "));
bytesWritten += sprintf_s(buffer + bytesWritten, 512 - bytesWritten, u8"%llu ", (*workSet)[0]);
for (unsigned int i = 1; i < workSet->size(); ++i)
{
(*workSet)[i] += (*workSet)[i - 1];
bytesWritten += sprintf_s(buffer + bytesWritten, 512 - bytesWritten, u8"%llu ", (*workSet)[i]);
}
bytesWritten += sprintf_s(buffer + bytesWritten, 512 - bytesWritten, u8"}");
s_sample->Log(buffer);
});
// Finally just end the test
RunTask(m_taskQueue_SerializedAsync,
[&, workSet]()
{
delete workSet;
s_sample->Log(u8"SerializedAsync test finished");
s_sample->Log(u8"==========================================================================");
m_testInProgress = false;
});
}
// This test shows how to use the XAsync libraries to setup a ParallelFor style function to take advantage of multi-process
// calculations. A prefix sum calculation is performed in both single-thread and multi-thread manner to show the difference
// of timings. The parallelization is handled via a manual work port in the task queue. A thread was spawned with affinity set
// to a single core and these threads dispatch the work port themselves. Finally, the test itself is run on another thread
// using the default process task queue so-as to prevent the application from freezing while the tests are running.
void XAsyncExamples::StartTest_ParallelFor()
{
if (m_testInProgress)
{
return;
}
m_testInProgress = true;
s_sample->Log(u8"==========================================================================");
s_sample->Log(u8"Beginning ParallelFor test");
// nullptr specifies to use the process default queue which is [ThreadPool,ThreadPool] when not overridden
RunTask(nullptr,
// Work callback
[&]()
{
// Calculate prefix sum
char buffer[256] = {};
// Setup elements to calculate over. Parallel case uses double-buffered shared memory.
// Don't time the setup.
const unsigned int numElements = 100000000;
uint64_t* elements = new uint64_t[numElements];
for (unsigned int i = 0; i < numElements; ++i)
{
elements[i] = i + 1;
}
// Sync calculation
{
sprintf_s(buffer, 256, u8"Starting synchronous prefix sum calculation for %d elements", numElements);
s_sample->Log(buffer);
// Allocate outside of timing
uint64_t* finalElements = new uint64_t[numElements];
memset(finalElements, 0, sizeof(uint64_t) * numElements);
double startTime = GetTime();
finalElements[0] = elements[0];
for (unsigned int i = 1; i < numElements; ++i)
{
finalElements[i] = finalElements[i - 1] + elements[i];
}
double endTime = GetTime();
sprintf_s(buffer, 256, u8"Synchronous prefix sum calculation for %d elements took %.03f milliseconds", numElements, (endTime - startTime) * 1000.0);
s_sample->Log(buffer);
delete[] finalElements;
}
// Async calculation
{
sprintf_s(buffer, 256, u8"Starting asynchronous (ParallelFor) prefix sum calculation for %d elements", numElements);
s_sample->Log(buffer);
// Allocate outside of timing
uint64_t* finalElements = new uint64_t[numElements];
memset(finalElements, 0, sizeof(uint64_t) * numElements);
uint64_t* offsetElements = new uint64_t[s_singleton->m_numCores];
memset(offsetElements, 0, sizeof(uint64_t) * s_singleton->m_numCores);
double startTime = GetTime();
const unsigned int numElementsPerThread = numElements / (s_singleton->m_numCores + 1);
const unsigned int extraElementsForLastThread = numElements % (s_singleton->m_numCores + 1);
// First sweep to calculate offsets
// First sweep only does the first m_numCores sets of data out of (m_numCores + 1)
ParallelFor(0, s_singleton->m_numCores,
[&](unsigned int index)
{
const unsigned int offset = (index * numElementsPerThread);
finalElements[offset + 0] = elements[offset + 0];
for (unsigned int i = 1; i < numElementsPerThread; ++i)
{
finalElements[offset + i] = finalElements[offset + i - 1] + elements[offset + i];
}
});
// Fast calculation of prefix sum of offsets
offsetElements[0] = finalElements[numElementsPerThread - 1];
for (unsigned int i = 1; i < s_singleton->m_numCores; ++i)
{
const unsigned int curFinalElementsOffset = (numElementsPerThread - 1) + (numElementsPerThread * i);
offsetElements[i] = finalElements[curFinalElementsOffset] + offsetElements[i - 1];
}
// Second sweep to finalize prefix sums
// Second sweep does the last m_numCores sets of data out of (m_numCores + 1), also taking into account the larger last set
ParallelFor(0, s_singleton->m_numCores,
[&](unsigned int index)
{
const unsigned int numElementsThisThread = (index == s_singleton->m_numCores - 1) ? numElementsPerThread + extraElementsForLastThread : numElementsPerThread;
const unsigned int offset = ((index + 1) * numElementsPerThread);
finalElements[offset + 0] = elements[offset + 0] + offsetElements[index];
for (unsigned int i = 1; i < numElementsThisThread; ++i)
{
finalElements[offset + i] = finalElements[offset + i - 1] + elements[offset + i];
}
});
double endTime = GetTime();
sprintf_s(buffer, 256, u8"Asynchronous (ParallelFor) prefix sum calculation for %d elements took %.03f milliseconds", numElements, (endTime - startTime) * 1000.0);
s_sample->Log(buffer);
delete[] offsetElements;
delete[] finalElements;
}
delete[] elements;
},
// Completion callback
[&]()
{
s_sample->Log(u8"ParallelFor test finished");
s_sample->Log(u8"==========================================================================");
m_testInProgress = false;
});
}
// This test shows how to use some more advanced features of XAsync/XTaskQueue. Instead of creating resources at class creation,
// all resources needed for this test will be created inline for demonstration. Demonstrated features include composite queues,
// duplicating queue handles, and using waiters and delayed dispatching. See documentation for more information about the behavior
// of these features. Note that all these tests will run in parallel as well.
void XAsyncExamples::StartTest_AdvancedUsage()
{
if (m_testInProgress)
{
return;
}
m_testInProgress = true;
s_sample->Log(u8"==========================================================================");
s_sample->Log(u8"Beginning AdvancedUsage test");
// Test data
struct TestData
{
XTaskQueueHandle baseQueue;
XTaskQueueHandle compositeQueue;
XTaskQueuePortHandle baseQueueWorkPort;
std::thread baseQueueDispatcherThread;
XTaskQueueHandle duplicateBaseQueueHandle;
HANDLE waiterEvent;
XTaskQueueRegistrationToken waiterToken;
std::function<void(TestData*)> autoCleanupFn;
volatile LONG* testCounter;
};
TestData* testData = new TestData();
testData->testCounter = new volatile LONG(0);
// Make a function that will auto-cleanup on the last call as long as it's called "totalTests" amount of times.
static constexpr LONG totalTests = 4;
testData->autoCleanupFn = [&](TestData* testData)
{
LONG counterValue = InterlockedIncrement(testData->testCounter);
if (counterValue == totalTests)
{
s_sample->Log(u8"AdvancedUsage test finished. Cleaning up.");
XTaskQueueUnregisterWaiter(testData->baseQueue, testData->waiterToken);
CloseHandle(testData->waiterEvent);
XTaskQueueTerminate(testData->compositeQueue, true, nullptr, nullptr);
XTaskQueueTerminate(testData->baseQueue, true, nullptr, nullptr);
XTaskQueueCloseHandle(testData->compositeQueue);
XTaskQueueCloseHandle(testData->duplicateBaseQueueHandle);
XTaskQueueCloseHandle(testData->baseQueue);
testData->baseQueueDispatcherThread.join();
m_testInProgress = false;
delete testData->testCounter;
delete testData;
s_sample->Log(u8"==========================================================================");
}
};
// Composite Queue
// Composite queues are queues composed of ports of other queues instead of owning their own ports. In this test, the composite
// queue uses the work port from the base queue as its own work and completion port. Therefore, dispatching work to the composite
// queue will all fall on the work port thread from the base queue which is set to ThreadPool.
{
DX::ThrowIfFailed(XTaskQueueCreate(XTaskQueueDispatchMode::ThreadPool, XTaskQueueDispatchMode::Manual, &testData->baseQueue));
DX::ThrowIfFailed(XTaskQueueGetPort(testData->baseQueue, XTaskQueuePort::Work, &testData->baseQueueWorkPort));
DX::ThrowIfFailed(XTaskQueueCreateComposite(testData->baseQueueWorkPort, testData->baseQueueWorkPort, &testData->compositeQueue));
s_sample->Log(u8"Running a task on the compositeQueue for the compositeQueue test");
RunTask(testData->compositeQueue,
// Work callback
[]()
{
s_sample->Log(u8"Work callback executed on composite queue work port (base queue work port)");
},
// Completion callback
[testData]()
{
s_sample->Log(u8"Completion callback executed on composite queue completion port (base queue work port)");
// Ensures to clean up if this callback is last
testData->autoCleanupFn(testData);
});
// Spawn a thread to handle the manual port of the base queue
testData->baseQueueDispatcherThread = std::thread(
[testData]()
{
while (true)
{
bool processed = XTaskQueueDispatch(testData->baseQueue, XTaskQueuePort::Completion, INFINITE);
if (!processed)
{
s_sample->Log(u8"XTaskQueueDispatch on baseQueue returned false, exiting thread.");
break;
}
}
});
}
// Duplicating Queue Handles
// Duplicate handles to increase ref count and introduce multiple ownership. For proper closing of the task queue, all handles must be closed.
{
DX::ThrowIfFailed(XTaskQueueDuplicateHandle(testData->baseQueue, &testData->duplicateBaseQueueHandle));
// baseQueue and duplicateBaseQueueHandle now both reference the same queue and its ref count is 2 requiring both handles to be closed in cleanup.
s_sample->Log(u8"Running a task on the baseQueue for duplicate handles test");
RunTask(testData->baseQueue,
// Work callback
[]()
{
s_sample->Log(u8"Work callback invoked on baseQueue");
},
// Completion callback
[testData]()
{
s_sample->Log(u8"Completion callback invoked on baseQueue");
// Ensures to clean up if this callback is last
testData->autoCleanupFn(testData);
});
s_sample->Log(u8"Running a task on the duplicateBaseQueueHandle for duplicate handles test");
RunTask(testData->duplicateBaseQueueHandle,
// Work callback
[]()
{
s_sample->Log(u8"Work callback invoked on baseQueue via duplicateBaseQueueHandle");
},
// Completion callback
[testData]()
{
s_sample->Log(u8"Completion callback invoked on baseQueue via duplicateBaseQueueHandle");
// Ensures to clean up if this callback is last
testData->autoCleanupFn(testData);
});
}
// Waiters and delayed dispatching
// Callbacks can be directly submitted to the ports of a task queue with optional wait intervals. In addition, a special waiter can be used
// to cause the callback to not be submitted until an event is signaled.
{
testData->waiterEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
DX::ThrowIfFailed(XTaskQueueRegisterWaiter(testData->baseQueue, XTaskQueuePort::Work, testData->waiterEvent, testData,
[](void* context, bool /*cancel*/)
{
TestData* testData = static_cast<TestData*>(context);
s_sample->Log(u8"Callback registered via waiter invoked");
// Ensures to clean up if this callback is last
testData->autoCleanupFn(testData);
},
&testData->waiterToken));
// First immediately submit callback to the work port of the base queue
s_sample->Log(u8"Starting waiter and delayed callback submissions test with XTaskQueueSubmitCallback");
DX::ThrowIfFailed(XTaskQueueSubmitCallback(testData->baseQueue, XTaskQueuePort::Work, testData,
[](void* context, bool /*cancel*/)
{
TestData* testData = static_cast<TestData*>(context);
s_sample->Log(u8"XTaskQueueSubmitCallback callback invoked, calling XTaskQueueSubmitDelayedCallback");
// Next submit a delayed callback
DX::ThrowIfFailed(XTaskQueueSubmitDelayedCallback(testData->baseQueue, XTaskQueuePort::Work, 1000, context,
[](void* context, bool /*cancel*/)
{
TestData* testData = static_cast<TestData*>(context);
s_sample->Log(u8"XTaskQueueSubmitDelayedCallback callback invoked, setting auto-reset event to cause callback to be invoked via the waiter");
// Finally, use the waiter to fire a callback
Sleep(1000);
SetEvent(testData->waiterEvent);
}));
}));
}
}
// XAsyncCancel can be used to cancel requests, but that request must have implemented a cancel path in its provider.
// Not all async requests can be canceled and any request started with XAsyncRun cannot be canceled. This test will show how
// an async request can be implemented using the GDK-style to handle canceling and how to invoke the cancel.
void XAsyncExamples::StartTest_Canceling()
{
if (m_testInProgress)
{
return;
}
m_testInProgress = true;
s_sample->Log(u8"==========================================================================");
s_sample->Log(u8"Beginning Canceling test");
XAsyncBlock* async = new XAsyncBlock{};
ZeroMemory(async, sizeof(XAsyncBlock));
async->queue = nullptr; // default process queue
async->context = this;
async->callback = [](XAsyncBlock* async)
{
XAsyncExamples* examples = static_cast<XAsyncExamples*>(async->context);
HRESULT status = XAsyncGetStatus(async, false);
char buffer[256] = {};
sprintf_s(buffer, 256, u8"CancelableInfiniteTaskAsync() finished with status hr=0x%08X", status);
s_sample->Log(buffer);
assert(status == E_ABORT); // This is an abort test, so the status should be E_ABORT
s_sample->Log(u8"Canceling test finished");
s_sample->Log(u8"==========================================================================");
examples->m_testInProgress = false;
delete async;
};
s_sample->Log(u8"Starting CancelableInfiniteTaskAsync() on the default process task queue");
DX::ThrowIfFailed(CancelableInfiniteTaskAsync(async));
// Also start a task to cancel the task after a short time
RunTask(nullptr,
[async]()
{
s_sample->Log(u8"Waiting several seconds before canceling CancelableInfiniteTaskAsync()...");
Sleep(5250);
s_sample->Log(u8"Canceling CancelableInfiniteTaskAsync() with XAsyncCancel()");
XAsyncCancel(async);
});
}
// It's important to know the overhead of the async systems being used. This test will calculate some overhead costs for different
// usages of the async libraries to get an understanding of the performance. All costs calculated here are background costs and not
// costs of the user async work being dispatched.
void XAsyncExamples::StartTest_OverheadCalculations()
{
if (m_testInProgress)
{
return;
}
m_testInProgress = true;
s_sample->Log(u8"==========================================================================");
s_sample->Log(u8"Beginning overhead calculation test");
StopwatchProfiler<Overhead_Total>* overheadProfiler_DefaultProcessQueue = new StopwatchProfiler<Overhead_Total>();
StopwatchProfiler<Overhead_Total>* overheadProfiler_ManualQueue = new StopwatchProfiler<Overhead_Total>();