blob: ab01def9e90d4ba2579404773afd29b6f364f69d [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 "webrtc/base/task.h"
nisseede5da42017-01-12 05:15:36 -080012#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013#include "webrtc/base/common.h"
14#include "webrtc/base/taskrunner.h"
15
16namespace rtc {
17
Peter Boström0c4e06b2015-10-07 12:23:21 +020018int32_t Task::unique_id_seed_ = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
20Task::Task(TaskParent *parent)
21 : TaskParent(this, parent),
22 state_(STATE_INIT),
23 blocked_(false),
24 done_(false),
25 aborted_(false),
26 busy_(false),
27 error_(false),
28 start_time_(0),
29 timeout_time_(0),
30 timeout_seconds_(0),
31 timeout_suspended_(false) {
32 unique_id_ = unique_id_seed_++;
33
34 // sanity check that we didn't roll-over our id seed
nisseede5da42017-01-12 05:15:36 -080035 RTC_DCHECK(unique_id_ < unique_id_seed_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036}
37
38Task::~Task() {
39 // Is this task being deleted in the correct manner?
nisseede5da42017-01-12 05:15:36 -080040#if RTC_DCHECK_IS_ON
41 RTC_DCHECK(!done_ || GetRunner()->is_ok_to_delete(this));
42#endif
43 RTC_DCHECK(state_ == STATE_INIT || done_);
44 RTC_DCHECK(state_ == STATE_INIT || blocked_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000045
46 // If the task is being deleted without being done, it
47 // means that it hasn't been removed from its parent.
48 // This happens if a task is deleted outside of TaskRunner.
49 if (!done_) {
50 Stop();
51 }
52}
53
Peter Boström0c4e06b2015-10-07 12:23:21 +020054int64_t Task::CurrentTime() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000055 return GetRunner()->CurrentTime();
56}
57
Peter Boström0c4e06b2015-10-07 12:23:21 +020058int64_t Task::ElapsedTime() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059 return CurrentTime() - start_time_;
60}
61
62void Task::Start() {
63 if (state_ != STATE_INIT)
64 return;
65 // Set the start time before starting the task. Otherwise if the task
66 // finishes quickly and deletes the Task object, setting start_time_
67 // will crash.
68 start_time_ = CurrentTime();
69 GetRunner()->StartTask(this);
70}
71
72void Task::Step() {
73 if (done_) {
nisseede5da42017-01-12 05:15:36 -080074#if RTC_DCHECK_IS_ON
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075 // we do not know how !blocked_ happens when done_ - should be impossible.
76 // But it causes problems, so in retail build, we force blocked_, and
77 // under debug we assert.
nisseede5da42017-01-12 05:15:36 -080078 RTC_DCHECK(blocked_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079#else
80 blocked_ = true;
81#endif
82 return;
83 }
84
85 // Async Error() was called
86 if (error_) {
87 done_ = true;
88 state_ = STATE_ERROR;
89 blocked_ = true;
90// obsolete - an errored task is not considered done now
91// SignalDone();
92
93 Stop();
nisseede5da42017-01-12 05:15:36 -080094#if RTC_DCHECK_IS_ON
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095 // verify that stop removed this from its parent
nisseede5da42017-01-12 05:15:36 -080096 RTC_DCHECK(!parent()->IsChildTask(this));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000097#endif
98 return;
99 }
100
101 busy_ = true;
102 int new_state = Process(state_);
103 busy_ = false;
104
105 if (aborted_) {
106 Abort(true); // no need to wake because we're awake
107 return;
108 }
109
110 if (new_state == STATE_BLOCKED) {
111 blocked_ = true;
112 // Let the timeout continue
113 } else {
114 state_ = new_state;
115 blocked_ = false;
116 ResetTimeout();
117 }
118
119 if (new_state == STATE_DONE) {
120 done_ = true;
121 } else if (new_state == STATE_ERROR) {
122 done_ = true;
123 error_ = true;
124 }
125
126 if (done_) {
127// obsolete - call this yourself
128// SignalDone();
129
130 Stop();
nisseede5da42017-01-12 05:15:36 -0800131#if RTC_DCHECK_IS_ON
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000132 // verify that stop removed this from its parent
nisseede5da42017-01-12 05:15:36 -0800133 RTC_DCHECK(!parent()->IsChildTask(this));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000134#endif
135 blocked_ = true;
136 }
137}
138
139void Task::Abort(bool nowake) {
140 // Why only check for done_ (instead of "aborted_ || done_")?
141 //
142 // If aborted_ && !done_, it means the logic for aborting still
143 // needs to be executed (because busy_ must have been true when
144 // Abort() was previously called).
145 if (done_)
146 return;
147 aborted_ = true;
148 if (!busy_) {
149 done_ = true;
150 blocked_ = true;
151 error_ = true;
152
153 // "done_" is set before calling "Stop()" to ensure that this code
154 // doesn't execute more than once (recursively) for the same task.
155 Stop();
nisseede5da42017-01-12 05:15:36 -0800156#if RTC_DCHECK_IS_ON
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000157 // verify that stop removed this from its parent
nisseede5da42017-01-12 05:15:36 -0800158 RTC_DCHECK(!parent()->IsChildTask(this));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000159#endif
160 if (!nowake) {
161 // WakeTasks to self-delete.
162 // Don't call Wake() because it is a no-op after "done_" is set.
163 // Even if Wake() did run, it clears "blocked_" which isn't desireable.
164 GetRunner()->WakeTasks();
165 }
166 }
167}
168
169void Task::Wake() {
170 if (done_)
171 return;
172 if (blocked_) {
173 blocked_ = false;
174 GetRunner()->WakeTasks();
175 }
176}
177
178void Task::Error() {
179 if (error_ || done_)
180 return;
181 error_ = true;
182 Wake();
183}
184
185std::string Task::GetStateName(int state) const {
186 switch (state) {
187 case STATE_BLOCKED: return "BLOCKED";
188 case STATE_INIT: return "INIT";
189 case STATE_START: return "START";
190 case STATE_DONE: return "DONE";
191 case STATE_ERROR: return "ERROR";
192 case STATE_RESPONSE: return "RESPONSE";
193 }
194 return "??";
195}
196
197int Task::Process(int state) {
198 int newstate = STATE_ERROR;
199
200 if (TimedOut()) {
201 ClearTimeout();
202 newstate = OnTimeout();
203 SignalTimeout();
204 } else {
205 switch (state) {
206 case STATE_INIT:
207 newstate = STATE_START;
208 break;
209 case STATE_START:
210 newstate = ProcessStart();
211 break;
212 case STATE_RESPONSE:
213 newstate = ProcessResponse();
214 break;
215 case STATE_DONE:
216 case STATE_ERROR:
217 newstate = STATE_BLOCKED;
218 break;
219 }
220 }
221
222 return newstate;
223}
224
225void Task::Stop() {
226 // No need to wake because we're either awake or in abort
227 TaskParent::OnStopped(this);
228}
229
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000230int Task::ProcessResponse() {
231 return STATE_DONE;
232}
233
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000234void Task::set_timeout_seconds(const int timeout_seconds) {
235 timeout_seconds_ = timeout_seconds;
236 ResetTimeout();
237}
238
239bool Task::TimedOut() {
240 return timeout_seconds_ &&
241 timeout_time_ &&
242 CurrentTime() >= timeout_time_;
243}
244
245void Task::ResetTimeout() {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200246 int64_t previous_timeout_time = timeout_time_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000247 bool timeout_allowed = (state_ != STATE_INIT)
248 && (state_ != STATE_DONE)
249 && (state_ != STATE_ERROR);
250 if (timeout_seconds_ && timeout_allowed && !timeout_suspended_)
251 timeout_time_ = CurrentTime() +
252 (timeout_seconds_ * kSecToMsec * kMsecTo100ns);
253 else
254 timeout_time_ = 0;
255
256 GetRunner()->UpdateTaskTimeout(this, previous_timeout_time);
257}
258
259void Task::ClearTimeout() {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200260 int64_t previous_timeout_time = timeout_time_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000261 timeout_time_ = 0;
262 GetRunner()->UpdateTaskTimeout(this, previous_timeout_time);
263}
264
265void Task::SuspendTimeout() {
266 if (!timeout_suspended_) {
267 timeout_suspended_ = true;
268 ResetTimeout();
269 }
270}
271
272void Task::ResumeTimeout() {
273 if (timeout_suspended_) {
274 timeout_suspended_ = false;
275 ResetTimeout();
276 }
277}
278
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000279int Task::OnTimeout() {
280 // by default, we are finished after timing out
281 return STATE_DONE;
282}
283
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000284} // namespace rtc