annotate Implab/PromiseAll.cs @ 260:547a2fc0d93e v3 v3.0.6

minor fixes
author cin
date Fri, 13 Apr 2018 19:14:59 +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.Threading;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
3
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
4 namespace Implab
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
5 {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
6 class PromiseAll : IResolvable {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
7 int m_count;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
8
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
9 readonly Deferred m_deferred;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
10
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
11 public bool Done {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
12 get { return m_deferred.Promise.IsResolved; }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
13 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
14
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
15 public IPromise ResultPromise {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
16 get { return m_deferred.Promise; }
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 AddPromise(IPromise promise) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
20 Interlocked.Increment(ref m_count);
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 PromiseAll(Deferred deferred) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
24 m_deferred = deferred;
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 public void Resolve() {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
28 if (Interlocked.Decrement(ref m_count) == 0)
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
29 m_deferred.Resolve();
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
30 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
31
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
32 public void Complete() {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
33 Resolve();
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 void Reject(Exception error) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
37 m_deferred.Reject(error);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
38 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
39 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
40 }