Home | History | Annotate | Download | only in base
      1 /*
      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 
     15 #include "webrtc/base/common.h"
     16 #include "webrtc/base/task.h"
     17 #include "webrtc/base/taskrunner.h"
     18 
     19 namespace rtc {
     20 
     21 TaskParent::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 
     30 TaskParent::TaskParent(TaskRunner *derived_instance)
     31     : parent_(NULL),
     32       runner_(derived_instance) {
     33   ASSERT(derived_instance != NULL);
     34   Initialize();
     35 }
     36 
     37 TaskParent::~TaskParent() = default;
     38 
     39 // Does common initialization of member variables
     40 void TaskParent::Initialize() {
     41   children_.reset(new ChildSet());
     42   child_error_ = false;
     43 }
     44 
     45 void TaskParent::AddChild(Task *child) {
     46   children_->insert(child);
     47 }
     48 
     49 #if !defined(NDEBUG)
     50 bool TaskParent::IsChildTask(Task *task) {
     51   ASSERT(task != NULL);
     52   return task->parent_ == this && children_->find(task) != children_->end();
     53 }
     54 #endif
     55 
     56 bool 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 
     66 bool TaskParent::AnyChildError() {
     67   return child_error_;
     68 }
     69 
     70 void TaskParent::AbortAllChildren() {
     71   if (children_->size() > 0) {
     72 #if !defined(NDEBUG)
     73     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 
     81 #if !defined(NDEBUG)
     82     runner_->DecrementAbortCount();
     83 #endif
     84   }
     85 }
     86 
     87 void TaskParent::OnStopped(Task *task) {
     88   AbortAllChildren();
     89   parent_->OnChildStopped(task);
     90 }
     91 
     92 void TaskParent::OnChildStopped(Task *child) {
     93   if (child->HasError())
     94     child_error_ = true;
     95   children_->erase(child);
     96 }
     97 
     98 } // namespace rtc
     99