@@ -419,6 +419,23 @@ class queue_impl {
419
419
}
420
420
421
421
private:
422
+ void finalizeHandler (handler &Handler, bool NeedSeparateDependencyMgmt,
423
+ event &EventRet) {
424
+ if (MIsInorder) {
425
+ // Accessing and changing of an event isn't atomic operation.
426
+ // Hence, here is the lock for thread-safety.
427
+ std::lock_guard<std::mutex> Lock{MLastEventMtx};
428
+
429
+ if (NeedSeparateDependencyMgmt)
430
+ Handler.depends_on (MLastEvent);
431
+
432
+ EventRet = Handler.finalize ();
433
+
434
+ MLastEvent = EventRet;
435
+ } else
436
+ EventRet = Handler.finalize ();
437
+ }
438
+
422
439
// / Performs command group submission to the queue.
423
440
// /
424
441
// / \param CGF is a function object containing command group.
@@ -437,9 +454,9 @@ class queue_impl {
437
454
// Host and interop tasks, however, are not submitted to low-level runtimes
438
455
// and require separate dependency management.
439
456
const CG::CGTYPE Type = Handler.getType ();
440
- if (MIsInorder && (Type == CG::CGTYPE::CodeplayHostTask ||
441
- Type == CG::CGTYPE::CodeplayInteropTask))
442
- Handler. depends_on (MLastEvent );
457
+ bool NeedSeparateDependencyMgmt =
458
+ MIsInorder && ( Type == CG::CGTYPE::CodeplayHostTask ||
459
+ Type == CG::CGTYPE::CodeplayInteropTask );
443
460
444
461
event Event;
445
462
@@ -452,14 +469,11 @@ class queue_impl {
452
469
: ProgramManager::getInstance ().kernelUsesAssert (
453
470
Handler.MOSModuleHandle , Handler.MKernelName );
454
471
455
- Event = Handler. finalize ( );
472
+ finalizeHandler (Handler, NeedSeparateDependencyMgmt, Event );
456
473
457
474
(*PostProcess)(IsKernel, KernelUsesAssert, Event);
458
475
} else
459
- Event = Handler.finalize ();
460
-
461
- if (MIsInorder)
462
- MLastEvent = Event;
476
+ finalizeHandler (Handler, NeedSeparateDependencyMgmt, Event);
463
477
464
478
addEvent (Event);
465
479
return Event;
@@ -521,7 +535,11 @@ class queue_impl {
521
535
// Buffer to store assert failure descriptor
522
536
buffer<AssertHappened, 1 > MAssertHappenedBuffer;
523
537
538
+ // This event is employed for enhanced dependency tracking with in-order queue
539
+ // Access to the event should be guarded with MLastEventMtx
524
540
event MLastEvent;
541
+ std::mutex MLastEventMtx;
542
+
525
543
const bool MIsInorder;
526
544
};
527
545
0 commit comments