[Mono-devel-list] new ThreadPool implementation

dietmar dietmar at ximian.com
Tue Jun 24 07:46:11 EDT 2003


Hi all,

I tried to simplify the ThreadPool implementation using the async
delegate invoke feature of the runtime. The result is really simple. 
If there are no objections I will commit that code after impl. the
remaining 2 function (GetMaxThreads, GetAvailableThreads).

Any objections?

- Dietmar
-------------- next part --------------
//
// System.Threading.ThreadPool
//
// Author:
//   Maurer Dietmar (dietmar at ximian.com)
//
// (C) Ximian, Inc.  http://www.ximian.com
//
using System;
using System.Collections;

namespace System.Threading {

	public sealed class ThreadPool {

		private ThreadPool ()
		{
		}

		public static bool BindHandle (IntPtr osHandle)
		{
			throw new NotSupportedException("This is MS specific");
		}

		public static void GetAvailableThreads (out int workerThreads, out int completionPortThreads)
		{
			throw new NotImplementedException ();
		}

		public static void GetMaxThreads (out int workerThreads, out int completionPortThreads)
		{
			throw new NotImplementedException ();
		}
			
		public static bool QueueUserWorkItem (WaitCallback callback)
		{
			IAsyncResult ar = callback.BeginInvoke (null, null, null);
			return true;
		}

		public static bool QueueUserWorkItem (WaitCallback callback, object state)
		{
			IAsyncResult ar = callback.BeginInvoke (state, null, null);
			return true;
		}

		public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
										WaitOrTimerCallback callBack,
										object state,
										int millisecondsTimeOutInterval,
										bool executeOnlyOnce)
		{
			return RegisterWaitForSingleObject (waitObject, callBack, state,
							    (long) millisecondsTimeOutInterval, executeOnlyOnce);
		}

		public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
										WaitOrTimerCallback callBack,
										object state,
										long millisecondsTimeOutInterval,
										bool executeOnlyOnce)
		{
			if (millisecondsTimeOutInterval < -1)
				throw new ArgumentOutOfRangeException ("timeout", "timeout < -1");

			if (millisecondsTimeOutInterval > Int32.MaxValue)
				throw new NotSupportedException ("Timeout is too big. Maximum is Int32.MaxValue");

			TimeSpan timeout = new TimeSpan (0, 0, 0, 0, (int) millisecondsTimeOutInterval);
			
			RegisteredWaitHandle waiter = new RegisteredWaitHandle (waitObject, callBack, state,
										timeout, executeOnlyOnce);
			QueueUserWorkItem (new WaitCallback (waiter.Wait), null);
			return waiter;
		}

		public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
										WaitOrTimerCallback callBack,
										object state,
										TimeSpan timeout,
										bool executeOnlyOnce)
		{
			return RegisterWaitForSingleObject (waitObject, callBack, state,
							    (long) timeout.TotalMilliseconds, executeOnlyOnce);

		}

		[CLSCompliant(false)]
		public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
										WaitOrTimerCallback callBack,
										object state,
										uint millisecondsTimeOutInterval,
										bool executeOnlyOnce)
		{
			return RegisterWaitForSingleObject (waitObject, callBack, state,
							    (long) millisecondsTimeOutInterval, executeOnlyOnce);
		}

		public static bool UnsafeQueueUserWorkItem (WaitCallback callback, object state)
		{
			throw new NotImplementedException ();
		}

	}
}


More information about the Mono-devel-list mailing list