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