Click here to Skip to main content
11,501,561 members (79,291 online)
Click here to Skip to main content
Articles » Languages » C# » Applications » Downloads
Add your own
alternative version

Some Useful Concurrency Classes and A Small Testbench

, 15 Jan 2007 CPOL 79.2K 950 140
Useful concurrency classes and small test bench in C#

#region Using Directives

using System;
using System.Threading;
using MPFramework.AppCore.PlatformUtilities;
using MPFramework.AppCore.Manufacturing.SpecializedTypes;

#endregion

namespace MPFramework.AppCore.MultiProcessing
{
	#region Delegates
	/// <summary>
	/// This delegate is used to implement an operation on a generic Type.
	/// </summary>
	/// <param name="target">
	/// This is the output target of the operation.
	/// </param>
	/// <param name="source">
	/// This is the source of the operation. It is used to somehow modify the
	/// target. An example would be an integer to add to the target.
	/// </param>
	/// <typeparam name="T">
	/// This is the Type that the delegate operates on. It is constrained to be
	/// a class so we have a piece of data with a sync block pointer that can be
	/// locked with .Net synchronization primitives.
	/// </typeparam>
	/// <returns>
	/// The <see typeparamref="T"/>-valued original value of <see paramref="target"/>.
	/// </returns>
	public delegate T Op<T>(ref T target, T source);
	/// <summary>
	/// Operation on a Type implementing <see cref="IReferencedValueType&lt;UValueType&gt;"/>.
	/// </summary>
	/// <typeparam name="T">
	/// A Type that must implement the interface. 
	/// </typeparam>
	/// <typeparam name="UValueType">
	/// This is the internal value Type wrapped by IReferenceValueType.
	/// </typeparam>
	/// <param name="target">
	/// This is the output target of the operation. It is passed by value, since it is
	/// the internal wrapped value that is modified.
	/// </param>
	/// <param name="source">
	/// This is the source of the operation.
	/// </param>
	/// <returns>
	/// The <see typeparamref="UValueType"/>-valued updated value of <see paramref="target"/>'s
	/// internal wrapped value.
	/// </returns>
	public delegate UValueType RVTOp<T, UValueType>(T target, T source)
		where T : class, IReferencedValueType<UValueType>
		where UValueType : struct;
	/// <summary>
	/// Operation on a <see cref="IReferencedValueType&lt;UValueType&gt;"/>.
	/// </summary>
	/// <typeparam name="UValueType">
	/// This is the internal value Type wrapped by IReferenceValueType.
	/// </typeparam>
	/// <param name="target">
	/// This is the output target of the operation. It is passed by value, since it is
	/// the internal wrapped value that is modified.
	/// </param>
	/// <param name="source">
	/// This is the source of the operation.
	/// </param>
	/// <returns>
	/// The <see typeparamref="UValueType"/>-valued updated value of <see paramref="target"/>'s
	/// internal value.
	/// </returns>
	/// <remarks>
	/// This operation can be applied to any Type implementing the interface. This may
	/// be a more complex Type than simply an RVT.
	/// </remarks>
	public delegate UValueType IRVTOp<UValueType>
		(IReferencedValueType<UValueType> target, IReferencedValueType<UValueType> source)
		where UValueType : struct;
	/// <summary>
	/// This delegate is used to implement an operation on an integer.
	/// </summary>
	public delegate System.Int32 Int32Op(ref System.Int32 target, System.Int32 source);
	#endregion
	/// <summary>
	/// This class implements simple utilities that do atomic .Net operations
	/// that are useful for synchronized objects.
	/// </summary>
	public static class AtomicUtils
	{
		#region Class Fields
		#endregion
		#region Constructors
		/// <summary>
		/// static constructor.
		/// </summary>
		static AtomicUtils () {}
		#endregion
		#region Utility Methods
		///// <summary>
		///// This OP just adds two Int32s.
		///// </summary>
		///// <param name="target">
		///// The number to receive the sum of itself plus the source.
		///// </param>
		///// <param name="source">
		///// The number to be added to the target.
		///// </param>
		///// <returns>
		///// A copy of the original target value.
		///// </returns>
		//public static System.Int32 Int32AddOp(ref System.Int32 target, System.Int32 source)
		//{
		//    Int32 oldTarget = target;
		//    target += source;
		//    return oldTarget;
		//}
		/// <summary>
		/// This OP just moves the wrapped number over to the inside of the target.
		/// </summary>
		/// <param name="targetRVT">
		/// The <see cref="IReferencedValueType&lt;UValueType&gt;"/> receive the source's
		/// wrapped value.
		/// </param>
		/// <param name="sourceRVT">
		/// The <see cref="IReferencedValueType&lt;UValueType&gt;"/> to have its internal
		/// <see typeparamref="UValueType"/> unwrapped and placed inside the target.
		/// </param>
		/// <returns>
		/// A copy of the original target's wrapped <see typeparamref="UValueType"/> value.
		/// </returns>
		public static UValueType ReplaceOp<T,UValueType>(ref T targetRVT, T sourceRVT)
			where T : class, IReferencedValueType<UValueType> where UValueType : struct
		{
			// Remember the old value on the stack.
			UValueType oldTargetValue = targetRVT.Value;
			targetRVT.Value = sourceRVT.Value;
			return oldTargetValue;
		}
		#endregion
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

krogerma
Web Developer
United States United States
Kurt R. Matis received the B.S degree in Applied Mathemetics from Empire State College in 1981 and the PhD. degree in Electrical Engineering from Rensselaer Polytechnic Institute in 1984. He has been involved in several companies over the past 30 years, but has been most recently involved with the Macher-Plander Software Engineering Consortium, of which he is a co-founder. The Consortium is involved with education in .Net technologies and Software Quality Management topics.

Dr. Matis is a member of IEEE and the American Historical Truck Society. Kurt lives happily in Troy, NY with his beautiful wife, two beautiful daughters and his beautiful trucks.

Dr. Matis is interested in working with companies who wish assistance in porting legacy applications of all types to .Net. He can be reached at krogerma@aol.com.




| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 15 Jan 2007
Article Copyright 2007 by krogerma
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid