| OLD | NEW |
| (Empty) |
| 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/threading/sequenced_worker_pool.h" | |
| 6 | |
| 7 #include <list> | |
| 8 #include <map> | |
| 9 #include <set> | |
| 10 #include <utility> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/atomic_sequence_num.h" | |
| 14 #include "base/callback.h" | |
| 15 #include "base/compiler_specific.h" | |
| 16 #include "base/critical_closure.h" | |
| 17 #include "base/lazy_instance.h" | |
| 18 #include "base/logging.h" | |
| 19 #include "base/memory/linked_ptr.h" | |
| 20 #include "base/stl_util.h" | |
| 21 #include "base/strings/stringprintf.h" | |
| 22 #include "base/synchronization/condition_variable.h" | |
| 23 #include "base/synchronization/lock.h" | |
| 24 #include "base/thread_task_runner_handle.h" | |
| 25 #include "base/threading/platform_thread.h" | |
| 26 #include "base/threading/simple_thread.h" | |
| 27 #include "base/threading/thread_local.h" | |
| 28 #include "base/threading/thread_restrictions.h" | |
| 29 #include "base/time/time.h" | |
| 30 #include "base/trace_event/trace_event.h" | |
| 31 #include "base/tracked_objects.h" | |
| 32 | |
| 33 #if defined(OS_MACOSX) | |
| 34 #include "base/mac/scoped_nsautorelease_pool.h" | |
| 35 #elif defined(OS_WIN) | |
| 36 #include "base/win/scoped_com_initializer.h" | |
| 37 #endif | |
| 38 | |
| 39 #if !defined(OS_NACL) | |
| 40 #include "base/metrics/histogram.h" | |
| 41 #endif | |
| 42 | |
| 43 namespace base { | |
| 44 | |
| 45 namespace { | |
| 46 | |
| 47 struct SequencedTask : public TrackingInfo { | |
| 48 SequencedTask() | |
| 49 : sequence_token_id(0), | |
| 50 trace_id(0), | |
| 51 sequence_task_number(0), | |
| 52 shutdown_behavior(SequencedWorkerPool::BLOCK_SHUTDOWN) {} | |
| 53 | |
| 54 explicit SequencedTask(const tracked_objects::Location& from_here) | |
| 55 : base::TrackingInfo(from_here, TimeTicks()), | |
| 56 sequence_token_id(0), | |
| 57 trace_id(0), | |
| 58 sequence_task_number(0), | |
| 59 shutdown_behavior(SequencedWorkerPool::BLOCK_SHUTDOWN) {} | |
| 60 | |
| 61 ~SequencedTask() {} | |
| 62 | |
| 63 int sequence_token_id; | |
| 64 int trace_id; | |
| 65 int64 sequence_task_number; | |
| 66 SequencedWorkerPool::WorkerShutdown shutdown_behavior; | |
| 67 tracked_objects::Location posted_from; | |
| 68 Closure task; | |
| 69 | |
| 70 // Non-delayed tasks and delayed tasks are managed together by time-to-run | |
| 71 // order. We calculate the time by adding the posted time and the given delay. | |
| 72 TimeTicks time_to_run; | |
| 73 }; | |
| 74 | |
| 75 struct SequencedTaskLessThan { | |
| 76 public: | |
| 77 bool operator()(const SequencedTask& lhs, const SequencedTask& rhs) const { | |
| 78 if (lhs.time_to_run < rhs.time_to_run) | |
| 79 return true; | |
| 80 | |
| 81 if (lhs.time_to_run > rhs.time_to_run) | |
| 82 return false; | |
| 83 | |
| 84 // If the time happen to match, then we use the sequence number to decide. | |
| 85 return lhs.sequence_task_number < rhs.sequence_task_number; | |
| 86 } | |
| 87 }; | |
| 88 | |
| 89 // SequencedWorkerPoolTaskRunner --------------------------------------------- | |
| 90 // A TaskRunner which posts tasks to a SequencedWorkerPool with a | |
| 91 // fixed ShutdownBehavior. | |
| 92 // | |
| 93 // Note that this class is RefCountedThreadSafe (inherited from TaskRunner). | |
| 94 class SequencedWorkerPoolTaskRunner : public TaskRunner { | |
| 95 public: | |
| 96 SequencedWorkerPoolTaskRunner( | |
| 97 const scoped_refptr<SequencedWorkerPool>& pool, | |
| 98 SequencedWorkerPool::WorkerShutdown shutdown_behavior); | |
| 99 | |
| 100 // TaskRunner implementation | |
| 101 bool PostDelayedTask(const tracked_objects::Location& from_here, | |
| 102 const Closure& task, | |
| 103 TimeDelta delay) override; | |
| 104 bool RunsTasksOnCurrentThread() const override; | |
| 105 | |
| 106 private: | |
| 107 ~SequencedWorkerPoolTaskRunner() override; | |
| 108 | |
| 109 const scoped_refptr<SequencedWorkerPool> pool_; | |
| 110 | |
| 111 const SequencedWorkerPool::WorkerShutdown shutdown_behavior_; | |
| 112 | |
| 113 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPoolTaskRunner); | |
| 114 }; | |
| 115 | |
| 116 SequencedWorkerPoolTaskRunner::SequencedWorkerPoolTaskRunner( | |
| 117 const scoped_refptr<SequencedWorkerPool>& pool, | |
| 118 SequencedWorkerPool::WorkerShutdown shutdown_behavior) | |
| 119 : pool_(pool), | |
| 120 shutdown_behavior_(shutdown_behavior) { | |
| 121 } | |
| 122 | |
| 123 SequencedWorkerPoolTaskRunner::~SequencedWorkerPoolTaskRunner() { | |
| 124 } | |
| 125 | |
| 126 bool SequencedWorkerPoolTaskRunner::PostDelayedTask( | |
| 127 const tracked_objects::Location& from_here, | |
| 128 const Closure& task, | |
| 129 TimeDelta delay) { | |
| 130 if (delay == TimeDelta()) { | |
| 131 return pool_->PostWorkerTaskWithShutdownBehavior( | |
| 132 from_here, task, shutdown_behavior_); | |
| 133 } | |
| 134 return pool_->PostDelayedWorkerTask(from_here, task, delay); | |
| 135 } | |
| 136 | |
| 137 bool SequencedWorkerPoolTaskRunner::RunsTasksOnCurrentThread() const { | |
| 138 return pool_->RunsTasksOnCurrentThread(); | |
| 139 } | |
| 140 | |
| 141 // SequencedWorkerPoolSequencedTaskRunner ------------------------------------ | |
| 142 // A SequencedTaskRunner which posts tasks to a SequencedWorkerPool with a | |
| 143 // fixed sequence token. | |
| 144 // | |
| 145 // Note that this class is RefCountedThreadSafe (inherited from TaskRunner). | |
| 146 class SequencedWorkerPoolSequencedTaskRunner : public SequencedTaskRunner { | |
| 147 public: | |
| 148 SequencedWorkerPoolSequencedTaskRunner( | |
| 149 const scoped_refptr<SequencedWorkerPool>& pool, | |
| 150 SequencedWorkerPool::SequenceToken token, | |
| 151 SequencedWorkerPool::WorkerShutdown shutdown_behavior); | |
| 152 | |
| 153 // TaskRunner implementation | |
| 154 bool PostDelayedTask(const tracked_objects::Location& from_here, | |
| 155 const Closure& task, | |
| 156 TimeDelta delay) override; | |
| 157 bool RunsTasksOnCurrentThread() const override; | |
| 158 | |
| 159 // SequencedTaskRunner implementation | |
| 160 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | |
| 161 const Closure& task, | |
| 162 TimeDelta delay) override; | |
| 163 | |
| 164 private: | |
| 165 ~SequencedWorkerPoolSequencedTaskRunner() override; | |
| 166 | |
| 167 const scoped_refptr<SequencedWorkerPool> pool_; | |
| 168 | |
| 169 const SequencedWorkerPool::SequenceToken token_; | |
| 170 | |
| 171 const SequencedWorkerPool::WorkerShutdown shutdown_behavior_; | |
| 172 | |
| 173 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPoolSequencedTaskRunner); | |
| 174 }; | |
| 175 | |
| 176 SequencedWorkerPoolSequencedTaskRunner::SequencedWorkerPoolSequencedTaskRunner( | |
| 177 const scoped_refptr<SequencedWorkerPool>& pool, | |
| 178 SequencedWorkerPool::SequenceToken token, | |
| 179 SequencedWorkerPool::WorkerShutdown shutdown_behavior) | |
| 180 : pool_(pool), | |
| 181 token_(token), | |
| 182 shutdown_behavior_(shutdown_behavior) { | |
| 183 } | |
| 184 | |
| 185 SequencedWorkerPoolSequencedTaskRunner:: | |
| 186 ~SequencedWorkerPoolSequencedTaskRunner() { | |
| 187 } | |
| 188 | |
| 189 bool SequencedWorkerPoolSequencedTaskRunner::PostDelayedTask( | |
| 190 const tracked_objects::Location& from_here, | |
| 191 const Closure& task, | |
| 192 TimeDelta delay) { | |
| 193 if (delay == TimeDelta()) { | |
| 194 return pool_->PostSequencedWorkerTaskWithShutdownBehavior( | |
| 195 token_, from_here, task, shutdown_behavior_); | |
| 196 } | |
| 197 return pool_->PostDelayedSequencedWorkerTask(token_, from_here, task, delay); | |
| 198 } | |
| 199 | |
| 200 bool SequencedWorkerPoolSequencedTaskRunner::RunsTasksOnCurrentThread() const { | |
| 201 return pool_->IsRunningSequenceOnCurrentThread(token_); | |
| 202 } | |
| 203 | |
| 204 bool SequencedWorkerPoolSequencedTaskRunner::PostNonNestableDelayedTask( | |
| 205 const tracked_objects::Location& from_here, | |
| 206 const Closure& task, | |
| 207 TimeDelta delay) { | |
| 208 // There's no way to run nested tasks, so simply forward to | |
| 209 // PostDelayedTask. | |
| 210 return PostDelayedTask(from_here, task, delay); | |
| 211 } | |
| 212 | |
| 213 // Create a process-wide unique ID to represent this task in trace events. This | |
| 214 // will be mangled with a Process ID hash to reduce the likelyhood of colliding | |
| 215 // with MessageLoop pointers on other processes. | |
| 216 uint64 GetTaskTraceID(const SequencedTask& task, | |
| 217 void* pool) { | |
| 218 return (static_cast<uint64>(task.trace_id) << 32) | | |
| 219 static_cast<uint64>(reinterpret_cast<intptr_t>(pool)); | |
| 220 } | |
| 221 | |
| 222 base::LazyInstance<base::ThreadLocalPointer< | |
| 223 SequencedWorkerPool::SequenceToken> >::Leaky g_lazy_tls_ptr = | |
| 224 LAZY_INSTANCE_INITIALIZER; | |
| 225 | |
| 226 } // namespace | |
| 227 | |
| 228 // Worker --------------------------------------------------------------------- | |
| 229 | |
| 230 class SequencedWorkerPool::Worker : public SimpleThread { | |
| 231 public: | |
| 232 // Hold a (cyclic) ref to |worker_pool|, since we want to keep it | |
| 233 // around as long as we are running. | |
| 234 Worker(const scoped_refptr<SequencedWorkerPool>& worker_pool, | |
| 235 int thread_number, | |
| 236 const std::string& thread_name_prefix); | |
| 237 ~Worker() override; | |
| 238 | |
| 239 // SimpleThread implementation. This actually runs the background thread. | |
| 240 void Run() override; | |
| 241 | |
| 242 // Indicates that a task is about to be run. The parameters provide | |
| 243 // additional metainformation about the task being run. | |
| 244 void set_running_task_info(SequenceToken token, | |
| 245 WorkerShutdown shutdown_behavior) { | |
| 246 is_processing_task_ = true; | |
| 247 task_sequence_token_ = token; | |
| 248 task_shutdown_behavior_ = shutdown_behavior; | |
| 249 } | |
| 250 | |
| 251 // Indicates that the task has finished running. | |
| 252 void reset_running_task_info() { is_processing_task_ = false; } | |
| 253 | |
| 254 // Whether the worker is processing a task. | |
| 255 bool is_processing_task() { return is_processing_task_; } | |
| 256 | |
| 257 SequenceToken task_sequence_token() const { | |
| 258 DCHECK(is_processing_task_); | |
| 259 return task_sequence_token_; | |
| 260 } | |
| 261 | |
| 262 WorkerShutdown task_shutdown_behavior() const { | |
| 263 DCHECK(is_processing_task_); | |
| 264 return task_shutdown_behavior_; | |
| 265 } | |
| 266 | |
| 267 private: | |
| 268 scoped_refptr<SequencedWorkerPool> worker_pool_; | |
| 269 // The sequence token of the task being processed. Only valid when | |
| 270 // is_processing_task_ is true. | |
| 271 SequenceToken task_sequence_token_; | |
| 272 // The shutdown behavior of the task being processed. Only valid when | |
| 273 // is_processing_task_ is true. | |
| 274 WorkerShutdown task_shutdown_behavior_; | |
| 275 // Whether the Worker is processing a task. | |
| 276 bool is_processing_task_; | |
| 277 | |
| 278 DISALLOW_COPY_AND_ASSIGN(Worker); | |
| 279 }; | |
| 280 | |
| 281 // Inner ---------------------------------------------------------------------- | |
| 282 | |
| 283 class SequencedWorkerPool::Inner { | |
| 284 public: | |
| 285 // Take a raw pointer to |worker| to avoid cycles (since we're owned | |
| 286 // by it). | |
| 287 Inner(SequencedWorkerPool* worker_pool, size_t max_threads, | |
| 288 const std::string& thread_name_prefix, | |
| 289 TestingObserver* observer); | |
| 290 | |
| 291 ~Inner(); | |
| 292 | |
| 293 SequenceToken GetSequenceToken(); | |
| 294 | |
| 295 SequenceToken GetNamedSequenceToken(const std::string& name); | |
| 296 | |
| 297 // This function accepts a name and an ID. If the name is null, the | |
| 298 // token ID is used. This allows us to implement the optional name lookup | |
| 299 // from a single function without having to enter the lock a separate time. | |
| 300 bool PostTask(const std::string* optional_token_name, | |
| 301 SequenceToken sequence_token, | |
| 302 WorkerShutdown shutdown_behavior, | |
| 303 const tracked_objects::Location& from_here, | |
| 304 const Closure& task, | |
| 305 TimeDelta delay); | |
| 306 | |
| 307 bool RunsTasksOnCurrentThread() const; | |
| 308 | |
| 309 bool IsRunningSequenceOnCurrentThread(SequenceToken sequence_token) const; | |
| 310 | |
| 311 void CleanupForTesting(); | |
| 312 | |
| 313 void SignalHasWorkForTesting(); | |
| 314 | |
| 315 int GetWorkSignalCountForTesting() const; | |
| 316 | |
| 317 void Shutdown(int max_blocking_tasks_after_shutdown); | |
| 318 | |
| 319 bool IsShutdownInProgress(); | |
| 320 | |
| 321 // Runs the worker loop on the background thread. | |
| 322 void ThreadLoop(Worker* this_worker); | |
| 323 | |
| 324 private: | |
| 325 enum GetWorkStatus { | |
| 326 GET_WORK_FOUND, | |
| 327 GET_WORK_NOT_FOUND, | |
| 328 GET_WORK_WAIT, | |
| 329 }; | |
| 330 | |
| 331 enum CleanupState { | |
| 332 CLEANUP_REQUESTED, | |
| 333 CLEANUP_STARTING, | |
| 334 CLEANUP_RUNNING, | |
| 335 CLEANUP_FINISHING, | |
| 336 CLEANUP_DONE, | |
| 337 }; | |
| 338 | |
| 339 // Called from within the lock, this converts the given token name into a | |
| 340 // token ID, creating a new one if necessary. | |
| 341 int LockedGetNamedTokenID(const std::string& name); | |
| 342 | |
| 343 // Called from within the lock, this returns the next sequence task number. | |
| 344 int64 LockedGetNextSequenceTaskNumber(); | |
| 345 | |
| 346 // Gets new task. There are 3 cases depending on the return value: | |
| 347 // | |
| 348 // 1) If the return value is |GET_WORK_FOUND|, |task| is filled in and should | |
| 349 // be run immediately. | |
| 350 // 2) If the return value is |GET_WORK_NOT_FOUND|, there are no tasks to run, | |
| 351 // and |task| is not filled in. In this case, the caller should wait until | |
| 352 // a task is posted. | |
| 353 // 3) If the return value is |GET_WORK_WAIT|, there are no tasks to run | |
| 354 // immediately, and |task| is not filled in. Likewise, |wait_time| is | |
| 355 // filled in the time to wait until the next task to run. In this case, the | |
| 356 // caller should wait the time. | |
| 357 // | |
| 358 // In any case, the calling code should clear the given | |
| 359 // delete_these_outside_lock vector the next time the lock is released. | |
| 360 // See the implementation for a more detailed description. | |
| 361 GetWorkStatus GetWork(SequencedTask* task, | |
| 362 TimeDelta* wait_time, | |
| 363 std::vector<Closure>* delete_these_outside_lock); | |
| 364 | |
| 365 void HandleCleanup(); | |
| 366 | |
| 367 // Peforms init and cleanup around running the given task. WillRun... | |
| 368 // returns the value from PrepareToStartAdditionalThreadIfNecessary. | |
| 369 // The calling code should call FinishStartingAdditionalThread once the | |
| 370 // lock is released if the return values is nonzero. | |
| 371 int WillRunWorkerTask(const SequencedTask& task); | |
| 372 void DidRunWorkerTask(const SequencedTask& task); | |
| 373 | |
| 374 // Returns true if there are no threads currently running the given | |
| 375 // sequence token. | |
| 376 bool IsSequenceTokenRunnable(int sequence_token_id) const; | |
| 377 | |
| 378 // Checks if all threads are busy and the addition of one more could run an | |
| 379 // additional task waiting in the queue. This must be called from within | |
| 380 // the lock. | |
| 381 // | |
| 382 // If another thread is helpful, this will mark the thread as being in the | |
| 383 // process of starting and returns the index of the new thread which will be | |
| 384 // 0 or more. The caller should then call FinishStartingAdditionalThread to | |
| 385 // complete initialization once the lock is released. | |
| 386 // | |
| 387 // If another thread is not necessary, returne 0; | |
| 388 // | |
| 389 // See the implementedion for more. | |
| 390 int PrepareToStartAdditionalThreadIfHelpful(); | |
| 391 | |
| 392 // The second part of thread creation after | |
| 393 // PrepareToStartAdditionalThreadIfHelpful with the thread number it | |
| 394 // generated. This actually creates the thread and should be called outside | |
| 395 // the lock to avoid blocking important work starting a thread in the lock. | |
| 396 void FinishStartingAdditionalThread(int thread_number); | |
| 397 | |
| 398 // Signal |has_work_| and increment |has_work_signal_count_|. | |
| 399 void SignalHasWork(); | |
| 400 | |
| 401 // Checks whether there is work left that's blocking shutdown. Must be | |
| 402 // called inside the lock. | |
| 403 bool CanShutdown() const; | |
| 404 | |
| 405 SequencedWorkerPool* const worker_pool_; | |
| 406 | |
| 407 // The last sequence number used. Managed by GetSequenceToken, since this | |
| 408 // only does threadsafe increment operations, you do not need to hold the | |
| 409 // lock. This is class-static to make SequenceTokens issued by | |
| 410 // GetSequenceToken unique across SequencedWorkerPool instances. | |
| 411 static base::StaticAtomicSequenceNumber g_last_sequence_number_; | |
| 412 | |
| 413 // This lock protects |everything in this class|. Do not read or modify | |
| 414 // anything without holding this lock. Do not block while holding this | |
| 415 // lock. | |
| 416 mutable Lock lock_; | |
| 417 | |
| 418 // Condition variable that is waited on by worker threads until new | |
| 419 // tasks are posted or shutdown starts. | |
| 420 ConditionVariable has_work_cv_; | |
| 421 | |
| 422 // Condition variable that is waited on by non-worker threads (in | |
| 423 // Shutdown()) until CanShutdown() goes to true. | |
| 424 ConditionVariable can_shutdown_cv_; | |
| 425 | |
| 426 // The maximum number of worker threads we'll create. | |
| 427 const size_t max_threads_; | |
| 428 | |
| 429 const std::string thread_name_prefix_; | |
| 430 | |
| 431 // Associates all known sequence token names with their IDs. | |
| 432 std::map<std::string, int> named_sequence_tokens_; | |
| 433 | |
| 434 // Owning pointers to all threads we've created so far, indexed by | |
| 435 // ID. Since we lazily create threads, this may be less than | |
| 436 // max_threads_ and will be initially empty. | |
| 437 typedef std::map<PlatformThreadId, linked_ptr<Worker> > ThreadMap; | |
| 438 ThreadMap threads_; | |
| 439 | |
| 440 // Set to true when we're in the process of creating another thread. | |
| 441 // See PrepareToStartAdditionalThreadIfHelpful for more. | |
| 442 bool thread_being_created_; | |
| 443 | |
| 444 // Number of threads currently waiting for work. | |
| 445 size_t waiting_thread_count_; | |
| 446 | |
| 447 // Number of threads currently running tasks that have the BLOCK_SHUTDOWN | |
| 448 // or SKIP_ON_SHUTDOWN flag set. | |
| 449 size_t blocking_shutdown_thread_count_; | |
| 450 | |
| 451 // A set of all pending tasks in time-to-run order. These are tasks that are | |
| 452 // either waiting for a thread to run on, waiting for their time to run, | |
| 453 // or blocked on a previous task in their sequence. We have to iterate over | |
| 454 // the tasks by time-to-run order, so we use the set instead of the | |
| 455 // traditional priority_queue. | |
| 456 typedef std::set<SequencedTask, SequencedTaskLessThan> PendingTaskSet; | |
| 457 PendingTaskSet pending_tasks_; | |
| 458 | |
| 459 // The next sequence number for a new sequenced task. | |
| 460 int64 next_sequence_task_number_; | |
| 461 | |
| 462 // Number of tasks in the pending_tasks_ list that are marked as blocking | |
| 463 // shutdown. | |
| 464 size_t blocking_shutdown_pending_task_count_; | |
| 465 | |
| 466 // Lists all sequence tokens currently executing. | |
| 467 std::set<int> current_sequences_; | |
| 468 | |
| 469 // An ID for each posted task to distinguish the task from others in traces. | |
| 470 int trace_id_; | |
| 471 | |
| 472 // Set when Shutdown is called and no further tasks should be | |
| 473 // allowed, though we may still be running existing tasks. | |
| 474 bool shutdown_called_; | |
| 475 | |
| 476 // The number of new BLOCK_SHUTDOWN tasks that may be posted after Shudown() | |
| 477 // has been called. | |
| 478 int max_blocking_tasks_after_shutdown_; | |
| 479 | |
| 480 // State used to cleanup for testing, all guarded by lock_. | |
| 481 CleanupState cleanup_state_; | |
| 482 size_t cleanup_idlers_; | |
| 483 ConditionVariable cleanup_cv_; | |
| 484 | |
| 485 TestingObserver* const testing_observer_; | |
| 486 | |
| 487 DISALLOW_COPY_AND_ASSIGN(Inner); | |
| 488 }; | |
| 489 | |
| 490 // Worker definitions --------------------------------------------------------- | |
| 491 | |
| 492 SequencedWorkerPool::Worker::Worker( | |
| 493 const scoped_refptr<SequencedWorkerPool>& worker_pool, | |
| 494 int thread_number, | |
| 495 const std::string& prefix) | |
| 496 : SimpleThread(prefix + StringPrintf("Worker%d", thread_number)), | |
| 497 worker_pool_(worker_pool), | |
| 498 task_shutdown_behavior_(BLOCK_SHUTDOWN), | |
| 499 is_processing_task_(false) { | |
| 500 Start(); | |
| 501 } | |
| 502 | |
| 503 SequencedWorkerPool::Worker::~Worker() { | |
| 504 } | |
| 505 | |
| 506 void SequencedWorkerPool::Worker::Run() { | |
| 507 #if defined(OS_WIN) | |
| 508 win::ScopedCOMInitializer com_initializer; | |
| 509 #endif | |
| 510 | |
| 511 // Store a pointer to the running sequence in thread local storage for | |
| 512 // static function access. | |
| 513 g_lazy_tls_ptr.Get().Set(&task_sequence_token_); | |
| 514 | |
| 515 // Just jump back to the Inner object to run the thread, since it has all the | |
| 516 // tracking information and queues. It might be more natural to implement | |
| 517 // using DelegateSimpleThread and have Inner implement the Delegate to avoid | |
| 518 // having these worker objects at all, but that method lacks the ability to | |
| 519 // send thread-specific information easily to the thread loop. | |
| 520 worker_pool_->inner_->ThreadLoop(this); | |
| 521 // Release our cyclic reference once we're done. | |
| 522 worker_pool_ = NULL; | |
| 523 } | |
| 524 | |
| 525 // Inner definitions --------------------------------------------------------- | |
| 526 | |
| 527 SequencedWorkerPool::Inner::Inner( | |
| 528 SequencedWorkerPool* worker_pool, | |
| 529 size_t max_threads, | |
| 530 const std::string& thread_name_prefix, | |
| 531 TestingObserver* observer) | |
| 532 : worker_pool_(worker_pool), | |
| 533 lock_(), | |
| 534 has_work_cv_(&lock_), | |
| 535 can_shutdown_cv_(&lock_), | |
| 536 max_threads_(max_threads), | |
| 537 thread_name_prefix_(thread_name_prefix), | |
| 538 thread_being_created_(false), | |
| 539 waiting_thread_count_(0), | |
| 540 blocking_shutdown_thread_count_(0), | |
| 541 next_sequence_task_number_(0), | |
| 542 blocking_shutdown_pending_task_count_(0), | |
| 543 trace_id_(0), | |
| 544 shutdown_called_(false), | |
| 545 max_blocking_tasks_after_shutdown_(0), | |
| 546 cleanup_state_(CLEANUP_DONE), | |
| 547 cleanup_idlers_(0), | |
| 548 cleanup_cv_(&lock_), | |
| 549 testing_observer_(observer) {} | |
| 550 | |
| 551 SequencedWorkerPool::Inner::~Inner() { | |
| 552 // You must call Shutdown() before destroying the pool. | |
| 553 DCHECK(shutdown_called_); | |
| 554 | |
| 555 // Need to explicitly join with the threads before they're destroyed or else | |
| 556 // they will be running when our object is half torn down. | |
| 557 for (ThreadMap::iterator it = threads_.begin(); it != threads_.end(); ++it) | |
| 558 it->second->Join(); | |
| 559 threads_.clear(); | |
| 560 | |
| 561 if (testing_observer_) | |
| 562 testing_observer_->OnDestruct(); | |
| 563 } | |
| 564 | |
| 565 SequencedWorkerPool::SequenceToken | |
| 566 SequencedWorkerPool::Inner::GetSequenceToken() { | |
| 567 // Need to add one because StaticAtomicSequenceNumber starts at zero, which | |
| 568 // is used as a sentinel value in SequenceTokens. | |
| 569 return SequenceToken(g_last_sequence_number_.GetNext() + 1); | |
| 570 } | |
| 571 | |
| 572 SequencedWorkerPool::SequenceToken | |
| 573 SequencedWorkerPool::Inner::GetNamedSequenceToken(const std::string& name) { | |
| 574 AutoLock lock(lock_); | |
| 575 return SequenceToken(LockedGetNamedTokenID(name)); | |
| 576 } | |
| 577 | |
| 578 bool SequencedWorkerPool::Inner::PostTask( | |
| 579 const std::string* optional_token_name, | |
| 580 SequenceToken sequence_token, | |
| 581 WorkerShutdown shutdown_behavior, | |
| 582 const tracked_objects::Location& from_here, | |
| 583 const Closure& task, | |
| 584 TimeDelta delay) { | |
| 585 DCHECK(delay == TimeDelta() || shutdown_behavior == SKIP_ON_SHUTDOWN); | |
| 586 SequencedTask sequenced(from_here); | |
| 587 sequenced.sequence_token_id = sequence_token.id_; | |
| 588 sequenced.shutdown_behavior = shutdown_behavior; | |
| 589 sequenced.posted_from = from_here; | |
| 590 sequenced.task = | |
| 591 shutdown_behavior == BLOCK_SHUTDOWN ? | |
| 592 base::MakeCriticalClosure(task) : task; | |
| 593 sequenced.time_to_run = TimeTicks::Now() + delay; | |
| 594 | |
| 595 int create_thread_id = 0; | |
| 596 { | |
| 597 AutoLock lock(lock_); | |
| 598 if (shutdown_called_) { | |
| 599 // Don't allow a new task to be posted if it doesn't block shutdown. | |
| 600 if (shutdown_behavior != BLOCK_SHUTDOWN) | |
| 601 return false; | |
| 602 | |
| 603 // If the current thread is running a task, and that task doesn't block | |
| 604 // shutdown, then it shouldn't be allowed to post any more tasks. | |
| 605 ThreadMap::const_iterator found = | |
| 606 threads_.find(PlatformThread::CurrentId()); | |
| 607 if (found != threads_.end() && found->second->is_processing_task() && | |
| 608 found->second->task_shutdown_behavior() != BLOCK_SHUTDOWN) { | |
| 609 return false; | |
| 610 } | |
| 611 | |
| 612 if (max_blocking_tasks_after_shutdown_ <= 0) { | |
| 613 DLOG(WARNING) << "BLOCK_SHUTDOWN task disallowed"; | |
| 614 return false; | |
| 615 } | |
| 616 max_blocking_tasks_after_shutdown_ -= 1; | |
| 617 } | |
| 618 | |
| 619 // The trace_id is used for identifying the task in about:tracing. | |
| 620 sequenced.trace_id = trace_id_++; | |
| 621 | |
| 622 TRACE_EVENT_FLOW_BEGIN0(TRACE_DISABLED_BY_DEFAULT("toplevel.flow"), | |
| 623 "SequencedWorkerPool::PostTask", | |
| 624 TRACE_ID_MANGLE(GetTaskTraceID(sequenced, static_cast<void*>(this)))); | |
| 625 | |
| 626 sequenced.sequence_task_number = LockedGetNextSequenceTaskNumber(); | |
| 627 | |
| 628 // Now that we have the lock, apply the named token rules. | |
| 629 if (optional_token_name) | |
| 630 sequenced.sequence_token_id = LockedGetNamedTokenID(*optional_token_name); | |
| 631 | |
| 632 pending_tasks_.insert(sequenced); | |
| 633 if (shutdown_behavior == BLOCK_SHUTDOWN) | |
| 634 blocking_shutdown_pending_task_count_++; | |
| 635 | |
| 636 create_thread_id = PrepareToStartAdditionalThreadIfHelpful(); | |
| 637 } | |
| 638 | |
| 639 // Actually start the additional thread or signal an existing one now that | |
| 640 // we're outside the lock. | |
| 641 if (create_thread_id) | |
| 642 FinishStartingAdditionalThread(create_thread_id); | |
| 643 else | |
| 644 SignalHasWork(); | |
| 645 | |
| 646 return true; | |
| 647 } | |
| 648 | |
| 649 bool SequencedWorkerPool::Inner::RunsTasksOnCurrentThread() const { | |
| 650 AutoLock lock(lock_); | |
| 651 return ContainsKey(threads_, PlatformThread::CurrentId()); | |
| 652 } | |
| 653 | |
| 654 bool SequencedWorkerPool::Inner::IsRunningSequenceOnCurrentThread( | |
| 655 SequenceToken sequence_token) const { | |
| 656 AutoLock lock(lock_); | |
| 657 ThreadMap::const_iterator found = threads_.find(PlatformThread::CurrentId()); | |
| 658 if (found == threads_.end()) | |
| 659 return false; | |
| 660 return found->second->is_processing_task() && | |
| 661 sequence_token.Equals(found->second->task_sequence_token()); | |
| 662 } | |
| 663 | |
| 664 // See https://code.google.com/p/chromium/issues/detail?id=168415 | |
| 665 void SequencedWorkerPool::Inner::CleanupForTesting() { | |
| 666 DCHECK(!RunsTasksOnCurrentThread()); | |
| 667 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 668 AutoLock lock(lock_); | |
| 669 CHECK_EQ(CLEANUP_DONE, cleanup_state_); | |
| 670 if (shutdown_called_) | |
| 671 return; | |
| 672 if (pending_tasks_.empty() && waiting_thread_count_ == threads_.size()) | |
| 673 return; | |
| 674 cleanup_state_ = CLEANUP_REQUESTED; | |
| 675 cleanup_idlers_ = 0; | |
| 676 has_work_cv_.Signal(); | |
| 677 while (cleanup_state_ != CLEANUP_DONE) | |
| 678 cleanup_cv_.Wait(); | |
| 679 } | |
| 680 | |
| 681 void SequencedWorkerPool::Inner::SignalHasWorkForTesting() { | |
| 682 SignalHasWork(); | |
| 683 } | |
| 684 | |
| 685 void SequencedWorkerPool::Inner::Shutdown( | |
| 686 int max_new_blocking_tasks_after_shutdown) { | |
| 687 DCHECK_GE(max_new_blocking_tasks_after_shutdown, 0); | |
| 688 { | |
| 689 AutoLock lock(lock_); | |
| 690 // Cleanup and Shutdown should not be called concurrently. | |
| 691 CHECK_EQ(CLEANUP_DONE, cleanup_state_); | |
| 692 if (shutdown_called_) | |
| 693 return; | |
| 694 shutdown_called_ = true; | |
| 695 max_blocking_tasks_after_shutdown_ = max_new_blocking_tasks_after_shutdown; | |
| 696 | |
| 697 // Tickle the threads. This will wake up a waiting one so it will know that | |
| 698 // it can exit, which in turn will wake up any other waiting ones. | |
| 699 SignalHasWork(); | |
| 700 | |
| 701 // There are no pending or running tasks blocking shutdown, we're done. | |
| 702 if (CanShutdown()) | |
| 703 return; | |
| 704 } | |
| 705 | |
| 706 // If we're here, then something is blocking shutdown. So wait for | |
| 707 // CanShutdown() to go to true. | |
| 708 | |
| 709 if (testing_observer_) | |
| 710 testing_observer_->WillWaitForShutdown(); | |
| 711 | |
| 712 #if !defined(OS_NACL) | |
| 713 TimeTicks shutdown_wait_begin = TimeTicks::Now(); | |
| 714 #endif | |
| 715 | |
| 716 { | |
| 717 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
| 718 AutoLock lock(lock_); | |
| 719 while (!CanShutdown()) | |
| 720 can_shutdown_cv_.Wait(); | |
| 721 } | |
| 722 #if !defined(OS_NACL) | |
| 723 UMA_HISTOGRAM_TIMES("SequencedWorkerPool.ShutdownDelayTime", | |
| 724 TimeTicks::Now() - shutdown_wait_begin); | |
| 725 #endif | |
| 726 } | |
| 727 | |
| 728 bool SequencedWorkerPool::Inner::IsShutdownInProgress() { | |
| 729 AutoLock lock(lock_); | |
| 730 return shutdown_called_; | |
| 731 } | |
| 732 | |
| 733 void SequencedWorkerPool::Inner::ThreadLoop(Worker* this_worker) { | |
| 734 { | |
| 735 AutoLock lock(lock_); | |
| 736 DCHECK(thread_being_created_); | |
| 737 thread_being_created_ = false; | |
| 738 std::pair<ThreadMap::iterator, bool> result = | |
| 739 threads_.insert( | |
| 740 std::make_pair(this_worker->tid(), make_linked_ptr(this_worker))); | |
| 741 DCHECK(result.second); | |
| 742 | |
| 743 while (true) { | |
| 744 #if defined(OS_MACOSX) | |
| 745 base::mac::ScopedNSAutoreleasePool autorelease_pool; | |
| 746 #endif | |
| 747 | |
| 748 HandleCleanup(); | |
| 749 | |
| 750 // See GetWork for what delete_these_outside_lock is doing. | |
| 751 SequencedTask task; | |
| 752 TimeDelta wait_time; | |
| 753 std::vector<Closure> delete_these_outside_lock; | |
| 754 GetWorkStatus status = | |
| 755 GetWork(&task, &wait_time, &delete_these_outside_lock); | |
| 756 if (status == GET_WORK_FOUND) { | |
| 757 TRACE_EVENT_FLOW_END0(TRACE_DISABLED_BY_DEFAULT("toplevel.flow"), | |
| 758 "SequencedWorkerPool::PostTask", | |
| 759 TRACE_ID_MANGLE(GetTaskTraceID(task, static_cast<void*>(this)))); | |
| 760 TRACE_EVENT2("toplevel", "SequencedWorkerPool::ThreadLoop", | |
| 761 "src_file", task.posted_from.file_name(), | |
| 762 "src_func", task.posted_from.function_name()); | |
| 763 int new_thread_id = WillRunWorkerTask(task); | |
| 764 { | |
| 765 AutoUnlock unlock(lock_); | |
| 766 // There may be more work available, so wake up another | |
| 767 // worker thread. (Technically not required, since we | |
| 768 // already get a signal for each new task, but it doesn't | |
| 769 // hurt.) | |
| 770 SignalHasWork(); | |
| 771 delete_these_outside_lock.clear(); | |
| 772 | |
| 773 // Complete thread creation outside the lock if necessary. | |
| 774 if (new_thread_id) | |
| 775 FinishStartingAdditionalThread(new_thread_id); | |
| 776 | |
| 777 this_worker->set_running_task_info( | |
| 778 SequenceToken(task.sequence_token_id), task.shutdown_behavior); | |
| 779 | |
| 780 tracked_objects::TaskStopwatch stopwatch; | |
| 781 stopwatch.Start(); | |
| 782 task.task.Run(); | |
| 783 stopwatch.Stop(); | |
| 784 | |
| 785 tracked_objects::ThreadData::TallyRunOnNamedThreadIfTracking( | |
| 786 task, stopwatch); | |
| 787 | |
| 788 // Make sure our task is erased outside the lock for the | |
| 789 // same reason we do this with delete_these_oustide_lock. | |
| 790 // Also, do it before calling reset_running_task_info() so | |
| 791 // that sequence-checking from within the task's destructor | |
| 792 // still works. | |
| 793 task.task = Closure(); | |
| 794 | |
| 795 this_worker->reset_running_task_info(); | |
| 796 } | |
| 797 DidRunWorkerTask(task); // Must be done inside the lock. | |
| 798 } else if (cleanup_state_ == CLEANUP_RUNNING) { | |
| 799 switch (status) { | |
| 800 case GET_WORK_WAIT: { | |
| 801 AutoUnlock unlock(lock_); | |
| 802 delete_these_outside_lock.clear(); | |
| 803 } | |
| 804 break; | |
| 805 case GET_WORK_NOT_FOUND: | |
| 806 CHECK(delete_these_outside_lock.empty()); | |
| 807 cleanup_state_ = CLEANUP_FINISHING; | |
| 808 cleanup_cv_.Broadcast(); | |
| 809 break; | |
| 810 default: | |
| 811 NOTREACHED(); | |
| 812 } | |
| 813 } else { | |
| 814 // When we're terminating and there's no more work, we can | |
| 815 // shut down, other workers can complete any pending or new tasks. | |
| 816 // We can get additional tasks posted after shutdown_called_ is set | |
| 817 // but only worker threads are allowed to post tasks at that time, and | |
| 818 // the workers responsible for posting those tasks will be available | |
| 819 // to run them. Also, there may be some tasks stuck behind running | |
| 820 // ones with the same sequence token, but additional threads won't | |
| 821 // help this case. | |
| 822 if (shutdown_called_ && blocking_shutdown_pending_task_count_ == 0) { | |
| 823 AutoUnlock unlock(lock_); | |
| 824 delete_these_outside_lock.clear(); | |
| 825 break; | |
| 826 } | |
| 827 | |
| 828 // No work was found, but there are tasks that need deletion. The | |
| 829 // deletion must happen outside of the lock. | |
| 830 if (delete_these_outside_lock.size()) { | |
| 831 AutoUnlock unlock(lock_); | |
| 832 delete_these_outside_lock.clear(); | |
| 833 | |
| 834 // Since the lock has been released, |status| may no longer be | |
| 835 // accurate. It might read GET_WORK_WAIT even if there are tasks | |
| 836 // ready to perform work. Jump to the top of the loop to recalculate | |
| 837 // |status|. | |
| 838 continue; | |
| 839 } | |
| 840 | |
| 841 waiting_thread_count_++; | |
| 842 | |
| 843 switch (status) { | |
| 844 case GET_WORK_NOT_FOUND: | |
| 845 has_work_cv_.Wait(); | |
| 846 break; | |
| 847 case GET_WORK_WAIT: | |
| 848 has_work_cv_.TimedWait(wait_time); | |
| 849 break; | |
| 850 default: | |
| 851 NOTREACHED(); | |
| 852 } | |
| 853 waiting_thread_count_--; | |
| 854 } | |
| 855 } | |
| 856 } // Release lock_. | |
| 857 | |
| 858 // We noticed we should exit. Wake up the next worker so it knows it should | |
| 859 // exit as well (because the Shutdown() code only signals once). | |
| 860 SignalHasWork(); | |
| 861 | |
| 862 // Possibly unblock shutdown. | |
| 863 can_shutdown_cv_.Signal(); | |
| 864 } | |
| 865 | |
| 866 void SequencedWorkerPool::Inner::HandleCleanup() { | |
| 867 lock_.AssertAcquired(); | |
| 868 if (cleanup_state_ == CLEANUP_DONE) | |
| 869 return; | |
| 870 if (cleanup_state_ == CLEANUP_REQUESTED) { | |
| 871 // We win, we get to do the cleanup as soon as the others wise up and idle. | |
| 872 cleanup_state_ = CLEANUP_STARTING; | |
| 873 while (thread_being_created_ || | |
| 874 cleanup_idlers_ != threads_.size() - 1) { | |
| 875 has_work_cv_.Signal(); | |
| 876 cleanup_cv_.Wait(); | |
| 877 } | |
| 878 cleanup_state_ = CLEANUP_RUNNING; | |
| 879 return; | |
| 880 } | |
| 881 if (cleanup_state_ == CLEANUP_STARTING) { | |
| 882 // Another worker thread is cleaning up, we idle here until thats done. | |
| 883 ++cleanup_idlers_; | |
| 884 cleanup_cv_.Broadcast(); | |
| 885 while (cleanup_state_ != CLEANUP_FINISHING) { | |
| 886 cleanup_cv_.Wait(); | |
| 887 } | |
| 888 --cleanup_idlers_; | |
| 889 cleanup_cv_.Broadcast(); | |
| 890 return; | |
| 891 } | |
| 892 if (cleanup_state_ == CLEANUP_FINISHING) { | |
| 893 // We wait for all idlers to wake up prior to being DONE. | |
| 894 while (cleanup_idlers_ != 0) { | |
| 895 cleanup_cv_.Broadcast(); | |
| 896 cleanup_cv_.Wait(); | |
| 897 } | |
| 898 if (cleanup_state_ == CLEANUP_FINISHING) { | |
| 899 cleanup_state_ = CLEANUP_DONE; | |
| 900 cleanup_cv_.Signal(); | |
| 901 } | |
| 902 return; | |
| 903 } | |
| 904 } | |
| 905 | |
| 906 int SequencedWorkerPool::Inner::LockedGetNamedTokenID( | |
| 907 const std::string& name) { | |
| 908 lock_.AssertAcquired(); | |
| 909 DCHECK(!name.empty()); | |
| 910 | |
| 911 std::map<std::string, int>::const_iterator found = | |
| 912 named_sequence_tokens_.find(name); | |
| 913 if (found != named_sequence_tokens_.end()) | |
| 914 return found->second; // Got an existing one. | |
| 915 | |
| 916 // Create a new one for this name. | |
| 917 SequenceToken result = GetSequenceToken(); | |
| 918 named_sequence_tokens_.insert(std::make_pair(name, result.id_)); | |
| 919 return result.id_; | |
| 920 } | |
| 921 | |
| 922 int64 SequencedWorkerPool::Inner::LockedGetNextSequenceTaskNumber() { | |
| 923 lock_.AssertAcquired(); | |
| 924 // We assume that we never create enough tasks to wrap around. | |
| 925 return next_sequence_task_number_++; | |
| 926 } | |
| 927 | |
| 928 SequencedWorkerPool::Inner::GetWorkStatus SequencedWorkerPool::Inner::GetWork( | |
| 929 SequencedTask* task, | |
| 930 TimeDelta* wait_time, | |
| 931 std::vector<Closure>* delete_these_outside_lock) { | |
| 932 lock_.AssertAcquired(); | |
| 933 | |
| 934 // Find the next task with a sequence token that's not currently in use. | |
| 935 // If the token is in use, that means another thread is running something | |
| 936 // in that sequence, and we can't run it without going out-of-order. | |
| 937 // | |
| 938 // This algorithm is simple and fair, but inefficient in some cases. For | |
| 939 // example, say somebody schedules 1000 slow tasks with the same sequence | |
| 940 // number. We'll have to go through all those tasks each time we feel like | |
| 941 // there might be work to schedule. If this proves to be a problem, we | |
| 942 // should make this more efficient. | |
| 943 // | |
| 944 // One possible enhancement would be to keep a map from sequence ID to a | |
| 945 // list of pending but currently blocked SequencedTasks for that ID. | |
| 946 // When a worker finishes a task of one sequence token, it can pick up the | |
| 947 // next one from that token right away. | |
| 948 // | |
| 949 // This may lead to starvation if there are sufficient numbers of sequences | |
| 950 // in use. To alleviate this, we could add an incrementing priority counter | |
| 951 // to each SequencedTask. Then maintain a priority_queue of all runnable | |
| 952 // tasks, sorted by priority counter. When a sequenced task is completed | |
| 953 // we would pop the head element off of that tasks pending list and add it | |
| 954 // to the priority queue. Then we would run the first item in the priority | |
| 955 // queue. | |
| 956 | |
| 957 GetWorkStatus status = GET_WORK_NOT_FOUND; | |
| 958 int unrunnable_tasks = 0; | |
| 959 PendingTaskSet::iterator i = pending_tasks_.begin(); | |
| 960 // We assume that the loop below doesn't take too long and so we can just do | |
| 961 // a single call to TimeTicks::Now(). | |
| 962 const TimeTicks current_time = TimeTicks::Now(); | |
| 963 while (i != pending_tasks_.end()) { | |
| 964 if (!IsSequenceTokenRunnable(i->sequence_token_id)) { | |
| 965 unrunnable_tasks++; | |
| 966 ++i; | |
| 967 continue; | |
| 968 } | |
| 969 | |
| 970 if (shutdown_called_ && i->shutdown_behavior != BLOCK_SHUTDOWN) { | |
| 971 // We're shutting down and the task we just found isn't blocking | |
| 972 // shutdown. Delete it and get more work. | |
| 973 // | |
| 974 // Note that we do not want to delete unrunnable tasks. Deleting a task | |
| 975 // can have side effects (like freeing some objects) and deleting a | |
| 976 // task that's supposed to run after one that's currently running could | |
| 977 // cause an obscure crash. | |
| 978 // | |
| 979 // We really want to delete these tasks outside the lock in case the | |
| 980 // closures are holding refs to objects that want to post work from | |
| 981 // their destructorss (which would deadlock). The closures are | |
| 982 // internally refcounted, so we just need to keep a copy of them alive | |
| 983 // until the lock is exited. The calling code can just clear() the | |
| 984 // vector they passed to us once the lock is exited to make this | |
| 985 // happen. | |
| 986 delete_these_outside_lock->push_back(i->task); | |
| 987 pending_tasks_.erase(i++); | |
| 988 continue; | |
| 989 } | |
| 990 | |
| 991 if (i->time_to_run > current_time) { | |
| 992 // The time to run has not come yet. | |
| 993 *wait_time = i->time_to_run - current_time; | |
| 994 status = GET_WORK_WAIT; | |
| 995 if (cleanup_state_ == CLEANUP_RUNNING) { | |
| 996 // Deferred tasks are deleted when cleaning up, see Inner::ThreadLoop. | |
| 997 delete_these_outside_lock->push_back(i->task); | |
| 998 pending_tasks_.erase(i); | |
| 999 } | |
| 1000 break; | |
| 1001 } | |
| 1002 | |
| 1003 // Found a runnable task. | |
| 1004 *task = *i; | |
| 1005 pending_tasks_.erase(i); | |
| 1006 if (task->shutdown_behavior == BLOCK_SHUTDOWN) { | |
| 1007 blocking_shutdown_pending_task_count_--; | |
| 1008 } | |
| 1009 | |
| 1010 status = GET_WORK_FOUND; | |
| 1011 break; | |
| 1012 } | |
| 1013 | |
| 1014 return status; | |
| 1015 } | |
| 1016 | |
| 1017 int SequencedWorkerPool::Inner::WillRunWorkerTask(const SequencedTask& task) { | |
| 1018 lock_.AssertAcquired(); | |
| 1019 | |
| 1020 // Mark the task's sequence number as in use. | |
| 1021 if (task.sequence_token_id) | |
| 1022 current_sequences_.insert(task.sequence_token_id); | |
| 1023 | |
| 1024 // Ensure that threads running tasks posted with either SKIP_ON_SHUTDOWN | |
| 1025 // or BLOCK_SHUTDOWN will prevent shutdown until that task or thread | |
| 1026 // completes. | |
| 1027 if (task.shutdown_behavior != CONTINUE_ON_SHUTDOWN) | |
| 1028 blocking_shutdown_thread_count_++; | |
| 1029 | |
| 1030 // We just picked up a task. Since StartAdditionalThreadIfHelpful only | |
| 1031 // creates a new thread if there is no free one, there is a race when posting | |
| 1032 // tasks that many tasks could have been posted before a thread started | |
| 1033 // running them, so only one thread would have been created. So we also check | |
| 1034 // whether we should create more threads after removing our task from the | |
| 1035 // queue, which also has the nice side effect of creating the workers from | |
| 1036 // background threads rather than the main thread of the app. | |
| 1037 // | |
| 1038 // If another thread wasn't created, we want to wake up an existing thread | |
| 1039 // if there is one waiting to pick up the next task. | |
| 1040 // | |
| 1041 // Note that we really need to do this *before* running the task, not | |
| 1042 // after. Otherwise, if more than one task is posted, the creation of the | |
| 1043 // second thread (since we only create one at a time) will be blocked by | |
| 1044 // the execution of the first task, which could be arbitrarily long. | |
| 1045 return PrepareToStartAdditionalThreadIfHelpful(); | |
| 1046 } | |
| 1047 | |
| 1048 void SequencedWorkerPool::Inner::DidRunWorkerTask(const SequencedTask& task) { | |
| 1049 lock_.AssertAcquired(); | |
| 1050 | |
| 1051 if (task.shutdown_behavior != CONTINUE_ON_SHUTDOWN) { | |
| 1052 DCHECK_GT(blocking_shutdown_thread_count_, 0u); | |
| 1053 blocking_shutdown_thread_count_--; | |
| 1054 } | |
| 1055 | |
| 1056 if (task.sequence_token_id) | |
| 1057 current_sequences_.erase(task.sequence_token_id); | |
| 1058 } | |
| 1059 | |
| 1060 bool SequencedWorkerPool::Inner::IsSequenceTokenRunnable( | |
| 1061 int sequence_token_id) const { | |
| 1062 lock_.AssertAcquired(); | |
| 1063 return !sequence_token_id || | |
| 1064 current_sequences_.find(sequence_token_id) == | |
| 1065 current_sequences_.end(); | |
| 1066 } | |
| 1067 | |
| 1068 int SequencedWorkerPool::Inner::PrepareToStartAdditionalThreadIfHelpful() { | |
| 1069 lock_.AssertAcquired(); | |
| 1070 // How thread creation works: | |
| 1071 // | |
| 1072 // We'de like to avoid creating threads with the lock held. However, we | |
| 1073 // need to be sure that we have an accurate accounting of the threads for | |
| 1074 // proper Joining and deltion on shutdown. | |
| 1075 // | |
| 1076 // We need to figure out if we need another thread with the lock held, which | |
| 1077 // is what this function does. It then marks us as in the process of creating | |
| 1078 // a thread. When we do shutdown, we wait until the thread_being_created_ | |
| 1079 // flag is cleared, which ensures that the new thread is properly added to | |
| 1080 // all the data structures and we can't leak it. Once shutdown starts, we'll | |
| 1081 // refuse to create more threads or they would be leaked. | |
| 1082 // | |
| 1083 // Note that this creates a mostly benign race condition on shutdown that | |
| 1084 // will cause fewer workers to be created than one would expect. It isn't | |
| 1085 // much of an issue in real life, but affects some tests. Since we only spawn | |
| 1086 // one worker at a time, the following sequence of events can happen: | |
| 1087 // | |
| 1088 // 1. Main thread posts a bunch of unrelated tasks that would normally be | |
| 1089 // run on separate threads. | |
| 1090 // 2. The first task post causes us to start a worker. Other tasks do not | |
| 1091 // cause a worker to start since one is pending. | |
| 1092 // 3. Main thread initiates shutdown. | |
| 1093 // 4. No more threads are created since the shutdown_called_ flag is set. | |
| 1094 // | |
| 1095 // The result is that one may expect that max_threads_ workers to be created | |
| 1096 // given the workload, but in reality fewer may be created because the | |
| 1097 // sequence of thread creation on the background threads is racing with the | |
| 1098 // shutdown call. | |
| 1099 if (!shutdown_called_ && | |
| 1100 !thread_being_created_ && | |
| 1101 cleanup_state_ == CLEANUP_DONE && | |
| 1102 threads_.size() < max_threads_ && | |
| 1103 waiting_thread_count_ == 0) { | |
| 1104 // We could use an additional thread if there's work to be done. | |
| 1105 for (PendingTaskSet::const_iterator i = pending_tasks_.begin(); | |
| 1106 i != pending_tasks_.end(); ++i) { | |
| 1107 if (IsSequenceTokenRunnable(i->sequence_token_id)) { | |
| 1108 // Found a runnable task, mark the thread as being started. | |
| 1109 thread_being_created_ = true; | |
| 1110 return static_cast<int>(threads_.size() + 1); | |
| 1111 } | |
| 1112 } | |
| 1113 } | |
| 1114 return 0; | |
| 1115 } | |
| 1116 | |
| 1117 void SequencedWorkerPool::Inner::FinishStartingAdditionalThread( | |
| 1118 int thread_number) { | |
| 1119 // Called outside of the lock. | |
| 1120 DCHECK_GT(thread_number, 0); | |
| 1121 | |
| 1122 // The worker is assigned to the list when the thread actually starts, which | |
| 1123 // will manage the memory of the pointer. | |
| 1124 new Worker(worker_pool_, thread_number, thread_name_prefix_); | |
| 1125 } | |
| 1126 | |
| 1127 void SequencedWorkerPool::Inner::SignalHasWork() { | |
| 1128 has_work_cv_.Signal(); | |
| 1129 if (testing_observer_) { | |
| 1130 testing_observer_->OnHasWork(); | |
| 1131 } | |
| 1132 } | |
| 1133 | |
| 1134 bool SequencedWorkerPool::Inner::CanShutdown() const { | |
| 1135 lock_.AssertAcquired(); | |
| 1136 // See PrepareToStartAdditionalThreadIfHelpful for how thread creation works. | |
| 1137 return !thread_being_created_ && | |
| 1138 blocking_shutdown_thread_count_ == 0 && | |
| 1139 blocking_shutdown_pending_task_count_ == 0; | |
| 1140 } | |
| 1141 | |
| 1142 base::StaticAtomicSequenceNumber | |
| 1143 SequencedWorkerPool::Inner::g_last_sequence_number_; | |
| 1144 | |
| 1145 // SequencedWorkerPool -------------------------------------------------------- | |
| 1146 | |
| 1147 // static | |
| 1148 SequencedWorkerPool::SequenceToken | |
| 1149 SequencedWorkerPool::GetSequenceTokenForCurrentThread() { | |
| 1150 // Don't construct lazy instance on check. | |
| 1151 if (g_lazy_tls_ptr == NULL) | |
| 1152 return SequenceToken(); | |
| 1153 | |
| 1154 SequencedWorkerPool::SequenceToken* token = g_lazy_tls_ptr.Get().Get(); | |
| 1155 if (!token) | |
| 1156 return SequenceToken(); | |
| 1157 return *token; | |
| 1158 } | |
| 1159 | |
| 1160 SequencedWorkerPool::SequencedWorkerPool(size_t max_threads, | |
| 1161 const std::string& thread_name_prefix) | |
| 1162 : constructor_task_runner_(ThreadTaskRunnerHandle::Get()), | |
| 1163 inner_(new Inner(this, max_threads, thread_name_prefix, NULL)) { | |
| 1164 } | |
| 1165 | |
| 1166 SequencedWorkerPool::SequencedWorkerPool(size_t max_threads, | |
| 1167 const std::string& thread_name_prefix, | |
| 1168 TestingObserver* observer) | |
| 1169 : constructor_task_runner_(ThreadTaskRunnerHandle::Get()), | |
| 1170 inner_(new Inner(this, max_threads, thread_name_prefix, observer)) { | |
| 1171 } | |
| 1172 | |
| 1173 SequencedWorkerPool::~SequencedWorkerPool() {} | |
| 1174 | |
| 1175 void SequencedWorkerPool::OnDestruct() const { | |
| 1176 // Avoid deleting ourselves on a worker thread (which would | |
| 1177 // deadlock). | |
| 1178 if (RunsTasksOnCurrentThread()) { | |
| 1179 constructor_task_runner_->DeleteSoon(FROM_HERE, this); | |
| 1180 } else { | |
| 1181 delete this; | |
| 1182 } | |
| 1183 } | |
| 1184 | |
| 1185 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetSequenceToken() { | |
| 1186 return inner_->GetSequenceToken(); | |
| 1187 } | |
| 1188 | |
| 1189 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetNamedSequenceToken( | |
| 1190 const std::string& name) { | |
| 1191 return inner_->GetNamedSequenceToken(name); | |
| 1192 } | |
| 1193 | |
| 1194 scoped_refptr<SequencedTaskRunner> SequencedWorkerPool::GetSequencedTaskRunner( | |
| 1195 SequenceToken token) { | |
| 1196 return GetSequencedTaskRunnerWithShutdownBehavior(token, BLOCK_SHUTDOWN); | |
| 1197 } | |
| 1198 | |
| 1199 scoped_refptr<SequencedTaskRunner> | |
| 1200 SequencedWorkerPool::GetSequencedTaskRunnerWithShutdownBehavior( | |
| 1201 SequenceToken token, WorkerShutdown shutdown_behavior) { | |
| 1202 return new SequencedWorkerPoolSequencedTaskRunner( | |
| 1203 this, token, shutdown_behavior); | |
| 1204 } | |
| 1205 | |
| 1206 scoped_refptr<TaskRunner> | |
| 1207 SequencedWorkerPool::GetTaskRunnerWithShutdownBehavior( | |
| 1208 WorkerShutdown shutdown_behavior) { | |
| 1209 return new SequencedWorkerPoolTaskRunner(this, shutdown_behavior); | |
| 1210 } | |
| 1211 | |
| 1212 bool SequencedWorkerPool::PostWorkerTask( | |
| 1213 const tracked_objects::Location& from_here, | |
| 1214 const Closure& task) { | |
| 1215 return inner_->PostTask(NULL, SequenceToken(), BLOCK_SHUTDOWN, | |
| 1216 from_here, task, TimeDelta()); | |
| 1217 } | |
| 1218 | |
| 1219 bool SequencedWorkerPool::PostDelayedWorkerTask( | |
| 1220 const tracked_objects::Location& from_here, | |
| 1221 const Closure& task, | |
| 1222 TimeDelta delay) { | |
| 1223 WorkerShutdown shutdown_behavior = | |
| 1224 delay == TimeDelta() ? BLOCK_SHUTDOWN : SKIP_ON_SHUTDOWN; | |
| 1225 return inner_->PostTask(NULL, SequenceToken(), shutdown_behavior, | |
| 1226 from_here, task, delay); | |
| 1227 } | |
| 1228 | |
| 1229 bool SequencedWorkerPool::PostWorkerTaskWithShutdownBehavior( | |
| 1230 const tracked_objects::Location& from_here, | |
| 1231 const Closure& task, | |
| 1232 WorkerShutdown shutdown_behavior) { | |
| 1233 return inner_->PostTask(NULL, SequenceToken(), shutdown_behavior, | |
| 1234 from_here, task, TimeDelta()); | |
| 1235 } | |
| 1236 | |
| 1237 bool SequencedWorkerPool::PostSequencedWorkerTask( | |
| 1238 SequenceToken sequence_token, | |
| 1239 const tracked_objects::Location& from_here, | |
| 1240 const Closure& task) { | |
| 1241 return inner_->PostTask(NULL, sequence_token, BLOCK_SHUTDOWN, | |
| 1242 from_here, task, TimeDelta()); | |
| 1243 } | |
| 1244 | |
| 1245 bool SequencedWorkerPool::PostDelayedSequencedWorkerTask( | |
| 1246 SequenceToken sequence_token, | |
| 1247 const tracked_objects::Location& from_here, | |
| 1248 const Closure& task, | |
| 1249 TimeDelta delay) { | |
| 1250 WorkerShutdown shutdown_behavior = | |
| 1251 delay == TimeDelta() ? BLOCK_SHUTDOWN : SKIP_ON_SHUTDOWN; | |
| 1252 return inner_->PostTask(NULL, sequence_token, shutdown_behavior, | |
| 1253 from_here, task, delay); | |
| 1254 } | |
| 1255 | |
| 1256 bool SequencedWorkerPool::PostNamedSequencedWorkerTask( | |
| 1257 const std::string& token_name, | |
| 1258 const tracked_objects::Location& from_here, | |
| 1259 const Closure& task) { | |
| 1260 DCHECK(!token_name.empty()); | |
| 1261 return inner_->PostTask(&token_name, SequenceToken(), BLOCK_SHUTDOWN, | |
| 1262 from_here, task, TimeDelta()); | |
| 1263 } | |
| 1264 | |
| 1265 bool SequencedWorkerPool::PostSequencedWorkerTaskWithShutdownBehavior( | |
| 1266 SequenceToken sequence_token, | |
| 1267 const tracked_objects::Location& from_here, | |
| 1268 const Closure& task, | |
| 1269 WorkerShutdown shutdown_behavior) { | |
| 1270 return inner_->PostTask(NULL, sequence_token, shutdown_behavior, | |
| 1271 from_here, task, TimeDelta()); | |
| 1272 } | |
| 1273 | |
| 1274 bool SequencedWorkerPool::PostDelayedTask( | |
| 1275 const tracked_objects::Location& from_here, | |
| 1276 const Closure& task, | |
| 1277 TimeDelta delay) { | |
| 1278 return PostDelayedWorkerTask(from_here, task, delay); | |
| 1279 } | |
| 1280 | |
| 1281 bool SequencedWorkerPool::RunsTasksOnCurrentThread() const { | |
| 1282 return inner_->RunsTasksOnCurrentThread(); | |
| 1283 } | |
| 1284 | |
| 1285 bool SequencedWorkerPool::IsRunningSequenceOnCurrentThread( | |
| 1286 SequenceToken sequence_token) const { | |
| 1287 return inner_->IsRunningSequenceOnCurrentThread(sequence_token); | |
| 1288 } | |
| 1289 | |
| 1290 void SequencedWorkerPool::FlushForTesting() { | |
| 1291 inner_->CleanupForTesting(); | |
| 1292 } | |
| 1293 | |
| 1294 void SequencedWorkerPool::SignalHasWorkForTesting() { | |
| 1295 inner_->SignalHasWorkForTesting(); | |
| 1296 } | |
| 1297 | |
| 1298 void SequencedWorkerPool::Shutdown(int max_new_blocking_tasks_after_shutdown) { | |
| 1299 DCHECK(constructor_task_runner_->BelongsToCurrentThread()); | |
| 1300 inner_->Shutdown(max_new_blocking_tasks_after_shutdown); | |
| 1301 } | |
| 1302 | |
| 1303 bool SequencedWorkerPool::IsShutdownInProgress() { | |
| 1304 return inner_->IsShutdownInProgress(); | |
| 1305 } | |
| 1306 | |
| 1307 } // namespace base | |
| OLD | NEW |