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