New test.
[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.Security.Permissions;
37
38 namespace System.Threading {
39
40 #if NET_2_0
41         public static class ThreadPool {
42 #else
43         public sealed class ThreadPool {
44
45                 private ThreadPool ()
46                 {
47                         /* nothing to do */
48                 }
49 #endif
50                 public static bool BindHandle (IntPtr osHandle)
51                 {
52                         return true;
53                 }
54                 
55                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
56                 public static extern void GetAvailableThreads (out int workerThreads, out int completionPortThreads);
57
58                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
59                 public static extern void GetMaxThreads (out int workerThreads, out int completionPortThreads);
60                         
61                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
62                 public static extern void GetMinThreads (out int workerThreads, out int completionPortThreads);
63                         
64                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
65                 [SecurityPermission (SecurityAction.Demand, ControlThread=true)]
66                 public static extern bool SetMinThreads (int workerThreads, int completionPortThreads);
67                         
68                 public static bool QueueUserWorkItem (WaitCallback callback)
69                 {
70                         IAsyncResult ar = callback.BeginInvoke (null, null, null);
71                         if (ar == null)
72                                 return false;
73                         return true;
74                 }
75
76                 public static bool QueueUserWorkItem (WaitCallback callback, object state)
77                 {
78                         IAsyncResult ar = callback.BeginInvoke (state, null, null);
79                         if (ar == null)
80                                 return false;
81                         return true;
82                 }
83
84                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
85                                                                                 WaitOrTimerCallback callBack,
86                                                                                 object state,
87                                                                                 int millisecondsTimeOutInterval,
88                                                                                 bool executeOnlyOnce)
89                 {
90                         return RegisterWaitForSingleObject (waitObject, callBack, state,
91                                                             (long) millisecondsTimeOutInterval, executeOnlyOnce);
92                 }
93
94                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
95                                                                                 WaitOrTimerCallback callBack,
96                                                                                 object state,
97                                                                                 long millisecondsTimeOutInterval,
98                                                                                 bool executeOnlyOnce)
99                 {
100                         if (millisecondsTimeOutInterval < -1)
101                                 throw new ArgumentOutOfRangeException ("timeout", "timeout < -1");
102
103                         if (millisecondsTimeOutInterval > Int32.MaxValue)
104                                 throw new NotSupportedException ("Timeout is too big. Maximum is Int32.MaxValue");
105
106                         TimeSpan timeout = new TimeSpan (0, 0, 0, 0, (int) millisecondsTimeOutInterval);
107                         
108                         RegisteredWaitHandle waiter = new RegisteredWaitHandle (waitObject, callBack, state,
109                                                                                 timeout, executeOnlyOnce);
110                         QueueUserWorkItem (new WaitCallback (waiter.Wait), null);
111                         return waiter;
112                 }
113
114                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
115                                                                                 WaitOrTimerCallback callBack,
116                                                                                 object state,
117                                                                                 TimeSpan timeout,
118                                                                                 bool executeOnlyOnce)
119                 {
120                         return RegisterWaitForSingleObject (waitObject, callBack, state,
121                                                             (long) timeout.TotalMilliseconds, executeOnlyOnce);
122
123                 }
124
125                 [CLSCompliant(false)]
126                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
127                                                                                 WaitOrTimerCallback callBack,
128                                                                                 object state,
129                                                                                 uint millisecondsTimeOutInterval,
130                                                                                 bool executeOnlyOnce)
131                 {
132                         return RegisterWaitForSingleObject (waitObject, callBack, state,
133                                                             (long) millisecondsTimeOutInterval, executeOnlyOnce);
134                 }
135
136                 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
137                 public static bool UnsafeQueueUserWorkItem (WaitCallback callback, object state)
138                 {
139                         // no stack propagation here (that's why it's unsafe and requires extra security permissions)
140                         IAsyncResult ar = null;
141                         try {
142                                 if (!ExecutionContext.IsFlowSuppressed ())
143                                         ExecutionContext.SuppressFlow (); // on current thread only
144
145                                 ar = callback.BeginInvoke (state, null, null);
146                         }
147                         finally {
148                                 if (ExecutionContext.IsFlowSuppressed ())
149                                         ExecutionContext.RestoreFlow ();
150                         }
151                         return (ar != null);
152                 }
153                 
154                 [MonoTODO]
155                 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
156                 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
157                         WaitOrTimerCallback callBack, object state, int millisecondsTimeOutInterval,
158                         bool executeOnlyOnce) 
159                 {
160                         throw new NotImplementedException ();
161                 }
162                 
163                 [MonoTODO]
164                 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
165                 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
166                         WaitOrTimerCallback callBack, object state, long millisecondsTimeOutInterval,
167                         bool executeOnlyOnce) 
168                 {
169                         throw new NotImplementedException ();
170                 }
171
172                 [MonoTODO]
173                 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
174                 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
175                         WaitOrTimerCallback callBack, object state, TimeSpan timeout,
176                         bool executeOnlyOnce) 
177                 {
178                         throw new NotImplementedException ();
179                 }
180
181                 [MonoTODO]
182                 [CLSCompliant (false)]
183                 [SecurityPermission (SecurityAction.Demand, ControlEvidence=true, ControlPolicy=true)]
184                 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
185                         WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval,
186                         bool executeOnlyOnce) 
187                 {
188                         throw new NotImplementedException ();
189                 }
190         }
191 }