Esempio n. 1
0
 /// <summary>
 /// Create a hashtable with the given amount of entries
 /// </summary>
 /// <param name="capacity"></param>
 public Hashtable(int capacity)
 {
     Table      = new Node[capacity];
     TableLocks = new GatewayLock[capacity];
     for (var i = 0; i < capacity; i++)
     {
         TableLocks[i] = new GatewayLock();
     }
 }
Esempio n. 2
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. 3
0
 public HouseholdData()
 {
     this.EntryList = new List<HouesholdAgeEntry>( 25 );
     this.EntryLock = new GatewayLock();
 }
Esempio n. 4
0
 public PersonData(ExtractPersonData self)
 {
     this.self = self;
     this.EntryList = new List<PersonEntry>( 25 );
     this.EntryLock = new GatewayLock();
 }
Esempio n. 5
0
 public void TestMultipleWriters()
 {
     GatewayLock gate = new GatewayLock();
     Stopwatch watch = new Stopwatch();
     watch.Start();
     long startLock1 = 0, startLock2 = 0;
     long endLock1 = 0, endLock2 = 0;
     for ( int i = 0; i < 100; i++ )
     {
         Parallel.Invoke(
             () =>
             {
                 gate.Lock(
                     () =>
                     {
                         startLock1 = watch.ElapsedTicks;
                         Thread.Sleep( 10 );
                         endLock1 = watch.ElapsedTicks;
                     } );
             },
             () =>
             {
                 gate.Lock(
                     () =>
                     {
                         startLock2 = watch.ElapsedTicks;
                         Thread.Sleep( 10 );
                         endLock2 = watch.ElapsedTicks;
                     } );
             } );
         if ( startLock1 < startLock2 )
         {
             Assert.IsTrue( endLock1 <= startLock2 );
         }
         else
         {
             Assert.IsTrue( endLock1 >= startLock2 );
         }
     }
 }
Esempio n. 6
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. 7
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 );
 }