You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2006, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <algorithm>
  28. #include "talk/base/taskparent.h"
  29. #include "talk/base/task.h"
  30. #include "talk/base/taskrunner.h"
  31. namespace talk_base {
  32. TaskParent::TaskParent(Task* derived_instance, TaskParent *parent)
  33. : parent_(parent) {
  34. ASSERT(derived_instance != NULL);
  35. ASSERT(parent != NULL);
  36. runner_ = parent->GetRunner();
  37. parent_->AddChild(derived_instance);
  38. Initialize();
  39. }
  40. TaskParent::TaskParent(TaskRunner *derived_instance)
  41. : parent_(NULL),
  42. runner_(derived_instance) {
  43. ASSERT(derived_instance != NULL);
  44. Initialize();
  45. }
  46. // Does common initialization of member variables
  47. void TaskParent::Initialize() {
  48. children_.reset(new ChildSet());
  49. child_error_ = false;
  50. }
  51. void TaskParent::AddChild(Task *child) {
  52. children_->insert(child);
  53. }
  54. #ifdef _DEBUG
  55. bool TaskParent::IsChildTask(Task *task) {
  56. ASSERT(task != NULL);
  57. return task->parent_ == this && children_->find(task) != children_->end();
  58. }
  59. #endif
  60. bool TaskParent::AllChildrenDone() {
  61. for (ChildSet::iterator it = children_->begin();
  62. it != children_->end();
  63. ++it) {
  64. if (!(*it)->IsDone())
  65. return false;
  66. }
  67. return true;
  68. }
  69. bool TaskParent::AnyChildError() {
  70. return child_error_;
  71. }
  72. void TaskParent::AbortAllChildren() {
  73. if (children_->size() > 0) {
  74. #ifdef _DEBUG
  75. runner_->IncrementAbortCount();
  76. #endif
  77. ChildSet copy = *children_;
  78. for (ChildSet::iterator it = copy.begin(); it != copy.end(); ++it) {
  79. (*it)->Abort(true); // Note we do not wake
  80. }
  81. #ifdef _DEBUG
  82. runner_->DecrementAbortCount();
  83. #endif
  84. }
  85. }
  86. void TaskParent::OnStopped(Task *task) {
  87. AbortAllChildren();
  88. parent_->OnChildStopped(task);
  89. }
  90. void TaskParent::OnChildStopped(Task *child) {
  91. if (child->HasError())
  92. child_error_ = true;
  93. children_->erase(child);
  94. }
  95. } // namespace talk_base