OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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. | |
4 | |
5 #include "media/base/callback_util.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/ref_counted.h" | |
9 | |
10 namespace media { | |
11 | |
12 namespace { | |
acolwell GONE FROM CHROMIUM
2012/07/19 00:58:18
remove per our discussion.
scherkus (not reviewing)
2012/07/19 21:29:02
Done.
| |
13 | |
14 // Executes a callback when all references to the object have been | |
15 // released. We use reference counting as it already implements a thread safe | |
16 // counter and avoids the use of base::Unretained() and delete this, which may | |
17 // lead to use-after-free if clients retain closures after executing them. | |
18 class CountingCB : public base::RefCountedThreadSafe<CountingCB> { | |
19 public: | |
20 CountingCB(const base::Closure& done_cb) | |
acolwell GONE FROM CHROMIUM
2012/07/19 00:58:18
explicit
scherkus (not reviewing)
2012/07/19 21:29:02
Done.
| |
21 : done_cb_(done_cb) { | |
22 } | |
23 | |
24 // Returns a closure bound to this object. | |
25 base::Closure GetClosure() { | |
26 return base::Bind(&CountingCB::DoNothing, this); | |
27 } | |
28 | |
29 protected: | |
30 friend class base::RefCountedThreadSafe<CountingCB>; | |
31 virtual ~CountingCB() { | |
32 done_cb_.Run(); | |
33 } | |
34 | |
35 private: | |
36 void DoNothing() {} | |
37 | |
38 base::Closure done_cb_; | |
39 | |
40 DISALLOW_COPY_AND_ASSIGN(CountingCB); | |
41 }; | |
42 | |
43 } // namespace | |
44 | |
45 void RunInSeries(scoped_ptr<std::queue<ClosureCB> > closures, | |
46 const base::Closure& done_cb) { | |
47 if (closures->empty()) { | |
48 done_cb.Run(); | |
49 return; | |
50 } | |
51 | |
52 ClosureCB cb = closures->front(); | |
53 closures->pop(); | |
54 cb.Run(base::Bind(&RunInSeries, base::Passed(&closures), done_cb)); | |
55 } | |
56 | |
57 void RunInParallel(scoped_ptr<std::queue<ClosureCB> > closures, | |
58 const base::Closure& done_cb) { | |
59 scoped_refptr<CountingCB> counting_cb = new CountingCB(done_cb); | |
60 while (!closures->empty()) { | |
61 closures->front().Run(counting_cb->GetClosure()); | |
62 closures->pop(); | |
63 } | |
64 } | |
65 | |
66 } // namespace media | |
OLD | NEW |