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); } }
public void ConcurrentCorrectness() { SpinLockUC spinlock = new SpinLockUC(); int index = 0; Task[] tasks = Enumerable .Range(0, Math.Max(Environment.ProcessorCount, 4)) .Select(x => Task.Run(() => { for (int i = 0; i < 100; i++) { using (spinlock.Enter()) { Assert.AreEqual(Interlocked.Increment(ref index), 1); Thread.Sleep(1); Assert.AreEqual(Interlocked.Decrement(ref index), 0); } } })) .ToArray() ; Task.WaitAll(tasks); }
public void SyncExceptionTestMethod(SpinLockUC spinlock) { using (spinlock.Enter()) { throw new Exception(); } }
private static void ConcurrentSequencingWorker(SpinLockUC spinLock, ISequencerUC sequencer) { sequencer.Point(SeqPointTypeUC.Match, ConcurrentSequencingPhase.Begin); sequencer.Point(SeqPointTypeUC.Notify, ConcurrentSequencingPhase.EnteringSpinLock, Interlocked.Increment(ref StepConcurrentSequencing)); using (spinLock.Enter()) { sequencer.Point(SeqPointTypeUC.Match, ConcurrentSequencingPhase.Locked, Interlocked.Decrement(ref StepConcurrentSequencing)); sequencer.Point(SeqPointTypeUC.Notify, ConcurrentSequencingPhase.LockedNotify, Interlocked.Add(ref StepConcurrentSequencing, 0)); } sequencer.Point(SeqPointTypeUC.Match, ConcurrentSequencingPhase.End); }
public EntryBlockUC Enter() { using (SpinLockUC.Enter()) { if (!Locked) { Locked = true; return(new EntryBlockUC(EntryTypeUC.Exclusive, EntryCompletion)); } } }
private static void ConcurrentSequencingConditionalWorker(ISequencerUC sequencer, SpinLockUC spinLock) { ConditionalSequencerUC.Point(sequencer, SeqPointTypeUC.Match, ConcurrentSequencingPhaseConditional.Begin); ConditionalSequencerUC.Point(sequencer, SeqPointTypeUC.Notify, ConcurrentSequencingPhaseConditional.EnteringSpinLock, Interlocked.Increment(ref StepConcurrentSequencingConditional)); using (spinLock.Enter()) { ConditionalSequencerUC.Point(sequencer, SeqPointTypeUC.Match, ConcurrentSequencingPhaseConditional.Locked, Interlocked.Decrement(ref StepConcurrentSequencingConditional)); ConditionalSequencerUC.Point(sequencer, SeqPointTypeUC.Notify, ConcurrentSequencingPhaseConditional.LockedNotify, Interlocked.Add(ref StepConcurrentSequencingConditional, 0)); } ConditionalSequencerUC.Point(sequencer, SeqPointTypeUC.Match, ConcurrentSequencingPhaseConditional.End); }
private static async Task ConcurrentSequencingAsyncWorker(SpinLockUC spinLock, ISequencerUC sequencer) { await sequencer.PointAsync(SeqPointTypeUC.Match, ConcurrentSequencingPhaseAsync.Begin); await sequencer.PointAsync(SeqPointTypeUC.Notify, ConcurrentSequencingPhaseAsync.EnteringSpinLock, Interlocked.Increment(ref StepConcurrentSequencingAsync)); using (spinLock.Enter()) { await sequencer.PointAsync(SeqPointTypeUC.Match, ConcurrentSequencingPhaseAsync.Locked, Interlocked.Decrement(ref StepConcurrentSequencingAsync)); await sequencer.PointAsync(SeqPointTypeUC.Notify, ConcurrentSequencingPhaseAsync.LockedNotify, Interlocked.Add(ref StepConcurrentSequencingAsync, 0)); } await sequencer.PointAsync(SeqPointTypeUC.Match, ConcurrentSequencingPhaseAsync.End); }
private void Exit() { using (SpinLockUC.Enter()) { } }