コード例 #1
0
ファイル: Hashtable.cs プロジェクト: lunaxi7/XTMF
 /// <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();
     }
 }
コード例 #2
0
ファイル: TestGatewayLock.cs プロジェクト: Cocotus/XTMF
 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 );
     }
 }
コード例 #3
0
ファイル: ExtractHouseholdData.cs プロジェクト: Cocotus/XTMF
 public HouseholdData()
 {
     this.EntryList = new List<HouesholdAgeEntry>( 25 );
     this.EntryLock = new GatewayLock();
 }
コード例 #4
0
ファイル: ExtractPersonData.cs プロジェクト: Cocotus/XTMF
 public PersonData(ExtractPersonData self)
 {
     this.self = self;
     this.EntryList = new List<PersonEntry>( 25 );
     this.EntryLock = new GatewayLock();
 }
コード例 #5
0
ファイル: TestGatewayLock.cs プロジェクト: Cocotus/XTMF
 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 );
         }
     }
 }
コード例 #6
0
ファイル: TestGatewayLock.cs プロジェクト: Cocotus/XTMF
 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 );
 }
コード例 #7
0
ファイル: TestGatewayLock.cs プロジェクト: Cocotus/XTMF
 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 );
 }