144
|
1 using System;
|
|
2 using Implab.Parallels;
|
|
3 using System.Threading;
|
|
4 using System.Reflection;
|
|
5
|
|
6 namespace Implab {
|
145
|
7 public abstract class AbstractEvent<THandler> : ICancellationToken, ICancellable {
|
144
|
8
|
|
9 const int UNRESOLVED_SATE = 0;
|
|
10 const int TRANSITIONAL_STATE = 1;
|
|
11 const int SUCCEEDED_STATE = 2;
|
|
12 const int REJECTED_STATE = 3;
|
|
13 const int CANCELLED_STATE = 4;
|
|
14
|
|
15 const int CANCEL_NOT_REQUESTED = 0;
|
|
16 const int CANCEL_REQUESTING = 1;
|
|
17 const int CANCEL_REQUESTED = 2;
|
|
18
|
|
19 const int RESERVED_HANDLERS_COUNT = 4;
|
|
20
|
|
21 int m_state;
|
|
22 Exception m_error;
|
|
23 int m_handlersCount;
|
|
24
|
|
25 readonly THandler[] m_handlers = new THandler[RESERVED_HANDLERS_COUNT];
|
|
26 MTQueue<THandler> m_extraHandlers;
|
|
27 int m_handlerPointer = -1;
|
|
28 int m_handlersCommited;
|
|
29
|
|
30 int m_cancelRequest;
|
|
31 Exception m_cancelationReason;
|
|
32 MTQueue<Action<Exception>> m_cancelationHandlers;
|
|
33
|
|
34
|
|
35 #region state managment
|
|
36 bool BeginTransit() {
|
|
37 return UNRESOLVED_SATE == Interlocked.CompareExchange(ref m_state, TRANSITIONAL_STATE, UNRESOLVED_SATE);
|
|
38 }
|
|
39
|
|
40 void CompleteTransit(int state) {
|
|
41 if (TRANSITIONAL_STATE != Interlocked.CompareExchange(ref m_state, state, TRANSITIONAL_STATE))
|
|
42 throw new InvalidOperationException("Can't complete transition when the object isn't in the transitional state");
|
|
43 }
|
|
44
|
|
45 void WaitTransition() {
|
|
46 while (m_state == TRANSITIONAL_STATE) {
|
|
47 Thread.MemoryBarrier();
|
|
48 }
|
|
49 }
|
|
50
|
|
51 protected bool BeginSetResult() {
|
|
52 if (!BeginTransit()) {
|
|
53 WaitTransition();
|
|
54 if (m_state != CANCELLED_STATE)
|
|
55 throw new InvalidOperationException("The promise is already resolved");
|
|
56 return false;
|
|
57 }
|
|
58 return true;
|
|
59 }
|
|
60
|
|
61 protected void EndSetResult() {
|
|
62 CompleteTransit(SUCCEEDED_STATE);
|
|
63 OnSuccess();
|
|
64 }
|
|
65
|
|
66
|
|
67
|
|
68 /// <summary>
|
|
69 /// Выполняет обещание, сообщая об ошибке
|
|
70 /// </summary>
|
|
71 /// <remarks>
|
|
72 /// Поскольку обещание должно работать в многопточной среде, при его выполнении сразу несколько потоков
|
|
73 /// могу вернуть ошибку, при этом только первая будет использована в качестве результата, остальные
|
|
74 /// будут проигнорированы.
|
|
75 /// </remarks>
|
|
76 /// <param name="error">Исключение возникшее при выполнении операции</param>
|
|
77 /// <exception cref="InvalidOperationException">Данное обещание уже выполнено</exception>
|
|
78 protected void SetError(Exception error) {
|
|
79 if (BeginTransit()) {
|
|
80 if (error is OperationCanceledException) {
|
|
81 CompleteTransit(CANCELLED_STATE);
|
|
82 m_error = error.InnerException;
|
|
83 OnCancelled();
|
|
84 } else {
|
|
85 m_error = error is PromiseTransientException ? error.InnerException : error;
|
|
86 CompleteTransit(REJECTED_STATE);
|
|
87 OnError();
|
|
88 }
|
|
89 } else {
|
|
90 WaitTransition();
|
|
91 if (m_state == SUCCEEDED_STATE)
|
|
92 throw new InvalidOperationException("The promise is already resolved");
|
|
93 }
|
|
94 }
|
|
95
|
|
96 /// <summary>
|
|
97 /// Отменяет операцию, если это возможно.
|
|
98 /// </summary>
|
|
99 /// <remarks>Для определения была ли операция отменена следует использовать свойство <see cref="IsCancelled"/>.</remarks>
|
|
100 protected void SetCancelled(Exception reason) {
|
|
101 if (BeginTransit()) {
|
|
102 m_error = reason;
|
|
103 CompleteTransit(CANCELLED_STATE);
|
|
104 OnCancelled();
|
|
105 }
|
|
106 }
|
|
107
|
|
108 protected abstract void SignalSuccess(THandler handler);
|
|
109
|
|
110 protected abstract void SignalError(THandler handler, Exception error);
|
|
111
|
|
112 protected abstract void SignalCancelled(THandler handler, Exception reason);
|
|
113
|
|
114 void OnSuccess() {
|
|
115 var hp = m_handlerPointer;
|
|
116 var slot = hp +1 ;
|
|
117 while (slot < m_handlersCommited) {
|
|
118 if (Interlocked.CompareExchange(ref m_handlerPointer, slot, hp) == hp) {
|
|
119 SignalSuccess(m_handlers[slot]);
|
|
120 }
|
|
121 hp = m_handlerPointer;
|
|
122 slot = hp +1 ;
|
|
123 }
|
|
124
|
|
125
|
|
126 if (m_extraHandlers != null) {
|
|
127 THandler handler;
|
|
128 while (m_extraHandlers.TryDequeue(out handler))
|
|
129 SignalSuccess(handler);
|
|
130 }
|
|
131 }
|
|
132
|
|
133 void OnError() {
|
|
134 var hp = m_handlerPointer;
|
|
135 var slot = hp +1 ;
|
|
136 while (slot < m_handlersCommited) {
|
|
137 if (Interlocked.CompareExchange(ref m_handlerPointer, slot, hp) == hp) {
|
|
138 SignalError(m_handlers[slot],m_error);
|
|
139 }
|
|
140 hp = m_handlerPointer;
|
|
141 slot = hp +1 ;
|
|
142 }
|
|
143
|
|
144 if (m_extraHandlers != null) {
|
|
145 THandler handler;
|
|
146 while (m_extraHandlers.TryDequeue(out handler))
|
|
147 SignalError(handler, m_error);
|
|
148 }
|
|
149 }
|
|
150
|
|
151 void OnCancelled() {
|
|
152 var hp = m_handlerPointer;
|
|
153 var slot = hp +1 ;
|
|
154 while (slot < m_handlersCommited) {
|
|
155 if (Interlocked.CompareExchange(ref m_handlerPointer, slot, hp) == hp) {
|
|
156 SignalCancelled(m_handlers[slot], m_error);
|
|
157 }
|
|
158 hp = m_handlerPointer;
|
|
159 slot = hp +1 ;
|
|
160 }
|
|
161
|
|
162 if (m_extraHandlers != null) {
|
|
163 THandler handler;
|
|
164 while (m_extraHandlers.TryDequeue(out handler))
|
|
165 SignalCancelled(handler, m_error);
|
|
166 }
|
|
167 }
|
|
168
|
|
169 #endregion
|
|
170
|
|
171 protected abstract Signal GetResolveSignal();
|
|
172
|
|
173 #region synchronization traits
|
|
174 protected void WaitResult(int timeout) {
|
148
|
175 if (!(IsResolved || GetResolveSignal().Wait(timeout)))
|
|
176 throw new TimeoutException();
|
144
|
177
|
|
178 switch (m_state) {
|
|
179 case SUCCEEDED_STATE:
|
|
180 return;
|
|
181 case CANCELLED_STATE:
|
|
182 throw new OperationCanceledException();
|
|
183 case REJECTED_STATE:
|
|
184 throw new TargetInvocationException(m_error);
|
|
185 default:
|
|
186 throw new ApplicationException(String.Format("Invalid promise state {0}", m_state));
|
|
187 }
|
|
188 }
|
|
189 #endregion
|
|
190
|
|
191 #region handlers managment
|
|
192
|
|
193 protected void AddHandler(THandler handler) {
|
|
194
|
|
195 if (m_state > 1) {
|
|
196 // the promise is in the resolved state, just invoke the handler
|
|
197 InvokeHandler(handler);
|
|
198 } else {
|
|
199 var slot = Interlocked.Increment(ref m_handlersCount) - 1;
|
|
200
|
|
201 if (slot < RESERVED_HANDLERS_COUNT) {
|
|
202
|
|
203 m_handlers[slot] = handler;
|
|
204
|
|
205 while (slot != Interlocked.CompareExchange(ref m_handlersCommited, slot + 1, slot)) {
|
|
206 }
|
|
207
|
|
208 if (m_state > 1) {
|
|
209 do {
|
|
210 var hp = m_handlerPointer;
|
|
211 slot = hp + 1;
|
|
212 if (slot < m_handlersCommited) {
|
|
213 if (Interlocked.CompareExchange(ref m_handlerPointer, slot, hp) != hp)
|
|
214 continue;
|
|
215 InvokeHandler(m_handlers[slot]);
|
|
216 }
|
|
217 break;
|
|
218 } while(true);
|
|
219 }
|
|
220 } else {
|
|
221 if (slot == RESERVED_HANDLERS_COUNT) {
|
|
222 m_extraHandlers = new MTQueue<THandler>();
|
|
223 } else {
|
|
224 while (m_extraHandlers == null)
|
|
225 Thread.MemoryBarrier();
|
|
226 }
|
|
227
|
|
228 m_extraHandlers.Enqueue(handler);
|
|
229
|
|
230 if (m_state > 1 && m_extraHandlers.TryDequeue(out handler))
|
|
231 // if the promise have been resolved while we was adding the handler to the queue
|
|
232 // we can't guarantee that someone is still processing it
|
|
233 // therefore we need to fetch a handler from the queue and execute it
|
|
234 // note that fetched handler may be not the one that we have added
|
|
235 // even we can fetch no handlers at all :)
|
|
236 InvokeHandler(handler);
|
|
237 }
|
|
238 }
|
|
239 }
|
|
240
|
|
241 protected void InvokeHandler(THandler handler) {
|
|
242 switch (m_state) {
|
|
243 case SUCCEEDED_STATE:
|
|
244 SignalSuccess(handler);
|
|
245 break;
|
|
246 case CANCELLED_STATE:
|
|
247 SignalCancelled(handler, m_error);
|
|
248 break;
|
|
249 case REJECTED_STATE:
|
|
250 SignalError(handler, m_error);
|
|
251 break;
|
|
252 default:
|
|
253 throw new Exception(String.Format("Invalid promise state {0}", m_state));
|
|
254 }
|
|
255 }
|
|
256
|
|
257 #endregion
|
|
258
|
|
259 #region IPromise implementation
|
|
260
|
|
261 public bool IsResolved {
|
|
262 get {
|
|
263 Thread.MemoryBarrier();
|
|
264 return m_state > 1;
|
|
265 }
|
|
266 }
|
|
267
|
|
268 public bool IsCancelled {
|
|
269 get {
|
|
270 Thread.MemoryBarrier();
|
|
271 return m_state == CANCELLED_STATE;
|
|
272 }
|
|
273 }
|
|
274
|
|
275 #endregion
|
|
276
|
|
277 public Exception Error {
|
|
278 get {
|
|
279 return m_error;
|
|
280 }
|
|
281 }
|
|
282
|
145
|
283 public bool CancelOperationIfRequested() {
|
|
284 if (IsCancellationRequested) {
|
|
285 CancelOperation(CancellationReason);
|
|
286 return true;
|
|
287 }
|
|
288 return false;
|
144
|
289 }
|
|
290
|
|
291 public virtual void CancelOperation(Exception reason) {
|
|
292 SetCancelled(reason);
|
|
293 }
|
|
294
|
145
|
295 public void CancellationRequested(Action<Exception> handler) {
|
144
|
296 Safe.ArgumentNotNull(handler, "handler");
|
145
|
297 if (IsCancellationRequested)
|
|
298 handler(CancellationReason);
|
144
|
299
|
|
300 if (m_cancelationHandlers == null)
|
|
301 Interlocked.CompareExchange(ref m_cancelationHandlers, new MTQueue<Action<Exception>>(), null);
|
|
302
|
|
303 m_cancelationHandlers.Enqueue(handler);
|
|
304
|
145
|
305 if (IsCancellationRequested && m_cancelationHandlers.TryDequeue(out handler))
|
144
|
306 // TryDeque implies MemoryBarrier()
|
|
307 handler(m_cancelationReason);
|
|
308 }
|
|
309
|
145
|
310 public bool IsCancellationRequested {
|
144
|
311 get {
|
|
312 do {
|
|
313 if (m_cancelRequest == CANCEL_NOT_REQUESTED)
|
|
314 return false;
|
|
315 if (m_cancelRequest == CANCEL_REQUESTED)
|
|
316 return true;
|
|
317 Thread.MemoryBarrier();
|
|
318 } while(true);
|
|
319 }
|
|
320 }
|
|
321
|
145
|
322 public Exception CancellationReason {
|
144
|
323 get {
|
|
324 do {
|
|
325 Thread.MemoryBarrier();
|
|
326 } while(m_cancelRequest == CANCEL_REQUESTING);
|
|
327
|
|
328 return m_cancelationReason;
|
|
329 }
|
|
330 }
|
|
331
|
|
332 #region ICancellable implementation
|
|
333
|
|
334 public void Cancel() {
|
|
335 Cancel(null);
|
|
336 }
|
|
337
|
|
338 public void Cancel(Exception reason) {
|
145
|
339 if (CANCEL_NOT_REQUESTED == Interlocked.CompareExchange(ref m_cancelRequest, CANCEL_REQUESTING, CANCEL_NOT_REQUESTED)) {
|
144
|
340 m_cancelationReason = reason;
|
|
341 m_cancelRequest = CANCEL_REQUESTED;
|
|
342 if (m_cancelationHandlers != null) {
|
|
343 Action<Exception> handler;
|
|
344 while (m_cancelationHandlers.TryDequeue(out handler))
|
|
345 handler(m_cancelationReason);
|
|
346 }
|
|
347 }
|
|
348 }
|
|
349
|
|
350 #endregion
|
|
351 }
|
|
352 }
|
|
353
|