annotate Implab/AbstractEvent.cs @ 185:822aab37b107 ref20160224

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