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 <utility>
8
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/message_loop/incoming_task_queue.h"
12
13 namespace base {
14 namespace internal {
15
MessageLoopTaskRunner(scoped_refptr<IncomingTaskQueue> incoming_queue)16 MessageLoopTaskRunner::MessageLoopTaskRunner(
17 scoped_refptr<IncomingTaskQueue> incoming_queue)
18 : incoming_queue_(incoming_queue), valid_thread_id_(kInvalidThreadId) {
19 }
20
BindToCurrentThread()21 void MessageLoopTaskRunner::BindToCurrentThread() {
22 AutoLock lock(valid_thread_id_lock_);
23 DCHECK_EQ(kInvalidThreadId, valid_thread_id_);
24 valid_thread_id_ = PlatformThread::CurrentId();
25 }
26
PostDelayedTask(const Location & from_here,OnceClosure task,base::TimeDelta delay)27 bool MessageLoopTaskRunner::PostDelayedTask(const Location& from_here,
28 OnceClosure task,
29 base::TimeDelta delay) {
30 DCHECK(!task.is_null()) << from_here.ToString();
31 return incoming_queue_->AddToIncomingQueue(from_here, std::move(task), delay,
32 Nestable::kNestable);
33 }
34
PostNonNestableDelayedTask(const Location & from_here,OnceClosure task,base::TimeDelta delay)35 bool MessageLoopTaskRunner::PostNonNestableDelayedTask(
36 const Location& from_here,
37 OnceClosure task,
38 base::TimeDelta delay) {
39 DCHECK(!task.is_null()) << from_here.ToString();
40 return incoming_queue_->AddToIncomingQueue(from_here, std::move(task), delay,
41 Nestable::kNonNestable);
42 }
43
RunsTasksInCurrentSequence() const44 bool MessageLoopTaskRunner::RunsTasksInCurrentSequence() const {
45 AutoLock lock(valid_thread_id_lock_);
46 return valid_thread_id_ == PlatformThread::CurrentId();
47 }
48
49 MessageLoopTaskRunner::~MessageLoopTaskRunner() = default;
50
51 } // namespace internal
52
53 } // namespace base
54