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.
5 #include "base/threading/sequenced_worker_pool.h"
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/message_loop/message_loop_proxy.h"
21 #include "base/stl_util.h"
22 #include "base/strings/stringprintf.h"
23 #include "base/synchronization/condition_variable.h"
24 #include "base/synchronization/lock.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"
33 #if defined(OS_MACOSX)
34 #include "base/mac/scoped_nsautorelease_pool.h"
36 #include "base/win/scoped_com_initializer.h"
40 #include "base/metrics/histogram.h"
47 struct SequencedTask
: public TrackingInfo
{
49 : sequence_token_id(0),
51 sequence_task_number(0),
52 shutdown_behavior(SequencedWorkerPool::BLOCK_SHUTDOWN
) {}
54 explicit SequencedTask(const tracked_objects::Location
& from_here
)
55 : base::TrackingInfo(from_here
, TimeTicks()),
58 sequence_task_number(0),
59 shutdown_behavior(SequencedWorkerPool::BLOCK_SHUTDOWN
) {}
63 int sequence_token_id
;
65 int64 sequence_task_number
;
66 SequencedWorkerPool::WorkerShutdown shutdown_behavior
;
67 tracked_objects::Location posted_from
;
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
;
75 struct SequencedTaskLessThan
{
77 bool operator()(const SequencedTask
& lhs
, const SequencedTask
& rhs
) const {
78 if (lhs
.time_to_run
< rhs
.time_to_run
)
81 if (lhs
.time_to_run
> rhs
.time_to_run
)
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
;
89 // SequencedWorkerPoolTaskRunner ---------------------------------------------
90 // A TaskRunner which posts tasks to a SequencedWorkerPool with a
91 // fixed ShutdownBehavior.
93 // Note that this class is RefCountedThreadSafe (inherited from TaskRunner).
94 class SequencedWorkerPoolTaskRunner
: public TaskRunner
{
96 SequencedWorkerPoolTaskRunner(
97 const scoped_refptr
<SequencedWorkerPool
>& pool
,
98 SequencedWorkerPool::WorkerShutdown shutdown_behavior
);
100 // TaskRunner implementation
101 bool PostDelayedTask(const tracked_objects::Location
& from_here
,
103 TimeDelta delay
) override
;
104 bool RunsTasksOnCurrentThread() const override
;
107 ~SequencedWorkerPoolTaskRunner() override
;
109 const scoped_refptr
<SequencedWorkerPool
> pool_
;
111 const SequencedWorkerPool::WorkerShutdown shutdown_behavior_
;
113 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPoolTaskRunner
);
116 SequencedWorkerPoolTaskRunner::SequencedWorkerPoolTaskRunner(
117 const scoped_refptr
<SequencedWorkerPool
>& pool
,
118 SequencedWorkerPool::WorkerShutdown shutdown_behavior
)
120 shutdown_behavior_(shutdown_behavior
) {
123 SequencedWorkerPoolTaskRunner::~SequencedWorkerPoolTaskRunner() {
126 bool SequencedWorkerPoolTaskRunner::PostDelayedTask(
127 const tracked_objects::Location
& from_here
,
130 if (delay
== TimeDelta()) {
131 return pool_
->PostWorkerTaskWithShutdownBehavior(
132 from_here
, task
, shutdown_behavior_
);
134 return pool_
->PostDelayedWorkerTask(from_here
, task
, delay
);
137 bool SequencedWorkerPoolTaskRunner::RunsTasksOnCurrentThread() const {
138 return pool_
->RunsTasksOnCurrentThread();
141 // SequencedWorkerPoolSequencedTaskRunner ------------------------------------
142 // A SequencedTaskRunner which posts tasks to a SequencedWorkerPool with a
143 // fixed sequence token.
145 // Note that this class is RefCountedThreadSafe (inherited from TaskRunner).
146 class SequencedWorkerPoolSequencedTaskRunner
: public SequencedTaskRunner
{
148 SequencedWorkerPoolSequencedTaskRunner(
149 const scoped_refptr
<SequencedWorkerPool
>& pool
,
150 SequencedWorkerPool::SequenceToken token
,
151 SequencedWorkerPool::WorkerShutdown shutdown_behavior
);
153 // TaskRunner implementation
154 bool PostDelayedTask(const tracked_objects::Location
& from_here
,
156 TimeDelta delay
) override
;
157 bool RunsTasksOnCurrentThread() const override
;
159 // SequencedTaskRunner implementation
160 bool PostNonNestableDelayedTask(const tracked_objects::Location
& from_here
,
162 TimeDelta delay
) override
;
165 ~SequencedWorkerPoolSequencedTaskRunner() override
;
167 const scoped_refptr
<SequencedWorkerPool
> pool_
;
169 const SequencedWorkerPool::SequenceToken token_
;
171 const SequencedWorkerPool::WorkerShutdown shutdown_behavior_
;
173 DISALLOW_COPY_AND_ASSIGN(SequencedWorkerPoolSequencedTaskRunner
);
176 SequencedWorkerPoolSequencedTaskRunner::SequencedWorkerPoolSequencedTaskRunner(
177 const scoped_refptr
<SequencedWorkerPool
>& pool
,
178 SequencedWorkerPool::SequenceToken token
,
179 SequencedWorkerPool::WorkerShutdown shutdown_behavior
)
182 shutdown_behavior_(shutdown_behavior
) {
185 SequencedWorkerPoolSequencedTaskRunner::
186 ~SequencedWorkerPoolSequencedTaskRunner() {
189 bool SequencedWorkerPoolSequencedTaskRunner::PostDelayedTask(
190 const tracked_objects::Location
& from_here
,
193 if (delay
== TimeDelta()) {
194 return pool_
->PostSequencedWorkerTaskWithShutdownBehavior(
195 token_
, from_here
, task
, shutdown_behavior_
);
197 return pool_
->PostDelayedSequencedWorkerTask(token_
, from_here
, task
, delay
);
200 bool SequencedWorkerPoolSequencedTaskRunner::RunsTasksOnCurrentThread() const {
201 return pool_
->IsRunningSequenceOnCurrentThread(token_
);
204 bool SequencedWorkerPoolSequencedTaskRunner::PostNonNestableDelayedTask(
205 const tracked_objects::Location
& from_here
,
208 // There's no way to run nested tasks, so simply forward to
210 return PostDelayedTask(from_here
, task
, delay
);
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
,
218 return (static_cast<uint64
>(task
.trace_id
) << 32) |
219 static_cast<uint64
>(reinterpret_cast<intptr_t>(pool
));
222 base::LazyInstance
<base::ThreadLocalPointer
<
223 SequencedWorkerPool::SequenceToken
> >::Leaky g_lazy_tls_ptr
=
224 LAZY_INSTANCE_INITIALIZER
;
228 // Worker ---------------------------------------------------------------------
230 class SequencedWorkerPool::Worker
: public SimpleThread
{
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
,
236 const std::string
& thread_name_prefix
);
239 // SimpleThread implementation. This actually runs the background thread.
242 void set_running_task_info(SequenceToken token
,
243 WorkerShutdown shutdown_behavior
) {
244 running_sequence_
= token
;
245 running_shutdown_behavior_
= shutdown_behavior
;
248 SequenceToken
running_sequence() const {
249 return running_sequence_
;
252 WorkerShutdown
running_shutdown_behavior() const {
253 return running_shutdown_behavior_
;
257 scoped_refptr
<SequencedWorkerPool
> worker_pool_
;
258 SequenceToken running_sequence_
;
259 WorkerShutdown running_shutdown_behavior_
;
261 DISALLOW_COPY_AND_ASSIGN(Worker
);
264 // Inner ----------------------------------------------------------------------
266 class SequencedWorkerPool::Inner
{
268 // Take a raw pointer to |worker| to avoid cycles (since we're owned
270 Inner(SequencedWorkerPool
* worker_pool
, size_t max_threads
,
271 const std::string
& thread_name_prefix
,
272 TestingObserver
* observer
);
276 SequenceToken
GetSequenceToken();
278 SequenceToken
GetNamedSequenceToken(const std::string
& name
);
280 // This function accepts a name and an ID. If the name is null, the
281 // token ID is used. This allows us to implement the optional name lookup
282 // from a single function without having to enter the lock a separate time.
283 bool PostTask(const std::string
* optional_token_name
,
284 SequenceToken sequence_token
,
285 WorkerShutdown shutdown_behavior
,
286 const tracked_objects::Location
& from_here
,
290 bool RunsTasksOnCurrentThread() const;
292 bool IsRunningSequenceOnCurrentThread(SequenceToken sequence_token
) const;
294 void CleanupForTesting();
296 void SignalHasWorkForTesting();
298 int GetWorkSignalCountForTesting() const;
300 void Shutdown(int max_blocking_tasks_after_shutdown
);
302 bool IsShutdownInProgress();
304 // Runs the worker loop on the background thread.
305 void ThreadLoop(Worker
* this_worker
);
322 // Called from within the lock, this converts the given token name into a
323 // token ID, creating a new one if necessary.
324 int LockedGetNamedTokenID(const std::string
& name
);
326 // Called from within the lock, this returns the next sequence task number.
327 int64
LockedGetNextSequenceTaskNumber();
329 // Called from within the lock, returns the shutdown behavior of the task
330 // running on the currently executing worker thread. If invoked from a thread
331 // that is not one of the workers, returns CONTINUE_ON_SHUTDOWN.
332 WorkerShutdown
LockedCurrentThreadShutdownBehavior() const;
334 // Gets new task. There are 3 cases depending on the return value:
336 // 1) If the return value is |GET_WORK_FOUND|, |task| is filled in and should
337 // be run immediately.
338 // 2) If the return value is |GET_WORK_NOT_FOUND|, there are no tasks to run,
339 // and |task| is not filled in. In this case, the caller should wait until
341 // 3) If the return value is |GET_WORK_WAIT|, there are no tasks to run
342 // immediately, and |task| is not filled in. Likewise, |wait_time| is
343 // filled in the time to wait until the next task to run. In this case, the
344 // caller should wait the time.
346 // In any case, the calling code should clear the given
347 // delete_these_outside_lock vector the next time the lock is released.
348 // See the implementation for a more detailed description.
349 GetWorkStatus
GetWork(SequencedTask
* task
,
350 TimeDelta
* wait_time
,
351 std::vector
<Closure
>* delete_these_outside_lock
);
353 void HandleCleanup();
355 // Peforms init and cleanup around running the given task. WillRun...
356 // returns the value from PrepareToStartAdditionalThreadIfNecessary.
357 // The calling code should call FinishStartingAdditionalThread once the
358 // lock is released if the return values is nonzero.
359 int WillRunWorkerTask(const SequencedTask
& task
);
360 void DidRunWorkerTask(const SequencedTask
& task
);
362 // Returns true if there are no threads currently running the given
364 bool IsSequenceTokenRunnable(int sequence_token_id
) const;
366 // Checks if all threads are busy and the addition of one more could run an
367 // additional task waiting in the queue. This must be called from within
370 // If another thread is helpful, this will mark the thread as being in the
371 // process of starting and returns the index of the new thread which will be
372 // 0 or more. The caller should then call FinishStartingAdditionalThread to
373 // complete initialization once the lock is released.
375 // If another thread is not necessary, returne 0;
377 // See the implementedion for more.
378 int PrepareToStartAdditionalThreadIfHelpful();
380 // The second part of thread creation after
381 // PrepareToStartAdditionalThreadIfHelpful with the thread number it
382 // generated. This actually creates the thread and should be called outside
383 // the lock to avoid blocking important work starting a thread in the lock.
384 void FinishStartingAdditionalThread(int thread_number
);
386 // Signal |has_work_| and increment |has_work_signal_count_|.
387 void SignalHasWork();
389 // Checks whether there is work left that's blocking shutdown. Must be
390 // called inside the lock.
391 bool CanShutdown() const;
393 SequencedWorkerPool
* const worker_pool_
;
395 // The last sequence number used. Managed by GetSequenceToken, since this
396 // only does threadsafe increment operations, you do not need to hold the
397 // lock. This is class-static to make SequenceTokens issued by
398 // GetSequenceToken unique across SequencedWorkerPool instances.
399 static base::StaticAtomicSequenceNumber g_last_sequence_number_
;
401 // This lock protects |everything in this class|. Do not read or modify
402 // anything without holding this lock. Do not block while holding this
406 // Condition variable that is waited on by worker threads until new
407 // tasks are posted or shutdown starts.
408 ConditionVariable has_work_cv_
;
410 // Condition variable that is waited on by non-worker threads (in
411 // Shutdown()) until CanShutdown() goes to true.
412 ConditionVariable can_shutdown_cv_
;
414 // The maximum number of worker threads we'll create.
415 const size_t max_threads_
;
417 const std::string thread_name_prefix_
;
419 // Associates all known sequence token names with their IDs.
420 std::map
<std::string
, int> named_sequence_tokens_
;
422 // Owning pointers to all threads we've created so far, indexed by
423 // ID. Since we lazily create threads, this may be less than
424 // max_threads_ and will be initially empty.
425 typedef std::map
<PlatformThreadId
, linked_ptr
<Worker
> > ThreadMap
;
428 // Set to true when we're in the process of creating another thread.
429 // See PrepareToStartAdditionalThreadIfHelpful for more.
430 bool thread_being_created_
;
432 // Number of threads currently waiting for work.
433 size_t waiting_thread_count_
;
435 // Number of threads currently running tasks that have the BLOCK_SHUTDOWN
436 // or SKIP_ON_SHUTDOWN flag set.
437 size_t blocking_shutdown_thread_count_
;
439 // A set of all pending tasks in time-to-run order. These are tasks that are
440 // either waiting for a thread to run on, waiting for their time to run,
441 // or blocked on a previous task in their sequence. We have to iterate over
442 // the tasks by time-to-run order, so we use the set instead of the
443 // traditional priority_queue.
444 typedef std::set
<SequencedTask
, SequencedTaskLessThan
> PendingTaskSet
;
445 PendingTaskSet pending_tasks_
;
447 // The next sequence number for a new sequenced task.
448 int64 next_sequence_task_number_
;
450 // Number of tasks in the pending_tasks_ list that are marked as blocking
452 size_t blocking_shutdown_pending_task_count_
;
454 // Lists all sequence tokens currently executing.
455 std::set
<int> current_sequences_
;
457 // An ID for each posted task to distinguish the task from others in traces.
460 // Set when Shutdown is called and no further tasks should be
461 // allowed, though we may still be running existing tasks.
462 bool shutdown_called_
;
464 // The number of new BLOCK_SHUTDOWN tasks that may be posted after Shudown()
466 int max_blocking_tasks_after_shutdown_
;
468 // State used to cleanup for testing, all guarded by lock_.
469 CleanupState cleanup_state_
;
470 size_t cleanup_idlers_
;
471 ConditionVariable cleanup_cv_
;
473 TestingObserver
* const testing_observer_
;
475 DISALLOW_COPY_AND_ASSIGN(Inner
);
478 // Worker definitions ---------------------------------------------------------
480 SequencedWorkerPool::Worker::Worker(
481 const scoped_refptr
<SequencedWorkerPool
>& worker_pool
,
483 const std::string
& prefix
)
484 : SimpleThread(prefix
+ StringPrintf("Worker%d", thread_number
)),
485 worker_pool_(worker_pool
),
486 running_shutdown_behavior_(CONTINUE_ON_SHUTDOWN
) {
490 SequencedWorkerPool::Worker::~Worker() {
493 void SequencedWorkerPool::Worker::Run() {
495 win::ScopedCOMInitializer com_initializer
;
498 // Store a pointer to the running sequence in thread local storage for
499 // static function access.
500 g_lazy_tls_ptr
.Get().Set(&running_sequence_
);
502 // Just jump back to the Inner object to run the thread, since it has all the
503 // tracking information and queues. It might be more natural to implement
504 // using DelegateSimpleThread and have Inner implement the Delegate to avoid
505 // having these worker objects at all, but that method lacks the ability to
506 // send thread-specific information easily to the thread loop.
507 worker_pool_
->inner_
->ThreadLoop(this);
508 // Release our cyclic reference once we're done.
512 // Inner definitions ---------------------------------------------------------
514 SequencedWorkerPool::Inner::Inner(
515 SequencedWorkerPool
* worker_pool
,
517 const std::string
& thread_name_prefix
,
518 TestingObserver
* observer
)
519 : worker_pool_(worker_pool
),
521 has_work_cv_(&lock_
),
522 can_shutdown_cv_(&lock_
),
523 max_threads_(max_threads
),
524 thread_name_prefix_(thread_name_prefix
),
525 thread_being_created_(false),
526 waiting_thread_count_(0),
527 blocking_shutdown_thread_count_(0),
528 next_sequence_task_number_(0),
529 blocking_shutdown_pending_task_count_(0),
531 shutdown_called_(false),
532 max_blocking_tasks_after_shutdown_(0),
533 cleanup_state_(CLEANUP_DONE
),
536 testing_observer_(observer
) {}
538 SequencedWorkerPool::Inner::~Inner() {
539 // You must call Shutdown() before destroying the pool.
540 DCHECK(shutdown_called_
);
542 // Need to explicitly join with the threads before they're destroyed or else
543 // they will be running when our object is half torn down.
544 for (ThreadMap::iterator it
= threads_
.begin(); it
!= threads_
.end(); ++it
)
548 if (testing_observer_
)
549 testing_observer_
->OnDestruct();
552 SequencedWorkerPool::SequenceToken
553 SequencedWorkerPool::Inner::GetSequenceToken() {
554 // Need to add one because StaticAtomicSequenceNumber starts at zero, which
555 // is used as a sentinel value in SequenceTokens.
556 return SequenceToken(g_last_sequence_number_
.GetNext() + 1);
559 SequencedWorkerPool::SequenceToken
560 SequencedWorkerPool::Inner::GetNamedSequenceToken(const std::string
& name
) {
561 AutoLock
lock(lock_
);
562 return SequenceToken(LockedGetNamedTokenID(name
));
565 bool SequencedWorkerPool::Inner::PostTask(
566 const std::string
* optional_token_name
,
567 SequenceToken sequence_token
,
568 WorkerShutdown shutdown_behavior
,
569 const tracked_objects::Location
& from_here
,
572 DCHECK(delay
== TimeDelta() || shutdown_behavior
== SKIP_ON_SHUTDOWN
);
573 SequencedTask
sequenced(from_here
);
574 sequenced
.sequence_token_id
= sequence_token
.id_
;
575 sequenced
.shutdown_behavior
= shutdown_behavior
;
576 sequenced
.posted_from
= from_here
;
578 shutdown_behavior
== BLOCK_SHUTDOWN
?
579 base::MakeCriticalClosure(task
) : task
;
580 sequenced
.time_to_run
= TimeTicks::Now() + delay
;
582 int create_thread_id
= 0;
584 AutoLock
lock(lock_
);
585 if (shutdown_called_
) {
586 if (shutdown_behavior
!= BLOCK_SHUTDOWN
||
587 LockedCurrentThreadShutdownBehavior() == CONTINUE_ON_SHUTDOWN
) {
590 if (max_blocking_tasks_after_shutdown_
<= 0) {
591 DLOG(WARNING
) << "BLOCK_SHUTDOWN task disallowed";
594 max_blocking_tasks_after_shutdown_
-= 1;
597 // The trace_id is used for identifying the task in about:tracing.
598 sequenced
.trace_id
= trace_id_
++;
600 TRACE_EVENT_FLOW_BEGIN0(TRACE_DISABLED_BY_DEFAULT("toplevel.flow"),
601 "SequencedWorkerPool::PostTask",
602 TRACE_ID_MANGLE(GetTaskTraceID(sequenced
, static_cast<void*>(this))));
604 sequenced
.sequence_task_number
= LockedGetNextSequenceTaskNumber();
606 // Now that we have the lock, apply the named token rules.
607 if (optional_token_name
)
608 sequenced
.sequence_token_id
= LockedGetNamedTokenID(*optional_token_name
);
610 pending_tasks_
.insert(sequenced
);
611 if (shutdown_behavior
== BLOCK_SHUTDOWN
)
612 blocking_shutdown_pending_task_count_
++;
614 create_thread_id
= PrepareToStartAdditionalThreadIfHelpful();
617 // Actually start the additional thread or signal an existing one now that
618 // we're outside the lock.
619 if (create_thread_id
)
620 FinishStartingAdditionalThread(create_thread_id
);
627 bool SequencedWorkerPool::Inner::RunsTasksOnCurrentThread() const {
628 AutoLock
lock(lock_
);
629 return ContainsKey(threads_
, PlatformThread::CurrentId());
632 bool SequencedWorkerPool::Inner::IsRunningSequenceOnCurrentThread(
633 SequenceToken sequence_token
) const {
634 AutoLock
lock(lock_
);
635 ThreadMap::const_iterator found
= threads_
.find(PlatformThread::CurrentId());
636 if (found
== threads_
.end())
638 return sequence_token
.Equals(found
->second
->running_sequence());
641 // See https://code.google.com/p/chromium/issues/detail?id=168415
642 void SequencedWorkerPool::Inner::CleanupForTesting() {
643 DCHECK(!RunsTasksOnCurrentThread());
644 base::ThreadRestrictions::ScopedAllowWait allow_wait
;
645 AutoLock
lock(lock_
);
646 CHECK_EQ(CLEANUP_DONE
, cleanup_state_
);
647 if (shutdown_called_
)
649 if (pending_tasks_
.empty() && waiting_thread_count_
== threads_
.size())
651 cleanup_state_
= CLEANUP_REQUESTED
;
653 has_work_cv_
.Signal();
654 while (cleanup_state_
!= CLEANUP_DONE
)
658 void SequencedWorkerPool::Inner::SignalHasWorkForTesting() {
662 void SequencedWorkerPool::Inner::Shutdown(
663 int max_new_blocking_tasks_after_shutdown
) {
664 DCHECK_GE(max_new_blocking_tasks_after_shutdown
, 0);
666 AutoLock
lock(lock_
);
667 // Cleanup and Shutdown should not be called concurrently.
668 CHECK_EQ(CLEANUP_DONE
, cleanup_state_
);
669 if (shutdown_called_
)
671 shutdown_called_
= true;
672 max_blocking_tasks_after_shutdown_
= max_new_blocking_tasks_after_shutdown
;
674 // Tickle the threads. This will wake up a waiting one so it will know that
675 // it can exit, which in turn will wake up any other waiting ones.
678 // There are no pending or running tasks blocking shutdown, we're done.
683 // If we're here, then something is blocking shutdown. So wait for
684 // CanShutdown() to go to true.
686 if (testing_observer_
)
687 testing_observer_
->WillWaitForShutdown();
689 #if !defined(OS_NACL)
690 TimeTicks shutdown_wait_begin
= TimeTicks::Now();
694 base::ThreadRestrictions::ScopedAllowWait allow_wait
;
695 AutoLock
lock(lock_
);
696 while (!CanShutdown())
697 can_shutdown_cv_
.Wait();
699 #if !defined(OS_NACL)
700 UMA_HISTOGRAM_TIMES("SequencedWorkerPool.ShutdownDelayTime",
701 TimeTicks::Now() - shutdown_wait_begin
);
705 bool SequencedWorkerPool::Inner::IsShutdownInProgress() {
706 AutoLock
lock(lock_
);
707 return shutdown_called_
;
710 void SequencedWorkerPool::Inner::ThreadLoop(Worker
* this_worker
) {
712 AutoLock
lock(lock_
);
713 DCHECK(thread_being_created_
);
714 thread_being_created_
= false;
715 std::pair
<ThreadMap::iterator
, bool> result
=
717 std::make_pair(this_worker
->tid(), make_linked_ptr(this_worker
)));
718 DCHECK(result
.second
);
721 #if defined(OS_MACOSX)
722 base::mac::ScopedNSAutoreleasePool autorelease_pool
;
727 // See GetWork for what delete_these_outside_lock is doing.
730 std::vector
<Closure
> delete_these_outside_lock
;
731 GetWorkStatus status
=
732 GetWork(&task
, &wait_time
, &delete_these_outside_lock
);
733 if (status
== GET_WORK_FOUND
) {
734 TRACE_EVENT_FLOW_END0(TRACE_DISABLED_BY_DEFAULT("toplevel.flow"),
735 "SequencedWorkerPool::PostTask",
736 TRACE_ID_MANGLE(GetTaskTraceID(task
, static_cast<void*>(this))));
737 TRACE_EVENT2("toplevel", "SequencedWorkerPool::ThreadLoop",
738 "src_file", task
.posted_from
.file_name(),
739 "src_func", task
.posted_from
.function_name());
740 int new_thread_id
= WillRunWorkerTask(task
);
742 AutoUnlock
unlock(lock_
);
743 // There may be more work available, so wake up another
744 // worker thread. (Technically not required, since we
745 // already get a signal for each new task, but it doesn't
748 delete_these_outside_lock
.clear();
750 // Complete thread creation outside the lock if necessary.
752 FinishStartingAdditionalThread(new_thread_id
);
754 this_worker
->set_running_task_info(
755 SequenceToken(task
.sequence_token_id
), task
.shutdown_behavior
);
757 tracked_objects::ThreadData::PrepareForStartOfRun(task
.birth_tally
);
758 tracked_objects::TaskStopwatch stopwatch
;
763 tracked_objects::ThreadData::TallyRunOnNamedThreadIfTracking(
766 // Make sure our task is erased outside the lock for the
767 // same reason we do this with delete_these_oustide_lock.
768 // Also, do it before calling set_running_task_info() so
769 // that sequence-checking from within the task's destructor
771 task
.task
= Closure();
773 this_worker
->set_running_task_info(
774 SequenceToken(), CONTINUE_ON_SHUTDOWN
);
776 DidRunWorkerTask(task
); // Must be done inside the lock.
777 } else if (cleanup_state_
== CLEANUP_RUNNING
) {
779 case GET_WORK_WAIT
: {
780 AutoUnlock
unlock(lock_
);
781 delete_these_outside_lock
.clear();
784 case GET_WORK_NOT_FOUND
:
785 CHECK(delete_these_outside_lock
.empty());
786 cleanup_state_
= CLEANUP_FINISHING
;
787 cleanup_cv_
.Broadcast();
793 // When we're terminating and there's no more work, we can
794 // shut down, other workers can complete any pending or new tasks.
795 // We can get additional tasks posted after shutdown_called_ is set
796 // but only worker threads are allowed to post tasks at that time, and
797 // the workers responsible for posting those tasks will be available
798 // to run them. Also, there may be some tasks stuck behind running
799 // ones with the same sequence token, but additional threads won't
801 if (shutdown_called_
&& blocking_shutdown_pending_task_count_
== 0) {
802 AutoUnlock
unlock(lock_
);
803 delete_these_outside_lock
.clear();
807 // No work was found, but there are tasks that need deletion. The
808 // deletion must happen outside of the lock.
809 if (delete_these_outside_lock
.size()) {
810 AutoUnlock
unlock(lock_
);
811 delete_these_outside_lock
.clear();
813 // Since the lock has been released, |status| may no longer be
814 // accurate. It might read GET_WORK_WAIT even if there are tasks
815 // ready to perform work. Jump to the top of the loop to recalculate
820 waiting_thread_count_
++;
823 case GET_WORK_NOT_FOUND
:
827 has_work_cv_
.TimedWait(wait_time
);
832 waiting_thread_count_
--;
837 // We noticed we should exit. Wake up the next worker so it knows it should
838 // exit as well (because the Shutdown() code only signals once).
841 // Possibly unblock shutdown.
842 can_shutdown_cv_
.Signal();
845 void SequencedWorkerPool::Inner::HandleCleanup() {
846 lock_
.AssertAcquired();
847 if (cleanup_state_
== CLEANUP_DONE
)
849 if (cleanup_state_
== CLEANUP_REQUESTED
) {
850 // We win, we get to do the cleanup as soon as the others wise up and idle.
851 cleanup_state_
= CLEANUP_STARTING
;
852 while (thread_being_created_
||
853 cleanup_idlers_
!= threads_
.size() - 1) {
854 has_work_cv_
.Signal();
857 cleanup_state_
= CLEANUP_RUNNING
;
860 if (cleanup_state_
== CLEANUP_STARTING
) {
861 // Another worker thread is cleaning up, we idle here until thats done.
863 cleanup_cv_
.Broadcast();
864 while (cleanup_state_
!= CLEANUP_FINISHING
) {
868 cleanup_cv_
.Broadcast();
871 if (cleanup_state_
== CLEANUP_FINISHING
) {
872 // We wait for all idlers to wake up prior to being DONE.
873 while (cleanup_idlers_
!= 0) {
874 cleanup_cv_
.Broadcast();
877 if (cleanup_state_
== CLEANUP_FINISHING
) {
878 cleanup_state_
= CLEANUP_DONE
;
879 cleanup_cv_
.Signal();
885 int SequencedWorkerPool::Inner::LockedGetNamedTokenID(
886 const std::string
& name
) {
887 lock_
.AssertAcquired();
888 DCHECK(!name
.empty());
890 std::map
<std::string
, int>::const_iterator found
=
891 named_sequence_tokens_
.find(name
);
892 if (found
!= named_sequence_tokens_
.end())
893 return found
->second
; // Got an existing one.
895 // Create a new one for this name.
896 SequenceToken result
= GetSequenceToken();
897 named_sequence_tokens_
.insert(std::make_pair(name
, result
.id_
));
901 int64
SequencedWorkerPool::Inner::LockedGetNextSequenceTaskNumber() {
902 lock_
.AssertAcquired();
903 // We assume that we never create enough tasks to wrap around.
904 return next_sequence_task_number_
++;
907 SequencedWorkerPool::WorkerShutdown
908 SequencedWorkerPool::Inner::LockedCurrentThreadShutdownBehavior() const {
909 lock_
.AssertAcquired();
910 ThreadMap::const_iterator found
= threads_
.find(PlatformThread::CurrentId());
911 if (found
== threads_
.end())
912 return CONTINUE_ON_SHUTDOWN
;
913 return found
->second
->running_shutdown_behavior();
916 SequencedWorkerPool::Inner::GetWorkStatus
SequencedWorkerPool::Inner::GetWork(
918 TimeDelta
* wait_time
,
919 std::vector
<Closure
>* delete_these_outside_lock
) {
920 lock_
.AssertAcquired();
922 // Find the next task with a sequence token that's not currently in use.
923 // If the token is in use, that means another thread is running something
924 // in that sequence, and we can't run it without going out-of-order.
926 // This algorithm is simple and fair, but inefficient in some cases. For
927 // example, say somebody schedules 1000 slow tasks with the same sequence
928 // number. We'll have to go through all those tasks each time we feel like
929 // there might be work to schedule. If this proves to be a problem, we
930 // should make this more efficient.
932 // One possible enhancement would be to keep a map from sequence ID to a
933 // list of pending but currently blocked SequencedTasks for that ID.
934 // When a worker finishes a task of one sequence token, it can pick up the
935 // next one from that token right away.
937 // This may lead to starvation if there are sufficient numbers of sequences
938 // in use. To alleviate this, we could add an incrementing priority counter
939 // to each SequencedTask. Then maintain a priority_queue of all runnable
940 // tasks, sorted by priority counter. When a sequenced task is completed
941 // we would pop the head element off of that tasks pending list and add it
942 // to the priority queue. Then we would run the first item in the priority
945 GetWorkStatus status
= GET_WORK_NOT_FOUND
;
946 int unrunnable_tasks
= 0;
947 PendingTaskSet::iterator i
= pending_tasks_
.begin();
948 // We assume that the loop below doesn't take too long and so we can just do
949 // a single call to TimeTicks::Now().
950 const TimeTicks current_time
= TimeTicks::Now();
951 while (i
!= pending_tasks_
.end()) {
952 if (!IsSequenceTokenRunnable(i
->sequence_token_id
)) {
958 if (shutdown_called_
&& i
->shutdown_behavior
!= BLOCK_SHUTDOWN
) {
959 // We're shutting down and the task we just found isn't blocking
960 // shutdown. Delete it and get more work.
962 // Note that we do not want to delete unrunnable tasks. Deleting a task
963 // can have side effects (like freeing some objects) and deleting a
964 // task that's supposed to run after one that's currently running could
965 // cause an obscure crash.
967 // We really want to delete these tasks outside the lock in case the
968 // closures are holding refs to objects that want to post work from
969 // their destructorss (which would deadlock). The closures are
970 // internally refcounted, so we just need to keep a copy of them alive
971 // until the lock is exited. The calling code can just clear() the
972 // vector they passed to us once the lock is exited to make this
974 delete_these_outside_lock
->push_back(i
->task
);
975 pending_tasks_
.erase(i
++);
979 if (i
->time_to_run
> current_time
) {
980 // The time to run has not come yet.
981 *wait_time
= i
->time_to_run
- current_time
;
982 status
= GET_WORK_WAIT
;
983 if (cleanup_state_
== CLEANUP_RUNNING
) {
984 // Deferred tasks are deleted when cleaning up, see Inner::ThreadLoop.
985 delete_these_outside_lock
->push_back(i
->task
);
986 pending_tasks_
.erase(i
);
991 // Found a runnable task.
993 pending_tasks_
.erase(i
);
994 if (task
->shutdown_behavior
== BLOCK_SHUTDOWN
) {
995 blocking_shutdown_pending_task_count_
--;
998 status
= GET_WORK_FOUND
;
1005 int SequencedWorkerPool::Inner::WillRunWorkerTask(const SequencedTask
& task
) {
1006 lock_
.AssertAcquired();
1008 // Mark the task's sequence number as in use.
1009 if (task
.sequence_token_id
)
1010 current_sequences_
.insert(task
.sequence_token_id
);
1012 // Ensure that threads running tasks posted with either SKIP_ON_SHUTDOWN
1013 // or BLOCK_SHUTDOWN will prevent shutdown until that task or thread
1015 if (task
.shutdown_behavior
!= CONTINUE_ON_SHUTDOWN
)
1016 blocking_shutdown_thread_count_
++;
1018 // We just picked up a task. Since StartAdditionalThreadIfHelpful only
1019 // creates a new thread if there is no free one, there is a race when posting
1020 // tasks that many tasks could have been posted before a thread started
1021 // running them, so only one thread would have been created. So we also check
1022 // whether we should create more threads after removing our task from the
1023 // queue, which also has the nice side effect of creating the workers from
1024 // background threads rather than the main thread of the app.
1026 // If another thread wasn't created, we want to wake up an existing thread
1027 // if there is one waiting to pick up the next task.
1029 // Note that we really need to do this *before* running the task, not
1030 // after. Otherwise, if more than one task is posted, the creation of the
1031 // second thread (since we only create one at a time) will be blocked by
1032 // the execution of the first task, which could be arbitrarily long.
1033 return PrepareToStartAdditionalThreadIfHelpful();
1036 void SequencedWorkerPool::Inner::DidRunWorkerTask(const SequencedTask
& task
) {
1037 lock_
.AssertAcquired();
1039 if (task
.shutdown_behavior
!= CONTINUE_ON_SHUTDOWN
) {
1040 DCHECK_GT(blocking_shutdown_thread_count_
, 0u);
1041 blocking_shutdown_thread_count_
--;
1044 if (task
.sequence_token_id
)
1045 current_sequences_
.erase(task
.sequence_token_id
);
1048 bool SequencedWorkerPool::Inner::IsSequenceTokenRunnable(
1049 int sequence_token_id
) const {
1050 lock_
.AssertAcquired();
1051 return !sequence_token_id
||
1052 current_sequences_
.find(sequence_token_id
) ==
1053 current_sequences_
.end();
1056 int SequencedWorkerPool::Inner::PrepareToStartAdditionalThreadIfHelpful() {
1057 lock_
.AssertAcquired();
1058 // How thread creation works:
1060 // We'de like to avoid creating threads with the lock held. However, we
1061 // need to be sure that we have an accurate accounting of the threads for
1062 // proper Joining and deltion on shutdown.
1064 // We need to figure out if we need another thread with the lock held, which
1065 // is what this function does. It then marks us as in the process of creating
1066 // a thread. When we do shutdown, we wait until the thread_being_created_
1067 // flag is cleared, which ensures that the new thread is properly added to
1068 // all the data structures and we can't leak it. Once shutdown starts, we'll
1069 // refuse to create more threads or they would be leaked.
1071 // Note that this creates a mostly benign race condition on shutdown that
1072 // will cause fewer workers to be created than one would expect. It isn't
1073 // much of an issue in real life, but affects some tests. Since we only spawn
1074 // one worker at a time, the following sequence of events can happen:
1076 // 1. Main thread posts a bunch of unrelated tasks that would normally be
1077 // run on separate threads.
1078 // 2. The first task post causes us to start a worker. Other tasks do not
1079 // cause a worker to start since one is pending.
1080 // 3. Main thread initiates shutdown.
1081 // 4. No more threads are created since the shutdown_called_ flag is set.
1083 // The result is that one may expect that max_threads_ workers to be created
1084 // given the workload, but in reality fewer may be created because the
1085 // sequence of thread creation on the background threads is racing with the
1087 if (!shutdown_called_
&&
1088 !thread_being_created_
&&
1089 cleanup_state_
== CLEANUP_DONE
&&
1090 threads_
.size() < max_threads_
&&
1091 waiting_thread_count_
== 0) {
1092 // We could use an additional thread if there's work to be done.
1093 for (PendingTaskSet::const_iterator i
= pending_tasks_
.begin();
1094 i
!= pending_tasks_
.end(); ++i
) {
1095 if (IsSequenceTokenRunnable(i
->sequence_token_id
)) {
1096 // Found a runnable task, mark the thread as being started.
1097 thread_being_created_
= true;
1098 return static_cast<int>(threads_
.size() + 1);
1105 void SequencedWorkerPool::Inner::FinishStartingAdditionalThread(
1106 int thread_number
) {
1107 // Called outside of the lock.
1108 DCHECK_GT(thread_number
, 0);
1110 // The worker is assigned to the list when the thread actually starts, which
1111 // will manage the memory of the pointer.
1112 new Worker(worker_pool_
, thread_number
, thread_name_prefix_
);
1115 void SequencedWorkerPool::Inner::SignalHasWork() {
1116 has_work_cv_
.Signal();
1117 if (testing_observer_
) {
1118 testing_observer_
->OnHasWork();
1122 bool SequencedWorkerPool::Inner::CanShutdown() const {
1123 lock_
.AssertAcquired();
1124 // See PrepareToStartAdditionalThreadIfHelpful for how thread creation works.
1125 return !thread_being_created_
&&
1126 blocking_shutdown_thread_count_
== 0 &&
1127 blocking_shutdown_pending_task_count_
== 0;
1130 base::StaticAtomicSequenceNumber
1131 SequencedWorkerPool::Inner::g_last_sequence_number_
;
1133 // SequencedWorkerPool --------------------------------------------------------
1136 SequencedWorkerPool::SequenceToken
1137 SequencedWorkerPool::GetSequenceTokenForCurrentThread() {
1138 // Don't construct lazy instance on check.
1139 if (g_lazy_tls_ptr
== NULL
)
1140 return SequenceToken();
1142 SequencedWorkerPool::SequenceToken
* token
= g_lazy_tls_ptr
.Get().Get();
1144 return SequenceToken();
1148 SequencedWorkerPool::SequencedWorkerPool(
1150 const std::string
& thread_name_prefix
)
1151 : constructor_message_loop_(MessageLoopProxy::current()),
1152 inner_(new Inner(this, max_threads
, thread_name_prefix
, NULL
)) {
1155 SequencedWorkerPool::SequencedWorkerPool(
1157 const std::string
& thread_name_prefix
,
1158 TestingObserver
* observer
)
1159 : constructor_message_loop_(MessageLoopProxy::current()),
1160 inner_(new Inner(this, max_threads
, thread_name_prefix
, observer
)) {
1163 SequencedWorkerPool::~SequencedWorkerPool() {}
1165 void SequencedWorkerPool::OnDestruct() const {
1166 DCHECK(constructor_message_loop_
.get());
1167 // Avoid deleting ourselves on a worker thread (which would
1169 if (RunsTasksOnCurrentThread()) {
1170 constructor_message_loop_
->DeleteSoon(FROM_HERE
, this);
1176 SequencedWorkerPool::SequenceToken
SequencedWorkerPool::GetSequenceToken() {
1177 return inner_
->GetSequenceToken();
1180 SequencedWorkerPool::SequenceToken
SequencedWorkerPool::GetNamedSequenceToken(
1181 const std::string
& name
) {
1182 return inner_
->GetNamedSequenceToken(name
);
1185 scoped_refptr
<SequencedTaskRunner
> SequencedWorkerPool::GetSequencedTaskRunner(
1186 SequenceToken token
) {
1187 return GetSequencedTaskRunnerWithShutdownBehavior(token
, BLOCK_SHUTDOWN
);
1190 scoped_refptr
<SequencedTaskRunner
>
1191 SequencedWorkerPool::GetSequencedTaskRunnerWithShutdownBehavior(
1192 SequenceToken token
, WorkerShutdown shutdown_behavior
) {
1193 return new SequencedWorkerPoolSequencedTaskRunner(
1194 this, token
, shutdown_behavior
);
1197 scoped_refptr
<TaskRunner
>
1198 SequencedWorkerPool::GetTaskRunnerWithShutdownBehavior(
1199 WorkerShutdown shutdown_behavior
) {
1200 return new SequencedWorkerPoolTaskRunner(this, shutdown_behavior
);
1203 bool SequencedWorkerPool::PostWorkerTask(
1204 const tracked_objects::Location
& from_here
,
1205 const Closure
& task
) {
1206 return inner_
->PostTask(NULL
, SequenceToken(), BLOCK_SHUTDOWN
,
1207 from_here
, task
, TimeDelta());
1210 bool SequencedWorkerPool::PostDelayedWorkerTask(
1211 const tracked_objects::Location
& from_here
,
1212 const Closure
& task
,
1214 WorkerShutdown shutdown_behavior
=
1215 delay
== TimeDelta() ? BLOCK_SHUTDOWN
: SKIP_ON_SHUTDOWN
;
1216 return inner_
->PostTask(NULL
, SequenceToken(), shutdown_behavior
,
1217 from_here
, task
, delay
);
1220 bool SequencedWorkerPool::PostWorkerTaskWithShutdownBehavior(
1221 const tracked_objects::Location
& from_here
,
1222 const Closure
& task
,
1223 WorkerShutdown shutdown_behavior
) {
1224 return inner_
->PostTask(NULL
, SequenceToken(), shutdown_behavior
,
1225 from_here
, task
, TimeDelta());
1228 bool SequencedWorkerPool::PostSequencedWorkerTask(
1229 SequenceToken sequence_token
,
1230 const tracked_objects::Location
& from_here
,
1231 const Closure
& task
) {
1232 return inner_
->PostTask(NULL
, sequence_token
, BLOCK_SHUTDOWN
,
1233 from_here
, task
, TimeDelta());
1236 bool SequencedWorkerPool::PostDelayedSequencedWorkerTask(
1237 SequenceToken sequence_token
,
1238 const tracked_objects::Location
& from_here
,
1239 const Closure
& task
,
1241 WorkerShutdown shutdown_behavior
=
1242 delay
== TimeDelta() ? BLOCK_SHUTDOWN
: SKIP_ON_SHUTDOWN
;
1243 return inner_
->PostTask(NULL
, sequence_token
, shutdown_behavior
,
1244 from_here
, task
, delay
);
1247 bool SequencedWorkerPool::PostNamedSequencedWorkerTask(
1248 const std::string
& token_name
,
1249 const tracked_objects::Location
& from_here
,
1250 const Closure
& task
) {
1251 DCHECK(!token_name
.empty());
1252 return inner_
->PostTask(&token_name
, SequenceToken(), BLOCK_SHUTDOWN
,
1253 from_here
, task
, TimeDelta());
1256 bool SequencedWorkerPool::PostSequencedWorkerTaskWithShutdownBehavior(
1257 SequenceToken sequence_token
,
1258 const tracked_objects::Location
& from_here
,
1259 const Closure
& task
,
1260 WorkerShutdown shutdown_behavior
) {
1261 return inner_
->PostTask(NULL
, sequence_token
, shutdown_behavior
,
1262 from_here
, task
, TimeDelta());
1265 bool SequencedWorkerPool::PostDelayedTask(
1266 const tracked_objects::Location
& from_here
,
1267 const Closure
& task
,
1269 return PostDelayedWorkerTask(from_here
, task
, delay
);
1272 bool SequencedWorkerPool::RunsTasksOnCurrentThread() const {
1273 return inner_
->RunsTasksOnCurrentThread();
1276 bool SequencedWorkerPool::IsRunningSequenceOnCurrentThread(
1277 SequenceToken sequence_token
) const {
1278 return inner_
->IsRunningSequenceOnCurrentThread(sequence_token
);
1281 void SequencedWorkerPool::FlushForTesting() {
1282 inner_
->CleanupForTesting();
1285 void SequencedWorkerPool::SignalHasWorkForTesting() {
1286 inner_
->SignalHasWorkForTesting();
1289 void SequencedWorkerPool::Shutdown(int max_new_blocking_tasks_after_shutdown
) {
1290 DCHECK(constructor_message_loop_
->BelongsToCurrentThread());
1291 inner_
->Shutdown(max_new_blocking_tasks_after_shutdown
);
1294 bool SequencedWorkerPool::IsShutdownInProgress() {
1295 return inner_
->IsShutdownInProgress();