Index: base/threading/sequenced_worker_pool.cc |
diff --git a/base/threading/sequenced_worker_pool.cc b/base/threading/sequenced_worker_pool.cc |
index f98b23d5d6168a818178fe0c2e0a777edd618bd5..2042c975affca79c743ee6265c1fa0f89a93141a 100644 |
--- a/base/threading/sequenced_worker_pool.cc |
+++ b/base/threading/sequenced_worker_pool.cc |
@@ -15,6 +15,7 @@ |
#include "base/compiler_specific.h" |
#include "base/critical_closure.h" |
#include "base/debug/trace_event.h" |
+#include "base/lazy_instance.h" |
#include "base/logging.h" |
#include "base/memory/linked_ptr.h" |
#include "base/message_loop/message_loop_proxy.h" |
@@ -25,6 +26,7 @@ |
#include "base/synchronization/lock.h" |
#include "base/threading/platform_thread.h" |
#include "base/threading/simple_thread.h" |
+#include "base/threading/thread_local.h" |
#include "base/threading/thread_restrictions.h" |
#include "base/time/time.h" |
#include "base/tracked_objects.h" |
@@ -37,6 +39,9 @@ namespace base { |
namespace { |
+base::LazyInstance<base::ThreadLocalPointer<SequencedWorkerPool> > |
+ lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER; |
+ |
struct SequencedTask : public TrackingInfo { |
SequencedTask() |
: sequence_token_id(0), |
@@ -267,6 +272,8 @@ class SequencedWorkerPool::Inner { |
SequenceToken GetNamedSequenceToken(const std::string& name); |
+ bool GetCurrentThreadSequenceToken(SequenceToken* result_token) const; |
+ |
// This function accepts a name and an ID. If the name is null, the |
// token ID is used. This allows us to implement the optional name lookup |
// from a single function without having to enter the lock a separate time. |
@@ -479,6 +486,9 @@ SequencedWorkerPool::Worker::~Worker() { |
} |
void SequencedWorkerPool::Worker::Run() { |
+ // Store the SequencedWorkerPool associated with this worker thread. |
+ lazy_tls_ptr.Get().Set(worker_pool_.get()); |
+ |
// Just jump back to the Inner object to run the thread, since it has all the |
// tracking information and queues. It might be more natural to implement |
// using DelegateSimpleThread and have Inner implement the Delegate to avoid |
@@ -543,6 +553,17 @@ SequencedWorkerPool::Inner::GetNamedSequenceToken(const std::string& name) { |
return SequenceToken(LockedGetNamedTokenID(name)); |
} |
+bool SequencedWorkerPool::Inner::GetCurrentThreadSequenceToken( |
+ SequenceToken* result_token) const { |
+ DCHECK(result_token); |
+ AutoLock lock(lock_); |
+ ThreadMap::const_iterator found = threads_.find(PlatformThread::CurrentId()); |
+ if (found == threads_.end()) |
+ return false; |
+ *result_token = found->second->running_sequence(); |
+ return true; |
+} |
+ |
bool SequencedWorkerPool::Inner::PostTask( |
const std::string* optional_token_name, |
SequenceToken sequence_token, |
@@ -611,11 +632,10 @@ bool SequencedWorkerPool::Inner::RunsTasksOnCurrentThread() const { |
bool SequencedWorkerPool::Inner::IsRunningSequenceOnCurrentThread( |
SequenceToken sequence_token) const { |
- AutoLock lock(lock_); |
- ThreadMap::const_iterator found = threads_.find(PlatformThread::CurrentId()); |
- if (found == threads_.end()) |
+ SequenceToken current_thread_token; |
+ if (!GetCurrentThreadSequenceToken(¤t_thread_token)) |
return false; |
- return found->second->running_sequence().Equals(sequence_token); |
+ return current_thread_token.Equals(sequence_token); |
} |
// See https://code.google.com/p/chromium/issues/detail?id=168415 |
@@ -1090,6 +1110,11 @@ bool SequencedWorkerPool::Inner::CanShutdown() const { |
// SequencedWorkerPool -------------------------------------------------------- |
+// static |
+SequencedWorkerPool* SequencedWorkerPool::Get() { |
+ return lazy_tls_ptr.Get().Get(); |
+} |
+ |
SequencedWorkerPool::SequencedWorkerPool( |
size_t max_threads, |
const std::string& thread_name_prefix) |
@@ -1127,6 +1152,11 @@ SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetNamedSequenceToken( |
return inner_->GetNamedSequenceToken(name); |
} |
+bool SequencedWorkerPool::GetCurrentThreadSequenceToken( |
+ SequenceToken* result_token) const { |
+ return inner_->GetCurrentThreadSequenceToken(result_token); |
+} |
+ |
scoped_refptr<SequencedTaskRunner> SequencedWorkerPool::GetSequencedTaskRunner( |
SequenceToken token) { |
return GetSequencedTaskRunnerWithShutdownBehavior(token, BLOCK_SHUTDOWN); |