| 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/common.h" | 12 #include "webrtc/base/common.h" |
| 13 #include "webrtc/base/taskrunner.h" | 13 #include "webrtc/base/taskrunner.h" |
| 14 | 14 |
| 15 namespace rtc { | 15 namespace rtc { |
| 16 | 16 |
| 17 int32 Task::unique_id_seed_ = 0; | 17 int32_t Task::unique_id_seed_ = 0; |
| 18 | 18 |
| 19 Task::Task(TaskParent *parent) | 19 Task::Task(TaskParent *parent) |
| 20 : TaskParent(this, parent), | 20 : TaskParent(this, parent), |
| 21 state_(STATE_INIT), | 21 state_(STATE_INIT), |
| 22 blocked_(false), | 22 blocked_(false), |
| 23 done_(false), | 23 done_(false), |
| 24 aborted_(false), | 24 aborted_(false), |
| 25 busy_(false), | 25 busy_(false), |
| 26 error_(false), | 26 error_(false), |
| 27 start_time_(0), | 27 start_time_(0), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 41 ASSERT(state_ == STATE_INIT || blocked_); | 41 ASSERT(state_ == STATE_INIT || blocked_); |
| 42 | 42 |
| 43 // If the task is being deleted without being done, it | 43 // If the task is being deleted without being done, it |
| 44 // means that it hasn't been removed from its parent. | 44 // means that it hasn't been removed from its parent. |
| 45 // This happens if a task is deleted outside of TaskRunner. | 45 // This happens if a task is deleted outside of TaskRunner. |
| 46 if (!done_) { | 46 if (!done_) { |
| 47 Stop(); | 47 Stop(); |
| 48 } | 48 } |
| 49 } | 49 } |
| 50 | 50 |
| 51 int64 Task::CurrentTime() { | 51 int64_t Task::CurrentTime() { |
| 52 return GetRunner()->CurrentTime(); | 52 return GetRunner()->CurrentTime(); |
| 53 } | 53 } |
| 54 | 54 |
| 55 int64 Task::ElapsedTime() { | 55 int64_t Task::ElapsedTime() { |
| 56 return CurrentTime() - start_time_; | 56 return CurrentTime() - start_time_; |
| 57 } | 57 } |
| 58 | 58 |
| 59 void Task::Start() { | 59 void Task::Start() { |
| 60 if (state_ != STATE_INIT) | 60 if (state_ != STATE_INIT) |
| 61 return; | 61 return; |
| 62 // Set the start time before starting the task. Otherwise if the task | 62 // Set the start time before starting the task. Otherwise if the task |
| 63 // finishes quickly and deletes the Task object, setting start_time_ | 63 // finishes quickly and deletes the Task object, setting start_time_ |
| 64 // will crash. | 64 // will crash. |
| 65 start_time_ = CurrentTime(); | 65 start_time_ = CurrentTime(); |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 ResetTimeout(); | 233 ResetTimeout(); |
| 234 } | 234 } |
| 235 | 235 |
| 236 bool Task::TimedOut() { | 236 bool Task::TimedOut() { |
| 237 return timeout_seconds_ && | 237 return timeout_seconds_ && |
| 238 timeout_time_ && | 238 timeout_time_ && |
| 239 CurrentTime() >= timeout_time_; | 239 CurrentTime() >= timeout_time_; |
| 240 } | 240 } |
| 241 | 241 |
| 242 void Task::ResetTimeout() { | 242 void Task::ResetTimeout() { |
| 243 int64 previous_timeout_time = timeout_time_; | 243 int64_t previous_timeout_time = timeout_time_; |
| 244 bool timeout_allowed = (state_ != STATE_INIT) | 244 bool timeout_allowed = (state_ != STATE_INIT) |
| 245 && (state_ != STATE_DONE) | 245 && (state_ != STATE_DONE) |
| 246 && (state_ != STATE_ERROR); | 246 && (state_ != STATE_ERROR); |
| 247 if (timeout_seconds_ && timeout_allowed && !timeout_suspended_) | 247 if (timeout_seconds_ && timeout_allowed && !timeout_suspended_) |
| 248 timeout_time_ = CurrentTime() + | 248 timeout_time_ = CurrentTime() + |
| 249 (timeout_seconds_ * kSecToMsec * kMsecTo100ns); | 249 (timeout_seconds_ * kSecToMsec * kMsecTo100ns); |
| 250 else | 250 else |
| 251 timeout_time_ = 0; | 251 timeout_time_ = 0; |
| 252 | 252 |
| 253 GetRunner()->UpdateTaskTimeout(this, previous_timeout_time); | 253 GetRunner()->UpdateTaskTimeout(this, previous_timeout_time); |
| 254 } | 254 } |
| 255 | 255 |
| 256 void Task::ClearTimeout() { | 256 void Task::ClearTimeout() { |
| 257 int64 previous_timeout_time = timeout_time_; | 257 int64_t previous_timeout_time = timeout_time_; |
| 258 timeout_time_ = 0; | 258 timeout_time_ = 0; |
| 259 GetRunner()->UpdateTaskTimeout(this, previous_timeout_time); | 259 GetRunner()->UpdateTaskTimeout(this, previous_timeout_time); |
| 260 } | 260 } |
| 261 | 261 |
| 262 void Task::SuspendTimeout() { | 262 void Task::SuspendTimeout() { |
| 263 if (!timeout_suspended_) { | 263 if (!timeout_suspended_) { |
| 264 timeout_suspended_ = true; | 264 timeout_suspended_ = true; |
| 265 ResetTimeout(); | 265 ResetTimeout(); |
| 266 } | 266 } |
| 267 } | 267 } |
| 268 | 268 |
| 269 void Task::ResumeTimeout() { | 269 void Task::ResumeTimeout() { |
| 270 if (timeout_suspended_) { | 270 if (timeout_suspended_) { |
| 271 timeout_suspended_ = false; | 271 timeout_suspended_ = false; |
| 272 ResetTimeout(); | 272 ResetTimeout(); |
| 273 } | 273 } |
| 274 } | 274 } |
| 275 | 275 |
| 276 int Task::OnTimeout() { | 276 int Task::OnTimeout() { |
| 277 // by default, we are finished after timing out | 277 // by default, we are finished after timing out |
| 278 return STATE_DONE; | 278 return STATE_DONE; |
| 279 } | 279 } |
| 280 | 280 |
| 281 } // namespace rtc | 281 } // namespace rtc |
| OLD | NEW |