Example #1
0
        public void ExceptionThrownWhileLockHeld()
        {
            Padlock            p1 = new Padlock();
            ThreadShared <int> i  = new ThreadShared <int>(0, p1);

            try
            {
                using (p1.Lock())
                {
                    i.Value++;
                    throw new InvalidOperationException();
                }
            }
            catch (InvalidOperationException) {     }

            //Locks should have been released.  Create a new thread
            //to obtain the lock to ensure no deadlocks ensue.

            Thread secondThread = new Thread(delegate()
            {
                using (p1.Lock())
                {
                    i.Value++;
                }
            });

            secondThread.Start();
            secondThread.Join();

            using (p1.Lock())
            {
                Assert.AreEqual(i.Value, 2);
            }
        }
		public void Constructor_OtherPadlockNull()
		{
			Padlock p1 = new Padlock();
			Padlock p2 = null;
			Padlock p3 = new Padlock();
			ThreadShared<int> i = new ThreadShared<int>(0, p1, p2, p3);
		}
Example #3
0
 /// <summary>
 /// Set up the cache.
 /// </summary>
 static DescriptionResolver()
 {
     _cachePadlock = new Padlock();
     _cache        = new ThreadShared <Dictionary <string, Dictionary <string, string> > >(
         new Dictionary <string, Dictionary <string, string> >(),
         _cachePadlock);
 }
Example #4
0
        public void OnePadlockToMultipleResources()
        {
            Padlock            p1 = new Padlock();
            ThreadShared <int> i1 = new ThreadShared <int>(0, p1);
            ThreadShared <int> i2 = new ThreadShared <int>(0, p1);

            //Can access both ThreadShared in same block...
            using (p1.Lock())
            {
                i1.Value++;
                i2.Value++;
            }

            //... or individually
            using (p1.Lock())
            {
                i1.Value++;
            }

            using (p1.Lock())
            {
                i2.Value++;
            }

            using (p1.Lock())
            {
                Assert.AreEqual(i1.Value, 2);
                Assert.AreEqual(i2.Value, 2);
                Assert.AreEqual(i1.Value, i2.Value);
            }
        }
 /// <summary>
 /// Set up the cache.
 /// </summary>
 static DescriptionResolver()
 {
     _cachePadlock = new Padlock();
     _cache = new ThreadShared<Dictionary<string, Dictionary<string, string>>>(
         new Dictionary<string, Dictionary<string, string>>(),
         _cachePadlock);
 }
Example #6
0
 public void Constructor_OtherPadlockNull()
 {
     Padlock            p1 = new Padlock();
     Padlock            p2 = null;
     Padlock            p3 = new Padlock();
     ThreadShared <int> i  = new ThreadShared <int>(0, p1, p2, p3);
 }
Example #7
0
        public void OneLockNotTaken()
        {
            Padlock            p1 = new Padlock();
            ThreadShared <int> i  = new ThreadShared <int>(0, p1);

            //Access value outside of lock.
            i.Value++;
        }
		public void Constructor_ConstructWithValueType()
		{
			Padlock p = new Padlock();
			ThreadShared<int> i = new ThreadShared<int>(10, p);

			using (p.Lock())
			{
				Assert.AreEqual(i.Value, 10);
			}
		}
Example #9
0
        public void Constructor_ConstructWithValueType()
        {
            Padlock            p = new Padlock();
            ThreadShared <int> i = new ThreadShared <int>(10, p);

            using (p.Lock())
            {
                Assert.AreEqual(i.Value, 10);
            }
        }
Example #10
0
        public void OneLockTaken()
        {
            Padlock            p1 = new Padlock();
            ThreadShared <int> i  = new ThreadShared <int>(0, p1);

            using (p1.Lock())
            {
                i.Value++;
                Assert.AreEqual(i.Value, 1);
            }
        }
