Copied remotely
[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 Novell (http://www.novell.com)
11 //
12
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
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.
34 //
35
36 using System;
37 using System.Collections;
38 using System.Globalization;
39 using System.Runtime.CompilerServices;
40
41 namespace System.Threading {
42
43         public sealed class ThreadPool {
44
45                 private ThreadPool ()
46                 {
47                         /* nothing to do */
48                 }
49
50                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
51                 static extern bool BindHandleInternal (IntPtr osHandle);
52
53                 public static bool BindHandle (IntPtr osHandle)
54                 {
55                         return BindHandleInternal (osHandle);
56                 }
57                 
58                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
59                 public static extern void GetAvailableThreads (out int workerThreads, out int completionPortThreads);
60
61                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
62                 public static extern void GetMaxThreads (out int workerThreads, out int completionPortThreads);
63                         
64                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
65                 public static extern void GetMinThreads (out int workerThreads, out int completionPortThreads);
66                         
67                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
68                 public static extern bool SetMinThreads (int workerThreads, int completionPortThreads);
69                         
70                 public static bool QueueUserWorkItem (WaitCallback callback)
71                 {
72                         IAsyncResult ar = callback.BeginInvoke (null, null, null);
73                         if (ar == null)
74                                 return false;
75                         return true;
76                 }
77
78                 public static bool QueueUserWorkItem (WaitCallback callback, object state)
79                 {
80                         IAsyncResult ar = callback.BeginInvoke (state, null, null);
81                         if (ar == null)
82                                 return false;
83                         return true;
84                 }
85
86                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
87                                                                                 WaitOrTimerCallback callBack,
88                                                                                 object state,
89                                                                                 int millisecondsTimeOutInterval,
90                                                                                 bool executeOnlyOnce)
91                 {
92                         return RegisterWaitForSingleObject (waitObject, callBack, state,
93                                                             (long) millisecondsTimeOutInterval, executeOnlyOnce);
94                 }
95
96                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
97                                                                                 WaitOrTimerCallback callBack,
98                                                                                 object state,
99                                                                                 long millisecondsTimeOutInterval,
100                                                                                 bool executeOnlyOnce)
101                 {
102                         if (millisecondsTimeOutInterval < -1)
103                                 throw new ArgumentOutOfRangeException ("timeout", "timeout < -1");
104
105                         if (millisecondsTimeOutInterval > Int32.MaxValue)
106                                 throw new NotSupportedException ("Timeout is too big. Maximum is Int32.MaxValue");
107
108                         TimeSpan timeout = new TimeSpan (0, 0, 0, 0, (int) millisecondsTimeOutInterval);
109                         
110                         RegisteredWaitHandle waiter = new RegisteredWaitHandle (waitObject, callBack, state,
111                                                                                 timeout, executeOnlyOnce);
112                         QueueUserWorkItem (new WaitCallback (waiter.Wait), null);
113                         return waiter;
114                 }
115
116                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
117                                                                                 WaitOrTimerCallback callBack,
118                                                                                 object state,
119                                                                                 TimeSpan timeout,
120                                                                                 bool executeOnlyOnce)
121                 {
122                         return RegisterWaitForSingleObject (waitObject, callBack, state,
123                                                             (long) timeout.TotalMilliseconds, executeOnlyOnce);
124
125                 }
126
127                 [CLSCompliant(false)]
128                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
129                                                                                 WaitOrTimerCallback callBack,
130                                                                                 object state,
131                                                                                 uint millisecondsTimeOutInterval,
132                                                                                 bool executeOnlyOnce)
133                 {
134                         return RegisterWaitForSingleObject (waitObject, callBack, state,
135                                                             (long) millisecondsTimeOutInterval, executeOnlyOnce);
136                 }
137
138                 public static bool UnsafeQueueUserWorkItem (WaitCallback callback, object state)
139                 {
140                         IAsyncResult ar = callback.BeginInvoke (state, null, null);
141                         if (ar == null)
142                                 return false;
143                         return true;
144                 }
145                 
146                 [MonoTODO]
147                 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
148                         WaitOrTimerCallback callBack, object state, int millisecondsTimeOutInterval,
149                         bool executeOnlyOnce) 
150                 {
151                         throw new NotImplementedException ();
152                 }
153                 
154                 [MonoTODO]
155                 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
156                         WaitOrTimerCallback callBack, object state, long millisecondsTimeOutInterval,
157                         bool executeOnlyOnce) 
158                 {
159                         throw new NotImplementedException ();
160                 }
161
162                 [MonoTODO]
163                 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
164                         WaitOrTimerCallback callBack, object state, TimeSpan timeout,
165                         bool executeOnlyOnce) 
166                 {
167                         throw new NotImplementedException ();
168                 }
169
170                 [MonoTODO]
171                 [CLSCompliant (false)]
172                 public static RegisteredWaitHandle UnsafeRegisterWaitForSingleObject (WaitHandle waitObject,
173                         WaitOrTimerCallback callBack, object state, uint millisecondsTimeOutInterval,
174                         bool executeOnlyOnce) 
175                 {
176                         throw new NotImplementedException ();
177                 }
178         }
179 }