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
base / barrier_closure.cc [blame]
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/barrier_closure.h"
#include <utility>
#include "base/atomic_ref_count.h"
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
namespace base {
namespace {
// Maintains state for a BarrierClosure.
class BarrierInfo {
public:
BarrierInfo(size_t num_callbacks_left, OnceClosure done_closure);
BarrierInfo(const BarrierInfo&) = delete;
BarrierInfo& operator=(const BarrierInfo&) = delete;
void Run();
private:
AtomicRefCount num_callbacks_left_;
OnceClosure done_closure_;
};
BarrierInfo::BarrierInfo(size_t num_callbacks, OnceClosure done_closure)
: num_callbacks_left_(checked_cast<int>(num_callbacks)),
done_closure_(std::move(done_closure)) {}
void BarrierInfo::Run() {
DCHECK(!num_callbacks_left_.IsZero());
if (!num_callbacks_left_.Decrement())
std::move(done_closure_).Run();
}
void ShouldNeverRun() {
NOTREACHED();
}
} // namespace
RepeatingClosure BarrierClosure(size_t num_callbacks_left,
OnceClosure done_closure) {
if (num_callbacks_left == 0) {
std::move(done_closure).Run();
return BindRepeating(&ShouldNeverRun);
}
return BindRepeating(&BarrierInfo::Run,
std::make_unique<BarrierInfo>(num_callbacks_left,
std::move(done_closure)));
}
} // namespace base