OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/base/task.h" | 11 #include "webrtc/base/task.h" |
| 12 #include "webrtc/base/checks.h" |
12 #include "webrtc/base/common.h" | 13 #include "webrtc/base/common.h" |
13 #include "webrtc/base/taskrunner.h" | 14 #include "webrtc/base/taskrunner.h" |
14 | 15 |
15 namespace rtc { | 16 namespace rtc { |
16 | 17 |
17 int32_t Task::unique_id_seed_ = 0; | 18 int32_t Task::unique_id_seed_ = 0; |
18 | 19 |
19 Task::Task(TaskParent *parent) | 20 Task::Task(TaskParent *parent) |
20 : TaskParent(this, parent), | 21 : TaskParent(this, parent), |
21 state_(STATE_INIT), | 22 state_(STATE_INIT), |
22 blocked_(false), | 23 blocked_(false), |
23 done_(false), | 24 done_(false), |
24 aborted_(false), | 25 aborted_(false), |
25 busy_(false), | 26 busy_(false), |
26 error_(false), | 27 error_(false), |
27 start_time_(0), | 28 start_time_(0), |
28 timeout_time_(0), | 29 timeout_time_(0), |
29 timeout_seconds_(0), | 30 timeout_seconds_(0), |
30 timeout_suspended_(false) { | 31 timeout_suspended_(false) { |
31 unique_id_ = unique_id_seed_++; | 32 unique_id_ = unique_id_seed_++; |
32 | 33 |
33 // sanity check that we didn't roll-over our id seed | 34 // sanity check that we didn't roll-over our id seed |
34 ASSERT(unique_id_ < unique_id_seed_); | 35 RTC_DCHECK(unique_id_ < unique_id_seed_); |
35 } | 36 } |
36 | 37 |
37 Task::~Task() { | 38 Task::~Task() { |
38 // Is this task being deleted in the correct manner? | 39 // Is this task being deleted in the correct manner? |
39 ASSERT(!done_ || GetRunner()->is_ok_to_delete(this)); | 40 #if RTC_DCHECK_IS_ON |
40 ASSERT(state_ == STATE_INIT || done_); | 41 RTC_DCHECK(!done_ || GetRunner()->is_ok_to_delete(this)); |
41 ASSERT(state_ == STATE_INIT || blocked_); | 42 #endif |
| 43 RTC_DCHECK(state_ == STATE_INIT || done_); |
| 44 RTC_DCHECK(state_ == STATE_INIT || blocked_); |
42 | 45 |
43 // If the task is being deleted without being done, it | 46 // If the task is being deleted without being done, it |
44 // means that it hasn't been removed from its parent. | 47 // means that it hasn't been removed from its parent. |
45 // This happens if a task is deleted outside of TaskRunner. | 48 // This happens if a task is deleted outside of TaskRunner. |
46 if (!done_) { | 49 if (!done_) { |
47 Stop(); | 50 Stop(); |
48 } | 51 } |
49 } | 52 } |
50 | 53 |
51 int64_t Task::CurrentTime() { | 54 int64_t Task::CurrentTime() { |
52 return GetRunner()->CurrentTime(); | 55 return GetRunner()->CurrentTime(); |
53 } | 56 } |
54 | 57 |
55 int64_t Task::ElapsedTime() { | 58 int64_t Task::ElapsedTime() { |
56 return CurrentTime() - start_time_; | 59 return CurrentTime() - start_time_; |
57 } | 60 } |
58 | 61 |
59 void Task::Start() { | 62 void Task::Start() { |
60 if (state_ != STATE_INIT) | 63 if (state_ != STATE_INIT) |
61 return; | 64 return; |
62 // Set the start time before starting the task. Otherwise if the task | 65 // Set the start time before starting the task. Otherwise if the task |
63 // finishes quickly and deletes the Task object, setting start_time_ | 66 // finishes quickly and deletes the Task object, setting start_time_ |
64 // will crash. | 67 // will crash. |
65 start_time_ = CurrentTime(); | 68 start_time_ = CurrentTime(); |
66 GetRunner()->StartTask(this); | 69 GetRunner()->StartTask(this); |
67 } | 70 } |
68 | 71 |
69 void Task::Step() { | 72 void Task::Step() { |
70 if (done_) { | 73 if (done_) { |
71 #if !defined(NDEBUG) | 74 #if RTC_DCHECK_IS_ON |
72 // we do not know how !blocked_ happens when done_ - should be impossible. | 75 // we do not know how !blocked_ happens when done_ - should be impossible. |
73 // But it causes problems, so in retail build, we force blocked_, and | 76 // But it causes problems, so in retail build, we force blocked_, and |
74 // under debug we assert. | 77 // under debug we assert. |
75 ASSERT(blocked_); | 78 RTC_DCHECK(blocked_); |
76 #else | 79 #else |
77 blocked_ = true; | 80 blocked_ = true; |
78 #endif | 81 #endif |
79 return; | 82 return; |
80 } | 83 } |
81 | 84 |
82 // Async Error() was called | 85 // Async Error() was called |
83 if (error_) { | 86 if (error_) { |
84 done_ = true; | 87 done_ = true; |
85 state_ = STATE_ERROR; | 88 state_ = STATE_ERROR; |
86 blocked_ = true; | 89 blocked_ = true; |
87 // obsolete - an errored task is not considered done now | 90 // obsolete - an errored task is not considered done now |
88 // SignalDone(); | 91 // SignalDone(); |
89 | 92 |
90 Stop(); | 93 Stop(); |
91 #if !defined(NDEBUG) | 94 #if RTC_DCHECK_IS_ON |
92 // verify that stop removed this from its parent | 95 // verify that stop removed this from its parent |
93 ASSERT(!parent()->IsChildTask(this)); | 96 RTC_DCHECK(!parent()->IsChildTask(this)); |
94 #endif | 97 #endif |
95 return; | 98 return; |
96 } | 99 } |
97 | 100 |
98 busy_ = true; | 101 busy_ = true; |
99 int new_state = Process(state_); | 102 int new_state = Process(state_); |
100 busy_ = false; | 103 busy_ = false; |
101 | 104 |
102 if (aborted_) { | 105 if (aborted_) { |
103 Abort(true); // no need to wake because we're awake | 106 Abort(true); // no need to wake because we're awake |
(...skipping 14 matching lines...) Expand all Loading... |
118 } else if (new_state == STATE_ERROR) { | 121 } else if (new_state == STATE_ERROR) { |
119 done_ = true; | 122 done_ = true; |
120 error_ = true; | 123 error_ = true; |
121 } | 124 } |
122 | 125 |
123 if (done_) { | 126 if (done_) { |
124 // obsolete - call this yourself | 127 // obsolete - call this yourself |
125 // SignalDone(); | 128 // SignalDone(); |
126 | 129 |
127 Stop(); | 130 Stop(); |
128 #if !defined(NDEBUG) | 131 #if RTC_DCHECK_IS_ON |
129 // verify that stop removed this from its parent | 132 // verify that stop removed this from its parent |
130 ASSERT(!parent()->IsChildTask(this)); | 133 RTC_DCHECK(!parent()->IsChildTask(this)); |
131 #endif | 134 #endif |
132 blocked_ = true; | 135 blocked_ = true; |
133 } | 136 } |
134 } | 137 } |
135 | 138 |
136 void Task::Abort(bool nowake) { | 139 void Task::Abort(bool nowake) { |
137 // Why only check for done_ (instead of "aborted_ || done_")? | 140 // Why only check for done_ (instead of "aborted_ || done_")? |
138 // | 141 // |
139 // If aborted_ && !done_, it means the logic for aborting still | 142 // If aborted_ && !done_, it means the logic for aborting still |
140 // needs to be executed (because busy_ must have been true when | 143 // needs to be executed (because busy_ must have been true when |
141 // Abort() was previously called). | 144 // Abort() was previously called). |
142 if (done_) | 145 if (done_) |
143 return; | 146 return; |
144 aborted_ = true; | 147 aborted_ = true; |
145 if (!busy_) { | 148 if (!busy_) { |
146 done_ = true; | 149 done_ = true; |
147 blocked_ = true; | 150 blocked_ = true; |
148 error_ = true; | 151 error_ = true; |
149 | 152 |
150 // "done_" is set before calling "Stop()" to ensure that this code | 153 // "done_" is set before calling "Stop()" to ensure that this code |
151 // doesn't execute more than once (recursively) for the same task. | 154 // doesn't execute more than once (recursively) for the same task. |
152 Stop(); | 155 Stop(); |
153 #if !defined(NDEBUG) | 156 #if RTC_DCHECK_IS_ON |
154 // verify that stop removed this from its parent | 157 // verify that stop removed this from its parent |
155 ASSERT(!parent()->IsChildTask(this)); | 158 RTC_DCHECK(!parent()->IsChildTask(this)); |
156 #endif | 159 #endif |
157 if (!nowake) { | 160 if (!nowake) { |
158 // WakeTasks to self-delete. | 161 // WakeTasks to self-delete. |
159 // Don't call Wake() because it is a no-op after "done_" is set. | 162 // Don't call Wake() because it is a no-op after "done_" is set. |
160 // Even if Wake() did run, it clears "blocked_" which isn't desireable. | 163 // Even if Wake() did run, it clears "blocked_" which isn't desireable. |
161 GetRunner()->WakeTasks(); | 164 GetRunner()->WakeTasks(); |
162 } | 165 } |
163 } | 166 } |
164 } | 167 } |
165 | 168 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 ResetTimeout(); | 275 ResetTimeout(); |
273 } | 276 } |
274 } | 277 } |
275 | 278 |
276 int Task::OnTimeout() { | 279 int Task::OnTimeout() { |
277 // by default, we are finished after timing out | 280 // by default, we are finished after timing out |
278 return STATE_DONE; | 281 return STATE_DONE; |
279 } | 282 } |
280 | 283 |
281 } // namespace rtc | 284 } // namespace rtc |
OLD | NEW |