Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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 <deque> | |
| 8 #include <set> | |
| 9 | |
| 10 #include "base/atomicops.h" | |
| 11 #include "base/bind.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/metrics/histogram.h" | |
| 14 #include "base/stringprintf.h" | |
| 15 #include "base/synchronization/condition_variable.h" | |
| 16 #include "base/threading/simple_thread.h" | |
| 17 #include "base/threading/thread.h" | |
| 18 | |
| 19 namespace base { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 struct SequencedTask { | |
| 24 int sequence_token_id; | |
| 25 SequencedWorkerPool::WorkerShutdown shutdown_behavior; | |
| 26 tracked_objects::Location location; | |
| 27 base::Closure task; | |
| 28 }; | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 // Worker --------------------------------------------------------------------- | |
| 33 | |
| 34 class SequencedWorkerPool::Worker : public base::SimpleThread { | |
| 35 public: | |
| 36 Worker(SequencedWorkerPool::Inner* inner, | |
| 37 int thread_number, | |
| 38 const std::string& thread_name_prefix); | |
| 39 ~Worker(); | |
| 40 | |
| 41 // SimpleThread implementation. This actually runs the background thread. | |
| 42 virtual void Run(); | |
| 43 | |
| 44 private: | |
| 45 SequencedWorkerPool::Inner* inner_; | |
| 46 SequencedWorkerPool::WorkerShutdown current_shutdown_mode_; | |
| 47 | |
| 48 DISALLOW_COPY_AND_ASSIGN(Worker); | |
| 49 }; | |
| 50 | |
| 51 | |
| 52 // Inner ---------------------------------------------------------------------- | |
| 53 | |
| 54 class SequencedWorkerPool::Inner | |
| 55 : public base::RefCountedThreadSafe<SequencedWorkerPool::Inner> { | |
| 56 public: | |
| 57 Inner(size_t max_threads, const std::string& thread_name_prefix); | |
| 58 virtual ~Inner(); | |
| 59 | |
| 60 // Backends for SequenceWorkerPool. | |
| 61 SequenceToken GetSequenceToken(); | |
| 62 SequenceToken GetNamedSequenceToken(const std::string& name); | |
| 63 bool PostTask(int sequence_token_id, | |
| 64 SequencedWorkerPool::WorkerShutdown shutdown_behavior, | |
| 65 const tracked_objects::Location& from_here, | |
| 66 const base::Closure& task); | |
| 67 void Shutdown(); | |
| 68 void SetTestingObserver(SequencedWorkerPool::TestingObserver* observer); | |
| 69 | |
| 70 // Runs the worker loop on the background thread. | |
| 71 void ThreadLoop(Worker* this_worker); | |
| 72 | |
| 73 private: | |
| 74 // The calling code should clear the given delete_these_oustide_lock | |
| 75 // vector the next time the lock is released. See the implementation for | |
| 76 // a more detailed description. | |
| 77 bool GetWork(SequencedTask* task, | |
| 78 std::vector<base::Closure>* delete_these_outside_lock); | |
| 79 | |
| 80 // Peforms init and cleanup around running the given task. WillRun... | |
| 81 // returns the value from PrepareToStartAdditionalThreadIfNecessary. | |
| 82 // The calling code should call FinishStartingAdditionalThread once the | |
| 83 // lock is released if the return values is nonzero. | |
| 84 int WillRunWorkerTask(const SequencedTask& task); | |
| 85 void DidRunWorkerTask(const SequencedTask& task); | |
| 86 | |
| 87 // Returns true if there are no threads currently running the given | |
| 88 // sequence token. | |
| 89 bool IsSequenceTokenRunnable(int sequence_token_id) const; | |
| 90 | |
| 91 // Checks if all threads are busy and the addition of one more could run an | |
| 92 // additional task waiting in the queue. This must be called from within | |
| 93 // the lock. | |
| 94 // | |
| 95 // If another thread is helpful, this will mark the thread as being in the | |
| 96 // process of starting and returns the index of the new thread which will be | |
| 97 // 0 or more. The caller should then call FinishStartingAdditionalThread to | |
| 98 // complete initialization once the lock is released. | |
| 99 // | |
| 100 // If another thread is not necessary, returne 0; | |
| 101 // | |
| 102 // See the implementedion for more. | |
| 103 int PrepareToStartAdditionalThreadIfHelpful(); | |
| 104 | |
| 105 // The second part of thread creation after | |
| 106 // PrepareToStartAdditionalThreadIfHelpful with the thread number it | |
| 107 // generated. This actually creates the thread and should be called outside | |
| 108 // the lock to avoid blocking important work starting a thread in the lock. | |
| 109 void FinishStartingAdditionalThread(int thread_number); | |
| 110 | |
| 111 // Checks whether there is work left that's blocking shutdown. Must be | |
| 112 // called inside the lock. | |
| 113 bool CanShutdown() const; | |
| 114 | |
| 115 // The last sequence number used. Managed by GetSequenceToken, since this | |
| 116 // only does threadsafe increment operations, you do not need to hold the | |
| 117 // lock. | |
| 118 volatile base::subtle::Atomic32 last_sequence_number_; | |
| 119 | |
| 120 // This lock protects |everything in this class|. Do not read or modify | |
| 121 // anything without holding this lock. Do not block while holding this | |
| 122 // lock. | |
| 123 base::Lock lock_; | |
| 124 | |
| 125 // Condition variable used to wake up worker threads when a task is runnable. | |
| 126 base::ConditionVariable cond_var_; | |
| 127 | |
| 128 // The maximum number of worker threads we'll create. | |
| 129 size_t max_threads_; | |
| 130 | |
| 131 std::string thread_name_prefix_; | |
| 132 | |
| 133 // Associates all known sequence token names with their IDs. | |
| 134 std::map<std::string, int> named_sequence_tokens_; | |
| 135 | |
| 136 // Owning pointers to all threads we've created so far. Since we lazily | |
| 137 // create threads, this may be less than max_threads_ and will be initially | |
| 138 // empty. | |
| 139 std::vector<linked_ptr<Worker> > threads_; | |
| 140 | |
| 141 // Set to true when we're in the process of creating another thread. | |
| 142 // See PrepareToStartAdditionalThreadIfHelpful for more. | |
| 143 bool thread_being_created_; | |
| 144 | |
| 145 // Number of threads currently waiting for work. | |
| 146 size_t waiting_thread_count_; | |
| 147 | |
| 148 // Number of threads currently running tasks that have the BLOCK_SHUTDOWN | |
| 149 // flag set. | |
| 150 size_t blocking_shutdown_thread_count_; | |
| 151 | |
| 152 // In-order list of all pending tasks. These are tasks waiting for a thread | |
| 153 // to run on or that are blocked on a previous task in their sequence. | |
| 154 // | |
| 155 // We maintain the pending_task_count_ separately for metrics because | |
| 156 // list.size() can be linear time. | |
| 157 std::list<SequencedTask> pending_tasks_; | |
| 158 size_t pending_task_count_; | |
| 159 | |
| 160 // Number of tasks in the pending_tasks_ list that are marked as blocking | |
| 161 // shutdown. | |
| 162 size_t blocking_shutdown_pending_task_count_; | |
| 163 | |
| 164 // Lists all sequence tokens currently executing. | |
| 165 std::set<int> current_sequences_; | |
| 166 | |
| 167 // Set when the app is terminating and no further tasks should be allowed, | |
| 168 // though we may still be running existing tasks. | |
| 169 bool terminating_; | |
| 170 | |
| 171 // Set when Shutdown is called to do some assertions. | |
| 172 bool shutdown_called_; | |
| 173 | |
| 174 SequencedWorkerPool::TestingObserver* testing_observer_; | |
| 175 }; | |
| 176 | |
| 177 SequencedWorkerPool::Worker::Worker(SequencedWorkerPool::Inner* inner, | |
| 178 int thread_number, | |
| 179 const std::string& prefix) | |
| 180 : base::SimpleThread( | |
| 181 prefix + StringPrintf("Worker%d", thread_number).c_str()), | |
| 182 inner_(inner), | |
| 183 current_shutdown_mode_(SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) { | |
| 184 Start(); | |
| 185 } | |
| 186 | |
| 187 SequencedWorkerPool::Worker::~Worker() { | |
| 188 } | |
| 189 | |
| 190 void SequencedWorkerPool::Worker::Run() { | |
| 191 // Just jump back to the Inner object to run the thread, since it has all the | |
| 192 // tracking information and queues. It might be more natural to implement | |
| 193 // using DelegateSimpleThread and have Inner implement the Delegate to avoid | |
| 194 // having these worker objects at all, but that method lacks the ability to | |
| 195 // send thread-specific information easily to the thread loop. | |
| 196 inner_->ThreadLoop(this); | |
| 197 } | |
| 198 | |
| 199 SequencedWorkerPool::Inner::Inner(size_t max_threads, | |
| 200 const std::string& thread_name_prefix) | |
| 201 : last_sequence_number_(0), | |
| 202 lock_(), | |
| 203 cond_var_(&lock_), | |
| 204 max_threads_(max_threads), | |
| 205 thread_name_prefix_(thread_name_prefix), | |
| 206 thread_being_created_(false), | |
| 207 waiting_thread_count_(0), | |
| 208 blocking_shutdown_thread_count_(0), | |
| 209 pending_task_count_(0), | |
| 210 blocking_shutdown_pending_task_count_(0), | |
| 211 terminating_(false), | |
| 212 shutdown_called_(false) { | |
| 213 } | |
| 214 | |
| 215 SequencedWorkerPool::Inner::~Inner() { | |
| 216 // You must call Shutdown() before destroying the pool. | |
| 217 DCHECK(shutdown_called_); | |
| 218 | |
| 219 // Need to explicitly join with the threads before they're destroyed or else | |
| 220 // they will be running when our object is half torn down. | |
| 221 for (size_t i = 0; i < threads_.size(); i++) | |
| 222 threads_[i]->Join(); | |
| 223 threads_.clear(); | |
| 224 } | |
| 225 | |
| 226 SequencedWorkerPool::SequenceToken | |
| 227 SequencedWorkerPool::Inner::GetSequenceToken() { | |
| 228 base::subtle::Atomic32 result = | |
| 229 base::subtle::NoBarrier_AtomicIncrement(&last_sequence_number_, 1); | |
| 230 return SequenceToken(static_cast<int>(result)); | |
| 231 } | |
| 232 | |
| 233 SequencedWorkerPool::SequenceToken | |
| 234 SequencedWorkerPool::Inner::GetNamedSequenceToken( | |
| 235 const std::string& name) { | |
| 236 base::AutoLock lock(lock_); | |
| 237 std::map<std::string, int>::const_iterator found = | |
| 238 named_sequence_tokens_.find(name); | |
| 239 if (found != named_sequence_tokens_.end()) | |
| 240 return SequenceToken(found->second); // Got an existing one. | |
| 241 | |
| 242 // Create a new one for this name. | |
| 243 SequenceToken result = GetSequenceToken(); | |
| 244 named_sequence_tokens_.insert(std::make_pair(name, result.id_)); | |
| 245 return result; | |
| 246 } | |
| 247 | |
| 248 bool SequencedWorkerPool::Inner::PostTask( | |
| 249 int sequence_token_id, | |
| 250 SequencedWorkerPool::WorkerShutdown shutdown_behavior, | |
| 251 const tracked_objects::Location& from_here, | |
| 252 const base::Closure& task) { | |
| 253 SequencedTask sequenced; | |
| 254 sequenced.sequence_token_id = sequence_token_id; | |
| 255 sequenced.shutdown_behavior = shutdown_behavior; | |
| 256 sequenced.location = from_here; | |
| 257 sequenced.task = task; | |
| 258 | |
| 259 int create_thread_id = 0; | |
| 260 { | |
| 261 base::AutoLock lock(lock_); | |
| 262 if (terminating_) | |
| 263 return false; | |
| 264 | |
| 265 pending_tasks_.push_back(sequenced); | |
| 266 pending_task_count_++; | |
| 267 if (shutdown_behavior == BLOCK_SHUTDOWN) | |
| 268 blocking_shutdown_pending_task_count_++; | |
| 269 | |
| 270 create_thread_id = PrepareToStartAdditionalThreadIfHelpful(); | |
| 271 } | |
| 272 | |
| 273 // Actually start the additional thread or signal an existing one now that | |
| 274 // we're outside the lock. | |
| 275 if (create_thread_id) | |
| 276 FinishStartingAdditionalThread(create_thread_id); | |
| 277 else | |
| 278 cond_var_.Signal(); | |
| 279 | |
| 280 return true; | |
| 281 } | |
| 282 | |
| 283 void SequencedWorkerPool::Inner::Shutdown() { | |
| 284 if (shutdown_called_) | |
| 285 return; | |
| 286 shutdown_called_ = true; | |
| 287 | |
| 288 // Mark us as terminated and go through and drop all tasks that aren't | |
| 289 // required to run on shutdown. Since no new tasks will get posted once the | |
| 290 // terminated flag is set, this ensures that all remaining tasks are required | |
| 291 // for shutdown whenever the termianted_ flag is set. | |
| 292 { | |
| 293 base::AutoLock lock(lock_); | |
| 294 DCHECK(!terminating_); | |
| 295 terminating_ = true; | |
| 296 | |
| 297 // Tickle the threads. This will wake up a waiting one so it will know that | |
| 298 // it can exit, which in turn will wake up any other waiting ones. | |
| 299 cond_var_.Signal(); | |
| 300 | |
| 301 // There are no pending or running tasks blocking shutdown, we're done. | |
| 302 if (CanShutdown()) | |
| 303 return; | |
| 304 } | |
| 305 | |
| 306 // If we get here, we know we're either waiting on a blocking task that's | |
| 307 // currently running, waiting on a blocking task that hasn't been scheduled | |
| 308 // yet, or both. Block on the "queue empty" event to know when all tasks are | |
| 309 // complete. This must be done outside the lock. | |
| 310 if (testing_observer_) | |
| 311 testing_observer_->WillWaitForShutdown(); | |
| 312 | |
| 313 base::TimeTicks shutdown_wait_begin = base::TimeTicks::Now(); | |
| 314 | |
| 315 // Wait for no more tasks. | |
| 316 { | |
| 317 base::AutoLock lock(lock_); | |
| 318 while (!CanShutdown()) | |
| 319 cond_var_.Wait(); | |
| 320 } | |
| 321 UMA_HISTOGRAM_TIMES("SequencedWorkerPool.ShutdownDelayTime", | |
| 322 base::TimeTicks::Now() - shutdown_wait_begin); | |
| 323 } | |
| 324 | |
| 325 void SequencedWorkerPool::Inner::SetTestingObserver( | |
| 326 SequencedWorkerPool::TestingObserver* observer) { | |
| 327 base::AutoLock lock(lock_); | |
| 328 testing_observer_ = observer; | |
| 329 } | |
| 330 | |
| 331 void SequencedWorkerPool::Inner::ThreadLoop(Worker* this_worker) { | |
| 332 { | |
| 333 base::AutoLock lock(lock_); | |
| 334 DCHECK(thread_being_created_); | |
| 335 thread_being_created_ = false; | |
| 336 threads_.push_back(linked_ptr<Worker>(this_worker)); | |
|
jar (doing other things)
2011/12/31 20:27:27
This is nicer and cleaner too. I like that it is
| |
| 337 | |
| 338 while (true) { | |
| 339 // See GetWork for what delete_these_outside_lock is doing. | |
| 340 SequencedTask task; | |
| 341 std::vector<base::Closure> delete_these_outside_lock; | |
| 342 if (GetWork(&task, &delete_these_outside_lock)) { | |
| 343 int new_thread_id = WillRunWorkerTask(task); | |
| 344 { | |
| 345 base::AutoUnlock unlock(lock_); | |
| 346 cond_var_.Signal(); | |
| 347 delete_these_outside_lock.clear(); | |
| 348 | |
| 349 // Complete thread creation outside the lock if necessary. | |
| 350 if (new_thread_id) | |
| 351 FinishStartingAdditionalThread(new_thread_id); | |
| 352 | |
| 353 task.task.Run(); | |
| 354 | |
| 355 // Make sure our task is erased outside the lock for the same reason | |
| 356 // we do this with delete_these_oustide_lock. | |
| 357 task.task = base::Closure(); | |
| 358 } | |
| 359 DidRunWorkerTask(task); // Must be done inside the lock. | |
| 360 } else { | |
| 361 // When we're terminating and there's no more work, we can shut down. | |
| 362 // You can't get more tasks posted once terminating_ is set. There may | |
| 363 // be some tasks stuck behind running ones with the same sequence | |
| 364 // token, but additional threads won't help this case. | |
| 365 if (terminating_) | |
| 366 break; | |
| 367 waiting_thread_count_++; | |
| 368 cond_var_.Wait(); | |
| 369 waiting_thread_count_--; | |
| 370 } | |
| 371 } | |
| 372 } | |
| 373 | |
| 374 // We noticed we should exit. Wake up the next worker so it knows it should | |
| 375 // exit as well (because the Shutdown() code only signals once). | |
| 376 cond_var_.Signal(); | |
| 377 } | |
| 378 | |
| 379 bool SequencedWorkerPool::Inner::GetWork( | |
| 380 SequencedTask* task, | |
| 381 std::vector<base::Closure>* delete_these_outside_lock) { | |
| 382 lock_.AssertAcquired(); | |
| 383 | |
| 384 DCHECK_EQ(pending_tasks_.size(), pending_task_count_); | |
| 385 UMA_HISTOGRAM_COUNTS_100("SequencedWorkerPool.TaskCount", | |
| 386 static_cast<int>(pending_task_count_)); | |
| 387 | |
| 388 // Find the next task with a sequence token that's not currently in use. | |
| 389 // If the token is in use, that means another thread is running something | |
| 390 // in that sequence, and we can't run it without going out-of-order. | |
| 391 // | |
| 392 // This algorithm is simple and fair, but inefficient in some cases. For | |
| 393 // example, say somebody schedules 1000 slow tasks with the same sequence | |
| 394 // number. We'll have to go through all those tasks each time we feel like | |
| 395 // there might be work to schedule. If this proves to be a problem, we | |
| 396 // should make this more efficient. | |
| 397 // | |
| 398 // One possible enhancement would be to keep a map from sequence ID to a | |
| 399 // list of pending but currently blocked SequencedTasks for that ID. | |
| 400 // When a worker finishes a task of one sequence token, it can pick up the | |
| 401 // next one from that token right away. | |
| 402 // | |
| 403 // This may lead to starvation if there are sufficient numbers of sequences | |
| 404 // in use. To alleviate this, we could add an incrementing priority counter | |
| 405 // to each SequencedTask. Then maintain a priority_queue of all runnable | |
| 406 // tasks, sorted by priority counter. When a sequenced task is completed | |
| 407 // we would pop the head element off of that tasks pending list and add it | |
| 408 // to the priority queue. Then we would run the first item in the priority | |
| 409 // queue. | |
| 410 bool found_task = false; | |
| 411 int unrunnable_tasks = 0; | |
| 412 std::list<SequencedTask>::iterator i = pending_tasks_.begin(); | |
| 413 while (i != pending_tasks_.end()) { | |
| 414 if (!IsSequenceTokenRunnable(i->sequence_token_id)) { | |
| 415 unrunnable_tasks++; | |
| 416 ++i; | |
| 417 continue; | |
| 418 } | |
| 419 | |
| 420 if (terminating_ && i->shutdown_behavior != BLOCK_SHUTDOWN) { | |
| 421 // We're shutting down and the task we just found isn't blocking | |
| 422 // shutdown. Delete it and get more work. | |
| 423 // | |
| 424 // Note that we do not want to delete unrunnable tasks. Deleting a task | |
| 425 // can have side effects (like freeing some objects) and deleting a | |
| 426 // task that's supposed to run after one that's currently running could | |
| 427 // cause an obscure crash. | |
| 428 // | |
| 429 // We really want to delete these tasks outside the lock in case the | |
| 430 // closures are holding refs to objects that want to post work from | |
| 431 // their destructorss (which would deadlock). The closures are | |
| 432 // internally refcounted, so we just need to keep a copy of them alive | |
| 433 // until the lock is exited. The calling code can just clear() the | |
| 434 // vector they passed to us once the lock is exited to make this | |
| 435 // happen. | |
| 436 delete_these_outside_lock->push_back(i->task); | |
| 437 i = pending_tasks_.erase(i); | |
| 438 pending_task_count_--; | |
| 439 } else { | |
| 440 // Found a runnable task. | |
| 441 *task = *i; | |
| 442 i = pending_tasks_.erase(i); | |
| 443 pending_task_count_--; | |
| 444 if (task->shutdown_behavior == BLOCK_SHUTDOWN) | |
| 445 blocking_shutdown_pending_task_count_--; | |
| 446 | |
| 447 found_task = true; | |
| 448 break; | |
| 449 } | |
| 450 } | |
| 451 | |
| 452 // Track the number of tasks we had to skip over to see if we should be | |
| 453 // making this more efficient. If this number ever becomes large or is | |
| 454 // frequently "some", we should consider the optimization above. | |
| 455 UMA_HISTOGRAM_COUNTS_100("SequencedWorkerPool.UnrunnableTaskCount", | |
| 456 unrunnable_tasks); | |
| 457 return found_task; | |
| 458 } | |
| 459 | |
| 460 int SequencedWorkerPool::Inner::WillRunWorkerTask(const SequencedTask& task) { | |
| 461 lock_.AssertAcquired(); | |
| 462 | |
| 463 // Mark the task's sequence number as in use. | |
| 464 if (task.sequence_token_id) | |
| 465 current_sequences_.insert(task.sequence_token_id); | |
| 466 | |
| 467 if (task.shutdown_behavior == SequencedWorkerPool::BLOCK_SHUTDOWN) | |
| 468 blocking_shutdown_thread_count_++; | |
| 469 | |
| 470 // We just picked up a task. Since StartAdditionalThreadIfHelpful only | |
| 471 // creates a new thread if there is no free one, there is a race when posting | |
| 472 // tasks that many tasks could have been posted before a thread started | |
| 473 // running them, so only one thread would have been created. So we also check | |
| 474 // whether we should create more threads after removing our task from the | |
| 475 // queue, which also has the nice side effect of creating the workers from | |
| 476 // background threads rather than the main thread of the app. | |
| 477 // | |
| 478 // If another thread wasn't created, we want to wake up an existing thread | |
| 479 // if there is one waiting to pick up the next task. | |
| 480 // | |
| 481 // Note that we really need to do this *before* running the task, not | |
| 482 // after. Otherwise, if more than one task is posted, the creation of the | |
| 483 // second thread (since we only create one at a time) will be blocked by | |
| 484 // the execution of the first task, which could be arbitrarily long. | |
| 485 return PrepareToStartAdditionalThreadIfHelpful(); | |
| 486 } | |
| 487 | |
| 488 void SequencedWorkerPool::Inner::DidRunWorkerTask(const SequencedTask& task) { | |
| 489 lock_.AssertAcquired(); | |
| 490 | |
| 491 if (task.shutdown_behavior == SequencedWorkerPool::BLOCK_SHUTDOWN) { | |
| 492 DCHECK_GT(blocking_shutdown_thread_count_, 0u); | |
| 493 blocking_shutdown_thread_count_--; | |
| 494 } | |
| 495 | |
| 496 if (task.sequence_token_id) | |
| 497 current_sequences_.erase(task.sequence_token_id); | |
| 498 } | |
| 499 | |
| 500 bool SequencedWorkerPool::Inner::IsSequenceTokenRunnable( | |
| 501 int sequence_token_id) const { | |
| 502 lock_.AssertAcquired(); | |
| 503 return !sequence_token_id || | |
| 504 current_sequences_.find(sequence_token_id) == | |
| 505 current_sequences_.end(); | |
| 506 } | |
| 507 | |
| 508 int SequencedWorkerPool::Inner::PrepareToStartAdditionalThreadIfHelpful() { | |
| 509 // How thread creation works: | |
| 510 // | |
| 511 // We'de like to avoid creating threads with the lock held. However, we | |
| 512 // need to be sure that we have an accurate accounting of the threads for | |
| 513 // proper Joining and deltion on shutdown. | |
| 514 // | |
| 515 // We need to figure out if we need another thread with the lock held, which | |
| 516 // is what this function does. It then marks us as in the process of creating | |
| 517 // a thread. When we do shutdown, we wait until the thread_being_created_ | |
| 518 // flag is cleared, which ensures that the new thread is properly added to | |
| 519 // all the data structures and we can't leak it. Once shutdown starts, we'll | |
| 520 // refuse to create more threads or they would be leaked. | |
| 521 // | |
| 522 // Note that this creates a mostly benign race condition on shutdown that | |
| 523 // will cause fewer workers to be created than one would expect. It isn't | |
| 524 // much of an issue in real life, but affects some tests. Since we only spawn | |
| 525 // one worker at a time, the following sequence of events can happen: | |
| 526 // | |
| 527 // 1. Main thread posts a bunch of unrelated tasks that would normally be | |
| 528 // run on separate threads. | |
| 529 // 2. The first task post causes us to start a worker. Other tasks do not | |
| 530 // cause a worker to start since one is pending. | |
| 531 // 3. Main thread initiates shutdown. | |
| 532 // 4. No more threads are created since the terminating_ flag is set. | |
| 533 // | |
| 534 // The result is that one may expect that max_threads_ workers to be created | |
| 535 // given the workload, but in reality fewer may be created because the | |
| 536 // sequence of thread creation on the background threads is racing with the | |
| 537 // shutdown call. | |
| 538 if (!terminating_ && | |
| 539 !thread_being_created_ && | |
| 540 threads_.size() < max_threads_ && | |
|
jar (doing other things)
2011/12/31 20:27:27
Thinking about it it more, I remembered that you'r
| |
| 541 waiting_thread_count_ == 0) { | |
| 542 // We could use an additional thread if there's work to be done. | |
| 543 for (std::list<SequencedTask>::iterator i = pending_tasks_.begin(); | |
| 544 i != pending_tasks_.end(); ++i) { | |
| 545 if (IsSequenceTokenRunnable(i->sequence_token_id)) { | |
| 546 // Found a runnable task, mark the thread as being started. | |
| 547 thread_being_created_ = true; | |
| 548 return static_cast<int>(threads_.size() + 1); | |
| 549 } | |
| 550 } | |
| 551 } | |
| 552 return 0; | |
| 553 } | |
| 554 | |
| 555 void SequencedWorkerPool::Inner::FinishStartingAdditionalThread( | |
| 556 int thread_number) { | |
| 557 // Called outside of the lock. | |
| 558 DCHECK(thread_number > 0); | |
| 559 Worker* new_thread = new Worker(this, thread_number, thread_name_prefix_); | |
| 560 | |
| 561 // The worker is assigned to the list when the thread actually starts, which | |
| 562 // will manage the memory of the pointer. | |
| 563 } | |
| 564 | |
| 565 bool SequencedWorkerPool::Inner::CanShutdown() const { | |
| 566 lock_.AssertAcquired(); | |
| 567 // See PrepareToStartAdditionalThreadIfHelpful for how thread creation works. | |
| 568 return !thread_being_created_ && | |
| 569 blocking_shutdown_thread_count_ == 0 && | |
| 570 blocking_shutdown_pending_task_count_ == 0; | |
| 571 } | |
| 572 | |
| 573 // SequencedWorkerPool -------------------------------------------------------- | |
| 574 | |
| 575 SequencedWorkerPool::SequencedWorkerPool(size_t max_threads, | |
| 576 const std::string& thread_name_prefix) | |
| 577 : inner_(new Inner(max_threads, thread_name_prefix)) { | |
| 578 } | |
| 579 | |
| 580 SequencedWorkerPool::~SequencedWorkerPool() { | |
| 581 } | |
| 582 | |
| 583 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetSequenceToken() { | |
| 584 return inner_->GetSequenceToken(); | |
| 585 } | |
| 586 | |
| 587 SequencedWorkerPool::SequenceToken SequencedWorkerPool::GetNamedSequenceToken( | |
| 588 const std::string& name) { | |
| 589 return inner_->GetNamedSequenceToken(name); | |
| 590 } | |
| 591 | |
| 592 bool SequencedWorkerPool::PostWorkerTask( | |
| 593 const tracked_objects::Location& from_here, | |
| 594 const base::Closure& task) { | |
| 595 return inner_->PostTask(0, BLOCK_SHUTDOWN, from_here, task); | |
| 596 } | |
| 597 | |
| 598 bool SequencedWorkerPool::PostWorkerTaskWithShutdownBehavior( | |
| 599 const tracked_objects::Location& from_here, | |
| 600 const base::Closure& task, | |
| 601 WorkerShutdown shutdown_behavior) { | |
| 602 return inner_->PostTask(0, shutdown_behavior, from_here, task); | |
| 603 } | |
| 604 | |
| 605 bool SequencedWorkerPool::PostSequencedWorkerTask( | |
| 606 SequenceToken sequence_token, | |
| 607 const tracked_objects::Location& from_here, | |
| 608 const base::Closure& task) { | |
| 609 return inner_->PostTask(sequence_token.id_, BLOCK_SHUTDOWN, | |
| 610 from_here, task); | |
| 611 } | |
| 612 | |
| 613 bool SequencedWorkerPool::PostSequencedWorkerTaskWithShutdownBehavior( | |
| 614 SequenceToken sequence_token, | |
| 615 const tracked_objects::Location& from_here, | |
| 616 const base::Closure& task, | |
| 617 WorkerShutdown shutdown_behavior) { | |
| 618 return inner_->PostTask(sequence_token.id_, shutdown_behavior, | |
| 619 from_here, task); | |
| 620 } | |
| 621 | |
| 622 void SequencedWorkerPool::Shutdown() { | |
| 623 inner_->Shutdown(); | |
| 624 } | |
| 625 | |
| 626 void SequencedWorkerPool::SetTestingObserver(TestingObserver* observer) { | |
| 627 inner_->SetTestingObserver(observer); | |
| 628 } | |
| 629 | |
| 630 } // namespace base | |
| OLD | NEW |