Repository navigation
Expand file tree
/
Copy pathcallback_gate.h
More file actions
87 lines (74 loc) · 1.93 KB
/
Copy pathcallback_gate.h
File metadata and controls
87 lines (74 loc) · 1.93 KB
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
#pragma once
#include <condition_variable>
#include <memory>
#include <mutex>
namespace BT::details
{
/**
* @brief Synchronization gate used to coordinate callbacks with object teardown.
*
* Callbacks enter the gate with tryStart() and must signal completion when done
* (use Guard for RAII). During teardown, call close() to reject new callbacks
* and closeAndDrain() to block until the callbacks still running have completed.
*/
class CallbackGate
{
public:
using Ptr = std::shared_ptr<CallbackGate>;
/// RAII helper that signals the completion of a callback entered with tryStart()
class Guard
{
public:
explicit Guard(Ptr gate) : gate_(std::move(gate))
{}
Guard(const Guard&) = delete;
Guard& operator=(const Guard&) = delete;
Guard(Guard&&) = delete;
Guard& operator=(Guard&&) = delete;
~Guard()
{
gate_->completed();
}
private:
Ptr gate_;
};
/// Returns false if the gate has been closed and the callback must not run.
bool tryStart()
{
const std::lock_guard lk(mutex_);
if(!accepting_callbacks_)
{
return false;
}
running_callbacks_++;
return true;
}
/// Reject new callbacks, without waiting for the running ones.
void close()
{
const std::lock_guard lk(mutex_);
accepting_callbacks_ = false;
}
/// Reject new callbacks and wait until callbacks already running have completed.
void closeAndDrain()
{
std::unique_lock lk(mutex_);
accepting_callbacks_ = false;
condition_variable_.wait(lk, [this] { return running_callbacks_ == 0; });
}
private:
void completed()
{
const std::lock_guard lk(mutex_);
running_callbacks_--;
if(running_callbacks_ == 0 && !accepting_callbacks_)
{
condition_variable_.notify_all();
}
}
std::mutex mutex_;
std::condition_variable condition_variable_;
bool accepting_callbacks_ = true;
size_t running_callbacks_ = 0;
};
} // namespace BT::details