Click here to Skip to main content
15,896,111 members
Articles / High Performance Computing / Parallel Processing

Managed I/O Completion Ports (IOCP) - Part 2

Rate me:
Please Sign up or sign in to vote.
4.83/5 (31 votes)
26 Apr 200631 min read 204.3K   1.6K   127  
Lock-Free Object Pool, Lock-Free Queue, and Thread Pool for Managed IOCP.
using System;
using System.Threading;

namespace Sonic.Net.ThreadPoolTaskFramework
{
	/// <summary>
	/// Class that provides a ContextBound Task object to be used with ThreadPool
	/// </summary>
	public class WaitableContextBoundTask : Task
	{
		#region Public Constructor(s)

		/// <summary>
		/// Creates a new instance of the Task
		/// </summary>
		/// <param name="obj">User object to be used by this Task during its execution</param>
		/// <param name="hTask">TaskHandler used to execute this Task</param>
		/// <param name="tf">TaskFactory object from which this Task is created</param>
		public WaitableContextBoundTask(IContext ctx,object id,object obj,ContextBoundTaskHandler hTask,ContextBoundTaskFactory tf)
			: base(id)
		{
			_ctx = ctx;
			_object = obj;
			_hTask = hTask;
			_tf = tf;
		}

		#endregion

		#region Public Methods

		/// <summary>
		/// Get/Set the TaskHandler used by this Task during its execution
		/// </summary>
		public ContextBoundTaskHandler TaskHandler
		{
			get
			{
				return _hTask;
			}
			set
			{
				_hTask = value;
			}
		}
		
		public ContextBoundTaskFactory TaskFactory
		{
			get
			{
				return _tf;
			}
		}

		public bool Wait(int waitMilli)
		{
			return _ev.WaitOne(waitMilli,false);
		}

		#endregion

		#region Task Class Public Overrides

		/// <summary>
		/// Get/Set the user object used by this Task during its execution
		/// </summary>
		public override object UserObject
		{
			get
			{
				return _object;
			}
			set
			{
				_object = value;
			}
		}

		#endregion

		#region ITask Public Methods

		public override void Execute(ThreadPool tp)
		{
			try
			{
				_ctx.Lock();
				_hTask(_ctx,this,tp);
				_ev.Set();
			}
			catch(Exception e)
			{
				throw e;
			}
			finally
			{
				_ctx.UnLock();
			}
		}

		public override void Done()
		{
			// <TODO>
		}

		#endregion

		#region Private Data Members

		private IContext _ctx;
		private object _object;
		private ContextBoundTaskHandler _hTask;
		private ContextBoundTaskFactory _tf;
		private AutoResetEvent _ev = new AutoResetEvent(false);

		#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