blob: 14d236dc428dc6bc30512da5cda98da5dfa9ca6e [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include <algorithm>
12
13#include "webrtc/base/taskparent.h"
14
kwiberg@webrtc.org11426dc2015-02-11 14:30:34 +000015#include "webrtc/base/common.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016#include "webrtc/base/task.h"
17#include "webrtc/base/taskrunner.h"
18
19namespace rtc {
20
21TaskParent::TaskParent(Task* derived_instance, TaskParent *parent)
22 : parent_(parent) {
23 ASSERT(derived_instance != NULL);
24 ASSERT(parent != NULL);
25 runner_ = parent->GetRunner();
26 parent_->AddChild(derived_instance);
27 Initialize();
28}
29
30TaskParent::TaskParent(TaskRunner *derived_instance)
31 : parent_(NULL),
32 runner_(derived_instance) {
33 ASSERT(derived_instance != NULL);
34 Initialize();
35}
36
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000037TaskParent::~TaskParent() = default;
38
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039// Does common initialization of member variables
40void TaskParent::Initialize() {
41 children_.reset(new ChildSet());
42 child_error_ = false;
43}
44
45void TaskParent::AddChild(Task *child) {
46 children_->insert(child);
47}
48
tfarinaa41ab932015-10-30 16:08:48 -070049#if !defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050bool TaskParent::IsChildTask(Task *task) {
51 ASSERT(task != NULL);
52 return task->parent_ == this && children_->find(task) != children_->end();
53}
54#endif
55
56bool TaskParent::AllChildrenDone() {
57 for (ChildSet::iterator it = children_->begin();
58 it != children_->end();
59 ++it) {
60 if (!(*it)->IsDone())
61 return false;
62 }
63 return true;
64}
65
66bool TaskParent::AnyChildError() {
67 return child_error_;
68}
69
70void TaskParent::AbortAllChildren() {
71 if (children_->size() > 0) {
tfarinaa41ab932015-10-30 16:08:48 -070072#if !defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073 runner_->IncrementAbortCount();
74#endif
75
76 ChildSet copy = *children_;
77 for (ChildSet::iterator it = copy.begin(); it != copy.end(); ++it) {
78 (*it)->Abort(true); // Note we do not wake
79 }
80
tfarinaa41ab932015-10-30 16:08:48 -070081#if !defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082 runner_->DecrementAbortCount();
83#endif
84 }
85}
86
87void TaskParent::OnStopped(Task *task) {
88 AbortAllChildren();
89 parent_->OnChildStopped(task);
90}
91
92void TaskParent::OnChildStopped(Task *child) {
93 if (child->HasError())
94 child_error_ = true;
95 children_->erase(child);
96}
97
98} // namespace rtc