Esempio n. 1
0
 public void TestListWriter()
 {
     GatewayLock gate = new GatewayLock();
     for ( int iteration = 0; iteration < 100; iteration++ )
     {
         var list = new List<Entry>();
         Parallel.For( 0, 1000, (int i) =>
             {
                 Random r = new Random();
                 for ( int j = 0; j < 100; j++ )
                 {
                     var num = r.Next( 10 );
                     bool found = false;
                     gate.PassThrough( () =>
                         {
                             foreach ( var entry in list )
                             {
                                 if ( entry.Number == num )
                                 {
                                     lock ( entry )
                                     {
                                         entry.TimeFound++;
                                         found = true;
                                         return;
                                     }
                                 }
                             }
                         } );
                     if ( !found )
                     {
                         gate.Lock( () =>
                         {
                             foreach ( var entry in list )
                             {
                                 if ( entry.Number == num )
                                 {
                                     entry.TimeFound++;
                                     found = true;
                                     return;
                                 }
                             }
                             list.Add( new Entry() { Number = num, TimeFound = 1 } );
                         } );
                     }
                 }
             } );
         Assert.IsTrue( list.Count <= 10 );
     }
 }
Esempio n. 2
0
 public void TestWriterLock()
 {
     GatewayLock gate = new GatewayLock();
     Stopwatch watch = new Stopwatch();
     watch.Start();
     long inPassThrough = 0;
     long finishedPassThrough = 0;
     long inLock = 0;
     Task secondaryTask = null;
     var mainTask = Task.Factory.StartNew(
         () =>
         {
             gate.PassThrough(
                 () =>
                 {
                     inPassThrough = watch.ElapsedMilliseconds;
                     secondaryTask = Task.Factory.StartNew(
                         () =>
                         {
                             gate.Lock(
                                 () =>
                                 {
                                     // chill
                                     inLock = watch.ElapsedMilliseconds;
                                 } );
                         } );
                     Thread.Sleep( 100 );
                     finishedPassThrough = watch.ElapsedMilliseconds;
                 } );
         } );
     mainTask.Wait();
     secondaryTask.Wait();
     watch.Stop();
     Assert.IsTrue( inLock >= finishedPassThrough );
 }
Esempio n. 3
0
 public void TestWriterHoldThenReaders()
 {
     GatewayLock gate = new GatewayLock();
     Stopwatch watch = new Stopwatch();
     watch.Start();
     bool writerDone = false;
     bool anyFails = false;
     Task main = Task.Factory.StartNew(
         () =>
         {
             var ourTasks = new Task[10];
             gate.Lock( () =>
             {
                 for ( int i = 0; i < ourTasks.Length; i++ )
                 {
                     ourTasks[i] = Task.Factory.StartNew(
                         () =>
                         {
                             gate.PassThrough( () =>
                                 {
                                     if ( !writerDone )
                                     {
                                         anyFails = true;
                                     }
                                 } );
                         } );
                 }
                 Thread.Sleep( 1000 );
                 writerDone = true;
             } );
             Task.WaitAll( ourTasks );
         } );
     main.Wait();
     Thread.MemoryBarrier();
     Assert.IsFalse( anyFails );
 }