Beispiel #1
0
 public override bool Accept(
     IVisitor visitor
     ) =>
 visitor.Enter(this) &&
 Guard.Accept(visitor) &&
 (Guarded == null || Guarded.Accept(visitor)) &&
 visitor.Exit(this);
Beispiel #2
0
        public StoreEngine(ILogger logger, ServerCfg serverCfg)
            : base(PartNames.Store, logger)
        {
            _inboundCallQueue = new AsyncThreadQueue(Logger);
            string connectionString = EnvHelper.FormatDbCfgStr(serverCfg.ModuleInfo.ConfigEnv, serverCfg.DbServer, serverCfg.DbPrefix);

            Logger.LogDebug("Connection String: {0}", connectionString);
            _state = new Guarded <StoreEngineState>(new StoreEngineState(serverCfg, connectionString));
        }
Beispiel #3
0
 public override int GetHashCode()
 {
     unchecked
     {
         int result = (Source != null ? Source.GetHashCode() : 0);
         result = (result * 397) ^ (Guarded != null ? Guarded.GetHashCode() : 0);
         result = (result * 397) ^ (OnException != null ? OnException.GetHashCode() : 0);
         result = (result * 397) ^ (OnFinally != null ? OnFinally.GetHashCode() : 0);
         return(result);
     }
 }
 public BatchCollectionChange(object collection, bool requireSuspandable) :
     base(collection, requireSuspandable)
 {
     Guarded?.SuspendNotifications();
 }
 public override void Dispose()
 {
     Guarded?.ResumeNotifications(false);
     base.Dispose();
 }
 public BatchCollectionChange(ISuspendableNotifyCollectionChanged collection) :
     base(collection)
 {
     Guarded.SuspendNotifications();
 }
