2009-07-21 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / corlib / System.Threading / ThreadPool.cs
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)
11 //
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.
30 //
31
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;
38
39 namespace System.Threading {
40
41 #if NET_2_0
42         public static class ThreadPool {
43 #else
44         public sealed class ThreadPool {
45
46                 private ThreadPool ()
47                 {
48                         /* nothing to do */
49                 }
50 #endif
51
52 #if NET_2_0
53                 [Obsolete("This method is obsolete, use BindHandle(SafeHandle) instead")]
54 #endif
55                 public static bool BindHandle (IntPtr osHandle)
56                 {
57                         return true;
58                 }
59
60 #if NET_2_0
61                 public static bool BindHandle (SafeHandle osHandle)
62                 {
63                         if (osHandle == null)
64                                 throw new ArgumentNullException ("osHandle");
65                         
66                         return true;
67                 }
68 #endif
69                 
70                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
71                 public static extern void GetAvailableThreads (out int workerThreads, out int completionPortThreads);
72
73                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
74                 public static extern void GetMaxThreads (out int workerThreads, out int completionPortThreads);
75                         
76                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
77                 public static extern void GetMinThreads (out int workerThreads, out int completionPortThreads);
78
79                 [MonoTODO("The min number of completion port threads is not evaluated.")]
80                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
81                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
82                 public static extern bool SetMinThreads (int workerThreads, int completionPortThreads);
83
84 #if NET_2_0
85                 [MonoTODO("The max number of threads cannot be decremented.")]
86                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
87                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
88                 public static extern bool SetMaxThreads (int workerThreads, int completionPortThreads);
89 #endif
90                         
91                 public static bool QueueUserWorkItem (WaitCallback callBack)
92                 {
93                         return QueueUserWorkItem (callBack, null);
94                 }
95
96                 public static bool QueueUserWorkItem (WaitCallback callBack, object state)
97                 {
98 #if NET_2_1 && !MONOTOUCH
99                         callBack = MoonlightHandler (callBack);
100 #endif
101                         IAsyncResult ar = callBack.BeginInvoke (state, null, null);
102                         if (ar == null)
103                                 return false;
104                         return true;
105                 }
106
107                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
108                                                                                 WaitOrTimerCallback callBack,
109                                                                                 object state,
110                                                                                 int millisecondsTimeOutInterval,
111                                                                                 bool executeOnlyOnce)
112                 {
113                         return RegisterWaitForSingleObject (waitObject, callBack, state,
114                                                             (long) millisecondsTimeOutInterval, executeOnlyOnce);
115                 }
116
117                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
118                                                                                 WaitOrTimerCallback callBack,
119                                                                                 object state,
120                                                                                 long millisecondsTimeOutInterval,
121                                                                                 bool executeOnlyOnce)
122                 {
123                         if (millisecondsTimeOutInterval < -1)
124                                 throw new ArgumentOutOfRangeException ("timeout", "timeout < -1");
125
126                         if (millisecondsTimeOutInterval > Int32.MaxValue)
127                                 throw new NotSupportedException ("Timeout is too big. Maximum is Int32.MaxValue");
128
129                         TimeSpan timeout = new TimeSpan (0, 0, 0, 0, (int) millisecondsTimeOutInterval);
130                         
131                         RegisteredWaitHandle waiter = new RegisteredWaitHandle (waitObject, callBack, state,
132                                                                                 timeout, executeOnlyOnce);
133                         QueueUserWorkItem (new WaitCallback (waiter.Wait), null);
134                         return waiter;
135                 }
136
137                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
138                                                                                 WaitOrTimerCallback callBack,
139                                                                                 object state,
140                                                                                 TimeSpan timeout,
141                                                                                 bool executeOnlyOnce)
142                 {
143                         return RegisterWaitForSingleObject (waitObject, callBack, state,
144                                                             (long) timeout.TotalMilliseconds, executeOnlyOnce);
145
146                 }
147
148                 [CLSCompliant(false)]
149                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
150                                                                                 WaitOrTimerCallback callBack,
151                                                                                 object state,
152                                                                                 uint millisecondsTimeOutInterval,
153                                                                                 bool executeOnlyOnce)
154                 {
155                         return RegisterWaitForSingleObject (waitObject, callBack, state,
156                                                             (long) millisecondsTimeOutInterval, executeOnlyOnce);
157                 }
158
159 #if NET_2_0
160                 [CLSCompliant (false)]
161                 unsafe public static bool UnsafeQueueNativeOverlapped (NativeOverlapped *overlapped)
162                 {
163                         throw new NotImplementedException ();
164                 }
165 #endif
166
167                 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
168                 public static bool UnsafeQueueUserWorkItem (WaitCallback callBack, object state)
169                 {
170                         // no stack propagation here (that's why it's unsafe and requires extra security permissions)
171                         IAsyncResult ar = null;
172                         try {
173                                 if (!ExecutionContext.IsFlowSuppressed ())
174                                         ExecutionContext.SuppressFlow (); // on current thread only
175
176                                 ar = callBack.BeginInvoke (state, null, null);
177                         }
178                         finally {
179                                 if (ExecutionContext.IsFlowSuppressed ())
180                                         ExecutionContext.RestoreFlow ();
181                         }
182                         return (ar != null);
183                 }
184                 
185                 [MonoTODO("Not implemented")]
186                 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
187                 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
188                         WaitOrTimerCallback callBack, object state, int millisecondsTimeOutInterval,
189                         bool executeOnlyOnce) 
190                 {
191                         throw new NotImplementedException ();
192                 }
193                 
194                 [MonoTODO("Not implemented")]
195                 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
196                 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
197                         WaitOrTimerCallback callBack, object state, long millisecondsTimeOutInterval,
198                         bool executeOnlyOnce) 
199                 {
200                         throw new NotImplementedException ();
201                 }
202
203                 [MonoTODO("Not implemented")]
204                 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
205                 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
206                         WaitOrTimerCallback callBack, object state, TimeSpan timeout,
207                         bool executeOnlyOnce) 
208                 {
209                         throw new NotImplementedException ();
210                 }
211
212                 [MonoTODO("Not implemented")]
213                 [CLSCompliant (false)]
214                 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
215                 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
216                         WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval,
217                         bool executeOnlyOnce) 
218                 {
219                         throw new NotImplementedException ();
220                 }
221
222 #if NET_2_1 && !MONOTOUCH
223                 static WaitCallback MoonlightHandler (WaitCallback callback)
224                 {
225                         return delegate (object o) {
226                                 try {
227                                         callback (o);
228                                 } 
229                                 catch (Exception ex) {
230                                         Thread.MoonlightUnhandledException (ex);
231                                 } 
232                         };
233                 }
234 #endif
235         }
236 }