public void SingleThreadCorrectness() { SpinLockUC spinlock = new SpinLockUC(); using (spinlock.Enter()) { } using (EntryBlockUC entry = spinlock.Enter()) { Assert.AreEqual(entry.EntryTypeUC, EntryTypeUC.Exclusive); Assert.IsTrue(entry.HasEntry); } using (EntryBlockUC entry = spinlock.TryEnter()) { Assert.AreEqual(entry.EntryTypeUC, EntryTypeUC.Exclusive); Assert.IsTrue(entry.HasEntry); if (entry.HasEntry) { } } using (EntryBlockUC entry = spinlock.TryEnter(1)) { Assert.AreEqual(entry.EntryTypeUC, EntryTypeUC.Exclusive); Assert.IsTrue(entry.HasEntry); if (entry.HasEntry) { } } EntryBlockUC entryOutsideUsing1 = spinlock.Enter(); EntryBlockUC entryOutsideUsing2 = spinlock.TryEnter(); EntryBlockUC entryOutsideUsing3 = spinlock.TryEnter(1); Assert.IsTrue(entryOutsideUsing1.HasEntry); Assert.IsFalse(entryOutsideUsing2.HasEntry); Assert.IsFalse(entryOutsideUsing3.HasEntry); Assert.IsTrue(entryOutsideUsing2 == EntryBlockUC.RefusedEntry); entryOutsideUsing1.Dispose(); using (IEntryBlockUC entry = spinlock.Enter()) { Assert.IsTrue(entry.HasEntry); } using (IEntryBlockUC entry = spinlock.TryEnter()) { Assert.IsTrue(entry.HasEntry); } using (IEntryBlockUC entry = spinlock.TryEnter(1)) { Assert.IsTrue(entry.HasEntry); } }
protected override bool ExclusiveAccess() { using (EntryBlockUC entry = Lock.TryEnter()) { if (!entry.HasEntry) { return(true); //no entry, keep trying } return(ProcessExclusively()); } }
protected override bool ExclusiveAccess() { using (EntryBlockUC entry = Lock.Enter()) { if (!entry.HasEntry) { throw new Exception("should not happen"); } return(ProcessExclusively()); } }
protected override async Task <bool> ExclusiveAccess() { using (EntryBlockUC entry = await Lock.TryEnter(Delay)) { if (!entry.HasEntry) { return(true); //no entry, keep trying } return(ProcessExclusively()); } }
protected override async Task <bool> ExclusiveAccess() { using (EntryBlockUC entry = await Lock.Enter()) { if (!entry.HasEntry) { throw new Exception("hmmmmmmmmm"); } return(ProcessExclusively()); } }
public void SyncExceptionReleasingLockTest() { ILockUC spinlock = new SpinLockUC(); Assert.Throws <Exception>(() => SyncExceptionTestMethod(spinlock)); EntryBlockUC entry = spinlock.TryEnter(); Assert.IsTrue(entry.HasEntry); entry.Dispose(); entry = spinlock.TryEnter(); Assert.IsTrue(entry.HasEntry); entry.Dispose(); }
private static void ConcurrentSequencingWorker(ILockUC Lock, ISequencerUC sequencer) { sequencer.Point(SeqPointTypeUC.Match, ConcurrentTryEnterDelaySequencingPhase.Begin); sequencer.Point(SeqPointTypeUC.Notify, ConcurrentTryEnterDelaySequencingPhase.EnteringSimpleLock, Interlocked.Increment(ref StepConcurrentSequencing)); using (EntryBlockUC entry = Lock.TryEnter(150)) { sequencer.PointArg(SeqPointTypeUC.Match, ConcurrentTryEnterDelaySequencingPhase.Entry, entry.HasEntry); if (entry.HasEntry) { sequencer.Point(SeqPointTypeUC.Match, ConcurrentTryEnterDelaySequencingPhase.Locked, Interlocked.Decrement(ref StepConcurrentSequencing)); sequencer.Point(SeqPointTypeUC.Notify, ConcurrentTryEnterDelaySequencingPhase.LockedNotify, Interlocked.Add(ref StepConcurrentSequencing, 0)); } } sequencer.Point(SeqPointTypeUC.Match, ConcurrentTryEnterDelaySequencingPhase.End); }
public bool?TryStop() { using (EntryBlockUC entry = Lock.TryEnter()) { if (entry.EntryTypeUC == EntryTypeUC.None) { return(null); } if (StopWatch == null) { return(false); //disposed } if (!StopWatch.IsRunning) { return(true); } StopWatch.Stop(); StopWatch.Reset(); return(true); } }
public TimeSpan?TryRead(object unique = null) { using (EntryBlockUC entry = Lock.TryEnter()) { if (entry.EntryTypeUC == EntryTypeUC.None) { return(null); } if (StopWatch == null) { return(null); //disposed } if (!StopWatch.IsRunning) { return(null); } Unique.Add(unique); if (Unique.Count < MinUniques) { return(null); } TimeSpan now = StopWatch.Elapsed; if (Next > now) { return(Next - now); //not yet } Next += Period; if (ReadCounter++ > 0 || MinUniques == null) { Queue.Enqueue(PerfCounter.Performance); } else { // ReSharper disable once UnusedVariable var aa = PerfCounter.Performance; } return(Next - now); } }
public bool?TryClear() { using (EntryBlockUC entry = Lock.TryEnter()) { if (entry.EntryTypeUC == EntryTypeUC.None) { return(null); } if (Queue == null) { return(false); //disposed } if (StopWatch?.IsRunning == true) { return(false); } Queue.Clear(); Unique.Clear(); ReadCounter = 0; return(true); } }
public bool?TryStart(int?minUniques = null) { using (EntryBlockUC entry = Lock.TryEnter()) { MinUniques = minUniques; if (entry.EntryTypeUC == EntryTypeUC.None) { return(null); } if (StopWatch == null) { return(false); //disposed } if (StopWatch.IsRunning) { return(true); } Next = Period; StopWatch.Reset(); StopWatch.Start(); return(true); } }