##// END OF EJS Templates
Promises: SignalXXX methods merged into SignalHandler method....
cin -
r156:97fbbf816844 v2
parent child
Show More
@@ -0,0 +1,8
1 using System;
2
3 namespace Implab.Components {
4 public interface IFactory<out T> {
5 T Create();
6 }
7 }
8
@@ -0,0 +1,141
1 using System;
2 using Implab.Parsing;
3
4 namespace Implab.Components {
5 public class RunnableComponent : Disposable, IRunnable, IInitializable {
6 class Automaton : DFAutomaton<ExecutionState> {
7 static readonly EDFADefinition<ExecutionState> _dfa;
8
9 static Automaton() {
10
11 var token = Token
12 .New(ExecutionState.Uninitialized).Optional() // we can skip uninitialized state
13 .Cat(
14 Token.New(ExecutionState.Ready) // uninitialized -> initial
15 .Cat(
16 Token.New(ExecutionState.Starting) // initial -> starting
17 .Cat(
18 Token.New(ExecutionState.Running) // running -> {stopping -> stopped | failed }
19 .Cat(
20 Token.New(ExecutionState.Stopping) // running -> stopping
21 .Cat(
22 Token.New(ExecutionState.Stopped) // stopping -> stopped
23 .Or(Token.New(ExecutionState.Failed)) // stopping -> failed
24 )
25 .Or(Token.New(ExecutionState.Failed)) // running -> failed
26 )
27 .Or(Token.New(ExecutionState.Failed)) // starting -> failed
28 ).EClosure()
29 )
30 .Or(Token.New(ExecutionState.Failed)) // uninitialized->failed
31 .Cat(Token.New(ExecutionState.Disposed).Tag(0)) // ... -> disposed
32 );
33
34 var builder = new DFABuilder();
35 token.Accept(builder);
36
37 var _dfa = new EDFADefinition<ExecutionState>(EnumAlphabet<ExecutionState>.FullAlphabet);
38 builder.BuildDFA(_dfa); // don't optimize dfa to avoid remapping of the alphabet
39
40 }
41
42 public Automaton() : base(_dfa.States, INITIAL_STATE, ExecutionState.Reserved) {
43 }
44
45 public void MoveTo(ExecutionState state) {
46
47 if (!CanMove((int)state))
48 throw new InvalidOperationException(String.Format("Illegal state transition from {0} to {1}", Current, state));
49 Move((int)state);
50 m_context.info = state;
51 }
52
53 public ExecutionState Current {
54 get {
55 return (ExecutionState)m_context.info;
56 }
57 }
58 }
59
60 readonly Automaton m_automaton = new Automaton();
61 IPromise m_pending;
62 Exception m_lastError;
63
64 protected RunnableComponent(bool initialized) {
65 if (initialized)
66 m_automaton.MoveTo(ExecutionState.Ready);
67 else
68 m_automaton.MoveTo(ExecutionState.Uninitialized);
69 }
70
71 #region IInitializable implementation
72
73 public void Init() {
74
75 }
76
77 #endregion
78
79 #region IRunnable implementation
80
81 public IPromise Start() {
82 return Safe.InvokePromise(() => {
83 Promise promise;
84 lock (m_automaton) {
85 if (m_automaton.Current == ExecutionState.Starting)
86 return m_pending;
87 m_automaton.MoveTo(ExecutionState.Starting);
88 m_pending = promise = new Promise();
89 }
90
91 var start = Safe.InvokePromise(OnStart);
92 promise.On(null, null, start.Cancel);
93 start.On(promise.Resolve, promise.Reject, promise.CancelOperation);
94
95 return promise.Then(() => {
96 lock(m_automaton) {
97 m_automaton.MoveTo(ExecutionState.Running);
98 m_pending = null;
99 }
100
101 Run();
102 }, err => {
103 if (BeginTransition(RUNNING_REQUIRE)) {
104 m_lastError = err;
105 CompleteTransition(FAILED_STATE);
106 throw new PromiseTransientException(err);
107 }
108 throw new OperationCanceledException();
109 }, reason => {
110 throw new OperationCanceledException("The operation was cancelled", reason);
111 });
112 });
113 }
114
115 protected virtual IPromise OnStart() {
116 return Promise.SUCCESS;
117 }
118
119 protected virtual void Run() {
120 }
121
122 public IPromise Stop() {
123 throw new NotImplementedException();
124 }
125
126 public ExecutionState State {
127 get {
128 throw new NotImplementedException();
129 }
130 }
131
132 public Exception LastError {
133 get {
134 throw new NotImplementedException();
135 }
136 }
137
138 #endregion
139 }
140 }
141
@@ -12,27 +12,12 namespace Implab.Fx {
12 12 m_target = target;
13 13 }
14 14
15 protected override void SignalSuccess(Promise<T>.HandlerDescriptor handler) {
15 protected override void SignalHandler(HandlerDescriptor handler, int signal) {
16 16 if (m_target.InvokeRequired)
17 m_target.BeginInvoke(new Action<Promise<T>.HandlerDescriptor>(base.SignalSuccess), handler);
18 else
19 base.SignalSuccess(handler);
20 }
21
22 protected override void SignalCancelled(Promise<T>.HandlerDescriptor handler, Exception reason) {
23 if (m_target.InvokeRequired)
24 m_target.BeginInvoke(new Action<Promise<T>.HandlerDescriptor,Exception>(base.SignalCancelled), handler, reason);
17 m_target.BeginInvoke(new Action<Promise<T>.HandlerDescriptor, int>(base.SignalHandler), handler, signal);
25 18 else
26 base.SignalCancelled(handler, reason);
19 base.SignalHandler(handler, signal);
27 20 }
28
29 protected override void SignalError(Promise<T>.HandlerDescriptor handler, Exception error) {
30 if (m_target.InvokeRequired)
31 m_target.BeginInvoke(new Action<Promise<T>.HandlerDescriptor,Exception>(base.SignalError), handler, error);
32 else
33 base.SignalError(handler, error);
34 }
35
36 21 }
37 22 }
38 23
@@ -8,9 +8,9 namespace Implab {
8 8
9 9 const int UNRESOLVED_SATE = 0;
10 10 const int TRANSITIONAL_STATE = 1;
11 const int SUCCEEDED_STATE = 2;
12 const int REJECTED_STATE = 3;
13 const int CANCELLED_STATE = 4;
11 protected const int SUCCEEDED_STATE = 2;
12 protected const int REJECTED_STATE = 3;
13 protected const int CANCELLED_STATE = 4;
14 14
15 15 const int CANCEL_NOT_REQUESTED = 0;
16 16 const int CANCEL_REQUESTING = 1;
@@ -22,7 +22,8 namespace Implab {
22 22 Exception m_error;
23 23 int m_handlersCount;
24 24
25 readonly THandler[] m_handlers = new THandler[RESERVED_HANDLERS_COUNT];
25 //readonly THandler[] m_handlers = new THandler[RESERVED_HANDLERS_COUNT];
26 THandler[] m_handlers;
26 27 MTQueue<THandler> m_extraHandlers;
27 28 int m_handlerPointer = -1;
28 29 int m_handlersCommited;
@@ -60,7 +61,7 namespace Implab {
60 61
61 62 protected void EndSetResult() {
62 63 CompleteTransit(SUCCEEDED_STATE);
63 OnSuccess();
64 Signal();
64 65 }
65 66
66 67
@@ -78,14 +79,13 namespace Implab {
78 79 protected void SetError(Exception error) {
79 80 if (BeginTransit()) {
80 81 if (error is OperationCanceledException) {
82 m_error = error.InnerException;
81 83 CompleteTransit(CANCELLED_STATE);
82 m_error = error.InnerException;
83 OnCancelled();
84 84 } else {
85 85 m_error = error is PromiseTransientException ? error.InnerException : error;
86 86 CompleteTransit(REJECTED_STATE);
87 OnError();
88 87 }
88 Signal();
89 89 } else {
90 90 WaitTransition();
91 91 if (m_state == SUCCEEDED_STATE)
@@ -101,22 +101,18 namespace Implab {
101 101 if (BeginTransit()) {
102 102 m_error = reason;
103 103 CompleteTransit(CANCELLED_STATE);
104 OnCancelled();
104 Signal();
105 105 }
106 106 }
107 107
108 protected abstract void SignalSuccess(THandler handler);
109
110 protected abstract void SignalError(THandler handler, Exception error);
108 protected abstract void SignalHandler(THandler handler, int signal);
111 109
112 protected abstract void SignalCancelled(THandler handler, Exception reason);
113
114 void OnSuccess() {
110 void Signal() {
115 111 var hp = m_handlerPointer;
116 112 var slot = hp +1 ;
117 113 while (slot < m_handlersCommited) {
118 114 if (Interlocked.CompareExchange(ref m_handlerPointer, slot, hp) == hp) {
119 SignalSuccess(m_handlers[slot]);
115 SignalHandler(m_handlers[slot], m_state);
120 116 }
121 117 hp = m_handlerPointer;
122 118 slot = hp +1 ;
@@ -126,43 +122,7 namespace Implab {
126 122 if (m_extraHandlers != null) {
127 123 THandler handler;
128 124 while (m_extraHandlers.TryDequeue(out handler))
129 SignalSuccess(handler);
130 }
131 }
132
133 void OnError() {
134 var hp = m_handlerPointer;
135 var slot = hp +1 ;
136 while (slot < m_handlersCommited) {
137 if (Interlocked.CompareExchange(ref m_handlerPointer, slot, hp) == hp) {
138 SignalError(m_handlers[slot],m_error);
139 }
140 hp = m_handlerPointer;
141 slot = hp +1 ;
142 }
143
144 if (m_extraHandlers != null) {
145 THandler handler;
146 while (m_extraHandlers.TryDequeue(out handler))
147 SignalError(handler, m_error);
148 }
149 }
150
151 void OnCancelled() {
152 var hp = m_handlerPointer;
153 var slot = hp +1 ;
154 while (slot < m_handlersCommited) {
155 if (Interlocked.CompareExchange(ref m_handlerPointer, slot, hp) == hp) {
156 SignalCancelled(m_handlers[slot], m_error);
157 }
158 hp = m_handlerPointer;
159 slot = hp +1 ;
160 }
161
162 if (m_extraHandlers != null) {
163 THandler handler;
164 while (m_extraHandlers.TryDequeue(out handler))
165 SignalCancelled(handler, m_error);
125 SignalHandler(handler, m_state);
166 126 }
167 127 }
168 128
@@ -194,12 +154,15 namespace Implab {
194 154
195 155 if (m_state > 1) {
196 156 // the promise is in the resolved state, just invoke the handler
197 InvokeHandler(handler);
157 SignalHandler(handler, m_state);
198 158 } else {
199 159 var slot = Interlocked.Increment(ref m_handlersCount) - 1;
200 160
201 161 if (slot < RESERVED_HANDLERS_COUNT) {
202 162
163 if (slot == 0)
164 Interlocked.CompareExchange(ref m_handlers, new THandler[RESERVED_HANDLERS_COUNT], null);
165
203 166 m_handlers[slot] = handler;
204 167
205 168 while (slot != Interlocked.CompareExchange(ref m_handlersCommited, slot + 1, slot)) {
@@ -212,7 +175,7 namespace Implab {
212 175 if (slot < m_handlersCommited) {
213 176 if (Interlocked.CompareExchange(ref m_handlerPointer, slot, hp) != hp)
214 177 continue;
215 InvokeHandler(m_handlers[slot]);
178 SignalHandler(m_handlers[slot], m_state);
216 179 }
217 180 break;
218 181 } while(true);
@@ -233,24 +196,8 namespace Implab {
233 196 // therefore we need to fetch a handler from the queue and execute it
234 197 // note that fetched handler may be not the one that we have added
235 198 // even we can fetch no handlers at all :)
236 InvokeHandler(handler);
237 }
238 }
199 SignalHandler(handler, m_state);
239 200 }
240
241 protected void InvokeHandler(THandler handler) {
242 switch (m_state) {
243 case SUCCEEDED_STATE:
244 SignalSuccess(handler);
245 break;
246 case CANCELLED_STATE:
247 SignalCancelled(handler, m_error);
248 break;
249 case REJECTED_STATE:
250 SignalError(handler, m_error);
251 break;
252 default:
253 throw new Exception(String.Format("Invalid promise state {0}", m_state));
254 201 }
255 202 }
256 203
@@ -71,16 +71,20 namespace Implab {
71 71
72 72 #region implemented abstract members of AbstractPromise
73 73
74 protected override void SignalSuccess(HandlerDescriptor handler) {
74 protected override void SignalHandler(HandlerDescriptor handler, int signal) {
75 switch (signal) {
76 case SUCCEEDED_STATE:
75 77 handler.SignalSuccess();
78 break;
79 case REJECTED_STATE:
80 handler.SignalError(Error);
81 break;
82 case CANCELLED_STATE:
83 handler.SignalCancel(CancellationReason);
84 break;
85 default:
86 throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", signal));
76 87 }
77
78 protected override void SignalError(HandlerDescriptor handler, Exception error) {
79 handler.SignalError(error);
80 }
81
82 protected override void SignalCancelled(HandlerDescriptor handler, Exception reason) {
83 handler.SignalCancel(reason);
84 88 }
85 89
86 90 protected override Signal GetResolveSignal() {
@@ -175,16 +175,20 namespace Implab {
175 175 return signal;
176 176 }
177 177
178 protected override void SignalSuccess(HandlerDescriptor handler) {
178 protected override void SignalHandler(HandlerDescriptor handler, int signal) {
179 switch (signal) {
180 case SUCCEEDED_STATE:
179 181 handler.SignalSuccess(m_result);
182 break;
183 case REJECTED_STATE:
184 handler.SignalError(Error);
185 break;
186 case CANCELLED_STATE:
187 handler.SignalCancel(CancellationReason);
188 break;
189 default:
190 throw new InvalidOperationException(String.Format("Invalid promise signal: {0}", signal));
180 191 }
181
182 protected override void SignalError(HandlerDescriptor handler, Exception error) {
183 handler.SignalError(error);
184 }
185
186 protected override void SignalCancelled(HandlerDescriptor handler, Exception reason) {
187 handler.SignalCancel(reason);
188 192 }
189 193
190 194 #endregion
@@ -1,8 +1,9
1 1 namespace Implab.Components {
2 2
3 3 public enum ExecutionState {
4 Reserved = 0,
4 5 Uninitialized,
5 Initial,
6 Ready,
6 7 Starting,
7 8 Running,
8 9 Stopping,
@@ -182,6 +182,8
182 182 <Compile Include="Components\App.cs" />
183 183 <Compile Include="Components\IRunnable.cs" />
184 184 <Compile Include="Components\ExecutionState.cs" />
185 <Compile Include="Components\RunnableComponent.cs" />
186 <Compile Include="Components\IFactory.cs" />
185 187 </ItemGroup>
186 188 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
187 189 <ItemGroup />
@@ -1,13 +1,9
1 1 using Implab.Parsing;
2 using System;
3 using System.Collections.Generic;
4 2 using System.Linq;
5 using System.Text;
6 using System.Threading.Tasks;
7 3
8 4 namespace Implab.JSON {
9 internal class JSONGrammar : Grammar<JSONGrammar> {
10 public enum TokenType : int {
5 class JSONGrammar : Grammar<JSONGrammar> {
6 public enum TokenType {
11 7 None,
12 8 BeginObject,
13 9 EndObject,
@@ -1,12 +1,7
1 using Implab;
2 using Implab.Parsing;
1 using Implab.Parsing;
3 2 using System;
4 using System.Collections.Generic;
5 3 using System.Diagnostics;
6 4 using System.IO;
7 using System.Linq;
8 using System.Text;
9 using System.Threading.Tasks;
10 5
11 6 namespace Implab.JSON {
12 7 /// <summary>
@@ -192,11 +187,10 namespace Implab.JSON {
192 187 if (m_memberContext == MemberContext.MemberName) {
193 188 m_context.info.memberName = (string)tokenValue;
194 189 break;
195 } else {
190 }
196 191 m_elementType = JSONElementType.Value;
197 192 m_elementValue = tokenValue;
198 193 return true;
199 }
200 194 case JsonTokenType.Number:
201 195 m_elementType = JSONElementType.Value;
202 196 m_elementValue = tokenValue;
@@ -11,7 +11,7 namespace Implab.Parsing {
11 11 public const int UNCLASSIFIED = 0;
12 12
13 13 int m_nextId = 1;
14 int[] m_map;
14 readonly int[] m_map;
15 15
16 16 public int Count {
17 17 get { return m_nextId; }
@@ -1,9 +1,4
1 using Implab;
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6 using System.Threading.Tasks;
1 using System;
7 2
8 3 namespace Implab.Parsing {
9 4 public class AltToken: BinaryToken {
@@ -7,7 +7,7 using System.Threading.Tasks;
7 7
8 8 namespace Implab.Parsing {
9 9 public class CDFADefinition : DFADefinitionBase {
10 Alphabet m_alphabet;
10 readonly Alphabet m_alphabet;
11 11
12 12 public Alphabet Alphabet {
13 13 get { return m_alphabet; }
@@ -1,9 +1,4
1 using Implab;
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6 using System.Threading.Tasks;
1 using System;
7 2
8 3 namespace Implab.Parsing {
9 4 public class CatToken : BinaryToken {
@@ -166,7 +166,7 namespace Implab.Parsing {
166 166
167 167 int[] GetStateTags(HashSet<int> state) {
168 168 Debug.Assert(state != null);
169 return state.Where(pos => m_ends.ContainsKey(pos)).Select(pos => m_ends[pos]).ToArray();
169 return state.Where(m_ends.ContainsKey).Select(pos => m_ends[pos]).ToArray();
170 170 }
171 171
172 172 int DefineState(IDFADefinition automa, HashSet<int> state) {
@@ -15,7 +15,7 namespace Implab.Parsing {
15 15
16 16 DFAStateDescriptior[] m_statesArray;
17 17
18 public DFADefinitionBase() {
18 protected DFADefinitionBase() {
19 19 m_states = new List<DFAStateDescriptior>();
20 20
21 21 m_states.Add(new DFAStateDescriptior());
@@ -47,7 +47,7 namespace Implab.Parsing {
47 47
48 48 public int AddState(int[] tag) {
49 49 var index = m_states.Count;
50 bool final = tag == null || tag.Length == 0 ? false : true;
50 bool final = tag != null && tag.Length != 0;
51 51 m_states.Add(new DFAStateDescriptior {
52 52 final = final,
53 53 transitions = new int[AlphabetSize],
@@ -139,7 +139,7 namespace Implab.Parsing {
139 139
140 140 // строим карты соотвествия оптимальных состояний с оригинальными
141 141
142 var initialState = optimalStates.Where(x => x.Contains(INITIAL_STATE)).Single();
142 var initialState = optimalStates.Single(x => x.Contains(INITIAL_STATE));
143 143
144 144 // карта получения оптимального состояния по соотвествующему ему простому состоянию
145 145 int[] reveseOptimalMap = new int[m_states.Count];
@@ -184,9 +184,7 namespace Implab.Parsing {
184 184 foreach (var term in A) {
185 185 // ищем все переходы класса по символу term
186 186 var s2 = reveseOptimalMap[
187 optimalMap[s].Select(x => m_states[x].transitions[term]) // все элементарные состояния, куда переходит класс s
188 .Where(x => x != 0) // только допустимые
189 .FirstOrDefault() // первое допустимое элементарное состояние, если есть
187 optimalMap[s].Select(x => m_states[x].transitions[term]).FirstOrDefault(x => x != 0) // первое допустимое элементарное состояние, если есть
190 188 ];
191 189
192 190 HashSet<int> A2;
@@ -52,5 +52,10 namespace Implab.Parsing {
52 52 Debug.Assert(input > 0 && input < m_context.states[m_context.current].transitions.Length);
53 53 m_context.current = m_context.states[m_context.current].transitions[input];
54 54 }
55
56 protected bool CanMove(int input) {
57 Debug.Assert(input > 0 && input < m_context.states[m_context.current].transitions.Length);
58 return m_context.states[m_context.current].transitions[input] != UNREACHEBLE_STATE;
55 59 }
56 60 }
61 }
@@ -1,20 +1,15
1 1 using Implab;
2 2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6 using System.Threading.Tasks;
7 3
8 4 namespace Implab.Parsing {
9 5 public class EDFADefinition<T> : DFADefinitionBase where T : struct, IConvertible {
10 EnumAlphabet<T> m_alphabet;
6 readonly EnumAlphabet<T> m_alphabet;
11 7
12 8 public EnumAlphabet<T> Alphabet {
13 9 get { return m_alphabet; }
14 10 }
15 11
16 public EDFADefinition(EnumAlphabet<T> alphabet)
17 : base() {
12 public EDFADefinition(EnumAlphabet<T> alphabet) {
18 13 Safe.ArgumentNotNull(alphabet, "alphabet");
19 14 m_alphabet = alphabet;
20 15 }
@@ -1,11 +1,9
1 using Implab;
2 using System;
1 using System;
3 2 using System.Collections.Generic;
4 3 using System.Diagnostics;
5 4 using System.Globalization;
6 5 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
6 using System.Diagnostics.CodeAnalysis;
9 7
10 8 namespace Implab.Parsing {
11 9 /// <summary>
@@ -13,10 +11,11 namespace Implab.Parsing {
13 11 /// </summary>
14 12 /// <typeparam name="T">Тип перечислений</typeparam>
15 13 public class EnumAlphabet<T> : AlphabetBase<T> where T : struct, IConvertible {
14 [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
16 15 static readonly T[] _symbols;
17 16 static readonly EnumAlphabet<T> _fullAlphabet;
18 17
19 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")]
18 [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")]
20 19 static EnumAlphabet() {
21 20 if (!typeof(T).IsEnum)
22 21 throw new InvalidOperationException("Invalid generic parameter, enumeration is required");
@@ -10,16 +10,8 namespace Implab {
10 10 m_context = context;
11 11 }
12 12
13 protected override void SignalSuccess(Promise<T>.HandlerDescriptor handler) {
14 m_context.Post(x => base.SignalSuccess(handler), null);
15 }
16
17 protected override void SignalError(Promise<T>.HandlerDescriptor handler, Exception error) {
18 m_context.Post(x => base.SignalError(handler, error), null);
19 }
20
21 protected override void SignalCancelled(Promise<T>.HandlerDescriptor handler, Exception reason) {
22 m_context.Post(x => base.SignalCancelled(handler, reason), null);
13 protected override void SignalHandler(HandlerDescriptor handler, int signal) {
14 m_context.Post(x => base.SignalHandler(handler, signal), null);
23 15 }
24 16 }
25 17 }
@@ -1,12 +1,5
1 1 using System;
2 using Implab.Diagnostics;
3 using Implab.Parallels;
4 2 using Implab;
5 using System.Collections.Generic;
6 using System.Collections.Concurrent;
7 using System.Threading;
8 using Implab.JSON;
9 using System.IO;
10 3 using System.Threading.Tasks;
11 4
12 5 namespace MonoPlay {
@@ -27,7 +20,10 namespace MonoPlay {
27 20 }
28 21
29 22 static IPromise<int> DoItem(int x) {
30 return Promise<int>.FromResult(x + 1);
23 //return Promise<int>.FromResult(x + 1);
24 var p = new Promise<int>();
25 p.Resolve(x+1);
26 return p;
31 27 }
32 28
33 29 static async Task<int> DoWork() {
General Comments 0
You need to be logged in to leave comments. Login now