aboutsummaryrefslogtreecommitdiffstats
path: root/yt/yt/core/actions/signal-inl.h
blob: cb87697f0d26b299059b018be70e553d6a52a393 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#ifndef SIGNAL_INL_H_
#error "Direct inclusion of this file is not allowed, include signal.h"
// For the sake of sane code completion.
#include "signal.h"
#endif
#undef SIGNAL_INL_H_

#include <library/cpp/yt/memory/ref.h>

namespace NYT {

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

template <class TResult, class... TArgs>
void TCallbackList<TResult(TArgs...)>::Subscribe(const TCallback& callback)
{
    auto guard = WriterGuard(SpinLock_);
    Callbacks_.push_back(callback);
}

template <class TResult, class... TArgs>
void TCallbackList<TResult(TArgs...)>::Unsubscribe(const TCallback& callback)
{
    auto guard = WriterGuard(SpinLock_);
    for (auto it = Callbacks_.begin(); it != Callbacks_.end(); ++it) {
        if (*it == callback) {
            Callbacks_.erase(it);
            break;
        }
    }
}

template <class TResult, class... TArgs>
std::vector<TCallback<TResult(TArgs...)>> TCallbackList<TResult(TArgs...)>::ToVector() const
{
    auto guard = ReaderGuard(SpinLock_);
    return std::vector<TCallback>(Callbacks_.begin(), Callbacks_.end());
}

template <class TResult, class... TArgs>
int TCallbackList<TResult(TArgs...)>::Size() const
{
    auto guard = ReaderGuard(SpinLock_);
    return Callbacks_.size();
}

template <class TResult, class... TArgs>
bool TCallbackList<TResult(TArgs...)>::Empty() const
{
    auto guard = ReaderGuard(SpinLock_);
    return Callbacks_.empty();
}

template <class TResult, class... TArgs>
void TCallbackList<TResult(TArgs...)>::Clear()
{
    auto guard = WriterGuard(SpinLock_);
    Callbacks_.clear();
}

template <class TResult, class... TArgs>
template <class... TCallArgs>
void TCallbackList<TResult(TArgs...)>::Fire(TCallArgs&&... args) const
{
    TCallbackVector callbacks;
    {
        auto guard = ReaderGuard(SpinLock_);
        callbacks = Callbacks_;
    }

    for (const auto& callback : callbacks) {
        callback.Run(std::forward<TCallArgs>(args)...);
    }
}

template <class TResult, class... TArgs>
template <class... TCallArgs>
void TCallbackList<TResult(TArgs...)>::FireAndClear(TCallArgs&&... args)
{
    TCallbackVector callbacks;
    {
        auto guard = WriterGuard(SpinLock_);
        callbacks.swap(Callbacks_);
    }

    for (const auto& callback : callbacks) {
        callback(std::forward<TCallArgs>(args)...);
    }
}

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

template <class TResult, class... TArgs>
void TSimpleCallbackList<TResult(TArgs...)>::Subscribe(const TCallback& callback)
{
    Callbacks_.push_back(callback);
}

template <class TResult, class... TArgs>
void TSimpleCallbackList<TResult(TArgs...)>::Unsubscribe(const TCallback& callback)
{
    Callbacks_.erase(std::find(Callbacks_.begin(), Callbacks_.end(), callback));
}

template <class TResult, class... TArgs>
template <class... TCallArgs>
void TSimpleCallbackList<TResult(TArgs...)>::Fire(TCallArgs&&... args) const
{
    for (const auto& callback : Callbacks_) {
        callback(std::forward<TCallArgs>(args)...);
    }
}

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

template <class TResult, class... TArgs>
void TSingleShotCallbackList<TResult(TArgs...)>::Subscribe(const TCallback& callback)
{
    auto guard = WriterGuard(SpinLock_);
    if (Fired_.load(std::memory_order::acquire)) {
        guard.Release();
        std::apply(callback, Args_);
        return;
    }
    Callbacks_.push_back(callback);
}

template <class TResult, class... TArgs>
bool TSingleShotCallbackList<TResult(TArgs...)>::TrySubscribe(const TCallback& callback)
{
    auto guard = WriterGuard(SpinLock_);
    if (Fired_.load(std::memory_order::acquire)) {
        return false;
    }
    Callbacks_.push_back(callback);
    return true;
}

template <class TResult, class... TArgs>
void TSingleShotCallbackList<TResult(TArgs...)>::Unsubscribe(const TCallback& callback)
{
    auto guard = WriterGuard(SpinLock_);
    for (auto it = Callbacks_.begin(); it != Callbacks_.end(); ++it) {
        if (*it == callback) {
            Callbacks_.erase(it);
            break;
        }
    }
}

template <class TResult, class... TArgs>
std::vector<TCallback<TResult(TArgs...)>> TSingleShotCallbackList<TResult(TArgs...)>::ToVector() const
{
    auto guard = ReaderGuard(SpinLock_);
    return std::vector<TCallback>(Callbacks_.begin(), Callbacks_.end());
}

template <class TResult, class... TArgs>
template <class... TCallArgs>
bool TSingleShotCallbackList<TResult(TArgs...)>::Fire(TCallArgs&&... args)
{
    TCallbackVector callbacks;
    {
        auto guard = WriterGuard(SpinLock_);
        if (Fired_.load(std::memory_order::acquire)) {
            return false;
        }
        Args_ = std::make_tuple(std::forward<TCallArgs>(args)...);
        callbacks.swap(Callbacks_);
        Fired_.store(true, std::memory_order::release);
    }

    for (const auto& callback : callbacks) {
        std::apply(callback, Args_);
    }

    return true;
}

template <class TResult, class... TArgs>
bool TSingleShotCallbackList<TResult(TArgs...)>::IsFired() const
{
    return Fired_.load(std::memory_order::acquire);
}

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

} // namespace NYT