1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/barrier_closure.h"
9 #include "base/atomic_ref_count.h"
10 #include "base/bind.h"
11 #include "base/memory/ptr_util.h"
16 // Maintains state for a BarrierClosure.
19 BarrierInfo(int num_callbacks_left, OnceClosure done_closure);
23 AtomicRefCount num_callbacks_left_;
24 OnceClosure done_closure_;
27 BarrierInfo::BarrierInfo(int num_callbacks, OnceClosure done_closure)
28 : num_callbacks_left_(num_callbacks),
29 done_closure_(std::move(done_closure)) {}
31 void BarrierInfo::Run() {
32 DCHECK(!num_callbacks_left_.IsZero());
33 if (!num_callbacks_left_.Decrement())
34 std::move(done_closure_).Run();
39 RepeatingClosure BarrierClosure(int num_callbacks_left,
40 OnceClosure done_closure) {
41 DCHECK_GE(num_callbacks_left, 0);
43 if (num_callbacks_left == 0)
44 std::move(done_closure).Run();
48 Owned(new BarrierInfo(num_callbacks_left, std::move(done_closure))));