Click here to Skip to main content
15,894,546 members
Articles / Programming Languages / C#

Managed I/O Completion Ports (IOCP)

Rate me:
Please Sign up or sign in to vote.
4.92/5 (76 votes)
26 Apr 200621 min read 420K   5.3K   259  
A fully managed .NET implementation of Win32 IOCP's waitable event queuing mechanism.
using System;
using System.Threading;

namespace Sonic.Net.ThreadPoolTaskFramework
{
	#region Context Classes

	/// <summary>
	/// Interface that defines a type that is used to generate unique ids
	/// </summary>
	public interface IContextIdGenerator
	{
		/// <summary>
		/// Retun the next unique context id
		/// </summary>
		/// <returns>Unique context id</returns>
		object GetNextContextId();
		/// <summary>
		/// Reset the context id so that the next context id will
		/// start from the begining of its internal sequence.
		/// </summary>
		void Reset();
	}

	/// <summary>
	/// Interface that defines a type that is used to create a unique context.
	/// Context provide an environment for serialzed task execution. 
	/// </summary>
	public interface IContext
	{
		/// <summary>
		/// Unique id of the context
		/// </summary>
		object Id
		{
			get;
		}
		/// <summary>
		/// Lock the context
		/// </summary>
		void Lock();
		/// <summary>
		/// Unlock the context
		/// </summary>
		void UnLock();
	}

	/// <summary>
	/// Abstract Factory interface to create context types
	/// </summary>
	public interface IContextFactory
	{
		IContext CreateContext();
	}

	/// <summary>
	/// Monitor based implementation of IContext interface
	/// </summary>
	public class Context : IContext	
	{
		#region Public Constructor(s)

		public Context(object id)
		{
			_id = id;
		}

		#endregion

		#region IContext Public Methods

		public object Id
		{
			get
			{
				return _id;
			}
		}
		public void Lock()
		{
			Monitor.Enter(this);
		}
		public void UnLock()
		{
			Monitor.Exit(this);
		}

		#endregion

		#region Private Data Members
		
		private object _id;

		#endregion
	}

	/// <summary>
	/// Factory class for creating Monitor based Context objects
	/// </summary>
	public class ContextFactory : IContextFactory
	{
		#region Public Constructor(s)

		public ContextFactory(IContextIdGenerator ctxIdGen)
		{
			_ctxIdGen = ctxIdGen;
		}

		#endregion

		#region IContextFactory Members

		public IContext CreateContext()
		{
			return new Context(_ctxIdGen.GetNextContextId());
		}

		#endregion

		#region Private Data Members

		IContextIdGenerator _ctxIdGen;

		#endregion
	}

	/// <summary>
	/// Singleton instance implementation of Context Id generator
	/// </summary>
	public class ContextIdGenerator : IContextIdGenerator
	{
		#region IContextIdGenerator Members

		public object GetNextContextId()
		{
			return Interlocked.Increment(ref _ctxId);
		}

		public void Reset()
		{
			_ctxId = 0;
		}

		#endregion

		#region Public Static Methods

		public static IContextIdGenerator GetInstance()
		{
			return _ctxIdGen;
		}

		#endregion

		#region Public Static Data Members

		public static readonly ContextIdGenerator _ctxIdGen = new ContextIdGenerator();
		public static long _ctxId = 0;

		#endregion
	}



	#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 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


Written By
Architect
India India
Software Professional with 14+ Years of experience in design & development of server products using Microsoft Technologies.

Woked/Working on server side product development using Managed C++ & C#, including Thread pools, Asynchronous Procedure Calls (APC), Inter Process Communication (IPC) using named pipes, Lock Free data structures in C++ & .Net, etc.

Comments and Discussions