**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System.Threading / ThreadPool.cs
blobcaafdfe80c307d940a2f8ed4e0db8b49c6e7f6f6
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 Novell (http://www.novell.com)
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 //
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 //
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System;
37 using System.Collections;
38 using System.Globalization;
39 using System.Runtime.CompilerServices;
41 namespace System.Threading {
43 public sealed class ThreadPool {
45 private ThreadPool ()
47 /* nothing to do */
50 [MethodImplAttribute(MethodImplOptions.InternalCall)]
51 static extern bool BindHandleInternal (IntPtr osHandle);
53 public static bool BindHandle (IntPtr osHandle)
55 return BindHandleInternal (osHandle);
58 [MethodImplAttribute(MethodImplOptions.InternalCall)]
59 public static extern void GetAvailableThreads (out int workerThreads, out int completionPortThreads);
61 [MethodImplAttribute(MethodImplOptions.InternalCall)]
62 public static extern void GetMaxThreads (out int workerThreads, out int completionPortThreads);
64 [MethodImplAttribute(MethodImplOptions.InternalCall)]
65 public static extern void GetMinThreads (out int workerThreads, out int completionPortThreads);
67 [MethodImplAttribute(MethodImplOptions.InternalCall)]
68 public static extern bool SetMinThreads (int workerThreads, int completionPortThreads);
70 public static bool QueueUserWorkItem (WaitCallback callback)
72 IAsyncResult ar = callback.BeginInvoke (null, null, null);
73 if (ar == null)
74 return false;
75 return true;
78 public static bool QueueUserWorkItem (WaitCallback callback, object state)
80 IAsyncResult ar = callback.BeginInvoke (state, null, null);
81 if (ar == null)
82 return false;
83 return true;
86 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
87 WaitOrTimerCallback callBack,
88 object state,
89 int millisecondsTimeOutInterval,
90 bool executeOnlyOnce)
92 return RegisterWaitForSingleObject (waitObject, callBack, state,
93 (long) millisecondsTimeOutInterval, executeOnlyOnce);
96 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
97 WaitOrTimerCallback callBack,
98 object state,
99 long millisecondsTimeOutInterval,
100 bool executeOnlyOnce)
102 if (millisecondsTimeOutInterval < -1)
103 throw new ArgumentOutOfRangeException ("timeout", "timeout < -1");
105 if (millisecondsTimeOutInterval > Int32.MaxValue)
106 throw new NotSupportedException ("Timeout is too big. Maximum is Int32.MaxValue");
108 TimeSpan timeout = new TimeSpan (0, 0, 0, 0, (int) millisecondsTimeOutInterval);
110 RegisteredWaitHandle waiter = new RegisteredWaitHandle (waitObject, callBack, state,
111 timeout, executeOnlyOnce);
112 QueueUserWorkItem (new WaitCallback (waiter.Wait), null);
113 return waiter;
116 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
117 WaitOrTimerCallback callBack,
118 object state,
119 TimeSpan timeout,
120 bool executeOnlyOnce)
122 return RegisterWaitForSingleObject (waitObject, callBack, state,
123 (long) timeout.TotalMilliseconds, executeOnlyOnce);
127 [CLSCompliant(false)]
128 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
129 WaitOrTimerCallback callBack,
130 object state,
131 uint millisecondsTimeOutInterval,
132 bool executeOnlyOnce)
134 return RegisterWaitForSingleObject (waitObject, callBack, state,
135 (long) millisecondsTimeOutInterval, executeOnlyOnce);
138 public static bool UnsafeQueueUserWorkItem (WaitCallback callback, object state)
140 IAsyncResult ar = callback.BeginInvoke (state, null, null);
141 if (ar == null)
142 return false;
143 return true;
146 [MonoTODO]
147 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
148 WaitOrTimerCallback callBack, object state, int millisecondsTimeOutInterval,
149 bool executeOnlyOnce)
151 throw new NotImplementedException ();
154 [MonoTODO]
155 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
156 WaitOrTimerCallback callBack, object state, long millisecondsTimeOutInterval,
157 bool executeOnlyOnce)
159 throw new NotImplementedException ();
162 [MonoTODO]
163 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
164 WaitOrTimerCallback callBack, object state, TimeSpan timeout,
165 bool executeOnlyOnce)
167 throw new NotImplementedException ();
170 [MonoTODO]
171 [CLSCompliant (false)]
172 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
173 WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval,
174 bool executeOnlyOnce)
176 throw new NotImplementedException ();