summaryrefslogtreecommitdiffstats
path: root/media/base/callback_util.cc
blob: 6ab7818c8b193103e8750febb5192ecfccba80cb (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/callback_util.h"

#include "base/bind.h"
#include "base/synchronization/lock.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop.h"
#include "base/message_loop_proxy.h"

namespace media {

// Executes the given closure if and only if the closure returned by
// GetClosure() has been executed exactly |count| times.
//
// |done_cb| will be executed on the same thread that created the CountingCB.
class CountingCB : public base::RefCountedThreadSafe<CountingCB> {
 public:
  CountingCB(int count, const base::Closure& done_cb)
      : message_loop_(base::MessageLoopProxy::current()),
        count_(count),
        done_cb_(done_cb) {
  }

  // Returns a closure bound to this object.
  base::Closure GetClosure() {
    return base::Bind(&CountingCB::OnCallback, this);
  }

 protected:
  friend class base::RefCountedThreadSafe<CountingCB>;
  virtual ~CountingCB() {}

 private:
  void OnCallback() {
    {
      base::AutoLock l(lock_);
      count_--;
      DCHECK_GE(count_, 0) << "CountingCB executed too many times";
      if (count_ != 0)
        return;
    }

    if (!message_loop_->BelongsToCurrentThread()) {
      message_loop_->PostTask(FROM_HERE, done_cb_);
      return;
    }

    done_cb_.Run();
  }

  scoped_refptr<base::MessageLoopProxy> message_loop_;
  base::Lock lock_;
  int count_;
  base::Closure done_cb_;

  DISALLOW_COPY_AND_ASSIGN(CountingCB);
};

static void OnSeriesCallback(
    scoped_refptr<base::MessageLoopProxy> message_loop,
    scoped_ptr<std::queue<ClosureFunc> > closures,
    const base::Closure& done_cb) {
  if (!message_loop->BelongsToCurrentThread()) {
    message_loop->PostTask(FROM_HERE, base::Bind(
        &OnSeriesCallback, message_loop, base::Passed(&closures), done_cb));
    return;
  }

  if (closures->empty()) {
    done_cb.Run();
    return;
  }

  ClosureFunc cb = closures->front();
  closures->pop();
  cb.Run(base::Bind(
      &OnSeriesCallback, message_loop, base::Passed(&closures), done_cb));
}

void RunInSeries(scoped_ptr<std::queue<ClosureFunc> > closures,
                 const base::Closure& done_cb) {
  OnSeriesCallback(base::MessageLoopProxy::current(),
                   closures.Pass(), done_cb);
}

static void OnStatusCallback(
    scoped_refptr<base::MessageLoopProxy> message_loop,
    scoped_ptr<std::queue<PipelineStatusCBFunc> > status_cbs,
    const PipelineStatusCB& done_cb,
    PipelineStatus last_status) {
  if (!message_loop->BelongsToCurrentThread()) {
    message_loop->PostTask(FROM_HERE, base::Bind(
        &OnStatusCallback, message_loop, base::Passed(&status_cbs), done_cb,
        last_status));
    return;
  }

  if (status_cbs->empty() || last_status != PIPELINE_OK) {
    done_cb.Run(last_status);
    return;
  }

  PipelineStatusCBFunc status_cb = status_cbs->front();
  status_cbs->pop();
  status_cb.Run(base::Bind(
      &OnStatusCallback, message_loop, base::Passed(&status_cbs), done_cb));
}

void RunInSeriesWithStatus(
    scoped_ptr<std::queue<PipelineStatusCBFunc> > status_cbs,
    const PipelineStatusCB& done_cb) {
  OnStatusCallback(base::MessageLoopProxy::current(),
                   status_cbs.Pass(), done_cb, PIPELINE_OK);
}

void RunInParallel(scoped_ptr<std::queue<ClosureFunc> > closures,
                   const base::Closure& done_cb) {
  if (closures->empty()) {
    done_cb.Run();
    return;
  }

  scoped_refptr<CountingCB> counting_cb =
      new CountingCB(closures->size(), done_cb);
  while (!closures->empty()) {
    closures->front().Run(counting_cb->GetClosure());
    closures->pop();
  }
}

}  // namespace media