Beispiel #7
0
        public void TestObjectLockPerformance()
        {
            bool _IsSingleCore = (Environment.ProcessorCount == 1);

            //---------------------------------------------------------------
            {
                object    target = new object();
                Stopwatch sw     = new Stopwatch();
                sw.Start();
                int lockedCount = 0;
                for (int i = 0; i < TestIterations; i++)
                {
                    lock (target)
                    {
                        lockedCount++;
                    }
                }
                sw.Stop();
                Debug.Print("BenchmarkC#LockPattern : {0} seconds, {1} locks/ms",
                            sw.Elapsed.TotalSeconds, lockedCount / sw.ElapsedMilliseconds);
            }
            //---------------------------------------------------------------
            // ----------- this is the lowest overhead technique ------------
            //---------------------------------------------------------------
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                int lockedCount = 0;
                int _Spinlock   = 0;
                for (int i = 0; i < TestIterations; i++)
                {
                    while (Interlocked.CompareExchange(ref _Spinlock, 1, 0) != 0)
                    {
                        if (_IsSingleCore)
                        {
                            Thread.Sleep(0);
                        }
                    }
                    try
                    {
                        lockedCount++;
                    }
                    finally
                    {
                        Interlocked.Exchange(ref _Spinlock, 0);
                    }
                }
                sw.Stop();
                Debug.Print("InlineInterlockedInt32 : {0} seconds, {1} locks/ms",
                            sw.Elapsed.TotalSeconds, lockedCount / sw.ElapsedMilliseconds);
            }

            //---------------------------------------------------------------
            {
                object    target = new object();
                Stopwatch sw     = new Stopwatch();
                sw.Start();
                int lockedCount = 0;
                for (int i = 0; i < TestIterations; i++)
                {
                    Monitor.Enter(target);
                    try
                    {
                        lockedCount++;
                    }
                    finally
                    {
                        Monitor.Exit(target);
                    }
                }
                sw.Stop();
                Debug.Print("SystemMonitorEnterExit : {0} seconds, {1} locks/ms",
                            sw.Elapsed.TotalSeconds, lockedCount / sw.ElapsedMilliseconds);
            }
            //---------------------------------------------------------------
            {
                object    target = new object();
                Stopwatch sw     = new Stopwatch();
                sw.Start();
                int lockedCount = 0;
                for (int i = 0; i < TestIterations; i++)
                {
                    int    spinCount    = 0;
                    object lockedObject = null;
                    while ((lockedObject = Interlocked.Exchange <object>(ref target, null)) == null)
                    {
                        if (_IsSingleCore)
                        {
                            Thread.Sleep(0);
                        }
                        else
                        {
                            spinCount++;
                            if (spinCount % 4000 == 0)
                            {
                                Thread.Sleep(0);
                            }
                        }
                    }
                    try
                    {
                        lockedCount++;
                    }
                    finally
                    {
                        Interlocked.Exchange <object>(ref target, lockedObject);
                    }
                }
                sw.Stop();
                Debug.Print("InlineInterlockedObject: {0} seconds, {1} locks/ms",
                            sw.Elapsed.TotalSeconds, lockedCount / sw.ElapsedMilliseconds);
            }
            //---------------------------------------------------------------
            {
                SpinLock  spinlockFast = new SpinLock(false);
                Stopwatch sw           = new Stopwatch();
                sw.Start();
                int lockedCount = 0;
                for (int i = 0; i < TestIterations; i++)
                {
                    bool lockTaken = false;
                    while (!lockTaken)
                    {
                        spinlockFast.Enter(ref lockTaken);
                        try
                        {
                            if (lockTaken)
                            {
                                lockedCount++;
                            }
                        }
                        finally
                        {
                            if (lockTaken)
                            {
                                spinlockFast.Exit();
                            }
                        }
                    }
                }
                sw.Stop();
                Debug.Print("Net4SpinlockPatternFast: {0} seconds, {1} locks/ms",
                            sw.Elapsed.TotalSeconds, lockedCount / sw.ElapsedMilliseconds);
            }
            //---------------------------------------------------------------
            {
                SpinLock  spinlockSlow = new SpinLock(true);
                Stopwatch sw           = new Stopwatch();
                sw.Start();
                int lockedCount = 0;
                for (int i = 0; i < TestIterations; i++)
                {
                    bool lockTaken = false;
                    while (!lockTaken)
                    {
                        spinlockSlow.Enter(ref lockTaken);
                        try
                        {
                            if (lockTaken)
                            {
                                lockedCount++;
                            }
                        }
                        finally
                        {
                            if (lockTaken)
                            {
                                spinlockSlow.Exit();
                            }
                        }
                    }
                }
                sw.Stop();
                Debug.Print("Net4SpinlockPatternSlow: {0} seconds, {1} locks/ms",
                            sw.Elapsed.TotalSeconds, lockedCount / sw.ElapsedMilliseconds);
            }
            //---------------------------------------------------------------
            {
                object    target = new object();
                Stopwatch sw     = new Stopwatch();
                sw.Start();
                int lockedCount = 0;
                for (int i = 0; i < TestIterations; i++)
                {
                    object lockedObject = ObjectLock <object> .Enter(ref target);

                    try
                    {
                        lockedCount++;
                    }
                    finally
                    {
                        ObjectLock <object> .Leave(ref target, lockedObject);
                    }
                }
                sw.Stop();
                Debug.Print("EnterLeaveCallsPattern : {0} seconds, {1} locks/ms",
                            sw.Elapsed.TotalSeconds, lockedCount / sw.ElapsedMilliseconds);
            }
            //---------------------------------------------------------------
            {
                object    target = new object();
                Stopwatch sw     = new Stopwatch();
                sw.Start();
                int lockedCount = 0;
                for (int i = 0; i < TestIterations; i++)
                {
                    ObjectLock <object> .Protect(ref target, delegate(object lockedObject)
                    {
                        lockedCount++;
                    });
                }
                sw.Stop();
                Debug.Print("LockedDelegatePattern  : {0} seconds, {1} locks/ms",
                            sw.Elapsed.TotalSeconds, lockedCount / sw.ElapsedMilliseconds);
            }
            //---------------------------------------------------------------
            {
                object           target  = new object();
                Guarded <object> wrapper = new Guarded <object>(target);
                Stopwatch        sw      = new Stopwatch();
                sw.Start();
                int lockedCount = 0;
                for (int i = 0; i < TestIterations; i++)
                {
                    //int n = (i % TargetObjects);
                    wrapper.Locked((lockedObject) =>
                    {
                        lockedCount++;
                    });
                }
                sw.Stop();
                Debug.Print("GuardedWrapperPattern  : {0} seconds, {1} locks/ms",
                            sw.Elapsed.TotalSeconds, lockedCount / sw.ElapsedMilliseconds);
            }
        }