Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Side by Side Diff: media/base/callback_util.h

Issue 10830146: Replace RunInSeries() and RunInParallel() with CallbackSeries helper class. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src
Patch Set: more hotness Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | media/base/callback_util.cc » ('j') | media/base/callback_util.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MEDIA_BASE_CALLBACK_UTIL_H_ 5 #ifndef MEDIA_BASE_CALLBACK_UTIL_H_
6 #define MEDIA_BASE_CALLBACK_UTIL_H_ 6 #define MEDIA_BASE_CALLBACK_UTIL_H_
7 7
8 #include <queue> 8 #include <queue>
9 9
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
12 #include "media/base/pipeline_status.h" 14 #include "media/base/pipeline_status.h"
13 15
16 namespace base {
17 class MessageLoopProxy;
18 }
19
14 namespace media { 20 namespace media {
15 21
16 typedef base::Callback<void(const base::Closure&)> ClosureFunc; 22 // Runs a series of bound functions accepting Closures or PipelineStatusCB.
17 typedef base::Callback<void(const PipelineStatusCB&)> PipelineStatusCBFunc; 23 // SerialCallbackRunner doesn't use regular Closure/PipelineStatusCBs as it
24 // late binds the completion callback as the series progresses.
25 class SerialCallbackRunner {
26 public:
27 typedef base::Callback<void(const base::Closure&)> BoundClosure;
28 typedef base::Callback<void(const PipelineStatusCB&)> BoundPipelineStatusCB;
18 29
19 // Executes the closures in FIFO order, executing |done_cb| when the last 30 // Serial queue of bound functions to run.
20 // closure has completed running. 31 class Queue {
scherkus (not reviewing) 2012/08/03 20:19:05 PTAL here + usage in pipeline.cc
21 // 32 public:
22 // All closures (including |done_cb|) are executed on same thread as the 33 Queue();
23 // calling thread. 34 ~Queue();
24 void RunInSeries(scoped_ptr<std::queue<ClosureFunc> > closures,
25 const base::Closure& done_cb);
26 35
27 // Executes the closures in FIFO order, executing |done_cb| when the last 36 void Push(const BoundClosure& bound_fn);
28 // closure has completed running, reporting the final status code. 37 void Push(const BoundPipelineStatusCB& bound_fn);
29 //
30 // Closures will stop being executed if a previous closure in the series
31 // returned an error status and |done_cb| will be executed prematurely.
32 //
33 // All closures (including |done_cb|) are executed on same thread as the
34 // calling thread.
35 void RunInSeriesWithStatus(
36 scoped_ptr<std::queue<PipelineStatusCBFunc> > status_cbs,
37 const PipelineStatusCB& done_cb);
38 38
39 // Executes the closures in parallel, executing |done_cb| when all closures have 39 BoundPipelineStatusCB Pop();
40 // completed running. 40
41 // 41 bool empty() { return bound_fns_.empty(); }
42 // No attempt is made to parallelize execution of the closures. In other words, 42
43 // this method will run all closures in FIFO order if said closures execute 43 private:
44 // synchronously on the same call stack. 44 std::queue<BoundPipelineStatusCB> bound_fns_;
45 // 45
46 // All closures (including |done_cb|) are executed on same thread as the 46 DISALLOW_COPY_AND_ASSIGN(Queue);
47 // calling thread. 47 };
48 void RunInParallel(scoped_ptr<std::queue<ClosureFunc> > closures, 48
49 const base::Closure& done_cb); 49 // Executes the bound functions in series, executing |done_cb| when finished.
50 //
51 // All bound functions are executed on the thread that Run() is called on,
52 // including |done_cb|.
53 //
54 // Deleting the object will prevent execution of any unstarted bound
55 // functions, including |done_cb|.
56 static scoped_ptr<SerialCallbackRunner> Run(
57 scoped_ptr<Queue> bound_fns, const PipelineStatusCB& done_cb);
Ami GONE FROM CHROMIUM 2012/08/03 20:31:43 copy ctor bound_fns per offline convo
58
59 private:
60 friend class scoped_ptr<SerialCallbackRunner>;
61
62 SerialCallbackRunner(
63 scoped_ptr<Queue> bound_fns, const PipelineStatusCB& done_cb);
64 ~SerialCallbackRunner();
65
66 void RunNextInSeries(PipelineStatus last_status);
67
68 base::WeakPtrFactory<SerialCallbackRunner> weak_this_;
69 scoped_refptr<base::MessageLoopProxy> message_loop_;
70 scoped_ptr<Queue> bound_fns_;
Ami GONE FROM CHROMIUM 2012/08/03 20:31:43 can drop the scoped_ptr here too if pass-by-copy.
71 PipelineStatusCB done_cb_;
72
73 DISALLOW_COPY_AND_ASSIGN(SerialCallbackRunner);
74 };
50 75
51 } // namespace media 76 } // namespace media
52 77
53 #endif // MEDIA_BASE_CALLBACK_UTIL_H_ 78 #endif // MEDIA_BASE_CALLBACK_UTIL_H_
OLDNEW
« no previous file with comments | « no previous file | media/base/callback_util.cc » ('j') | media/base/callback_util.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698