Welcome to mirror list, hosted at ThFree Co, Russian Federation.

WaitHandle.cs « System.Threading « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1d6ec67ee80fbce350eee95b7d05432c2e41cd88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
// System.Threading.WaitHandle.cs
//
// Author:
//   Dick Porter (dick@ximian.com)
//
// (C) Ximian, Inc.  http://www.ximian.com
//

using System.Runtime.CompilerServices;

namespace System.Threading
{
	public abstract class WaitHandle : MarshalByRefObject, IDisposable
	{
		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		private static extern bool WaitAll_internal(WaitHandle[] handles, int ms, bool exitContext);
		
		public static bool WaitAll(WaitHandle[] waitHandles) {
			if(waitHandles.Length>64) {
				throw new NotSupportedException("Too many handles");
			}
			for(int i=0; i<waitHandles.Length; i++) {
				if(waitHandles[i]==null) {
					throw new ArgumentNullException("null handle");
				}
			}
			
			return(WaitAll_internal(waitHandles, Timeout.Infinite,
						false));
		}

		public static bool WaitAll(WaitHandle[] waitHandles,
					   int millisecondsTimeout,
					   bool exitContext) {
			if(waitHandles.Length>64) {
				throw new NotSupportedException("Too many handles");
			}
			for(int i=0; i<waitHandles.Length; i++) {
				if(waitHandles[i]==null) {
					throw new ArgumentNullException("null handle");
				}
			}
			
			return(WaitAll_internal(waitHandles, millisecondsTimeout, false));
		}

		public static bool WaitAll(WaitHandle[] waitHandles,
					   TimeSpan timeout,
					   bool exitContext) {
			if(timeout.Milliseconds < 0 ||
			   timeout.Milliseconds > Int32.MaxValue) {
				throw new ArgumentOutOfRangeException("Timeout out of range");
			}
			if(waitHandles.Length>64) {
				throw new NotSupportedException("Too many handles");
			}
			for(int i=0; i<waitHandles.Length; i++) {
				if(waitHandles[i]==null) {
					throw new ArgumentNullException("null handle");
				}
			}
			
			return(WaitAll_internal(waitHandles,
						timeout.Milliseconds,
						exitContext));
		}

		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		private static extern int WaitAny_internal(WaitHandle[] handles, int ms, bool exitContext);

		// LAMESPEC: Doesn't specify how to signal failures
		public static int WaitAny(WaitHandle[] waitHandles) {
			if(waitHandles.Length>64) {
				throw new NotSupportedException("Too many handles");
			}
			for(int i=0; i<waitHandles.Length; i++) {
				if(waitHandles[i]==null) {
					throw new ArgumentNullException("null handle");
				}
			}
			
			return(WaitAny_internal(waitHandles, Timeout.Infinite,
						false));
		}

		public static int WaitAny(WaitHandle[] waitHandles,
					  int millisecondsTimeout,
					  bool exitContext) {
			if(waitHandles.Length>64) {
				throw new NotSupportedException("Too many handles");
			}
			for(int i=0; i<waitHandles.Length; i++) {
				if(waitHandles[i]==null) {
					throw new ArgumentNullException("null handle");
				}
			}
			
			return(WaitAny_internal(waitHandles,
						millisecondsTimeout,
						exitContext));
		}

		public static int WaitAny(WaitHandle[] waitHandles,
					  TimeSpan timeout, bool exitContext) {
			if(timeout.Milliseconds < 0 ||
			   timeout.Milliseconds > Int32.MaxValue) {
				throw new ArgumentOutOfRangeException("Timeout out of range");
			}
			if(waitHandles.Length>64) {
				throw new NotSupportedException("Too many handles");
			}
			for(int i=0; i<waitHandles.Length; i++) {
				if(waitHandles[i]==null) {
					throw new ArgumentNullException("null handle");
				}
			}
			
			return(WaitAny_internal(waitHandles,
						timeout.Milliseconds,
						exitContext));
		}

		[MonoTODO]
		public WaitHandle() {
			// FIXME
		}

		public const int WaitTimeout = 258;

		protected IntPtr os_handle = IntPtr.Zero;
		
		public virtual IntPtr Handle {
			get {
				return(os_handle);
			}
				
			set {
				os_handle=value;
			}
		}

		public virtual void Close() {
			Dispose(false);
		}

		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		protected virtual extern bool WaitOne_internal(IntPtr handle, int ms, bool exitContext);

		public virtual bool WaitOne() {
			return(WaitOne_internal(os_handle, Timeout.Infinite,
						false));
		}

		public virtual bool WaitOne(int millisecondsTimeout, bool exitContext) {
			return(WaitOne_internal(os_handle,
						millisecondsTimeout,
						exitContext));
		}

		public virtual bool WaitOne(TimeSpan timeout, bool exitContext) {
			return(WaitOne_internal(os_handle,
						timeout.Milliseconds,
						exitContext));
		}

		protected static readonly IntPtr InvalidHandle;

		private bool disposed = false;

		public void Dispose() {
			Dispose(true);
			// Take yourself off the Finalization queue
			GC.SuppressFinalize(this);
		}
		
		protected virtual void Dispose(bool explicitDisposing) {
			// Check to see if Dispose has already been called.
			if(!this.disposed) {
				// If this is a call to Dispose,
				// dispose all managed resources.
				if(explicitDisposing) {
					// Free up stuff here
					//Components.Dispose();
				}

				// Release unmanaged resources
				// Note that this is not thread safe.
				// Another thread could start
				// disposing the object after the
				// managed resources are disposed, but
				// before the disposed flag is set to
				// true.
				this.disposed=true;
				//Release(handle);
				//handle=IntPtr.Zero;
			}
		}

		~WaitHandle() {
			Dispose(false);
		}
	}
}