use MOONLIGHT symbol
[mcs.git] / class / corlib / System.Threading / ThreadPool.cs
blob689a6bcf1200bcb8c30ce4d8353a62b96f93283e
1 //
2 // System.Threading.ThreadPool.cs
3 //
4 // Author:
5 // Patrik Torstensson
6 // Dick Porter (dick@ximian.com)
7 // Maurer Dietmar (dietmar@ximian.com)
8 //
9 // (C) Ximian, Inc. http://www.ximian.com
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Globalization;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.Remoting.Messaging;
36 using System.Runtime.InteropServices;
37 using System.Security.Permissions;
39 namespace System.Threading {
41 public static class ThreadPool {
42 [Obsolete("This method is obsolete, use BindHandle(SafeHandle) instead")]
43 public static bool BindHandle (IntPtr osHandle)
45 return true;
48 public static bool BindHandle (SafeHandle osHandle)
50 if (osHandle == null)
51 throw new ArgumentNullException ("osHandle");
53 return true;
56 #if !NET_2_1
57 [MethodImplAttribute(MethodImplOptions.InternalCall)]
58 public static extern void GetAvailableThreads (out int workerThreads, out int completionPortThreads);
59 #endif
60 [MethodImplAttribute(MethodImplOptions.InternalCall)]
61 public static extern void GetMaxThreads (out int workerThreads, out int completionPortThreads);
63 [MethodImplAttribute(MethodImplOptions.InternalCall)]
64 public static extern void GetMinThreads (out int workerThreads, out int completionPortThreads);
66 [MonoTODO("The min number of completion port threads is not evaluated.")]
67 [MethodImplAttribute(MethodImplOptions.InternalCall)]
68 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
69 public static extern bool SetMinThreads (int workerThreads, int completionPortThreads);
71 [MethodImplAttribute(MethodImplOptions.InternalCall)]
72 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
73 public static extern bool SetMaxThreads (int workerThreads, int completionPortThreads);
75 public static bool QueueUserWorkItem (WaitCallback callBack)
77 return QueueUserWorkItem (callBack, null);
80 public static bool QueueUserWorkItem (WaitCallback callBack, object state)
82 if (callBack == null)
83 throw new ArgumentNullException ("callBack");
85 #if MOONLIGHT
86 callBack = MoonlightHandler (callBack);
87 #endif
88 IAsyncResult ar = callBack.BeginInvoke (state, null, null);
89 if (ar == null)
90 return false;
91 return true;
94 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
95 WaitOrTimerCallback callBack,
96 object state,
97 int millisecondsTimeOutInterval,
98 bool executeOnlyOnce)
100 return RegisterWaitForSingleObject (waitObject, callBack, state,
101 (long) millisecondsTimeOutInterval, executeOnlyOnce);
104 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
105 WaitOrTimerCallback callBack,
106 object state,
107 long millisecondsTimeOutInterval,
108 bool executeOnlyOnce)
110 if (millisecondsTimeOutInterval < -1)
111 throw new ArgumentOutOfRangeException ("timeout", "timeout < -1");
113 if (millisecondsTimeOutInterval > Int32.MaxValue)
114 throw new NotSupportedException ("Timeout is too big. Maximum is Int32.MaxValue");
116 TimeSpan timeout = new TimeSpan (0, 0, 0, 0, (int) millisecondsTimeOutInterval);
118 RegisteredWaitHandle waiter = new RegisteredWaitHandle (waitObject, callBack, state,
119 timeout, executeOnlyOnce);
120 QueueUserWorkItem (new WaitCallback (waiter.Wait), null);
121 return waiter;
124 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
125 WaitOrTimerCallback callBack,
126 object state,
127 TimeSpan timeout,
128 bool executeOnlyOnce)
130 return RegisterWaitForSingleObject (waitObject, callBack, state,
131 (long) timeout.TotalMilliseconds, executeOnlyOnce);
135 [CLSCompliant(false)]
136 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
137 WaitOrTimerCallback callBack,
138 object state,
139 uint millisecondsTimeOutInterval,
140 bool executeOnlyOnce)
142 return RegisterWaitForSingleObject (waitObject, callBack, state,
143 (long) millisecondsTimeOutInterval, executeOnlyOnce);
146 #if !NET_2_1
148 [CLSCompliant (false)]
149 unsafe public static bool UnsafeQueueNativeOverlapped (NativeOverlapped *overlapped)
151 throw new NotImplementedException ();
154 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
155 public static bool UnsafeQueueUserWorkItem (WaitCallback callBack, object state)
157 // no stack propagation here (that's why it's unsafe and requires extra security permissions)
158 IAsyncResult ar = null;
159 try {
160 if (!ExecutionContext.IsFlowSuppressed ())
161 ExecutionContext.SuppressFlow (); // on current thread only
163 ar = callBack.BeginInvoke (state, null, null);
165 finally {
166 if (ExecutionContext.IsFlowSuppressed ())
167 ExecutionContext.RestoreFlow ();
169 return (ar != null);
172 [MonoTODO("Not implemented")]
173 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
174 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
175 WaitOrTimerCallback callBack, object state, int millisecondsTimeOutInterval,
176 bool executeOnlyOnce)
178 throw new NotImplementedException ();
181 [MonoTODO("Not implemented")]
182 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
183 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
184 WaitOrTimerCallback callBack, object state, long millisecondsTimeOutInterval,
185 bool executeOnlyOnce)
187 throw new NotImplementedException ();
190 [MonoTODO("Not implemented")]
191 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
192 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
193 WaitOrTimerCallback callBack, object state, TimeSpan timeout,
194 bool executeOnlyOnce)
196 throw new NotImplementedException ();
199 [MonoTODO("Not implemented")]
200 [CLSCompliant (false)]
201 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
202 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
203 WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval,
204 bool executeOnlyOnce)
206 throw new NotImplementedException ();
209 #endif
211 #if MOONLIGHT
212 static WaitCallback MoonlightHandler (WaitCallback callback)
214 return delegate (object o) {
215 try {
216 callback (o);
218 catch (Exception ex) {
219 Thread.MoonlightUnhandledException (ex);
223 #endif