Exemplo n.º 1
0
        public void TestWriterLock()
        {
            GatewayLock gate  = new GatewayLock();
            Stopwatch   watch = new Stopwatch();

            watch.Start();
            long finishedPassThrough = 0;
            long inLock        = 0;
            Task secondaryTask = null;
            var  mainTask      = Task.Factory.StartNew(
                () =>
            {
                gate.PassThrough(
                    () =>
                {
                    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);
        }
Exemplo 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);
            }
        }
Exemplo n.º 3
0
        public void TestMultipleWriters()
        {
            GatewayLock gate  = new GatewayLock();
            Stopwatch   watch = new Stopwatch();

            watch.Start();
            long startLock1 = 0, startLock2 = 0;
            // ReSharper disable once NotAccessedVariable
            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);
                }
            }
        }
Exemplo n.º 4
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(() =>
                            {
                                // ReSharper disable once AccessToModifiedClosure
                                if (!writerDone)
                                {
                                    anyFails = true;
                                }
                            });
                        });
                    }
                    Thread.Sleep(1000);
                    writerDone = true;
                });
                Task.WaitAll(ourTasks);
            });

            main.Wait();
            Thread.MemoryBarrier();
            Assert.IsFalse(anyFails);
        }
Exemplo n.º 5
0
 public HouseholdData()
 {
     this.EntryList = new List <HouesholdAgeEntry>(25);
     this.EntryLock = new GatewayLock();
 }
Exemplo n.º 6
0
 public PersonData(ExtractPersonData self)
 {
     this.self      = self;
     this.EntryList = new List <PersonEntry>(25);
     this.EntryLock = new GatewayLock();
 }