annotate Implab/AbstractPromise.cs @ 136:e9e7940c7d98 v2

shared locks + tests
author cin
date Mon, 16 Feb 2015 01:14:09 +0300
parents 671f60cd0250
children f75cfa58e3d4
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
130
671f60cd0250 fixed Resove method bug when calling it on already cancelled promise
cin
parents: 125
diff changeset
42 protected bool BeginSetResult() {
119
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");
130
671f60cd0250 fixed Resove method bug when calling it on already cancelled promise
cin
parents: 125
diff changeset
47 return false;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
48 }
130
671f60cd0250 fixed Resove method bug when calling it on already cancelled promise
cin
parents: 125
diff changeset
49 return true;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
50 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
51
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
52 protected void EndSetResult() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
53 CompleteTransit(SUCCEEDED_STATE);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
54 OnSuccess();
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
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 /// Выполняет обещание, сообщая об ошибке
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
61 /// </summary>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
62 /// <remarks>
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 /// могу вернуть ошибку, при этом только первая будет использована в качестве результата, остальные
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
65 /// будут проигнорированы.
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
66 /// </remarks>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
67 /// <param name="error">Исключение возникшее при выполнении операции</param>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
68 /// <exception cref="InvalidOperationException">Данное обещание уже выполнено</exception>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
69 protected void SetError(Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
70 if (BeginTransit()) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
71 m_error = error is PromiseTransientException ? error.InnerException : error;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
72 CompleteTransit(REJECTED_STATE);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
73 OnError();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
74 } else {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
75 WaitTransition();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
76 if (m_state == SUCCEEDED_STATE)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
77 throw new InvalidOperationException("The promise is already resolved");
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 }
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 /// Отменяет операцию, если это возможно.
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
83 /// </summary>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
84 /// <remarks>Для определения была ли операция отменена следует использовать свойство <see cref="IsCancelled"/>.</remarks>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
85 protected void SetCancelled() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
86 if (BeginTransit()) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
87 CompleteTransit(CANCELLED_STATE);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
88 OnCancelled();
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 }
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 SignalSuccess(THandler handler);
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 SignalError(THandler handler, Exception error);
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 protected abstract void SignalCancelled(THandler handler);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
97
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
98 void OnSuccess() {
125
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
99 var hp = m_handlerPointer;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
100 var slot = hp +1 ;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
101 while (slot < m_handlersCommited) {
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
102 if (Interlocked.CompareExchange(ref m_handlerPointer, slot, hp) == hp) {
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
103 SignalSuccess(m_handlers[slot]);
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
104 }
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
105 hp = m_handlerPointer;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
106 slot = hp +1 ;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
107 }
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
108
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
109
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
110 if (m_extraHandlers != null) {
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
111 THandler handler;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
112 while (m_extraHandlers.TryDequeue(out handler))
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
113 SignalSuccess(handler);
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
114 }
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
115 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
116
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
117 void OnError() {
125
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
118 var hp = m_handlerPointer;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
119 var slot = hp +1 ;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
120 while (slot < m_handlersCommited) {
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
121 if (Interlocked.CompareExchange(ref m_handlerPointer, slot, hp) == hp) {
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
122 SignalError(m_handlers[slot],m_error);
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
123 }
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
124 hp = m_handlerPointer;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
125 slot = hp +1 ;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
126 }
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
127
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
128 if (m_extraHandlers != null) {
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
129 THandler handler;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
130 while (m_extraHandlers.TryDequeue(out handler))
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
131 SignalError(handler, m_error);
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
132 }
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
133 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
134
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
135 void OnCancelled() {
125
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
136 var hp = m_handlerPointer;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
137 var slot = hp +1 ;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
138 while (slot < m_handlersCommited) {
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
139 if (Interlocked.CompareExchange(ref m_handlerPointer, slot, hp) == hp) {
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
140 SignalCancelled(m_handlers[slot]);
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
141 }
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
142 hp = m_handlerPointer;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
143 slot = hp +1 ;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
144 }
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
145
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
146 if (m_extraHandlers != null) {
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
147 THandler handler;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
148 while (m_extraHandlers.TryDequeue(out handler))
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
149 SignalCancelled(handler);
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
150 }
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
151 }
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 #endregion
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 protected abstract void Listen(PromiseEventType events, Action handler);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
156
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
157 #region synchronization traits
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
158 protected void WaitResult(int timeout) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
159 if (!IsResolved) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
160 var lk = new object();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
161
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
162 Listen(PromiseEventType.All, () => {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
163 lock(lk) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
164 Monitor.Pulse(lk);
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 });
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
167
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
168 lock (lk) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
169 while(!IsResolved) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
170 if(!Monitor.Wait(lk,timeout))
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
171 throw new TimeoutException();
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
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
175 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
176 switch (m_state) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
177 case SUCCEEDED_STATE:
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
178 return;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
179 case CANCELLED_STATE:
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
180 throw new OperationCanceledException();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
181 case REJECTED_STATE:
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
182 throw new TargetInvocationException(m_error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
183 default:
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
184 throw new ApplicationException(String.Format("Invalid promise state {0}", m_state));
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
185 }
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 #endregion
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 #region handlers managment
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
190
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
191 protected void AddHandler(THandler handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
192
125
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
193 if (m_state > 1) {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
194 // the promise is in the resolved state, just invoke the handler
125
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
195 InvokeHandler(handler);
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
196 } else {
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
197 var slot = Interlocked.Increment(ref m_handlersCount) - 1;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
198
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
199 if (slot < RESERVED_HANDLERS_COUNT) {
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
200 m_handlers[slot] = handler;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
201
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
202 while (slot != Interlocked.CompareExchange(ref m_handlersCommited, slot + 1, slot)) {
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
203 }
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
204
125
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
205 if (m_state > 1) {
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
206 do {
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
207 var hp = m_handlerPointer;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
208 slot = hp + 1;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
209 if (slot < m_handlersCommited) {
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
210 if (Interlocked.CompareExchange(ref m_handlerPointer, slot, hp) != hp)
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
211 continue;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
212 InvokeHandler(m_handlers[slot]);
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
213 }
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
214 break;
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
215 } while(true);
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
216 }
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
217 } else {
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
218 if (slot == RESERVED_HANDLERS_COUNT) {
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
219 m_extraHandlers = new MTQueue<THandler>();
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
220 } else {
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
221 while (m_extraHandlers == null)
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
222 Thread.MemoryBarrier();
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
223 }
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
224
125
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
225 m_extraHandlers.Enqueue(handler);
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
226
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
227 if (m_state > 1 && m_extraHandlers.TryDequeue(out handler))
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
228 // 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
229 // we can't guarantee that someone is still processing it
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
230 // 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
231 // 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
232 // even we can fetch no handlers at all :)
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
233 InvokeHandler(handler);
125
f803565868a4 improved performance of promises
cin
parents: 119
diff changeset
234 }
119
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 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
237
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
238 protected void InvokeHandler(THandler handler) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
239 switch (m_state) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
240 case SUCCEEDED_STATE:
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
241 SignalSuccess(handler);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
242 break;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
243 case CANCELLED_STATE:
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
244 SignalCancelled(handler);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
245 break;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
246 case REJECTED_STATE:
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
247 SignalError(handler, m_error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
248 break;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
249 default:
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
250 throw new Exception(String.Format("Invalid promise state {0}", m_state));
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 }
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 #endregion
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 #region IPromise implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
257
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
258 public void Join(int timeout) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
259 WaitResult(timeout);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
260 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
261
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
262 public void Join() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
263 WaitResult(-1);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
264 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
265
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
266 public bool IsResolved {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
267 get {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
268 Thread.MemoryBarrier();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
269 return m_state > 1;
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 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
272
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
273 public bool IsCancelled {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
274 get {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
275 Thread.MemoryBarrier();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
276 return m_state == CANCELLED_STATE;
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 }
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 #endregion
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 #region ICancellable implementation
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
283
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
284 public void Cancel() {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
285 SetCancelled();
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
286 }
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 #endregion
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
289 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
290 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
291