Example #11
0
        public void Constructor_ConstructWithReferenceType()
        {
            Padlock p = new Padlock();

            //Uri is a random reference type;
            ThreadShared <Uri> i = new ThreadShared <Uri>(new Uri("http://example.com"), p);

            using (p.Lock())
            {
                Assert.AreEqual(i.Value, new Uri("http://example.com"));
            }
        }
		public void Constructor_ConstructWithReferenceType()
		{
			Padlock p = new Padlock();

			//Uri is a random reference type;
			ThreadShared<Uri> i = new ThreadShared<Uri>(new Uri("http://example.com"), p);

			using (p.Lock())
			{
				Assert.AreEqual(i.Value, new Uri("http://example.com"));
			}
		}
Example #13
0
        public void LockTakenRecursively()
        {
            Padlock            p1 = new Padlock();
            ThreadShared <int> i  = new ThreadShared <int>(0, p1);

            RecursiveFunction(5, 0, p1, i);

            using (p1.Lock())
            {
                Assert.AreEqual(i.Value, 5);
            }
        }
Example #14
0
        public void TwoLocksSecondNotTaken()
        {
            Padlock            p1 = new Padlock();
            Padlock            p2 = new Padlock();
            ThreadShared <int> i  = new ThreadShared <int>(0, p1, p2);

            //Access value outside of both locks.
            using (p1.Lock())
            {
                i.Value++;
            }
        }
Example #15
0
        public void TwoLocksBothTaken()
        {
            Padlock            p1 = new Padlock();
            Padlock            p2 = new Padlock();
            ThreadShared <int> i  = new ThreadShared <int>(0, p1, p2);

            using (p1.Lock())
            {
                using (p2.Lock())
                {
                    i.Value++;
                    Assert.AreEqual(i.Value, 1);
                }
            }
        }
Example #16
0
        public void ValueGettersAreEqual()
        {
            Padlock            p1 = new Padlock();
            ThreadShared <int> i  = new ThreadShared <int>(0, p1);

            using (p1.Lock())
            {
                Assert.AreEqual(i.Value, i._);
                i.Value++;

                Assert.AreEqual(i.Value, i._);
                Assert.AreEqual(i.Value, 1);
                Assert.AreEqual(i._, 1);

                i._++;
                Assert.AreEqual(i.Value, i._);
                Assert.AreEqual(i.Value, 2);
                Assert.AreEqual(i._, 2);
            }
        }
		public void Constructor_FirstPadlockNull()
		{
			Padlock p = null;
			ThreadShared<int> i = new ThreadShared<int>(0, p);
		}
		public void ExceptionThrownWhileLockHeld()
		{
			Padlock p1 = new Padlock();
			ThreadShared<int> i = new ThreadShared<int>(0, p1);

			try
			{
				using (p1.Lock())
				{
					i.Value++;
					throw new InvalidOperationException();
				}
			}
			catch (InvalidOperationException) {	}

			//Locks should have been released.  Create a new thread
			//to obtain the lock to ensure no deadlocks ensue.

			Thread secondThread = new Thread(delegate()
				{
					using (p1.Lock())
					{
						i.Value++;
					}
				});

			secondThread.Start();
			secondThread.Join();

			using (p1.Lock())
			{
				Assert.AreEqual(i.Value, 2);
			}
		}
