[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 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 | |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 5 | #include "base/task/task_runner.h" |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 6 | |
tzik | 0352751 | 2017-02-08 12:29:47 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
Alex Clarke | 2b3cba8 | 2019-05-13 07:54:44 | [diff] [blame] | 9 | #include "base/bind.h" |
Hans Wennborg | c3cffa6 | 2020-04-27 10:09:12 | [diff] [blame] | 10 | #include "base/check.h" |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 12 | #include "base/memory/raw_ptr.h" |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 13 | #include "base/threading/post_task_and_reply_impl.h" |
David Sanders | 2799ee2 | 2022-02-03 05:01:48 | [diff] [blame^] | 14 | #include "base/time/time.h" |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 15 | |
| 16 | namespace base { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | // TODO(akalin): There's only one other implementation of |
Gabriel Charette | 3e2898f | 2019-05-01 14:55:01 | [diff] [blame] | 21 | // PostTaskAndReplyImpl in post_task.cc. Investigate whether it'll be |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 22 | // possible to merge the two. |
| 23 | class PostTaskAndReplyTaskRunner : public internal::PostTaskAndReplyImpl { |
| 24 | public: |
[email protected] | f3c697c5 | 2013-01-15 10:52:11 | [diff] [blame] | 25 | explicit PostTaskAndReplyTaskRunner(TaskRunner* destination); |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 26 | |
| 27 | private: |
Brett Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 28 | bool PostTask(const Location& from_here, OnceClosure task) override; |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 29 | |
| 30 | // Non-owning. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 31 | raw_ptr<TaskRunner> destination_; |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | PostTaskAndReplyTaskRunner::PostTaskAndReplyTaskRunner( |
| 35 | TaskRunner* destination) : destination_(destination) { |
| 36 | DCHECK(destination_); |
| 37 | } |
| 38 | |
Brett Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 39 | bool PostTaskAndReplyTaskRunner::PostTask(const Location& from_here, |
| 40 | OnceClosure task) { |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 41 | return destination_->PostTask(from_here, std::move(task)); |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | } // namespace |
| 45 | |
Brett Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 46 | bool TaskRunner::PostTask(const Location& from_here, OnceClosure task) { |
tzik | 070c8ffb | 2017-03-29 05:28:12 | [diff] [blame] | 47 | return PostDelayedTask(from_here, std::move(task), base::TimeDelta()); |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 48 | } |
| 49 | |
Brett Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 50 | bool TaskRunner::PostTaskAndReply(const Location& from_here, |
tzik | 6e42784 | 2017-04-05 10:13:21 | [diff] [blame] | 51 | OnceClosure task, |
| 52 | OnceClosure reply) { |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 53 | return PostTaskAndReplyTaskRunner(this).PostTaskAndReply( |
tzik | 0352751 | 2017-02-08 12:29:47 | [diff] [blame] | 54 | from_here, std::move(task), std::move(reply)); |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 55 | } |
| 56 | |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 57 | TaskRunner::TaskRunner() = default; |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 58 | |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 59 | TaskRunner::~TaskRunner() = default; |
[email protected] | 6b28d94 | 2012-02-15 01:43:19 | [diff] [blame] | 60 | |
| 61 | void TaskRunner::OnDestruct() const { |
| 62 | delete this; |
| 63 | } |
| 64 | |
| 65 | void TaskRunnerTraits::Destruct(const TaskRunner* task_runner) { |
| 66 | task_runner->OnDestruct(); |
| 67 | } |
| 68 | |
| 69 | } // namespace base |