Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(446)

Side by Side Diff: base/threading/worker_pool_posix.h

Issue 7316015: Support Closure in ALL the loops! (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: win compile fix Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // The thread pool used in the POSIX implementation of WorkerPool dynamically 5 // The thread pool used in the POSIX implementation of WorkerPool dynamically
6 // adds threads as necessary to handle all tasks. It keeps old threads around 6 // adds threads as necessary to handle all tasks. It keeps old threads around
7 // for a period of time to allow them to be reused. After this waiting period, 7 // for a period of time to allow them to be reused. After this waiting period,
8 // the threads exit. This thread pool uses non-joinable threads, therefore 8 // the threads exit. This thread pool uses non-joinable threads, therefore
9 // worker threads are not joined during process shutdown. This means that 9 // worker threads are not joined during process shutdown. This means that
10 // potentially long running tasks (such as DNS lookup) do not block process 10 // potentially long running tasks (such as DNS lookup) do not block process
(...skipping 11 matching lines...) Expand all
22 // These symbols are exported in a header purely for testing purposes. 22 // These symbols are exported in a header purely for testing purposes.
23 23
24 #ifndef BASE_THREADING_WORKER_POOL_POSIX_H_ 24 #ifndef BASE_THREADING_WORKER_POOL_POSIX_H_
25 #define BASE_THREADING_WORKER_POOL_POSIX_H_ 25 #define BASE_THREADING_WORKER_POOL_POSIX_H_
26 #pragma once 26 #pragma once
27 27
28 #include <queue> 28 #include <queue>
29 #include <string> 29 #include <string>
30 30
31 #include "base/basictypes.h" 31 #include "base/basictypes.h"
32 #include "base/callback.h"
32 #include "base/memory/ref_counted.h" 33 #include "base/memory/ref_counted.h"
33 #include "base/memory/scoped_ptr.h" 34 #include "base/memory/scoped_ptr.h"
34 #include "base/synchronization/condition_variable.h" 35 #include "base/synchronization/condition_variable.h"
35 #include "base/synchronization/lock.h" 36 #include "base/synchronization/lock.h"
36 #include "base/threading/platform_thread.h" 37 #include "base/threading/platform_thread.h"
38 #include "base/tracked.h"
37 39
38 class Task; 40 class Task;
39 41
40 namespace base { 42 namespace base {
41 43
42 class BASE_API PosixDynamicThreadPool 44 class BASE_API PosixDynamicThreadPool
darin (slow to review) 2011/07/25 19:47:37 note: this class name doesn't match the file name.
awong 2011/07/26 01:38:13 Yeah...will touch that in another CL.
43 : public RefCountedThreadSafe<PosixDynamicThreadPool> { 45 : public RefCountedThreadSafe<PosixDynamicThreadPool> {
44 public: 46 public:
45 class PosixDynamicThreadPoolPeer; 47 class PosixDynamicThreadPoolPeer;
46 48
49 struct PendingTask {
50 PendingTask(const tracked_objects::Location& posted_from,
51 const base::Closure& task);
52 ~PendingTask();
53 // TODO(ajwong): After we figure out why Mac's ~AtExitManager dies when
54 // destructing the lock, add in extra info so we can call
55 // tracked_objects::TallyADeathIfActive() and
56 // tracked_objects::TallyABirthIfActive correctly.
57
58 // The task to run.
59 base::Closure task;
60 };
61
47 // All worker threads will share the same |name_prefix|. They will exit after 62 // All worker threads will share the same |name_prefix|. They will exit after
48 // |idle_seconds_before_exit|. 63 // |idle_seconds_before_exit|.
49 PosixDynamicThreadPool(const std::string& name_prefix, 64 PosixDynamicThreadPool(const std::string& name_prefix,
50 int idle_seconds_before_exit); 65 int idle_seconds_before_exit);
51 ~PosixDynamicThreadPool(); 66 ~PosixDynamicThreadPool();
52 67
53 // Indicates that the thread pool is going away. Stops handing out tasks to 68 // Indicates that the thread pool is going away. Stops handing out tasks to
54 // worker threads. Wakes up all the idle threads to let them exit. 69 // worker threads. Wakes up all the idle threads to let them exit.
55 void Terminate(); 70 void Terminate();
56 71
57 // Adds |task| to the thread pool. PosixDynamicThreadPool assumes ownership 72 // Adds |task| to the thread pool. PosixDynamicThreadPool assumes ownership
58 // of |task|. 73 // of |task|.
59 void PostTask(Task* task); 74 //
75 // TODO(ajwong): Remove this compatibility API once the Task -> Closure
76 // migration is finished.
77 void PostTask(const tracked_objects::Location& from_here, Task* task);
60 78
79 // Adds |task| to the thread pool.
80 void PostTask(const tracked_objects::Location& from_here,
81 const base::Closure& task);
61 // Worker thread method to wait for up to |idle_seconds_before_exit| for more 82 // Worker thread method to wait for up to |idle_seconds_before_exit| for more
darin (slow to review) 2011/07/25 19:47:37 nit: add a new line above this comment.
awong 2011/07/26 01:38:13 Done.
62 // work from the thread pool. Returns NULL if no work is available. 83 // work from the thread pool. Returns NULL if no work is available.
63 Task* WaitForTask(); 84 PendingTask WaitForTask();
64 85
65 private: 86 private:
66 friend class PosixDynamicThreadPoolPeer; 87 friend class PosixDynamicThreadPoolPeer;
67 88
89 // Adds pending_task to the thread pool. This function will clear
90 // |pending_task->task|.
91 void AddTask(PendingTask* pending_task);
92
68 const std::string name_prefix_; 93 const std::string name_prefix_;
69 const int idle_seconds_before_exit_; 94 const int idle_seconds_before_exit_;
70 95
71 Lock lock_; // Protects all the variables below. 96 Lock lock_; // Protects all the variables below.
72 97
73 // Signal()s worker threads to let them know more tasks are available. 98 // Signal()s worker threads to let them know more tasks are available.
74 // Also used for Broadcast()'ing to worker threads to let them know the pool 99 // Also used for Broadcast()'ing to worker threads to let them know the pool
75 // is being deleted and they can exit. 100 // is being deleted and they can exit.
76 ConditionVariable tasks_available_cv_; 101 ConditionVariable pending_tasks_available_cv_;
77 int num_idle_threads_; 102 int num_idle_threads_;
78 std::queue<Task*> tasks_; 103 std::queue<PendingTask> pending_tasks_;
79 bool terminated_; 104 bool terminated_;
80 // Only used for tests to ensure correct thread ordering. It will always be 105 // Only used for tests to ensure correct thread ordering. It will always be
81 // NULL in non-test code. 106 // NULL in non-test code.
82 scoped_ptr<ConditionVariable> num_idle_threads_cv_; 107 scoped_ptr<ConditionVariable> num_idle_threads_cv_;
83 108
84 DISALLOW_COPY_AND_ASSIGN(PosixDynamicThreadPool); 109 DISALLOW_COPY_AND_ASSIGN(PosixDynamicThreadPool);
85 }; 110 };
86 111
87 } // namespace base 112 } // namespace base
88 113
89 #endif // BASE_THREADING_WORKER_POOL_POSIX_H_ 114 #endif // BASE_THREADING_WORKER_POOL_POSIX_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698