Index: base/threading/sequenced_worker_pool.cc |
=================================================================== |
--- base/threading/sequenced_worker_pool.cc (revision 116282) |
+++ base/threading/sequenced_worker_pool.cc (working copy) |
@@ -57,20 +57,31 @@ |
Inner(size_t max_threads, const std::string& thread_name_prefix); |
virtual ~Inner(); |
- // Backends for SequenceWorkerPool. |
SequenceToken GetSequenceToken(); |
+ |
SequenceToken GetNamedSequenceToken(const std::string& name); |
- bool PostTask(int sequence_token_id, |
+ |
+ // 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. |
+ bool PostTask(const std::string* optional_token_name, |
+ int sequence_token_id, |
SequencedWorkerPool::WorkerShutdown shutdown_behavior, |
const tracked_objects::Location& from_here, |
const base::Closure& task); |
+ |
void Shutdown(); |
+ |
void SetTestingObserver(SequencedWorkerPool::TestingObserver* observer); |
// Runs the worker loop on the background thread. |
void ThreadLoop(Worker* this_worker); |
private: |
+ // Called from within the lock, this converts the given token name into a |
+ // token ID, creating a new one if necessary. |
+ int LockedGetNamedTokenID(const std::string& name); |
+ |
// The calling code should clear the given delete_these_oustide_lock |
// vector the next time the lock is released. See the implementation for |
// a more detailed description. |
@@ -235,18 +246,11 @@ |
SequencedWorkerPool::Inner::GetNamedSequenceToken( |
const std::string& name) { |
base::AutoLock lock(lock_); |
- std::map<std::string, int>::const_iterator found = |
- named_sequence_tokens_.find(name); |
- if (found != named_sequence_tokens_.end()) |
- return SequenceToken(found->second); // Got an existing one. |
- |
- // Create a new one for this name. |
- SequenceToken result = GetSequenceToken(); |
- named_sequence_tokens_.insert(std::make_pair(name, result.id_)); |
- return result; |
+ return SequenceToken(LockedGetNamedTokenID(name)); |
} |
bool SequencedWorkerPool::Inner::PostTask( |
+ const std::string* optional_token_name, |
int sequence_token_id, |
SequencedWorkerPool::WorkerShutdown shutdown_behavior, |
const tracked_objects::Location& from_here, |
@@ -263,6 +267,10 @@ |
if (terminating_) |
return false; |
+ // Now that we have the lock, apply the named token rules. |
+ if (optional_token_name) |
+ sequenced.sequence_token_id = LockedGetNamedTokenID(*optional_token_name); |
+ |
pending_tasks_.push_back(sequenced); |
pending_task_count_++; |
if (shutdown_behavior == BLOCK_SHUTDOWN) |
@@ -377,6 +385,22 @@ |
cond_var_.Signal(); |
} |
+int SequencedWorkerPool::Inner::LockedGetNamedTokenID( |
+ const std::string& name) { |
+ lock_.AssertAcquired(); |
+ DCHECK(!name.empty()); |
+ |
+ std::map<std::string, int>::const_iterator found = |
+ named_sequence_tokens_.find(name); |
+ if (found != named_sequence_tokens_.end()) |
+ return found->second; // Got an existing one. |
+ |
+ // Create a new one for this name. |
+ SequenceToken result = GetSequenceToken(); |
+ named_sequence_tokens_.insert(std::make_pair(name, result.id_)); |
+ return result.id_; |
+} |
+ |
bool SequencedWorkerPool::Inner::GetWork( |
SequencedTask* task, |
std::vector<base::Closure>* delete_these_outside_lock) { |
@@ -593,30 +617,38 @@ |
bool SequencedWorkerPool::PostWorkerTask( |
const tracked_objects::Location& from_here, |
const base::Closure& task) { |
- return inner_->PostTask(0, BLOCK_SHUTDOWN, from_here, task); |
+ return inner_->PostTask(NULL, 0, BLOCK_SHUTDOWN, from_here, task); |
} |
bool SequencedWorkerPool::PostWorkerTaskWithShutdownBehavior( |
const tracked_objects::Location& from_here, |
const base::Closure& task, |
WorkerShutdown shutdown_behavior) { |
- return inner_->PostTask(0, shutdown_behavior, from_here, task); |
+ return inner_->PostTask(NULL, 0, shutdown_behavior, from_here, task); |
} |
bool SequencedWorkerPool::PostSequencedWorkerTask( |
SequenceToken sequence_token, |
const tracked_objects::Location& from_here, |
const base::Closure& task) { |
- return inner_->PostTask(sequence_token.id_, BLOCK_SHUTDOWN, |
+ return inner_->PostTask(NULL, sequence_token.id_, BLOCK_SHUTDOWN, |
from_here, task); |
} |
+bool SequencedWorkerPool::PostNamedSequencedWorkerTask( |
+ const std::string& token_name, |
+ const tracked_objects::Location& from_here, |
+ const base::Closure& task) { |
+ DCHECK(!token_name.empty()); |
+ return inner_->PostTask(&token_name, 0, BLOCK_SHUTDOWN, from_here, task); |
+} |
+ |
bool SequencedWorkerPool::PostSequencedWorkerTaskWithShutdownBehavior( |
SequenceToken sequence_token, |
const tracked_objects::Location& from_here, |
const base::Closure& task, |
WorkerShutdown shutdown_behavior) { |
- return inner_->PostTask(sequence_token.id_, shutdown_behavior, |
+ return inner_->PostTask(NULL, sequence_token.id_, shutdown_behavior, |
from_here, task); |
} |