1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/message_loop/message_loop_task_runner.h"
6
7 #include "base/location.h"
8 #include "base/logging.h"
9 #include "base/message_loop/incoming_task_queue.h"
10
11 namespace base {
12 namespace internal {
13
MessageLoopTaskRunner(scoped_refptr<IncomingTaskQueue> incoming_queue)14 MessageLoopTaskRunner::MessageLoopTaskRunner(
15 scoped_refptr<IncomingTaskQueue> incoming_queue)
16 : incoming_queue_(incoming_queue), valid_thread_id_(kInvalidThreadId) {
17 }
18
BindToCurrentThread()19 void MessageLoopTaskRunner::BindToCurrentThread() {
20 AutoLock lock(valid_thread_id_lock_);
21 DCHECK_EQ(kInvalidThreadId, valid_thread_id_);
22 valid_thread_id_ = PlatformThread::CurrentId();
23 }
24
PostDelayedTask(const tracked_objects::Location & from_here,const base::Closure & task,base::TimeDelta delay)25 bool MessageLoopTaskRunner::PostDelayedTask(
26 const tracked_objects::Location& from_here,
27 const base::Closure& task,
28 base::TimeDelta delay) {
29 DCHECK(!task.is_null()) << from_here.ToString();
30 return incoming_queue_->AddToIncomingQueue(from_here, task, delay, true);
31 }
32
PostNonNestableDelayedTask(const tracked_objects::Location & from_here,const base::Closure & task,base::TimeDelta delay)33 bool MessageLoopTaskRunner::PostNonNestableDelayedTask(
34 const tracked_objects::Location& from_here,
35 const base::Closure& task,
36 base::TimeDelta delay) {
37 DCHECK(!task.is_null()) << from_here.ToString();
38 return incoming_queue_->AddToIncomingQueue(from_here, task, delay, false);
39 }
40
RunsTasksOnCurrentThread() const41 bool MessageLoopTaskRunner::RunsTasksOnCurrentThread() const {
42 AutoLock lock(valid_thread_id_lock_);
43 return valid_thread_id_ == PlatformThread::CurrentId();
44 }
45
~MessageLoopTaskRunner()46 MessageLoopTaskRunner::~MessageLoopTaskRunner() {
47 }
48
49 } // namespace internal
50
51 } // namespace base
52