Example #19
0
 private void RecursiveFunction(int maxRecursionDepth, int currentDepth, Padlock p, ThreadShared <int> i)
 {
     if (currentDepth < maxRecursionDepth)
     {
         using (p.Lock())
         {
             i.Value++;
             RecursiveFunction(maxRecursionDepth, ++currentDepth, p, i);
         }
     }
 }
		private void RecursiveFunction(int maxRecursionDepth, int currentDepth, Padlock p, ThreadShared<int> i)
		{
			if (currentDepth < maxRecursionDepth)
			{
				using (p.Lock())
				{
					i.Value++;
					RecursiveFunction(maxRecursionDepth, ++currentDepth, p, i);
				}
			}
		}
		public void OnePadlockToMultipleResources()
		{
			Padlock p1 = new Padlock();
			ThreadShared<int> i1 = new ThreadShared<int>(0, p1);
			ThreadShared<int> i2 = new ThreadShared<int>(0, p1);

			//Can access both ThreadShared in same block...
			using (p1.Lock())
			{
				i1.Value++;
				i2.Value++;
			}

			//... or individually
			using (p1.Lock())
			{
				i1.Value++;
			}

			using (p1.Lock())
			{
				i2.Value++;
			}

			using(p1.Lock())
			{
				Assert.AreEqual(i1.Value, 2);
				Assert.AreEqual(i2.Value, 2);
				Assert.AreEqual(i1.Value, i2.Value);
			}
		}
		public void LockTakenRecursively()
		{
			Padlock p1 = new Padlock();
			ThreadShared<int> i = new ThreadShared<int>(0, p1);
			RecursiveFunction(5, 0, p1, i);

			using (p1.Lock())
			{
				Assert.AreEqual(i.Value, 5);
			}
		}
Example #23
0
 public void Constructor_FirstPadlockNull()
 {
     Padlock            p = null;
     ThreadShared <int> i = new ThreadShared <int>(0, p);
 }
		public void TwoLocksBothTaken()
		{
			Padlock p1 = new Padlock();
			Padlock p2 = new Padlock();
			ThreadShared<int> i = new ThreadShared<int>(0, p1, p2);

			using (p1.Lock())
			{
				using (p2.Lock())
				{
					i.Value++;
					Assert.AreEqual(i.Value, 1);
				}
			}
		}
		public void TwoLocksSecondNotTaken()
		{
			Padlock p1 = new Padlock();
			Padlock p2 = new Padlock();
			ThreadShared<int> i = new ThreadShared<int>(0, p1, p2);

			//Access value outside of both locks.
			using (p1.Lock())
			{
				i.Value++;
			}
		}
		public void OneLockTaken()
		{
			Padlock p1 = new Padlock();
			ThreadShared<int> i = new ThreadShared<int>(0, p1);

			using (p1.Lock())
			{
				i.Value++;
				Assert.AreEqual(i.Value, 1);
			}
		}
		public void ValueGettersAreEqual()
		{
			Padlock p1 = new Padlock();
			ThreadShared<int> i = new ThreadShared<int>(0, p1);

			using (p1.Lock())
			{
				Assert.AreEqual(i.Value, i._);
				i.Value++;

				Assert.AreEqual(i.Value, i._);
				Assert.AreEqual(i.Value, 1);
				Assert.AreEqual(i._, 1);

				i._++;
				Assert.AreEqual(i.Value, i._);
				Assert.AreEqual(i.Value, 2);
				Assert.AreEqual(i._, 2);
			}
		}
			public SafeIncrementor()
			{
				_i = new ThreadShared<int>(0, p);
			}
		public void Constructor_AllPadlocksValid()
		{
			Padlock p1 = new Padlock();
			Padlock p2 = new Padlock();
			ThreadShared<int> i = new ThreadShared<int>(0, p1, p2);
		}
Example #30
0
 public void Constructor_AllPadlocksValid()
 {
     Padlock            p1 = new Padlock();
     Padlock            p2 = new Padlock();
     ThreadShared <int> i  = new ThreadShared <int>(0, p1, p2);
 }
			public SafeList()
			{
				_list = new ThreadShared<List<object>>(new List<object>(), p);
			}
		public void OneLockNotTaken()
		{
			Padlock p1 = new Padlock();
			ThreadShared<int> i = new ThreadShared<int>(0, p1);

			//Access value outside of lock.
			i.Value++;
		}
 /// <summary>
 /// Initialize the random number generator
 /// </summary>
 static StaticRandom()
 {
     _padlock = new Padlock();
     _random  = new ThreadShared <Random>(new Random(), _padlock);
 }
		/// <summary>
		/// Initialize the random number generator
		/// </summary>
		static StaticRandom()
		{
			_padlock = new Padlock();
			_random = new ThreadShared<Random>(new Random(), _padlock);
		}