annotate Implab/PromiseAwaiter.cs @ 276:b4e0f81c7425 v3

container configuration docs
author cin
date Sat, 28 Apr 2018 00:11:38 +0300
parents 5cb4826c2c2a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
1 using System;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
2 using System.Runtime.CompilerServices;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
3 using System.Threading;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
4 using Implab.Parallels;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
5
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
6 namespace Implab
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
7 {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
8 public struct PromiseAwaiter : INotifyCompletion {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
9 class PromiseEvent : IResolvable {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
10 IDispatcher m_dispatcher;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
11
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
12 Action m_handler;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
13
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
14 public PromiseEvent(Action handler, IDispatcher dispatcher) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
15 m_handler = handler;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
16 m_dispatcher = dispatcher;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
17 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
18
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
19 public void Resolve() {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
20 m_dispatcher.Enqueue(m_handler);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
21 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
22
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
23 public void Reject(Exception error) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
24 m_dispatcher.Enqueue(m_handler);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
25 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
26 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
27
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
28 readonly IPromise m_promise;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
29 readonly IDispatcher m_dispatcher;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
30
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
31 public PromiseAwaiter(IPromise promise, IDispatcher dispatcher) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
32 m_promise = promise;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
33 m_dispatcher = dispatcher;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
34 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
35
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
36 public PromiseAwaiter(IPromise promise) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
37 m_promise = promise;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
38 m_dispatcher = GetDispatcher();
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
39 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
40
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
41 public void OnCompleted (Action continuation) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
42 if (m_promise != null)
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
43 m_promise.Then(new PromiseEvent(continuation, GetDispatcher()));
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
44 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
45
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
46 public void GetResult() {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
47 m_promise.Join();
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
48 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
49
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
50 static IDispatcher GetDispatcher() {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
51 if(SynchronizationContext.Current == null)
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
52 return ThreadPoolDispatcher.Instance;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
53 return new SyncContextDispatcher(SynchronizationContext.Current);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
54 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
55
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
56 public bool IsCompleted {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
57 get {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
58 return m_promise.IsResolved;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
59 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
60 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
61 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
62 }