task_runner.cc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #include "base/task/task_runner.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/check.h"
  8. #include "base/compiler_specific.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/threading/post_task_and_reply_impl.h"
  11. #include "base/time/time.h"
  12. namespace base {
  13. namespace {
  14. // TODO(akalin): There's only one other implementation of
  15. // PostTaskAndReplyImpl in post_task.cc. Investigate whether it'll be
  16. // possible to merge the two.
  17. class PostTaskAndReplyTaskRunner : public internal::PostTaskAndReplyImpl {
  18. public:
  19. explicit PostTaskAndReplyTaskRunner(TaskRunner* destination);
  20. private:
  21. bool PostTask(const Location& from_here, OnceClosure task) override;
  22. // Non-owning.
  23. raw_ptr<TaskRunner> destination_;
  24. };
  25. PostTaskAndReplyTaskRunner::PostTaskAndReplyTaskRunner(
  26. TaskRunner* destination) : destination_(destination) {
  27. DCHECK(destination_);
  28. }
  29. bool PostTaskAndReplyTaskRunner::PostTask(const Location& from_here,
  30. OnceClosure task) {
  31. return destination_->PostTask(from_here, std::move(task));
  32. }
  33. } // namespace
  34. bool TaskRunner::PostTask(const Location& from_here, OnceClosure task) {
  35. return PostDelayedTask(from_here, std::move(task), base::TimeDelta());
  36. }
  37. bool TaskRunner::PostTaskAndReply(const Location& from_here,
  38. OnceClosure task,
  39. OnceClosure reply) {
  40. return PostTaskAndReplyTaskRunner(this).PostTaskAndReply(
  41. from_here, std::move(task), std::move(reply));
  42. }
  43. TaskRunner::TaskRunner() = default;
  44. TaskRunner::~TaskRunner() = default;
  45. void TaskRunner::OnDestruct() const {
  46. delete this;
  47. }
  48. void TaskRunnerTraits::Destruct(const TaskRunner* task_runner) {
  49. task_runner->OnDestruct();
  50. }
  51. } // namespace base