[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/Threading RegisteredWa
From: |
Rhys Weatherley <address@hidden> |
Subject: |
[Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/Threading RegisteredWaitHandle.cs,1.2,1.3 ThreadPool.cs,1.1,1.2 Timer.cs,1.6,1.7 WaitCallback.cs,1.1,1.2WaitOrTimerCallback.cs,1.1,1.2 |
Date: |
Tue, 01 Apr 2003 23:58:27 -0500 |
Update of /cvsroot/dotgnu-pnet/pnetlib/runtime/System/Threading
In directory subversions:/tmp/cvs-serv18346/runtime/System/Threading
Modified Files:
RegisteredWaitHandle.cs ThreadPool.cs Timer.cs WaitCallback.cs
WaitOrTimerCallback.cs
Log Message:
Implement the "ThreadPool" class.
Index: RegisteredWaitHandle.cs
===================================================================
RCS file:
/cvsroot/dotgnu-pnet/pnetlib/runtime/System/Threading/RegisteredWaitHandle.cs,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** RegisteredWaitHandle.cs 2 Apr 2003 02:23:21 -0000 1.2
--- RegisteredWaitHandle.cs 2 Apr 2003 04:58:25 -0000 1.3
***************
*** 23,43 ****
{
! #if !ECMA_COMPAT
!
! public sealed class RegisteredWaitHandle : MarshalByRefObject
{
// Constructor.
! internal RegisteredWaitHandle() {}
// Unregister using a specific wait object.
public bool Unregister(WaitHandle waitObject)
{
! return true;
}
}; // class RegisteredWaitHandle
-
- #endif // !ECMA_COMPAT
}; // namespace System.Threading
--- 23,57 ----
{
! #if ECMA_COMPAT
! internal
! #else
! public
! #endif
! sealed class RegisteredWaitHandle : MarshalByRefObject
{
+ // Internal state.
+ private ThreadPool.WorkItem workItem;
// Constructor.
! internal RegisteredWaitHandle(ThreadPool.WorkItem workItem)
! {
! this.workItem = workItem;
! }
// Unregister using a specific wait object.
public bool Unregister(WaitHandle waitObject)
{
! if(workItem.waitObject == waitObject)
! {
! workItem.registered = false;
! return true;
! }
! else
! {
! return false;
! }
}
}; // class RegisteredWaitHandle
}; // namespace System.Threading
Index: ThreadPool.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/runtime/System/Threading/ThreadPool.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** ThreadPool.cs 16 Feb 2003 10:46:50 -0000 1.1
--- ThreadPool.cs 2 Apr 2003 04:58:25 -0000 1.2
***************
*** 1,8 ****
/*
! * ThreadPool.cs - Implementation of "ThreadPool" class
*
* Copyright (C) 2003 Southern Storm Software, Pty Ltd.
- *
- * Contributed by Gopal.V
*
* This program is free software; you can redistribute it and/or modify
--- 1,6 ----
/*
! * ThreadPool.cs - Implementation of the "System.Threading.ThreadPool" class.
*
* Copyright (C) 2003 Southern Storm Software, Pty Ltd.
*
* This program is free software; you can redistribute it and/or modify
***************
*** 21,166 ****
*/
! using System;
! #if !ECMA_COMPAT
! namespace System.Threading
{
! /* Note: I have this suspicion that non-ECMA builds would actually
! use this class internally ?
! */
! public sealed class ThreadPool
{
! [TODO]
! public static bool BindHandle(IntPtr osHandle)
! {
! // win32 Specific ?
! throw new NotImplementedException("BindHandle");
! }
!
! [TODO]
! public static void GetAvailableThreads( out int workerThreads,
!
out int completionPortThreads)
! {
! throw new
NotImplementedException("GetAvailableThreads");
! }
!
! [TODO]
! public static void GetMaxThreads(out int workerThreads,
!
out int completionPortThreads)
! {
! throw new NotImplementedException("GetMaxThreads");
! }
!
! [TODO]
! public static bool QueueUserWorkItem(WaitCallback callback)
! {
! throw new NotImplementedException("QueueUserWorkItem");
! }
!
! [TODO]
! public static bool QueueUserWorkItem(WaitCallback callback,
!
Object state)
! {
! throw new NotImplementedException("QueueUserWorkItem");
! }
!
! [TODO]
! public static RegisteredWaitHandle RegisterWaitForSingleObject(
! WaitHandle
waitObject,
!
WaitOrTimerCallback callback,
! Object state,
! int
millisecondsTimeOutInterval,
! bool
executeOnlyOnce)
! {
! throw new
NotImplementedException("RegisterWaitForSingleObject");
! }
!
! [TODO]
! public static RegisteredWaitHandle RegisterWaitForSingleObject(
! WaitHandle
waitObject,
!
WaitOrTimerCallback callback,
! Object state,
! long
millisecondsTimeOutInterval,
! bool
executeOnlyOnce)
! {
! throw new
NotImplementedException("RegisterWaitForSingleObject");
! }
!
! [TODO]
! public static RegisteredWaitHandle RegisterWaitForSingleObject(
! WaitHandle
waitObject,
!
WaitOrTimerCallback callback,
! Object state,
! TimeSpan
timeout,
! bool
executeOnlyOnce)
! {
! throw new
NotImplementedException("RegisterWaitForSingleObject");
! }
!
! [TODO]
! public static RegisteredWaitHandle RegisterWaitForSingleObject(
! WaitHandle
waitObject,
!
WaitOrTimerCallback callback,
! Object state,
! uint
millisecondsTimeOutInterval,
! bool
executeOnlyOnce)
! {
! throw new
NotImplementedException("RegisterWaitForSingleObject");
! }
!
! [TODO]
! public static bool UnsafeQueueUserWorkItem(
! WaitCallback
callback,
! Object state)
! {
! throw new
NotImplementedException("UnsafeQueueUserWorkItem");
! }
!
! [TODO]
! public static RegisteredWaitHandle
UnsafeRegisterWaitForSingleObject(
! WaitHandle
waitObject,
!
WaitOrTimerCallback callback,
! Object state,
! int
millisecondsTimeOutInterval,
! bool
executeOnlyOnce)
! {
! throw new
NotImplementedException("UnsafeRegisterWaitForSingleObject");
! }
!
! [TODO]
! public static RegisteredWaitHandle
UnsafeRegisterWaitForSingleObject(
! WaitHandle
waitObject,
!
WaitOrTimerCallback callback,
! Object state,
! long
millisecondsTimeOutInterval,
! bool
executeOnlyOnce)
! {
! throw new
NotImplementedException("UnsafeRegisterWaitForSingleObject");
! }
!
! [TODO]
! public static RegisteredWaitHandle
UnsafeRegisterWaitForSingleObject(
! WaitHandle
waitObject,
!
WaitOrTimerCallback callback,
! Object state,
! TimeSpan
timeout,
! bool
executeOnlyOnce)
! {
! throw new
NotImplementedException("UnsafeRegisterWaitForSingleObject");
! }
!
! [TODO]
! public static RegisteredWaitHandle
UnsafeRegisterWaitForSingleObject(
! WaitHandle
waitObject,
!
WaitOrTimerCallback callback,
! Object state,
! uint
millisecondsTimeOutInterval,
! bool
executeOnlyOnce)
! {
! throw new
NotImplementedException("UnsafeRegisterWaitForSingleObject");
! }
! }
! }//namespace
! #endif
--- 19,501 ----
*/
! namespace System.Threading
! {
! using System.Security;
! using System.Security.Permissions;
! #if ECMA_COMPAT
! internal
! #else
! public
! #endif
! sealed class ThreadPool
{
! // Maximum number of threads in the pool.
! private const int MaxWorkerThreads = 16;
! private const int MaxCompletionThreads = 16;
!
! // Internal state.
! private static int usedWorkerThreads;
! private static int usedCompletionThreads;
! private static WorkItem workItems, lastWorkItem;
! private static WorkItem completionItems, lastCompletionItem;
! private static Thread[] workerThreads;
! private static Thread[] completionThreads;
! private static int numWorkerThreads;
! private static int numCompletionThreads;
! private static Object completionWait;
!
! // Constructor.
! private ThreadPool() {}
!
! // Bind an operating system handle to this thread pool
! public static bool BindHandle(IntPtr osHandle)
! {
! // Not used in this implementation.
! return true;
! }
!
! // Get the number of available threads in the thread pool.
! public static void GetAvailableThreads(out int workerThreads,
!
out int completionPortThreads)
! {
! lock(typeof(ThreadPool))
! {
! workerThreads = MaxWorkerThreads -
usedWorkerThreads;
! completionPortThreads =
! MaxCompletionThreads -
usedCompletionThreads;
! }
! }
!
! // Get the maximum number of threads in the thread pool.
! public static void GetMaxThreads(out int workerThreads,
! out
int completionPortThreads)
! {
! workerThreads = MaxWorkerThreads;
! completionPortThreads = MaxCompletionThreads;
! }
!
! // Queue a new work item within the thread pool.
! public static bool QueueUserWorkItem(WaitCallback callBack, Object
state)
! {
! AddWorkItem(new
WorkItem(ClrSecurity.GetPermissionsFrom(1),
!
callBack, state));
! return true;
! }
! public static bool QueueUserWorkItem(WaitCallback callBack)
! {
! return QueueUserWorkItem(callBack, null);
! }
!
! // Queue a new I/O completion item within the thread pool.
! internal static bool QueueCompletionItem
! (WaitCallback callBack, Object state)
! {
! lock(typeof(ThreadPool))
! {
! if(completionWait == null)
! {
! completionWait = new Object();
! }
! }
! AddCompletionItem
! (new
WorkItem(ClrSecurity.GetPermissionsFrom(1),
! callBack, state));
! return true;
! }
!
! // Queue a new work item within the thread pool after dropping security.
! // This is "unsafe" in that it may elevate security permissions.
! // However, in our implementation we never elevate the security,
! // so the unsafe version is identical to the safe one above.
! public static bool UnsafeQueueUserWorkItem
! (WaitCallback callBack, Object state)
! {
! return QueueUserWorkItem(callBack, state);
! }
!
! // Register a callback to be invoked when a wait handle is available.
! public static RegisteredWaitHandle RegisterWaitForSingleObject
! (WaitHandle waitObject, WaitOrTimerCallback
callBack,
! Object state, int millisecondsTimeOutInterval,
! bool executeOnlyOnce)
! {
! if(waitObject == null)
! {
! throw new
ArgumentNullException("waitObject");
! }
! if(millisecondsTimeOutInterval < -1)
! {
! throw new ArgumentOutOfRangeException
! ("millisecondsTimeOutInterval",
! _("ArgRange_NonNegOrNegOne"));
! }
! WorkItem item = new
WorkItem(ClrSecurity.GetPermissionsFrom(1),
!
waitObject, callBack, state,
!
millisecondsTimeOutInterval,
!
executeOnlyOnce);
! AddWorkItem(item);
! return new RegisteredWaitHandle(item);
! }
! public static RegisteredWaitHandle RegisterWaitForSingleObject
! (WaitHandle waitObject, WaitOrTimerCallback
callBack,
! Object state, long millisecondsTimeOutInterval,
! bool executeOnlyOnce)
! {
! return RegisterWaitForSingleObject
! (waitObject, callBack, state,
!
Timer.LongToMS(millisecondsTimeOutInterval),
! executeOnlyOnce);
! }
! public static RegisteredWaitHandle RegisterWaitForSingleObject
! (WaitHandle waitObject, WaitOrTimerCallback
callBack,
! Object state, TimeSpan timeout,
! bool executeOnlyOnce)
! {
! return RegisterWaitForSingleObject
! (waitObject, callBack, state,
! Monitor.TimeSpanToMS(timeout),
executeOnlyOnce);
! }
! [CLSCompliant(false)]
! public static RegisteredWaitHandle RegisterWaitForSingleObject
! (WaitHandle waitObject, WaitOrTimerCallback
callBack,
! Object state, uint millisecondsTimeOutInterval,
! bool executeOnlyOnce)
! {
! return RegisterWaitForSingleObject
! (waitObject, callBack, state,
!
Timer.UIntToMS(millisecondsTimeOutInterval),
! executeOnlyOnce);
! }
!
! // Register a callback to be invoked when a wait handle is available.
! // This is "unsafe" in that it may elevate security permissions.
! // However, in our implementation we never elevate the security,
! // so the unsafe versions are identical to the safe ones above.
! public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject
! (WaitHandle waitObject, WaitOrTimerCallback
callBack,
! Object state, int millisecondsTimeOutInterval,
! bool executeOnlyOnce)
! {
! return RegisterWaitForSingleObject
! (waitObject, callBack, state,
millisecondsTimeOutInterval,
! executeOnlyOnce);
! }
! public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject
! (WaitHandle waitObject, WaitOrTimerCallback
callBack,
! Object state, long millisecondsTimeOutInterval,
! bool executeOnlyOnce)
! {
! return RegisterWaitForSingleObject
! (waitObject, callBack, state,
!
Timer.LongToMS(millisecondsTimeOutInterval),
! executeOnlyOnce);
! }
! public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject
! (WaitHandle waitObject, WaitOrTimerCallback
callBack,
! Object state, TimeSpan timeout,
! bool executeOnlyOnce)
! {
! return RegisterWaitForSingleObject
! (waitObject, callBack, state,
! Monitor.TimeSpanToMS(timeout),
executeOnlyOnce);
! }
! [CLSCompliant(false)]
! public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject
! (WaitHandle waitObject, WaitOrTimerCallback
callBack,
! Object state, uint millisecondsTimeOutInterval,
! bool executeOnlyOnce)
! {
! return RegisterWaitForSingleObject
! (waitObject, callBack, state,
!
Timer.UIntToMS(millisecondsTimeOutInterval),
! executeOnlyOnce);
! }
!
! // Get the next work item to be dispatched.
! private static WorkItem ItemToDispatch()
! {
! lock(typeof(ThreadPool))
! {
! WorkItem item = workItems;
! if(item != null)
! {
! workItems = item.next;
! if(item.next == null)
! {
! lastWorkItem = null;
! }
! }
! return item;
! }
! }
!
! // Get the next completion item to be dispatched.
! private static WorkItem CompletionItemToDispatch()
! {
! lock(completionWait)
! {
! WorkItem item = completionItems;
! if(item != null)
! {
! completionItems = item.next;
! if(item.next == null)
! {
! lastCompletionItem =
null;
! }
! }
! return item;
! }
! }
!
! // Run a worker thread.
! private static void Work()
! {
! // Assert unrestricted permissions for this
thread.
! // The permissions will be modified for each
work item
! // to reflect the context that created the work
item.
! ClrSecurity.SetPermissions(null, 0);
!
! // Wait for and dispatch work items.
! WorkItem item;
! lock(typeof(ThreadPool))
! {
! for(;;)
! {
! item = ItemToDispatch();
! if(item == null)
! {
!
Monitor.Wait(typeof(ThreadPool));
! }
! else
! {
! ++usedWorkerThreads;
!
Monitor.Exit(typeof(ThreadPool));
! item.Execute();
!
Monitor.Enter(typeof(ThreadPool));
! --usedWorkerThreads;
! }
! }
! }
! }
!
! // Run a completion thread.
! private static void Complete()
! {
! // Assert unrestricted permissions for this
thread.
! // The permissions will be modified for each
work item
! // to reflect the context that created the work
item.
! ClrSecurity.SetPermissions(null, 0);
!
! // Wait for and dispatch completion items.
! WorkItem item;
! lock(completionWait)
! {
! for(;;)
! {
! item =
CompletionItemToDispatch();
! if(item == null)
! {
!
Monitor.Wait(completionWait);
! }
! else
! {
! ++usedCompletionThreads;
!
Monitor.Exit(completionWait);
! item.Execute();
!
Monitor.Enter(completionWait);
! --usedCompletionThreads;
! }
! }
! }
! }
!
! // Add a work item to the worker queue.
! private static void AddWorkItem(WorkItem item)
! {
! // Add the item to the end of the worker queue.
! lock(typeof(ThreadPool))
! {
! if(lastWorkItem != null)
! {
! lastWorkItem.next = item;
! }
! else
! {
! workItems = item;
! }
! lastWorkItem = item;
! }
! if(!Thread.CanStartThreads())
! {
! // We don't have threads, so execute
the items now.
! WorkItem next = ItemToDispatch();
! while(next != null)
! {
! next.Execute();
! next = ItemToDispatch();
! }
! }
! else
! {
! // Determine if we need to spawn a new
worker thread.
! lock(typeof(ThreadPool))
! {
! if(workItems != null &&
! numWorkerThreads <
MaxWorkerThreads)
! {
! if(workerThreads ==
null)
! {
! workerThreads =
new Thread [MaxWorkerThreads];
! }
! Thread thread = new
Thread(new ThreadStart(Work));
!
workerThreads[numWorkerThreads++] = thread;
! thread.IsBackground =
true;
! thread.Start();
! }
!
Monitor.Pulse(typeof(ThreadPool));
! }
! }
! }
!
! // Add a work item to the completion queue.
! private static void AddCompletionItem(WorkItem item)
! {
! // Add the item to the end of the worker queue.
! lock(completionWait)
! {
! if(lastCompletionItem != null)
! {
! lastCompletionItem.next = item;
! }
! else
! {
! completionItems = item;
! }
! lastCompletionItem = item;
! }
! if(!Thread.CanStartThreads())
! {
! // We don't have threads, so execute
the items now.
! WorkItem next =
CompletionItemToDispatch();
! while(next != null)
! {
! next.Execute();
! next =
CompletionItemToDispatch();
! }
! }
! else
! {
! // Determine if we need to spawn a new
completion thread.
! lock(completionWait)
! {
! if(completionItems != null &&
! numCompletionThreads <
MaxCompletionThreads)
! {
! if(completionThreads ==
null)
! {
!
completionThreads =
! new
Thread [MaxCompletionThreads];
! }
! Thread thread =
! new Thread(new
ThreadStart(Complete));
!
completionThreads[numCompletionThreads++] = thread;
! thread.IsBackground =
true;
! thread.Start();
! }
! Monitor.Pulse(completionWait);
! }
! }
! }
!
! // Structure of a work item.
! internal sealed class WorkItem
{
! // Internal state.
! public WorkItem next;
! private ClrPermissions permissions;
! private bool userWorkItem;
! private WaitCallback callback;
! private WaitOrTimerCallback callback2;
! private Object state;
! private int timeout;
! private bool once;
! internal WaitHandle waitObject;
! internal bool registered;
!
! // Constructors.
! public WorkItem(ClrPermissions permissions,
! WaitCallback callback, Object
state)
! {
! this.permissions = permissions;
! this.userWorkItem = true;
! this.callback = callback;
! this.state = state;
! }
! public WorkItem(ClrPermissions permissions, WaitHandle
waitObject,
! WaitOrTimerCallback callback,
Object state,
! int
millisecondsTimeOutInterval, bool executeOnlyOnce)
! {
! this.permissions = permissions;
! this.userWorkItem = false;
! this.callback2 = callback;
! this.state = state;
! this.timeout =
millisecondsTimeOutInterval;
! this.once = executeOnlyOnce;
! this.registered = true;
! }
!
! // Execute this work item.
! public void Execute()
! {
! try
! {
! // Set the permissions for this
stack frame
! // to the caller's permission
set.
! if(permissions != null)
! {
!
ClrSecurity.SetPermissions(permissions, 0);
! }
!
! // Run the work item.
! if(userWorkItem)
! {
! if(callback != null)
! {
! callback(state);
! }
! }
! else
! {
! do
! {
!
if(waitObject.WaitOne(timeout, false))
! {
!
if(callback2 != null)
! {
!
callback2(state, false);
! }
! }
! else
if(registered)
! {
!
if(callback2 != null)
! {
!
callback2(state, true);
! }
! }
! }
! while(registered &&
!once);
! }
! }
! catch(Exception)
! {
! // Catch and ignore all
exceptions.
! }
! }
! }; // class WorkItem
!
! }; // class ThreadPool
!
! }; // namespace System.Threading
Index: Timer.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/runtime/System/Threading/Timer.cs,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -r1.6 -r1.7
*** Timer.cs 2 Apr 2003 00:04:54 -0000 1.6
--- Timer.cs 2 Apr 2003 04:58:25 -0000 1.7
***************
*** 233,240 ****
}
- #if !ECMA_COMPAT
-
// Convert an unsigned integer value into a milliseconds value.
! private static int UIntToMS(uint value)
{
if(value > (uint)(Int32.MaxValue))
--- 233,238 ----
}
// Convert an unsigned integer value into a milliseconds value.
! internal static int UIntToMS(uint value)
{
if(value > (uint)(Int32.MaxValue))
***************
*** 247,251 ****
// Convert a long integer value into a milliseconds value.
! private static int LongToMS(long value)
{
if(value < (-1L) || value >
(long)(Int32.MaxValue))
--- 245,249 ----
// Convert a long integer value into a milliseconds value.
! internal static int LongToMS(long value)
{
if(value < (-1L) || value >
(long)(Int32.MaxValue))
***************
*** 256,261 ****
return (int)value;
}
-
- #endif // !ECMA_COMPAT
// Method that runs the timer thread.
--- 254,257 ----
Index: WaitCallback.cs
===================================================================
RCS file:
/cvsroot/dotgnu-pnet/pnetlib/runtime/System/Threading/WaitCallback.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** WaitCallback.cs 16 Feb 2003 10:46:50 -0000 1.1
--- WaitCallback.cs 2 Apr 2003 04:58:25 -0000 1.2
***************
*** 23,33 ****
using System;
- #if !ECMA_COMPAT
-
namespace System.Threading
{
[Serializable]
! public delegate void WaitCallback(Object state);
! }//namespace
!
#endif
--- 23,34 ----
using System;
namespace System.Threading
{
[Serializable]
! #if ECMA_COMPAT
! internal
! #else
! public
#endif
+ delegate void WaitCallback(Object state);
+ }//namespace
Index: WaitOrTimerCallback.cs
===================================================================
RCS file:
/cvsroot/dotgnu-pnet/pnetlib/runtime/System/Threading/WaitOrTimerCallback.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** WaitOrTimerCallback.cs 16 Feb 2003 10:46:50 -0000 1.1
--- WaitOrTimerCallback.cs 2 Apr 2003 04:58:25 -0000 1.2
***************
*** 24,34 ****
using System;
- #if !ECMA_COMPAT
-
namespace System.Threading
{
[Serializable]
! public delegate void WaitOrTimerCallback(object state, bool timedOut);
! }//namespace
!
#endif
--- 24,35 ----
using System;
namespace System.Threading
{
[Serializable]
! #if ECMA_COMPAT
! internal
! #else
! public
#endif
+ delegate void WaitOrTimerCallback(object state, bool timedOut);
+ }//namespace
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/Threading RegisteredWaitHandle.cs,1.2,1.3 ThreadPool.cs,1.1,1.2 Timer.cs,1.6,1.7 WaitCallback.cs,1.1,1.2WaitOrTimerCallback.cs,1.1,1.2,
Rhys Weatherley <address@hidden> <=