Exemple #1
0
        public unsafe void CouldAcquireReleaseExclusiveLock()
        {
            var ptr = (long *)Marshal.AllocHGlobal(8);

            *   ptr   = 0;
            var wpid  = Wpid.Create();
            var wpid2 = Wpid.Create();

            var sl = new SharedSpinLock(ptr);

            Assert.AreEqual(Wpid.Empty, sl.TryAcquireExclusiveLock(wpid, out _, spinLimit: 0)); // fast path

            Assert.AreEqual(Wpid.Empty, sl.TryReleaseLock(wpid));

            Assert.AreEqual(Wpid.Empty, sl.TryAcquireExclusiveLock(wpid, out _));

            var sw = new Stopwatch();

            sw.Start();
            Assert.AreEqual(wpid, sl.TryAcquireExclusiveLock(wpid2, out _, spinLimit: 1000));
            sw.Stop();
            Console.WriteLine($"Elapsed: {sw.ElapsedMilliseconds}");

            Assert.AreEqual(Wpid.Empty, sl.TryReleaseLock(wpid));
        }
Exemple #2
0
        public unsafe void UncontentedBench()
        {
            var ptr = (long *)Marshal.AllocHGlobal(8);

            *   ptr  = 0;
            var wpid = Wpid.Create();

            var sl = new SharedSpinLock(ptr);

            var count = 10_000_000;

            for (int _ = 0; _ < 10; _++)
            {
                using (Benchmark.Run("Uncontented", count))
                {
                    for (int i = 0; i < count; i++)
                    {
                        sl.TryAcquireLock(wpid);
                        sl.TryReleaseLock(wpid);
                    }
                }
            }

            Benchmark.Dump();
        }
        public Wpid TryAcquireLock(Wpid wpid, IWpidHelper wpidHelper, int spinLimit = 0)
        {
            var content = *(long *)(_statePointer);

            *(long *)(_statePointer) = 0;
            return(SharedSpinLock.TryAcquireLock(ref *(long *)(_statePointer + StreamLogStateRecord.LockerOffset), wpid, spinLimit, wpidHelper));
        }
Exemple #4
0
        public unsafe void CouldAcquireEnterExitReleaseExclusiveLock()
        {
            var ptr = (long *)Marshal.AllocHGlobal(8);

            *   ptr   = 0;
            var wpid  = Wpid.Create();
            var wpid2 = Wpid.Create();

            var sl = new SharedSpinLock(ptr);

            Assert.AreEqual(Wpid.Empty, sl.TryAcquireExclusiveLock(wpid, out var tt, spinLimit: 0));
            Assert.AreEqual(Wpid.Empty, sl.TryReEnterExclusiveLock(wpid, tt, spinLimit: 0));

            Assert.Throws <InvalidOperationException>(() => { sl.TryReleaseLock(wpid); });
            Assert.AreEqual(Wpid.Empty, sl.TryExitExclusiveLock(wpid, tt));

            Assert.AreEqual(Wpid.Empty, sl.TryReleaseLock(wpid));
        }
Exemple #5
0
        public unsafe void ReleasingOthersLockReturnsOthersWpid()
        {
            var ptr = (long *)Marshal.AllocHGlobal(8);

            *   ptr   = 0;
            var wpid  = Wpid.Create();
            var wpid2 = Wpid.Create();

            var sl = new SharedSpinLock(ptr);

            Assert.AreEqual(Wpid.Empty, sl.TryAcquireLock(wpid, spinLimit: 0)); // fast path

            Assert.AreEqual(Wpid.Empty, sl.TryReleaseLock(wpid));

            Assert.AreEqual(Wpid.Empty, sl.TryAcquireLock(wpid));

            // wpid holding the lock

            var holder = sl.TryReleaseLock(wpid2);

            Assert.AreEqual(wpid, holder);
        }
 public Wpid TryReleaseLock(Wpid wpid)
 {
     return(SharedSpinLock.TryReleaseLock(ref *(long *)(_statePointer + StreamLogStateRecord.LockerOffset), wpid));
 }
 public Wpid TryExitExclusiveLock(Wpid wpid, byte threadToken)
 {
     return(SharedSpinLock.TryExitExclusiveLock(ref *(long *)(_statePointer + StreamLogStateRecord.LockerOffset), wpid, threadToken));
 }
        public Wpid TryReEnterExclusiveLock(Wpid wpid, IWpidHelper wpidHelper, byte threadToken, int spinLimit = 0)
        {
            var holder = LockHolder;

            return(SharedSpinLock.TryReEnterExclusiveLock(ref *(long *)(_statePointer + StreamLogStateRecord.LockerOffset), wpid, threadToken, spinLimit, wpidHelper));
        }
        public Wpid TryAcquireExclusiveLock(Wpid wpid, IWpidHelper wpidHelper, out byte threadToken, int spinLimit = 0)
        {
            var res = SharedSpinLock.TryAcquireExclusiveLock(ref *(long *)(_statePointer + StreamLogStateRecord.LockerOffset), wpid, out threadToken, spinLimit, wpidHelper);

            return(res);
        }