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

Monitor.cs « System.Threading « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 14fe105e6f108fc9147188a4e757f192c6850d92 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//
// System.Threading.Monitor.cs
//
// Author:
//   Dick Porter (dick@ximian.com)
//
// (C) Ximian, Inc.  http://www.ximian.com
//

using System.Runtime.CompilerServices;

namespace System.Threading
{
	public sealed class Monitor
	{
		private Monitor () {}

		// Grabs the mutex on object 'obj', with a maximum
		// wait time 'ms' but doesn't block - if it can't get
		// the lock it returns false, true if it can
		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		private extern static bool Monitor_try_enter(object obj, int ms);
		public static void Enter(object obj) {
			if(obj==null) {
				throw new ArgumentNullException("Object is null");
			}
			//if(obj.GetType().IsValueType==true) {
			//	throw new ArgumentException("Value type");
			//}

			Monitor_try_enter(obj, Timeout.Infinite);
		}

		// Releases the mutex on object 'obj'
		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		private extern static void Monitor_exit(object obj);

		// Checks whether the current thread currently owns
		// the lock on object 'obj'
		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		private extern static bool Monitor_test_owner(object obj);
		
		public static void Exit(object obj) {
			if(obj==null) {
				throw new ArgumentNullException("Object is null");
			}
			//if(obj.GetType().IsValueType==true) {
			//	throw new ArgumentException("Value type");
			//}

			if(Monitor_test_owner(obj)==false) {
				throw new SynchronizationLockException("The current thread does not own the lock");
			}
			
			Monitor_exit(obj);
		}

		// Signals one of potentially many objects waiting on
		// object 'obj'
		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		private extern static void Monitor_pulse(object obj);

		// Checks whether object 'obj' is currently synchronised
		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		private extern static bool Monitor_test_synchronised(object obj);

		public static void Pulse(object obj) {
			if(obj==null) {
				throw new ArgumentNullException("Object is null");
			}
			if(Monitor_test_synchronised(obj)==false) {
				throw new SynchronizationLockException("Object is not synchronised");
			}

			Monitor_pulse(obj);
		}

		// Signals all of potentially many objects waiting on
		// object 'obj'
		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		private extern static void Monitor_pulse_all(object obj);

		public static void PulseAll(object obj) {
			if(obj==null) {
				throw new ArgumentNullException("Object is null");
			}
			if(Monitor_test_synchronised(obj)==false) {
				throw new SynchronizationLockException("Object is not synchronised");
			}

			Monitor_pulse_all(obj);
		}

		public static bool TryEnter(object obj) {
			if(obj==null) {
				throw new ArgumentNullException("Object is null");
			}
			//if(obj.GetType().IsValueType==true) {
			//	throw new ArgumentException("Value type");
			//}
			
			return(Monitor_try_enter(obj, 0));
		}

		public static bool TryEnter(object obj, int millisecondsTimeout) {
			if(obj==null) {
				throw new ArgumentNullException("Object is null");
			}
			//if(obj.GetType().IsValueType==true) {
			//	throw new ArgumentException("Value type");
			//}

			// LAMESPEC: should throw an exception when ms<0, but
			// Timeout.Infinite is -1
			if(millisecondsTimeout == Timeout.Infinite) {
				Enter(obj);
				return(true);
			}
			
			if(millisecondsTimeout<0) {
				throw new ArgumentException("millisecondsTimeout negative");
			}
			
			return(Monitor_try_enter(obj, millisecondsTimeout));
		}

		public static bool TryEnter(object obj, TimeSpan timeout) {
			if(obj==null) {
				throw new ArgumentNullException("Object is null");
			}
			//if(obj.GetType().IsValueType==true) {
			//	throw new ArgumentException("Value type");
			//}

			// LAMESPEC: should throw an exception when ms<0, but
			// Timeout.Infinite is -1
			if(timeout.Milliseconds == Timeout.Infinite) {
				Enter(obj);
				return(true);
			}

			if(timeout.Milliseconds < 0 ||
			   timeout.Milliseconds > Int32.MaxValue) {
				throw new ArgumentOutOfRangeException("timeout out of range");
			}
			
			return(Monitor_try_enter(obj, timeout.Milliseconds));
		}

		// Waits for a signal on object 'obj' with maximum
		// wait time 'ms'. Returns true if the object was
		// signalled, false if it timed out
		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		private extern static bool Monitor_wait(object obj, int ms);

		public static bool Wait(object obj) {
			if(obj==null) {
				throw new ArgumentNullException("Object is null");
			}
			if(Monitor_test_synchronised(obj)==false) {
				throw new SynchronizationLockException("Object is not synchronised");
			}

			return(Monitor_wait(obj, 0));
		}

		public static bool Wait(object obj, int millisecondsTimeout) {
			if(obj==null) {
				throw new ArgumentNullException("Object is null");
			}
			if(Monitor_test_synchronised(obj)==false) {
				throw new SynchronizationLockException("Object is not synchronised");
			}
			// LAMESPEC: no mention of timeout sanity checking

			return(Monitor_wait(obj, millisecondsTimeout));
		}

		public static bool Wait(object obj, TimeSpan timeout) {
			if(obj==null) {
				throw new ArgumentNullException("Object is null");
			}
			// LAMESPEC: says to throw ArgumentException too
			if(timeout.Milliseconds < 0 || timeout.Milliseconds > Int32.MaxValue) {
				throw new ArgumentOutOfRangeException("timeout out of range");
			}
			if(Monitor_test_synchronised(obj)==false) {
				throw new SynchronizationLockException("Object is not synchronised");
			}

			return(Monitor_wait(obj, timeout.Milliseconds));
		}

		[MonoTODO]
		public static bool Wait(object obj, int millisecondsTimeout, bool exitContext) {
			if(obj==null) {
				throw new ArgumentNullException("Object is null");
			}
			// FIXME when I understand what a
			// "synchronisation domain" is and does
			return(false);
		}

		[MonoTODO]
		public static bool Wait(object obj, TimeSpan timeout, bool exitContext) {
			if(obj==null) {
				throw new ArgumentNullException("Object is null");
			}
			// LAMESPEC: says to throw ArgumentException too
			if(timeout.Milliseconds < 0 || timeout.Milliseconds > Int32.MaxValue) {
				throw new ArgumentOutOfRangeException("timeout out of range");
			}
			
			// FIXME when I understand what a
			// "synchronisation domain" is and does
			return(false);
		}
	}
}