annotate Implab/AbstractPromise.cs @ 129:471f596b2603 v2

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