aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/threading/future/subscription/wait_any_inl.h
blob: e80822bfc9cd1598a84eb8df6589a778f9da2961 (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
#pragma once

#if !defined(INCLUDE_LIBRARY_THREADING_FUTURE_WAIT_ANY_INL_H)
#error "you should never include wait_any_inl.h directly"
#endif

#include "subscription.h"

#include <initializer_list>

namespace NThreading::NWait {

namespace NPrivate {

class TWaitAny final : public NThreading::NPrivate::TWait<TWaitAny> {
private:
    static constexpr bool RevertOnSignaled = true;

    using TBase = NThreading::NPrivate::TWait<TWaitAny>;
    friend TBase;

private:
    TWaitAny(TSubscriptionManagerPtr manager)
        : TBase(std::move(manager))
    {
    }

    template <typename TFutures>
    void BeforeSubscribe(TFutures const& futures) {
        Y_ENSURE(std::size(futures) > 0, "Futures set cannot be empty");
    }

    template <typename T>
    void Set(TFuture<T> const& future) {
        with_lock (TBase::Lock) {
            TBase::Unsubscribe();
            try {
                future.TryRethrow();
                TBase::Promise.TrySetValue();
            } catch (...) {
                TBase::Promise.TrySetException(std::current_exception());
            }
        }
    }
};

}

template <typename TFutures, typename TCallbackExecutor = NThreading::NPrivate::TNoexceptExecutor>
TFuture<void> WaitAny(TFutures const& futures, TSubscriptionManagerPtr manager, TCallbackExecutor&& executor) {
    return NThreading::NPrivate::Wait<NPrivate::TWaitAny>(futures, std::move(manager), std::forward<TCallbackExecutor>(executor));
}

template <typename T, typename TCallbackExecutor = NThreading::NPrivate::TNoexceptExecutor>
TFuture<void> WaitAny(std::initializer_list<TFuture<T> const> futures, TSubscriptionManagerPtr manager, TCallbackExecutor&& executor) {
    return NThreading::NPrivate::Wait<NPrivate::TWaitAny>(futures, std::move(manager), std::forward<TCallbackExecutor>(executor));
}

template <typename T, typename TCallbackExecutor = NThreading::NPrivate::TNoexceptExecutor>
TFuture<void> WaitAny(TFuture<T> const& future1, TFuture<T> const& future2, TSubscriptionManagerPtr manager, TCallbackExecutor&& executor) {
    return NThreading::NPrivate::Wait<NPrivate::TWaitAny>(future1, future2, std::move(manager), std::forward<TCallbackExecutor>(executor));
}

}