Click here to Skip to main content
11,571,471 members (68,795 online)
Click here to Skip to main content
Add your own
alternative version

Lock Manager for .NET

, 11 Aug 2003 68.7K 853 49
Deadlock resolver for muti-threading applications.
lockmanager.zip
Free
bin
LockManager.csproj.user
obj
Step1
App.ico
bin
obj
Step1.csproj.user
Step2
App.ico
bin
obj
Step2.csproj.user
Step3
App.ico
bin
obj
Step3.csproj.user
Step4
App.ico
bin
obj
Step4.csproj.user
Step5
App.ico
bin
obj
Step5.csproj.user
ThreeObjectsTest
App.ico
bin
obj
ThreeObjectsTest.csproj.user
/// <disclaimer>
/// This software is protected by your own conscience (c).
/// You can use it however you want and wherever you want.
/// You are allowed to change this code, copy this code, or delete this code.
/// You can buy this code; sell this code; present this code to your mom on her birthday.
/// You can replace author�s name with your own. You also can replace all the code with your own leaving
/// only author�s name.
/// The only thing you cannot do, is to violate this license agreement. You simply are not able to.
/// </disclaimer>
/// <author>
/// Sergei Zotin 
/// szotin@shaw.ca
/// Burnaby, BC, Canada
/// Feel free to contact me for bug reports, feature requests and contract offers.
/// </author>
/// <version>1.6</version>

using System;
using System.Threading;
using ZEN.Threading;
using System.IO;

class A
{
	private B b;
	private int x;
	private int y;

	public B TheB
	{
		get
		{
			LockManager.Lock ( this );
			try
			{
				return b;
			}
			finally
			{
				LockManager.Unlock ( this );
			}
		}
		set
		{
			LockManager.Lock ( this );
			try
			{
				b = value;
			}
			finally
			{
				LockManager.Unlock ( this );
			}
		}
	}

	public int X
	{
		get
		{
			LockManager.Lock ( this );
			try
			{
				return x;
			}
			finally
			{
				LockManager.Unlock ( this );
			}
		}

		// NEW: Now we catch DeadlockException internaly, release all locks and retry operation.
		// NEW: That allows thread to continue excecution rather than breaking.
		set
		{
			bool retry;
			do
			{
				retry = false;
				LockManager.Lock ( this );
				try
				{
					Thread.Sleep ( 10 ); // just to increase the odds of a deadlock
					x = value;
					b.X = value;
				}
				catch ( DeadlockException )
				{
					retry = true;
				}
				finally
				{
					LockManager.Unlock ( this );
				}
			} while ( retry );
		}
	}

	public int Y
	{
		get
		{
			LockManager.Lock ( this );
			try
			{
				return y;
			}
			finally
			{
				LockManager.Unlock ( this );
			}
		}
		set
		{
			LockManager.Lock ( this );
			try
			{
				Thread.Sleep ( 10 ); // just to increase the odds of a deadlock
				y = value;
			}
			finally
			{
				LockManager.Unlock ( this );
			}
		}
	}
}

class B
{
	private A a;
	private int x;
	private int y;

	public A TheA
	{
		get
		{
			LockManager.Lock ( this );
			try
			{
				return a;
			}
			finally
			{
				LockManager.Unlock ( this );
			}
		}
		set
		{
			LockManager.Lock ( this );
			try
			{
				a = value;
			}
			finally
			{
				LockManager.Unlock ( this );
			}
		}
	}

	public int X
	{
		get
		{
			LockManager.Lock ( this );
			try
			{
				return x;
			}
			finally
			{
				LockManager.Unlock ( this );
			}
		}
		set
		{
			LockManager.Lock ( this );
			try
			{
				Thread.Sleep ( 10 ); // just to increase the odds of a deadlock
				x = value;
			}
			finally
			{
				LockManager.Unlock ( this );
			}
		}
	}

	public int Y
	{
		get
		{
			LockManager.Lock ( this );
			try
			{
				return y;
			}
			finally
			{
				LockManager.Unlock ( this );
			}
		}

		// NEW: Now we catch DeadlockException internaly, release all locks and retry operation.
		// NEW: That allows thread to continue excecution rather than breaking.
		set
		{
			bool retry;
			do
			{
				retry = false;
				LockManager.Lock ( this );
				try
				{
					Thread.Sleep ( 10 ); // just to increase the odds of a deadlock
					y = value;
					a.Y = value;
				}
				catch ( DeadlockException )
				{
					retry = true;
				}
				finally
				{
					LockManager.Unlock ( this );
				}
			} while ( retry );
		}
	}
}

class Step3
{
	private A a;
	private B b;

	private Step3 ( A _a, B _b )
	{
		a = _a;
		b = _b;
		a.TheB = b;
		b.TheA = a;
	}

	// NEW: Now exception handling is removed. a.X = i doesn't throw DeadlockException any more.
	private void RunX ()
	{
		for ( int i = 0; i < 20; i++ )
		{
			a.X = i;
			System.Console.WriteLine ( "Thread X: a.X={0}; b.X={1}", a.X, b.X );
		}
	}

	// NEW: Now exception handling is removed. a.X = i doesn't throw DeadlockException any more.
	private void RunY ()
	{
		for ( int i = 0; i < 20; i++ )
		{
			b.Y = i;
			System.Console.WriteLine ( "Thread Y: a.Y={0}; b.Y={1}", a.Y, b.Y );
		}
	}

	// NEW: We changed logging from console to file "deadlock.log".
	[STAThread]
	static void Main ( string[] args )
	{
		A a = new A();
		B b = new B();
		Step3 step3 = new Step3 ( a, b );

		TextWriter log = new StreamWriter ( "deadlock.log", false );
		LockManager.Log = log;

		Thread threadX = new Thread ( new ThreadStart ( step3.RunX ) );
		Thread threadY = new Thread ( new ThreadStart ( step3.RunY ) );

		threadX.Name = "X";
		threadY.Name = "Y";

		threadX.Start ();
		threadY.Start ();

		threadX.Join ();
		threadY.Join ();

		// closing the log
		LockManager.Log = null;
		log.Close ();

		System.Console.WriteLine ( "Press Enter..." );
		System.Console.In.Read();
		System.Environment.Exit ( 0 );
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

SZotin
Canada Canada
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150624.2 | Last Updated 12 Aug 2003
Article Copyright 2003 by SZotin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid