3
* Copyright 2004--2006, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
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.
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.
28
#include "talk/base/task.h"
29
#include "talk/base/common.h"
30
#include "talk/base/taskrunner.h"
34
int32 Task::unique_id_seed_ = 0;
36
Task::Task(TaskParent *parent)
37
: TaskParent(this, parent),
47
timeout_suspended_(false) {
48
unique_id_ = unique_id_seed_++;
50
// sanity check that we didn't roll-over our id seed
51
ASSERT(unique_id_ < unique_id_seed_);
55
// Is this task being deleted in the correct manner?
56
ASSERT(!done_ || GetRunner()->is_ok_to_delete(this));
57
ASSERT(state_ == STATE_INIT || done_);
58
ASSERT(state_ == STATE_INIT || blocked_);
60
// If the task is being deleted without being done, it
61
// means that it hasn't been removed from its parent.
62
// This happens if a task is deleted outside of TaskRunner.
68
int64 Task::CurrentTime() {
69
return GetRunner()->CurrentTime();
72
int64 Task::ElapsedTime() {
73
return CurrentTime() - start_time_;
77
if (state_ != STATE_INIT)
79
// Set the start time before starting the task. Otherwise if the task
80
// finishes quickly and deletes the Task object, setting start_time_
82
start_time_ = CurrentTime();
83
GetRunner()->StartTask(this);
89
// we do not know how !blocked_ happens when done_ - should be impossible.
90
// But it causes problems, so in retail build, we force blocked_, and
91
// under debug we assert.
99
// Async Error() was called
102
state_ = STATE_ERROR;
104
// obsolete - an errored task is not considered done now
109
// verify that stop removed this from its parent
110
ASSERT(!parent()->IsChildTask(this));
116
int new_state = Process(state_);
120
Abort(true); // no need to wake because we're awake
124
if (new_state == STATE_BLOCKED) {
126
// Let the timeout continue
133
if (new_state == STATE_DONE) {
135
} else if (new_state == STATE_ERROR) {
141
// obsolete - call this yourself
146
// verify that stop removed this from its parent
147
ASSERT(!parent()->IsChildTask(this));
153
void Task::Abort(bool nowake) {
154
// Why only check for done_ (instead of "aborted_ || done_")?
156
// If aborted_ && !done_, it means the logic for aborting still
157
// needs to be executed (because busy_ must have been true when
158
// Abort() was previously called).
167
// "done_" is set before calling "Stop()" to ensure that this code
168
// doesn't execute more than once (recursively) for the same task.
171
// verify that stop removed this from its parent
172
ASSERT(!parent()->IsChildTask(this));
175
// WakeTasks to self-delete.
176
// Don't call Wake() because it is a no-op after "done_" is set.
177
// Even if Wake() did run, it clears "blocked_" which isn't desireable.
178
GetRunner()->WakeTasks();
188
GetRunner()->WakeTasks();
199
std::string Task::GetStateName(int state) const {
201
case STATE_BLOCKED: return "BLOCKED";
202
case STATE_INIT: return "INIT";
203
case STATE_START: return "START";
204
case STATE_DONE: return "DONE";
205
case STATE_ERROR: return "ERROR";
206
case STATE_RESPONSE: return "RESPONSE";
211
int Task::Process(int state) {
212
int newstate = STATE_ERROR;
216
newstate = OnTimeout();
221
newstate = STATE_START;
224
newstate = ProcessStart();
227
newstate = ProcessResponse();
231
newstate = STATE_BLOCKED;
240
// No need to wake because we're either awake or in abort
241
TaskParent::OnStopped(this);
244
void Task::set_timeout_seconds(const int timeout_seconds) {
245
timeout_seconds_ = timeout_seconds;
249
bool Task::TimedOut() {
250
return timeout_seconds_ &&
252
CurrentTime() >= timeout_time_;
255
void Task::ResetTimeout() {
256
int64 previous_timeout_time = timeout_time_;
257
bool timeout_allowed = (state_ != STATE_INIT)
258
&& (state_ != STATE_DONE)
259
&& (state_ != STATE_ERROR);
260
if (timeout_seconds_ && timeout_allowed && !timeout_suspended_)
261
timeout_time_ = CurrentTime() +
262
(timeout_seconds_ * kSecToMsec * kMsecTo100ns);
266
GetRunner()->UpdateTaskTimeout(this, previous_timeout_time);
269
void Task::ClearTimeout() {
270
int64 previous_timeout_time = timeout_time_;
272
GetRunner()->UpdateTaskTimeout(this, previous_timeout_time);
275
void Task::SuspendTimeout() {
276
if (!timeout_suspended_) {
277
timeout_suspended_ = true;
282
void Task::ResumeTimeout() {
283
if (timeout_suspended_) {
284
timeout_suspended_ = false;
289
} // namespace talk_base