aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/threading/future/wait/wait.cpp
blob: cc33f3189ca568d3dda1e2c1e0b6471902f7dfd4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "wait.h" 

#include "wait_group.h"
#include "wait_policy.h"

namespace NThreading {
    namespace { 
        template <class WaitPolicy>
        TFuture<void> WaitGeneric(const TFuture<void>& f1) { 
            return f1;
        }

        template <class WaitPolicy>
        TFuture<void> WaitGeneric(const TFuture<void>& f1, const TFuture<void>& f2) { 
            TWaitGroup<WaitPolicy> wg;

            wg.Add(f1).Add(f2);

            return std::move(wg).Finish();
        }

        template <class WaitPolicy> 
        TFuture<void> WaitGeneric(TArrayRef<const TFuture<void>> futures) { 
            if (futures.empty()) {
                return MakeFuture();
            }
            if (futures.size() == 1) {
                return futures.front(); 
            }

            TWaitGroup<WaitPolicy> wg;
            for (const auto& fut : futures) {
                wg.Add(fut);
            }

            return std::move(wg).Finish();
        }
    }

    ////////////////////////////////////////////////////////////////////////////////

    TFuture<void> WaitAll(const TFuture<void>& f1) { 
        return WaitGeneric<TWaitPolicy::TAll>(f1); 
    }

    TFuture<void> WaitAll(const TFuture<void>& f1, const TFuture<void>& f2) { 
        return WaitGeneric<TWaitPolicy::TAll>(f1, f2); 
    }

    TFuture<void> WaitAll(TArrayRef<const TFuture<void>> futures) { 
        return WaitGeneric<TWaitPolicy::TAll>(futures); 
    }


    ////////////////////////////////////////////////////////////////////////////////

    TFuture<void> WaitExceptionOrAll(const TFuture<void>& f1) { 
        return WaitGeneric<TWaitPolicy::TExceptionOrAll>(f1); 
    }

    TFuture<void> WaitExceptionOrAll(const TFuture<void>& f1, const TFuture<void>& f2) { 
        return WaitGeneric<TWaitPolicy::TExceptionOrAll>(f1, f2); 
    }

    TFuture<void> WaitExceptionOrAll(TArrayRef<const TFuture<void>> futures) { 
        return WaitGeneric<TWaitPolicy::TExceptionOrAll>(futures); 
    }

    ////////////////////////////////////////////////////////////////////////////////

    TFuture<void> WaitAny(const TFuture<void>& f1) { 
        return WaitGeneric<TWaitPolicy::TAny>(f1); 
    }

    TFuture<void> WaitAny(const TFuture<void>& f1, const TFuture<void>& f2) { 
        return WaitGeneric<TWaitPolicy::TAny>(f1, f2); 
    }

    TFuture<void> WaitAny(TArrayRef<const TFuture<void>> futures) { 
        return WaitGeneric<TWaitPolicy::TAny>(futures); 
    }
}