use delegate async invoke where posssible
[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 //
11 using System;
12 using System.Collections;
13 using System.Runtime.CompilerServices;
14
15 namespace System.Threading {
16
17         public sealed class ThreadPool {
18
19                 private ThreadPool ()
20                 {
21                         /* nothing to do */
22                 }
23
24                 public static bool BindHandle (IntPtr osHandle)
25                 {
26                         throw new NotSupportedException("This is MS specific");
27                 }
28                 
29                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
30                 public static extern void GetAvailableThreads (out int workerThreads, out int completionPortThreads);
31
32                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
33                 public static extern void GetMaxThreads (out int workerThreads, out int completionPortThreads);
34                         
35                 public static bool QueueUserWorkItem (WaitCallback callback)
36                 {
37                         IAsyncResult ar = callback.BeginInvoke (null, null, null);
38                         if (ar == null)
39                                 return false;
40                         return true;
41                 }
42
43                 public static bool QueueUserWorkItem (WaitCallback callback, object state)
44                 {
45                         IAsyncResult ar = callback.BeginInvoke (state, null, null);
46                         if (ar == null)
47                                 return false;
48                         return true;
49                 }
50
51                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
52                                                                                 WaitOrTimerCallback callBack,
53                                                                                 object state,
54                                                                                 int millisecondsTimeOutInterval,
55                                                                                 bool executeOnlyOnce)
56                 {
57                         return RegisterWaitForSingleObject (waitObject, callBack, state,
58                                                             (long) millisecondsTimeOutInterval, executeOnlyOnce);
59                 }
60
61                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
62                                                                                 WaitOrTimerCallback callBack,
63                                                                                 object state,
64                                                                                 long millisecondsTimeOutInterval,
65                                                                                 bool executeOnlyOnce)
66                 {
67                         if (millisecondsTimeOutInterval < -1)
68                                 throw new ArgumentOutOfRangeException ("timeout", "timeout < -1");
69
70                         if (millisecondsTimeOutInterval > Int32.MaxValue)
71                                 throw new NotSupportedException ("Timeout is too big. Maximum is Int32.MaxValue");
72
73                         TimeSpan timeout = new TimeSpan (0, 0, 0, 0, (int) millisecondsTimeOutInterval);
74                         
75                         RegisteredWaitHandle waiter = new RegisteredWaitHandle (waitObject, callBack, state,
76                                                                                 timeout, executeOnlyOnce);
77                         QueueUserWorkItem (new WaitCallback (waiter.Wait), null);
78                         return waiter;
79                 }
80
81                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
82                                                                                 WaitOrTimerCallback callBack,
83                                                                                 object state,
84                                                                                 TimeSpan timeout,
85                                                                                 bool executeOnlyOnce)
86                 {
87                         return RegisterWaitForSingleObject (waitObject, callBack, state,
88                                                             (long) timeout.TotalMilliseconds, executeOnlyOnce);
89
90                 }
91
92                 [CLSCompliant(false)]
93                 public static RegisteredWaitHandle RegisterWaitForSingleObject (WaitHandle waitObject,
94                                                                                 WaitOrTimerCallback callBack,
95                                                                                 object state,
96                                                                                 uint millisecondsTimeOutInterval,
97                                                                                 bool executeOnlyOnce)
98                 {
99                         return RegisterWaitForSingleObject (waitObject, callBack, state,
100                                                             (long) millisecondsTimeOutInterval, executeOnlyOnce);
101                 }
102
103                 public static bool UnsafeQueueUserWorkItem (WaitCallback callback, object state)
104                 {
105                         IAsyncResult ar = callback.BeginInvoke (state, null, null);
106                         if (ar == null)
107                                 return false;
108                         return true;
109                 }
110
111         }
112 }