Exemple #1
0
 public Lock()
 {
     queueLock = new SimpleSpinLock();
     owningThreadId = -1;
     threadSuspensionEvent = new ManualResetEvent(true);
     waiters = new Queue<int>();
 }
    public static void Go()
    {
        Int32       x          = 0;
        const Int32 iterations = 10000000; // 10 million

        // How long does it take to increment x 10 million times?
        Stopwatch sw = Stopwatch.StartNew();

        for (Int32 i = 0; i < iterations; i++)
        {
            x++;
        }
        Console.WriteLine("Incrementing x: {0:N0}", sw.ElapsedMilliseconds);

        // How long does it take to increment x 10 million times
        // adding the overhead of calling a method that does nothing?
        sw.Restart();
        for (Int32 i = 0; i < iterations; i++)
        {
            M(); x++; M();
        }
        Console.WriteLine("Incrementing x in M: {0:N0}", sw.ElapsedMilliseconds);

        // How long does it take to increment x 10 million times
        // adding the overhead of calling an uncontended SimpleSpinLock?
        SimpleSpinLock ssl = new SimpleSpinLock();

        sw.Restart();
        for (Int32 i = 0; i < iterations; i++)
        {
            ssl.Enter(); x++; ssl.Leave();
        }
        Console.WriteLine("Incrementing x in SimpleSpinLock: {0:N0}", sw.ElapsedMilliseconds);

        // How long does it take to increment x 10 million times
        // adding the overhead of calling an uncontended SpinLock?
        SpinLock sl = new SpinLock(false);

        sw.Restart();
        for (Int32 i = 0; i < iterations; i++)
        {
            Boolean taken = false;
            sl.Enter(ref taken); x++; sl.Exit(false);
        }
        Console.WriteLine("Incrementing x in SpinLock: {0:N0}", sw.ElapsedMilliseconds);

        // How long does it take to increment x 10 million times
        // adding the overhead of calling an uncontended SimpleWaitLock?
        using (SimpleWaitLock swl = new SimpleWaitLock()) {
            sw.Restart();
            for (Int32 i = 0; i < iterations; i++)
            {
                swl.Enter(); x++; swl.Leave();
            }
            Console.WriteLine("Incrementing x in SimpleWaitLock: {0:N0}", sw.ElapsedMilliseconds);
        }
        Console.ReadLine();
    }
Exemple #3
0
 public LockTestSuits()
 {
     _resource     = 0;
     sslock        = new SimpleSpinLock();
     swlock        = new SimpleWaitLock();
     semaphorelock = new SimpleWaitWithSemaphoreLock();
     mutex         = new Mutex();
     recursivelock = new RecursiveAutoResetEventLock();
 }
Exemple #4
0
        public static void Go()
        {
            int       x          = 0;
            const int iterations = 1000000;
            Stopwatch sw         = Stopwatch.StartNew();

            for (int i = 0; i < iterations; i++)
            {
                x++;
            }
            Console.WriteLine("Incrementing x: {0:N0}", sw.ElapsedMilliseconds);

            sw.Restart();
            for (int i = 0; i < iterations; i++)
            {
                M(); x++; M();
            }
            Console.WriteLine("Incrementing x in M: {0:N0}", sw.ElapsedMilliseconds);

            SimpleSpinLock ssl = new SimpleSpinLock();

            sw.Restart();
            for (int i = 0; i < iterations; i++)
            {
                ssl.Enter(); x++; ssl.Leave();
            }
            Console.WriteLine("Incrementing x in SimpleSpinLock:{0:N0}", sw.ElapsedMilliseconds);

            SpinLock sl = new SpinLock(false);

            sw.Restart();
            for (int i = 0; i < iterations; i++)
            {
                bool taken = false;
                sl.Enter(ref taken); x++; sl.Exit(false);
            }
            Console.WriteLine("Incrementing x in SpinLock: {0:N0}", sw.ElapsedMilliseconds);

            using (SimpleWaitLock swl = new SimpleWaitLock())
            {
                sw.Restart();
                for (int i = 0; i < iterations; i++)
                {
                    swl.Enter(); x++; swl.Leave();
                }
                Console.WriteLine("Incrementing x in SimpleWaitLock: {0:N0}", sw.ElapsedMilliseconds);
            }
            Console.ReadLine();
        }
        public void SimpleSpinLock_Benchmark()
        {
            var m_sl  = new SimpleSpinLock();
            var model = new DummyModelLock();

            void Action()
            {
                m_sl.Enter();
                for (int i = 0; i < Size; i++)
                {
                    try
                    {
                        model.Counter += (i % 10);
                    }
                    finally
                    {
                    }
                }
                m_sl.Leave();
            }

            Parallel.Invoke(Action, Action, Action);
        }
   public static void Go() {
      Int32 x = 0;
      const Int32 iterations = 10000000;  // 10 million

      // How long does it take to increment x 10 million times?
      Stopwatch sw = Stopwatch.StartNew();
      for (Int32 i = 0; i < iterations; i++) {
         x++;
      }
      Console.WriteLine("Incrementing x: {0:N0}", sw.ElapsedMilliseconds);

      // How long does it take to increment x 10 million times 
      // adding the overhead of calling a method that does nothing?
      sw.Restart();
      for (Int32 i = 0; i < iterations; i++) {
         M(); x++; M();
      }
      Console.WriteLine("Incrementing x in M: {0:N0}", sw.ElapsedMilliseconds);

      // How long does it take to increment x 10 million times 
      // adding the overhead of calling an uncontended SimpleSpinLock?
      SimpleSpinLock ssl = new SimpleSpinLock();
      sw.Restart();
      for (Int32 i = 0; i < iterations; i++) {
         ssl.Enter(); x++; ssl.Leave();
      }
      Console.WriteLine("Incrementing x in SimpleSpinLock: {0:N0}", sw.ElapsedMilliseconds);

      // How long does it take to increment x 10 million times 
      // adding the overhead of calling an uncontended SpinLock?
      SpinLock sl = new SpinLock(false);
      sw.Restart();
      for (Int32 i = 0; i < iterations; i++) {
         Boolean taken = false;
         sl.Enter(ref taken); x++; sl.Exit(false);
      }
      Console.WriteLine("Incrementing x in SpinLock: {0:N0}", sw.ElapsedMilliseconds);

      // How long does it take to increment x 10 million times 
      // adding the overhead of calling an uncontended SimpleWaitLock?
      using (SimpleWaitLock swl = new SimpleWaitLock()) {
         sw.Restart();
         for (Int32 i = 0; i < iterations; i++) {
            swl.Enter(); x++; swl.Leave();
         }
         Console.WriteLine("Incrementing x in SimpleWaitLock: {0:N0}", sw.ElapsedMilliseconds);
      }
      Console.ReadLine();
   }
Exemple #7
0
 ObjectLock(object syncObject)
 {
     this.syncObject = syncObject;
     instanceLock = new SimpleSpinLock();
 }