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