-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CancelRequests - Allow multiple cancel request writers in a single job
Add the ability to require multiple cancel streams in a single job. Requiring is done as you would expect, just target the different streams on the JobConfig. Fulfilling the writer/instance is done by passing the stream instance into the `Fulfill` method. The access wrappers now represent access to the cancel type not a specific cancel stream instance. This works because all of the cancel streams share the same SharedWrite Pending collection so getting write access for one stream grants write access for all. The instance passed through `Fulfill` will get safety checked in the access wrapper and delivered back to be used for writer creation. Behaviour: - One cancel request stream required: `jobData.Fulfill` behaves as normal, no explicit instance required. - N cancel request stream required: `jobData.Fulfill` must be provided the instance for each stream to fulfill otherwise the safety system will throw an exception. Note: This is a workaround implementation. #224 will make this cleaner. This approach can be applied to all write wide (shard write) types. Support will be added in an upcoming commit.
- Loading branch information
Showing
6 changed files
with
118 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 69 additions & 6 deletions
75
...pts/Runtime/Entities/TaskDriver/TaskSet/Job/Wrapper/CancelRequestsPendingAccessWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,92 @@ | ||
using Anvil.CSharp.Logging; | ||
using Anvil.Unity.DOTS.Jobs; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using Unity.Jobs; | ||
|
||
namespace Anvil.Unity.DOTS.Entities.TaskDriver | ||
{ | ||
internal class CancelRequestsPendingAccessWrapper : AbstractAccessWrapper | ||
{ | ||
public CancelRequestsDataStream CancelRequestsDataStream { get; } | ||
private readonly CancelRequestsDataStream m_DefaultStream; | ||
|
||
public CancelRequestsPendingAccessWrapper( | ||
CancelRequestsDataStream cancelRequestsDataStream, | ||
CancelRequestsDataStream defaultStream, | ||
AccessType accessType, | ||
AbstractJobConfig.Usage usage) | ||
: base(accessType, usage) | ||
{ | ||
CancelRequestsDataStream = cancelRequestsDataStream; | ||
m_DefaultStream = defaultStream; | ||
DEBUG_TrackRequiredStream(defaultStream); | ||
} | ||
|
||
public override JobHandle AcquireAsync() | ||
{ | ||
return CancelRequestsDataStream.AcquirePendingAsync(AccessType); | ||
return m_DefaultStream.AcquirePendingAsync(AccessType); | ||
} | ||
|
||
public override void ReleaseAsync(JobHandle dependsOn) | ||
{ | ||
CancelRequestsDataStream.ReleasePendingAsync(dependsOn); | ||
m_DefaultStream.ReleasePendingAsync(dependsOn); | ||
} | ||
|
||
public CancelRequestsDataStream GetInstance(IAbstractCancelRequestDataStream explicitStream = null) | ||
{ | ||
DEBUG_EnforceExplicitStream(explicitStream); | ||
DEBUG_EnsureStreamWasRequired(explicitStream); | ||
|
||
return (CancelRequestsDataStream)explicitStream ?? m_DefaultStream; | ||
} | ||
|
||
public override void MergeStateFrom(AbstractAccessWrapper other) | ||
{ | ||
base.MergeStateFrom(other); | ||
|
||
CancelRequestsPendingAccessWrapper otherTyped = (CancelRequestsPendingAccessWrapper)other; | ||
m_DEBUG_RequiredStreams.UnionWith(otherTyped.m_DEBUG_RequiredStreams); | ||
DEBUG_TrackRequiredStream(otherTyped.m_DefaultStream); | ||
} | ||
|
||
//************************************************************************************************************* | ||
// SAFETY | ||
//************************************************************************************************************* | ||
|
||
// #if ANVIL_DEBUG_SAFETY | ||
private HashSet<CancelRequestsDataStream> m_DEBUG_RequiredStreams; | ||
|
||
[Conditional("ANVIL_DEBUG_SAFETY")] | ||
public void DEBUG_TrackRequiredStream(CancelRequestsDataStream stream) | ||
{ | ||
m_DEBUG_RequiredStreams ??= new HashSet<CancelRequestsDataStream>(1); | ||
m_DEBUG_RequiredStreams.Add(stream); | ||
} | ||
|
||
[Conditional("ANVIL_DEBUG_SAFETY")] | ||
private void DEBUG_EnsureStreamWasRequired(IAbstractCancelRequestDataStream stream) | ||
{ | ||
CancelRequestsDataStream cancelStream = (CancelRequestsDataStream)stream; | ||
if (stream == null || m_DEBUG_RequiredStreams.Contains(cancelStream)) | ||
{ | ||
return; | ||
} | ||
|
||
throw new Exception($"The explicit stream instance requested was not set as required. DataTargetID:{cancelStream.DataTargetID}"); | ||
} | ||
|
||
[Conditional("ANVIL_DEBUG_SAFETY")] | ||
private void DEBUG_EnforceExplicitStream(IAbstractCancelRequestDataStream stream) | ||
{ | ||
int requiredStreamCount = m_DEBUG_RequiredStreams.Count; | ||
if (stream == null && requiredStreamCount > 1) | ||
{ | ||
throw new Exception($"More than one stream has set this type as a requirement. The exact stream must be provided on retrieval. Type:{typeof(CancelRequestsDataStream).GetReadableName()}"); | ||
} | ||
|
||
if (stream != null && requiredStreamCount == 1) | ||
{ | ||
Logger.Warning($"An explicit stream was provided when not required. Consider using default fulfillment. Type:{typeof(CancelRequestsDataStream).GetReadableName()}"); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters