static void Main(string[] args) { var strVault = new BasicVault <string>(string.Empty); Thread t1 = new Thread(() => { Thread.SpinWait(50000); using var lck = strVault.SpinLock(); lck.Value += "Hello from thread 1, DotNetVault! "; }); Thread t2 = new Thread(() => { using var lck = strVault.SpinLock(); lck.Value += "Hello from thread 2, DotNetVault! "; }); t1.Start(); t2.Start(); t2.Join(); t1.Join(); string finalResult = strVault.CopyCurrentValue(TimeSpan.FromMilliseconds(100)); Console.WriteLine(finalResult); }
public ErrorState([NotNull] LaundryVault statusVault, BasicVault <LaundryMachineStateCode> basicV, IEventRaiser eventRaiser, [NotNull] ILaundryMachineTaskExecutionContext <TaskResult> context, TimeSpan addOneUnitDamp, TimeSpan removeOneUnitDirt, TimeSpan removeOneUnitDamp) : base( StateMachineStateType.Error, LaundryMachineStateCode.Error, statusVault, basicV, eventRaiser, context, addOneUnitDamp, removeOneUnitDirt, removeOneUnitDamp) => throw new NotImplementedException();
static void TestNoBasicVaultForStringBuilder() { BasicVault <string> bv = new BasicVault <string>("Hi mom", TimeSpan.FromSeconds(2)); //BasicVault<StringBuilder> bv1 = new BasicVault<StringBuilder>(new StringBuilder("Hi mom"), TimeSpan.FromSeconds(2)); using var l = bv.SpinLock(); Console.WriteLine(l.Value.ToString()); }
public FullState([NotNull] LaundryVault vault, [NotNull] BasicVault <LaundryMachineStateCode> stateCodeVault, [NotNull] IEventRaiser raiser, [NotNull] ILaundryMachineTaskExecutionContext <TaskResult> executionContext, TimeSpan addOneUnitDamp, TimeSpan removeOneUnitDirt, TimeSpan removeOneUnitDamp) : base( StateMachineStateType.WaitForMoreInputOnly, LaundryMachineStateCode.Full, vault, stateCodeVault, raiser, executionContext, addOneUnitDamp, removeOneUnitDirt, removeOneUnitDamp) { }
public static void DoTest() { BasicVault <BvIllegalRefExprTestCase1> vault = new BasicVault <BvIllegalRefExprTestCase1>(new BvIllegalRefExprTestCase1("Foobar")); { using var lck = vault.SpinLock(); ref var toodles = ref lck.Value; Console.WriteLine(toodles.ToString()); }
protected internal ActivatingState([NotNull] IEventRaiser raiser, [NotNull] BasicVault <LaundryMachineStateCode> stateVault, [NotNull] LaundryVault vault, [NotNull] ILaundryMachineTaskExecutionContext <TaskResult> executionContext, TimeSpan addOneUnitDamp, TimeSpan removeOneUnitDirt, TimeSpan removeOneUnitDamp) : base( LaundryMachineStateCode.Activating, CommandIds.PowerUp, raiser, stateVault, vault, executionContext, ImmutableArray.Create(LaundryMachineStateCode.Empty, LaundryMachineStateCode.Full, LaundryMachineStateCode.PoweredDown), addOneUnitDamp, removeOneUnitDirt, removeOneUnitDamp) { }
public static void TestUriIsVs() { BasicVault <string> bvs = new BasicVault <string>("Hi mom", TimeSpan.FromSeconds(2)); //BasicVault<StringBuilder> bvsb = new BasicVault<StringBuilder>(new StringBuilder(), TimeSpan.FromSeconds(2)); using (var lck = bvs.SpinLock()) { Console.WriteLine(lck.Value); } }
internal PoweredDownState([NotNull] LaundryVault vault, [NotNull] BasicVault <LaundryMachineStateCode> stateVault, [NotNull] IEventRaiser raiser, [NotNull] ILaundryMachineTaskExecutionContext <TaskResult> context, TimeSpan addOneUnitDamp, TimeSpan removeOneUnitDirt, TimeSpan removeOneUnitDamp) : base( StateMachineStateType.WaitForMoreInputOnly, LaundryMachineStateCode.PoweredDown, vault, stateVault, raiser, context, addOneUnitDamp, removeOneUnitDirt, removeOneUnitDamp) { if (!context.IsActive || context.IsDisposed || context.IsFaulted) { throw new ArgumentException("Context must not be in a disposed or a faulted state and must be active."); } }
protected LaundryStateMachineState(StateMachineStateType stateType, LaundryMachineStateCode code, [NotNull] LaundryVault vault, [NotNull] BasicVault <LaundryMachineStateCode> stateCodeVault, [NotNull] IEventRaiser raiser, [NotNull] ILaundryMachineTaskExecutionContext <TaskResult> executionContext, TimeSpan addOneUnitDamp, TimeSpan removeOneUnitDirt, TimeSpan removeOneUnitDamp) : base(code, vault, raiser) { TimeToAddOneUnitDampness = addOneUnitDamp; TimeToRemoveOneUnitDirt = removeOneUnitDirt; TimeToRemoveOneUnitDampness = removeOneUnitDamp; StateType = stateType.IsValueDefined() ? stateType : throw new ArgumentOutOfRangeException(nameof(stateType), stateType, "Supplied value [{stateType}] is not a defined " + "value of the [{typeof(StateMachineStateType).Name}] enumeration."); _context = executionContext ?? throw new ArgumentNullException(nameof(executionContext)); _stateCodeVault = stateCodeVault ?? throw new ArgumentNullException(nameof(stateCodeVault)); }
//BUG FIX 61 ... lck.Dispose used to cause potential race condition //bug fix 61 ... now lck.Dispose causes compiler error thanks to NotDirectlyInvocableAttribute //bug fix 61 ... which now annotates locked resource object's dispose method. They must be disposed //bug fix 61 ... via using, must be declared inline, must not be disposed manually. public static void DemonstrateDoubleDispose() { using (var bv = new BasicVault <DateTime>(DateTime.Now, TimeSpan.FromMilliseconds(250))) { { using var lck = bv.Lock(); Console.WriteLine(lck.Value.ToString("O")); lck.Value = lck.Value + TimeSpan.FromDays(25); //BUG 61 -- potential race condition / Known Flaw #1 //BUG 61 FIX -- POTENTIAL RACE CONDITION -- DO NOT DISPOSE MANUALLY IN THIS MANNER! //lck.Dispose(); AnotherDemonstrationOfBug61Fix(in lck); Console.WriteLine(lck.Value.ToString("O")); } //don't want this line but it will not trigger the NotDirectlyInvocable error because basic vault ctor not annotated with NotDirectlyInvocableAttribute // bv.Dispose(); } }
protected LaundryMachineTaskBasedStateBase(LaundryMachineStateCode code, CommandIds commandIds, [NotNull] IEventRaiser raiser, [NotNull] BasicVault <LaundryMachineStateCode> stateVault, [NotNull] LaundryVault vault, [NotNull] ILaundryMachineTaskExecutionContext <TaskResult> executionContext, ImmutableArray <LaundryMachineStateCode> nextStatesOnCompletion, TimeSpan addOneUnitDamp, TimeSpan removeOneUnitDirt, TimeSpan removeOneUnitDamp) : base( StateMachineStateType.WaitForTaskComplete, code, vault, stateVault, raiser, executionContext, addOneUnitDamp, removeOneUnitDirt, removeOneUnitDamp) { if (stateVault == null) { throw new ArgumentNullException(nameof(stateVault)); } if (nextStatesOnCompletion.IsDefault || nextStatesOnCompletion.IsEmpty || nextStatesOnCompletion.Contains(code)) { throw new ArgumentException( $@"Parameter must not be default, empty or contain the value passed by the {nameof(code)} parameter.", nameof(nextStatesOnCompletion)); } if (executionContext == null) { throw new ArgumentNullException(nameof(executionContext)); } if (!executionContext.IsActive || executionContext.IsDisposed) { throw new ArgumentException(@"The execution context is not in a useable state.", nameof(executionContext)); } if (code.GetStateTaskType() == TaskType.NullTask) { throw new ArgumentException("This state must be a task-based state."); } CommandId = commandIds; _taskEndedTransProcedure = new LocklessLazyWriteOnce <LTransProcedure>(InitTaskEndedTransProcedure); _taskEndedAdditionalTransProcedure = new LocklessLazyWriteOnce <LTransAdditionalProcedure>(InitTaskEndedAdditionalTransProc); _taskEndedTransition = new LocklessLazyWriteOnce <LaundryMachineStateTransition>(() => InitTaskEndedTransition(nextStatesOnCompletion)); _cancellationTransition = new LocklessLazyWriteOnce <LaundryMachineStateTransition>(() => InitCancellationTransition(code, commandIds)); }
public static void TestBasicVaultOfNullableNotUnmanagedValueType() { BasicVault <NotUnmanagedButVaultSafeValueType> bv = new BasicVault <NotUnmanagedButVaultSafeValueType>(new NotUnmanagedButVaultSafeValueType("Chris")); using (var lck = bv.Lock()) { Console.WriteLine(lck.Value.Name); } var thisTimeWithJohn = CreateBasicVault((string s) => new NotUnmanagedButVaultSafeValueType(s)); using (var lck = thisTimeWithJohn.Lock()) { Console.WriteLine(lck.Value.Name); } //BUG 49 fix verification BasicVault <NotUnmanagedButVaultSafeValueType?> nullableVb = new BasicVault <NotUnmanagedButVaultSafeValueType?>(null, TimeSpan.FromSeconds(2)); using (var lck = nullableVb.Lock()) { Console.WriteLine(lck.Value?.Name ?? "NULL"); lck.Value = new NotUnmanagedButVaultSafeValueType("Atticus"); } //Bug 49 fix verification as applied to Type Argument BasicVault <NotUnmanagedButVaultSafeValueType?> nullableVb2 = CreateBasicVault((string s) => (NotUnmanagedButVaultSafeValueType?)new NotUnmanagedButVaultSafeValueType(s)); using (var lck = nullableVb2.SpinLock()) { Console.WriteLine(lck.Value?.ToString() ?? "NULL"); lck.Value = null; Console.WriteLine(lck.Value?.ToString() ?? "NULL"); } }