merged Sys.Web.Services 2.0 support in my branch:
[mono.git] / mcs / class / corlib / System.Threading / ThreadPool.cs
1 //
2 // System.Threading.ThreadPool
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                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
80                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
81                 public static extern bool SetMinThreads (int workerThreads, int completionPortThreads);
82                         
83                 public static bool QueueUserWorkItem (WaitCallback callback)
84                 {
85                         IAsyncResult ar = callback.BeginInvoke (null, null, null);
86                         if (ar == null)
87                                 return false;
88                         return true;
89                 }
90
91                 public static bool QueueUserWorkItem (WaitCallback callback, object state)
92                 {
93                         IAsyncResult ar = callback.BeginInvoke (state, null, null);
94                         if (ar == null)
95                                 return false;
96                         return true;
97                 }
98
99                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
100                                                                                 WaitOrTimerCallback callBack,
101                                                                                 object state,
102                                                                                 int millisecondsTimeOutInterval,
103                                                                                 bool executeOnlyOnce)
104                 {
105                         return RegisterWaitForSingleObject (waitObject, callBack, state,
106                                                             (long) millisecondsTimeOutInterval, executeOnlyOnce);
107                 }
108
109                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
110                                                                                 WaitOrTimerCallback callBack,
111                                                                                 object state,
112                                                                                 long millisecondsTimeOutInterval,
113                                                                                 bool executeOnlyOnce)
114                 {
115                         if (millisecondsTimeOutInterval < -1)
116                                 throw new ArgumentOutOfRangeException ("timeout", "timeout < -1");
117
118                         if (millisecondsTimeOutInterval > Int32.MaxValue)
119                                 throw new NotSupportedException ("Timeout is too big. Maximum is Int32.MaxValue");
120
121                         TimeSpan timeout = new TimeSpan (0, 0, 0, 0, (int) millisecondsTimeOutInterval);
122                         
123                         RegisteredWaitHandle waiter = new RegisteredWaitHandle (waitObject, callBack, state,
124                                                                                 timeout, executeOnlyOnce);
125                         QueueUserWorkItem (new WaitCallback (waiter.Wait), null);
126                         return waiter;
127                 }
128
129                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
130                                                                                 WaitOrTimerCallback callBack,
131                                                                                 object state,
132                                                                                 TimeSpan timeout,
133                                                                                 bool executeOnlyOnce)
134                 {
135                         return RegisterWaitForSingleObject (waitObject, callBack, state,
136                                                             (long) timeout.TotalMilliseconds, executeOnlyOnce);
137
138                 }
139
140                 [CLSCompliant(false)]
141                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
142                                                                                 WaitOrTimerCallback callBack,
143                                                                                 object state,
144                                                                                 uint millisecondsTimeOutInterval,
145                                                                                 bool executeOnlyOnce)
146                 {
147                         return RegisterWaitForSingleObject (waitObject, callBack, state,
148                                                             (long) millisecondsTimeOutInterval, executeOnlyOnce);
149                 }
150
151                 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
152                 public static bool UnsafeQueueUserWorkItem (WaitCallback callback, object state)
153                 {
154                         // no stack propagation here (that's why it's unsafe and requires extra security permissions)
155                         IAsyncResult ar = null;
156                         try {
157                                 if (!ExecutionContext.IsFlowSuppressed ())
158                                         ExecutionContext.SuppressFlow (); // on current thread only
159
160                                 ar = callback.BeginInvoke (state, null, null);
161                         }
162                         finally {
163                                 if (ExecutionContext.IsFlowSuppressed ())
164                                         ExecutionContext.RestoreFlow ();
165                         }
166                         return (ar != null);
167                 }
168                 
169                 [MonoTODO("Not implemented")]
170                 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
171                 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
172                         WaitOrTimerCallback callBack, object state, int millisecondsTimeOutInterval,
173                         bool executeOnlyOnce) 
174                 {
175                         throw new NotImplementedException ();
176                 }
177                 
178                 [MonoTODO("Not implemented")]
179                 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
180                 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
181                         WaitOrTimerCallback callBack, object state, long millisecondsTimeOutInterval,
182                         bool executeOnlyOnce) 
183                 {
184                         throw new NotImplementedException ();
185                 }
186
187                 [MonoTODO("Not implemented")]
188                 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
189                 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
190                         WaitOrTimerCallback callBack, object state, TimeSpan timeout,
191                         bool executeOnlyOnce) 
192                 {
193                         throw new NotImplementedException ();
194                 }
195
196                 [MonoTODO("Not implemented")]
197                 [CLSCompliant (false)]
198                 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
199                 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
200                         WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval,
201                         bool executeOnlyOnce) 
202                 {
203                         throw new NotImplementedException ();
204                 }
205         }
206 }