Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: base/threading/worker_pool_posix.cc

Issue 2726523002: Pass Callback to TaskRunner by value and consume it on invocation (1) (Closed)
Patch Set: erase Closure* Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/threading/worker_pool_posix.h ('k') | base/threading/worker_pool_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/threading/worker_pool_posix.h" 5 #include "base/threading/worker_pool_posix.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility>
10
9 #include "base/bind.h" 11 #include "base/bind.h"
10 #include "base/callback.h" 12 #include "base/callback.h"
11 #include "base/lazy_instance.h" 13 #include "base/lazy_instance.h"
12 #include "base/logging.h" 14 #include "base/logging.h"
13 #include "base/macros.h" 15 #include "base/macros.h"
14 #include "base/memory/ref_counted.h" 16 #include "base/memory/ref_counted.h"
15 #include "base/strings/stringprintf.h" 17 #include "base/strings/stringprintf.h"
16 #include "base/threading/platform_thread.h" 18 #include "base/threading/platform_thread.h"
17 #include "base/threading/thread_local.h" 19 #include "base/threading/thread_local.h"
18 #include "base/threading/worker_pool.h" 20 #include "base/threading/worker_pool.h"
(...skipping 21 matching lines...) Expand all
40 42
41 class WorkerPoolImpl { 43 class WorkerPoolImpl {
42 public: 44 public:
43 WorkerPoolImpl(); 45 WorkerPoolImpl();
44 46
45 // WorkerPoolImpl is only instantiated as a leaky LazyInstance, so the 47 // WorkerPoolImpl is only instantiated as a leaky LazyInstance, so the
46 // destructor is never called. 48 // destructor is never called.
47 ~WorkerPoolImpl() = delete; 49 ~WorkerPoolImpl() = delete;
48 50
49 void PostTask(const tracked_objects::Location& from_here, 51 void PostTask(const tracked_objects::Location& from_here,
50 const base::Closure& task, 52 base::Closure task,
51 bool task_is_slow); 53 bool task_is_slow);
52 54
53 private: 55 private:
54 scoped_refptr<base::PosixDynamicThreadPool> pool_; 56 scoped_refptr<base::PosixDynamicThreadPool> pool_;
55 }; 57 };
56 58
57 WorkerPoolImpl::WorkerPoolImpl() 59 WorkerPoolImpl::WorkerPoolImpl()
58 : pool_(new base::PosixDynamicThreadPool("WorkerPool", 60 : pool_(new base::PosixDynamicThreadPool("WorkerPool",
59 kIdleSecondsBeforeExit)) {} 61 kIdleSecondsBeforeExit)) {}
60 62
61 void WorkerPoolImpl::PostTask(const tracked_objects::Location& from_here, 63 void WorkerPoolImpl::PostTask(const tracked_objects::Location& from_here,
62 const base::Closure& task, 64 base::Closure task,
63 bool task_is_slow) { 65 bool task_is_slow) {
64 pool_->PostTask(from_here, task); 66 pool_->PostTask(from_here, std::move(task));
65 } 67 }
66 68
67 base::LazyInstance<WorkerPoolImpl>::Leaky g_lazy_worker_pool = 69 base::LazyInstance<WorkerPoolImpl>::Leaky g_lazy_worker_pool =
68 LAZY_INSTANCE_INITIALIZER; 70 LAZY_INSTANCE_INITIALIZER;
69 71
70 class WorkerThread : public PlatformThread::Delegate { 72 class WorkerThread : public PlatformThread::Delegate {
71 public: 73 public:
72 WorkerThread(const std::string& name_prefix, 74 WorkerThread(const std::string& name_prefix,
73 base::PosixDynamicThreadPool* pool) 75 base::PosixDynamicThreadPool* pool)
74 : name_prefix_(name_prefix), pool_(pool) {} 76 : name_prefix_(name_prefix), pool_(pool) {}
(...skipping 30 matching lines...) Expand all
105 } 107 }
106 108
107 // The WorkerThread is non-joinable, so it deletes itself. 109 // The WorkerThread is non-joinable, so it deletes itself.
108 delete this; 110 delete this;
109 } 111 }
110 112
111 } // namespace 113 } // namespace
112 114
113 // static 115 // static
114 bool WorkerPool::PostTask(const tracked_objects::Location& from_here, 116 bool WorkerPool::PostTask(const tracked_objects::Location& from_here,
115 const base::Closure& task, 117 base::Closure task,
116 bool task_is_slow) { 118 bool task_is_slow) {
117 g_lazy_worker_pool.Pointer()->PostTask(from_here, task, task_is_slow); 119 g_lazy_worker_pool.Pointer()->PostTask(from_here, std::move(task),
120 task_is_slow);
118 return true; 121 return true;
119 } 122 }
120 123
121 // static 124 // static
122 bool WorkerPool::RunsTasksOnCurrentThread() { 125 bool WorkerPool::RunsTasksOnCurrentThread() {
123 return g_worker_pool_running_on_this_thread.Get().Get(); 126 return g_worker_pool_running_on_this_thread.Get().Get();
124 } 127 }
125 128
126 PosixDynamicThreadPool::PosixDynamicThreadPool(const std::string& name_prefix, 129 PosixDynamicThreadPool::PosixDynamicThreadPool(const std::string& name_prefix,
127 int idle_seconds_before_exit) 130 int idle_seconds_before_exit)
128 : name_prefix_(name_prefix), 131 : name_prefix_(name_prefix),
129 idle_seconds_before_exit_(idle_seconds_before_exit), 132 idle_seconds_before_exit_(idle_seconds_before_exit),
130 pending_tasks_available_cv_(&lock_), 133 pending_tasks_available_cv_(&lock_),
131 num_idle_threads_(0) {} 134 num_idle_threads_(0) {}
132 135
133 PosixDynamicThreadPool::~PosixDynamicThreadPool() { 136 PosixDynamicThreadPool::~PosixDynamicThreadPool() {
134 while (!pending_tasks_.empty()) 137 while (!pending_tasks_.empty())
135 pending_tasks_.pop(); 138 pending_tasks_.pop();
136 } 139 }
137 140
138 void PosixDynamicThreadPool::PostTask( 141 void PosixDynamicThreadPool::PostTask(
139 const tracked_objects::Location& from_here, 142 const tracked_objects::Location& from_here,
140 const base::Closure& task) { 143 base::Closure task) {
141 PendingTask pending_task(from_here, task); 144 PendingTask pending_task(from_here, std::move(task));
142 AddTask(&pending_task); 145 AddTask(&pending_task);
143 } 146 }
144 147
145 void PosixDynamicThreadPool::AddTask(PendingTask* pending_task) { 148 void PosixDynamicThreadPool::AddTask(PendingTask* pending_task) {
146 AutoLock locked(lock_); 149 AutoLock locked(lock_);
147 150
148 pending_tasks_.push(std::move(*pending_task)); 151 pending_tasks_.push(std::move(*pending_task));
149 152
150 // We have enough worker threads. 153 // We have enough worker threads.
151 if (static_cast<size_t>(num_idle_threads_) >= pending_tasks_.size()) { 154 if (static_cast<size_t>(num_idle_threads_) >= pending_tasks_.size()) {
(...skipping 24 matching lines...) Expand all
176 return PendingTask(FROM_HERE, base::Closure()); 179 return PendingTask(FROM_HERE, base::Closure());
177 } 180 }
178 } 181 }
179 182
180 PendingTask pending_task = std::move(pending_tasks_.front()); 183 PendingTask pending_task = std::move(pending_tasks_.front());
181 pending_tasks_.pop(); 184 pending_tasks_.pop();
182 return pending_task; 185 return pending_task;
183 } 186 }
184 187
185 } // namespace base 188 } // namespace base
OLDNEW
« no previous file with comments | « base/threading/worker_pool_posix.h ('k') | base/threading/worker_pool_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698