| 6 |
| 7 | #include "base/logging.h" |
| 8 | #include "base/task/scoped_set_task_priority_for_current_thread.h" |
| 9 | #include "base/task/task_traits.h" |
| 10 | #include "base/task/thread_pool/thread_pool_impl.h" |
| 11 | #include "base/task/thread_pool/thread_pool_instance.h" |
| 12 | #include "base/threading/post_task_and_reply_impl.h" |
| 13 |
| 14 | namespace base { |
| 15 |
| 16 | namespace { |
| 17 |
| 18 | class PostTaskAndReplyWithTraitsTaskRunner |
| 19 | : public internal::PostTaskAndReplyImpl { |
| 20 | public: |
| 21 | explicit PostTaskAndReplyWithTraitsTaskRunner(const TaskTraits& traits) |
| 22 | : traits_(traits) {} |
| 23 |
| 24 | private: |
| 25 | bool PostTask(const Location& from_here, OnceClosure task) override { |
| 26 | ThreadPool::PostTask(from_here, traits_, std::move(task)); |
| 27 | return true; |
| 28 | } |
| 29 |
| 30 | const TaskTraits traits_; |
| 31 | }; |
| 32 |
| 33 | internal::ThreadPoolImpl* GetThreadPoolImpl() { |
| 34 | auto* instance = ThreadPoolInstance::Get(); |
| 35 | DCHECK(instance) |
| 36 | << "Ref. Prerequisite section of base/task/thread_pool.h.\n" |
| 37 | "Hint: if this is in a unit test, you're likely merely missing a " |
| 38 | "base::test::TaskEnvironment member in your fixture (or your fixture " |
| 39 | "is using a base::test::SingleThreadTaskEnvironment and now needs a " |
| 40 | "full base::test::TaskEnvironment).\n"; |
| 41 | return static_cast<internal::ThreadPoolImpl*>(instance); |
| 42 | } |
| 43 |
| 44 | } // namespace |
| 45 |
| 46 | // static |
| 47 | bool ThreadPool::PostTask(const Location& from_here, OnceClosure task) { |
| 48 | return ThreadPool::PostDelayedTask(from_here, std::move(task), TimeDelta()); |
| 49 | } |
| 50 |
| 51 | // static |
| 52 | bool ThreadPool::PostDelayedTask(const Location& from_here, |
| 53 | OnceClosure task, |
| 54 | TimeDelta delay) { |
| 55 | return ThreadPool::PostDelayedTask(from_here, {}, std::move(task), delay); |
| 56 | } |
| 57 |
| 58 | // static |
| 59 | bool ThreadPool::PostTaskAndReply(const Location& from_here, |
| 60 | OnceClosure task, |
| 61 | OnceClosure reply) { |
| 62 | return ThreadPool::PostTaskAndReply(from_here, {}, std::move(task), |
| 63 | std::move(reply)); |
| 64 | } |
| 65 |
| 66 | // static |
| 67 | bool ThreadPool::PostTask(const Location& from_here, |
| 68 | const TaskTraits& traits, |
| 69 | OnceClosure task) { |
| 70 | return ThreadPool::PostDelayedTask(from_here, traits, std::move(task), |
| 71 | TimeDelta()); |
| 72 | } |
| 73 |
| 74 | // static |
| 75 | bool ThreadPool::PostDelayedTask(const Location& from_here, |
| 76 | const TaskTraits& traits, |
| 77 | OnceClosure task, |
| 78 | TimeDelta delay) { |
| 79 | return GetThreadPoolImpl()->PostDelayedTask(from_here, traits, |
| 80 | std::move(task), delay); |
| 81 | } |
| 82 |
| 83 | // static |
| 84 | bool ThreadPool::PostTaskAndReply(const Location& from_here, |
| 85 | const TaskTraits& traits, |
| 86 | OnceClosure task, |
| 87 | OnceClosure reply) { |
| 88 | return PostTaskAndReplyWithTraitsTaskRunner(traits).PostTaskAndReply( |
| 89 | from_here, std::move(task), std::move(reply)); |
| 90 | } |
| 91 |
| 92 | // static |
| 93 | scoped_refptr<TaskRunner> ThreadPool::CreateTaskRunner( |
| 94 | const TaskTraits& traits) { |
| 95 | return GetThreadPoolImpl()->CreateTaskRunner(traits); |
| 96 | } |
| 97 |
| 98 | // static |
| 99 | scoped_refptr<SequencedTaskRunner> ThreadPool::CreateSequencedTaskRunner( |
| 100 | const TaskTraits& traits) { |
| 101 | return GetThreadPoolImpl()->CreateSequencedTaskRunner(traits); |
| 102 | } |
| 103 |
| 104 | // static |
| 105 | scoped_refptr<UpdateableSequencedTaskRunner> |
| 106 | ThreadPool::CreateUpdateableSequencedTaskRunner(const TaskTraits& traits) { |
| 107 | return GetThreadPoolImpl()->CreateUpdateableSequencedTaskRunner(traits); |
| 108 | } |
| 109 |
| 110 | // static |
| 111 | scoped_refptr<SingleThreadTaskRunner> ThreadPool::CreateSingleThreadTaskRunner( |
| 112 | const TaskTraits& traits, |
| 113 | SingleThreadTaskRunnerThreadMode thread_mode) { |
| 114 | return GetThreadPoolImpl()->CreateSingleThreadTaskRunner(traits, thread_mode); |
| 115 | } |
| 116 |
| 117 | #if defined(OS_WIN) |
| 118 | // static |
| 119 | scoped_refptr<SingleThreadTaskRunner> ThreadPool::CreateCOMSTATaskRunner( |
| 120 | const TaskTraits& traits, |
| 121 | SingleThreadTaskRunnerThreadMode thread_mode) { |
| 122 | return GetThreadPoolImpl()->CreateCOMSTATaskRunner(traits, thread_mode); |
| 123 | } |
| 124 | #endif // defined(OS_WIN) |
| 125 |
| 126 | } // namespace base |
| 127 |