Ejemplo n.º 1
0
        public void ThreadLock_Threaded()
        {
            UT_INIT();

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

            ThreadLock aLock = new ThreadLock();

            Test_ThreadLock_SharedInt shared = new Test_ThreadLock_SharedInt();

            Log.Info("starting thread locked");
            aLock.Acquire();
            Test_ThreadLock_TestThreadParams tParam = new Test_ThreadLock_TestThreadParams(aLock, 10, 1, true, shared);

            Thread thread = new Thread(new ParameterizedThreadStart(Test_ThreadLock_Test_run));

            thread.Name = "A Thread";
            thread.Start(tParam);

            Log.Info("We wait 1100 ms. This should give a warning! ");
            ALIB.SleepMillis(1100);
            aLock.Release();

            // wait until t ended
            while (thread.IsAlive)
            {
                ALIB.SleepMillis(1);
            }

            // now we do the same with a higher wait limit, no erro should come
            aLock.waitWarningTimeLimitInMillis = 5;
            aLock.Acquire();
            tParam = new Test_ThreadLock_TestThreadParams(aLock, 10, 1, true, shared);
            thread = new Thread(new ParameterizedThreadStart(Test_ThreadLock_Test_run));
            thread.Start(tParam);
            Log.Info("We wait 1 ms. This should NOT give a warning! ");
            ALIB.SleepMillis(1);
            aLock.Release();

            // wait until t ended
            while (thread.IsAlive)
            {
                ALIB.SleepMillis(1);
            }
        }
Ejemplo n.º 2
0
        public void ThreadLock_HeavyLoad()
        {
            UT_INIT();
            Log.SetVerbosity(new ConsoleLogger(), Verbosity.Verbose, "/");
            Log.MapThreadName("UnitTest");
            Log.SetDomain("TestTLock", Scope.Filename);

            ThreadLock aLock = new ThreadLock();

            // uncomment this for unsafe mode
            // lock.setUnsafe( true );

            Test_ThreadLock_SharedInt shared = new Test_ThreadLock_SharedInt();
            int  holdTime = 0;
            int  repeats  = 5000;
            bool verbose  = false;

            Test_ThreadLock_TestThreadParams p1 = new Test_ThreadLock_TestThreadParams(aLock, holdTime, repeats, verbose, shared);
            Test_ThreadLock_TestThreadParams p2 = new Test_ThreadLock_TestThreadParams(aLock, holdTime, repeats, verbose, shared);
            Test_ThreadLock_TestThreadParams p3 = new Test_ThreadLock_TestThreadParams(aLock, holdTime, repeats, verbose, shared);

            Log.Info("starting three threads");

            Thread t1 = new Thread(new ParameterizedThreadStart(Test_ThreadLock_Test_run));
            Thread t2 = new Thread(new ParameterizedThreadStart(Test_ThreadLock_Test_run));
            Thread t3 = new Thread(new ParameterizedThreadStart(Test_ThreadLock_Test_run));

            t1.Start(p1);
            t2.Start(p2);
            t3.Start(p3);

            // wait until all ended
            while (t1.IsAlive || t2.IsAlive || t3.IsAlive)
            {
                ALIB.SleepMillis(1);
            }

            Log.Info("All threads ended. Shared value=" + shared.val);

            UT_TRUE(shared.val == 0);
        }
Ejemplo n.º 3
0
        public void Test_ThreadLock_Test_run(Object o)
        {
            Test_ThreadLock_TestThreadParams p = o as Test_ThreadLock_TestThreadParams;

            for (int i = 0; i < p.repeats; i++)
            {
                if (p.verbose)
                {
                    Log.Info("Thread: " + Thread.CurrentThread.Name + " acuiring lock...");
                }
                p.aLock.Acquire();
                if (p.verbose)
                {
                    Log.Info("Thread: " + Thread.CurrentThread.Name + " has lock.");
                }

                int sVal = ++p.shared.val;

                ALIB.SleepMillis(p.holdTime);

                p.shared.val = sVal - 1;

                if (p.verbose)
                {
                    Log.Info("Thread: " + Thread.CurrentThread.Name + " releasing lock.");
                }
                p.aLock.Release();
                if (p.verbose)
                {
                    Log.Info("Thread: " + Thread.CurrentThread.Name + " released lock.");
                }
            }

            p.result = 0;
            Log.Info("Thread: " + Thread.CurrentThread.Name + " terminates.");
        }
        public void ThreadLock_HeavyLoad()
        {
            UT_INIT();
            Log.SetVerbosity( new ConsoleLogger(), Verbosity.Verbose, "/" );
            Log.MapThreadName( "UnitTest" );
            Log.SetDomain( "TestTLock", Scope.Filename );

            ThreadLock aLock= new ThreadLock();

            // uncomment this for unsafe mode
            // lock.setUnsafe( true );

            Test_ThreadLock_SharedInt    shared=        new Test_ThreadLock_SharedInt();
            int                            holdTime=    0;
            int                            repeats=    5000;
            bool                        verbose=    false;

            Test_ThreadLock_TestThreadParams p1= new Test_ThreadLock_TestThreadParams( aLock, holdTime, repeats, verbose, shared );
            Test_ThreadLock_TestThreadParams p2= new Test_ThreadLock_TestThreadParams( aLock, holdTime, repeats, verbose, shared );
            Test_ThreadLock_TestThreadParams p3= new Test_ThreadLock_TestThreadParams( aLock, holdTime, repeats, verbose, shared );
            Log.Info("starting three threads");

            Thread t1= new Thread( new ParameterizedThreadStart( Test_ThreadLock_Test_run ) );
            Thread t2= new Thread( new ParameterizedThreadStart( Test_ThreadLock_Test_run ) );
            Thread t3= new Thread( new ParameterizedThreadStart( Test_ThreadLock_Test_run ) );
            t1.Start( p1 );
            t2.Start( p2 );
            t3.Start( p3 );

            // wait until all ended
            while ( t1.IsAlive || t2.IsAlive || t3.IsAlive )
                ALIB.SleepMillis( 1 );

            Log.Info("All threads ended. Shared value=" + shared.val );

            UT_TRUE( shared.val == 0 );

        }
        public void ThreadLock_Threaded()
        {
            UT_INIT();

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

            ThreadLock aLock= new ThreadLock();

            Test_ThreadLock_SharedInt shared= new Test_ThreadLock_SharedInt();

            Log.Info("starting thread locked");
            aLock.Acquire();
            Test_ThreadLock_TestThreadParams tParam= new Test_ThreadLock_TestThreadParams( aLock, 10, 1, true, shared );

            Thread thread = new Thread( new ParameterizedThreadStart( Test_ThreadLock_Test_run ) );
            thread.Name= "A Thread";
            thread.Start( tParam );

            Log.Info("We wait 1100 ms. This should give a warning! ");
            ALIB.SleepMillis( 1100 );
            aLock.Release();

            // wait until t ended
            while (thread.IsAlive)
                ALIB.SleepMillis( 1 );

            // now we do the same with a higher wait limit, no erro should come
            aLock.waitWarningTimeLimitInMillis= 5;
            aLock.Acquire();
            tParam= new Test_ThreadLock_TestThreadParams( aLock, 10, 1, true, shared );
            thread = new Thread( new ParameterizedThreadStart( Test_ThreadLock_Test_run ) );
            thread.Start( tParam );
            Log.Info("We wait 1 ms. This should NOT give a warning! ");
            ALIB.SleepMillis( 1 );
            aLock.Release();

            // wait until t ended
            while (thread.IsAlive)
                ALIB.SleepMillis( 1 );

        }