Exemplo n.º 1
0
        public void ThreadLock_SpeedTest()
        {
            UT_INIT();

            Log.SetVerbosity(new ConsoleLogger(), Verbosity.Verbose, "/");
            Log.MapThreadName("UnitTest");
            Log.SetDomain("TestTLock", Scope.Method);

            ThreadLock aLock = new ThreadLock();

            int repeats  = 100000;
            int rrepeats = 5;

            Ticks stopwatch = new Ticks();

            for (int r = 0; r < rrepeats; r++)
            {
                Log.Info("Run " + rrepeats);

                aLock.SetSafeness(Safeness.Unsafe);
                stopwatch.Set();
                for (int i = 0; i < repeats; i++)
                {
                    aLock.Acquire();
                    aLock.Release();
                }
                long time = stopwatch.Age().InMillis();
                Log.Info("Safe mode, " + repeats + " lock/unlock ops: " + time + " ms");

                aLock.SetSafeness(Safeness.Safe);
                stopwatch.Set();
                for (int i = 0; i < repeats; i++)
                {
                    //aLock.acquire();
                    //aLock.release();

                    // in java, adding the following two loops, results in similar execution speed
                    for (int tt = 0; tt < 70; tt++)
                    {
                        i += tt;
                    }
                    for (int tt = 0; tt < 70; tt++)
                    {
                        i -= tt;
                    }
                }
                time = stopwatch.Age().InMillis();
                Log.Info("Unsafe mode, " + repeats + " lock/unlock ops: " + time + " ms");
            }
        }
Exemplo n.º 2
0
        public void ThreadLock_SimpleTests()
        {
            UT_INIT();
            Report.GetDefault().PushHaltFlags(false, false);

            Log.AddDebugLogger();
            Log.MapThreadName("UnitTest");
            Log.SetDomain("TestTLock", Scope.Method);
            Log.SetVerbosity(Log.DebugLogger, Verbosity.Verbose, "ALIB");

            // lock a recursive lock
            ThreadLock aLock = new ThreadLock();

            aLock.Acquire();
            aLock.Release();

            aLock.Acquire();                                UT_TRUE(aLock.ToString().StartsWith("Locked"));
            aLock.Acquire();                                UT_TRUE(aLock.ToString().StartsWith("Locked"));
            aLock.Release();                                UT_TRUE(aLock.ToString().StartsWith("Locked"));

            aLock.Acquire();                                UT_TRUE(aLock.ToString().StartsWith("Locked"));
            aLock.Release();                                UT_TRUE(aLock.ToString().StartsWith("Locked"));
            aLock.Release();                                UT_TRUE(aLock.ToString().StartsWith("Unlocked"));

            // set unsafe
            aLock.SetSafeness(Safeness.Unsafe);           UT_TRUE(aLock.ToString().StartsWith("Unlocked"));
            UT_TRUE(aLock.ToString().Contains("Unsafe"));
            aLock.SetSafeness(Safeness.Safe);             UT_TRUE(!aLock.ToString().Contains("Unsafe"));

            aLock.SetSafeness(Safeness.Unsafe);           UT_TRUE(aLock.ToString().StartsWith("Unlocked"));
            UT_TRUE(aLock.ToString().Contains("Unsafe"));
            aLock.Acquire();                                UT_TRUE(aLock.ToString().StartsWith("Locked"));
            aLock.Release();                                UT_TRUE(aLock.ToString().StartsWith("Unlocked"));
            UT_TRUE(aLock.ToString().Contains("Unsafe"));

            // unsafe
            aLock.Acquire();                                UT_TRUE(aLock.ToString().StartsWith("Locked"));
            Log.Info("One warning should come now: ");
            aLock.SetSafeness(Safeness.Safe);             UT_TRUE(aLock.ToString().StartsWith("Locked"));
            UT_TRUE(aLock.ToString().Contains("Unsafe"));

            // safe (new lock)
            aLock = new ThreadLock();
            aLock.Acquire();                                UT_TRUE(aLock.ToString().StartsWith("Locked"));
            UT_TRUE(!aLock.ToString().Contains("Unsafe"));
            Log.Info("One warning should come now: ");
            aLock.SetSafeness(Safeness.Unsafe);           UT_TRUE(!aLock.ToString().StartsWith("null"));
            UT_TRUE(!aLock.ToString().Contains("Unsafe"));


            // test warnings (10) locks:
            aLock = new ThreadLock();
            Log.Info("Two warnings should come now: ");
            for (int i = 0; i < 20; i++)
            {
                aLock.Acquire();
            }
            UT_TRUE(aLock.ToString().StartsWith("Locked"));
            for (int i = 0; i < 20; i++)
            {
                aLock.Release();
            }
            UT_TRUE(aLock.ToString().StartsWith("Unlocked"));

            // test a non-recursive lock
            aLock = new ThreadLock(LockMode.SingleLocks);
            aLock.Acquire();                UT_TRUE(aLock.ToString().StartsWith("Locked"));
            aLock.Acquire();                UT_TRUE(aLock.ToString().StartsWith("Locked"));
            aLock.Release();                UT_TRUE(aLock.ToString().StartsWith("Unlocked"));

            Log.Info("One warning should come now: ");
            aLock.Release();                UT_TRUE(aLock.ToString().StartsWith("Unlocked"));

            Report.GetDefault().PopHaltFlags();
        }