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

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

Issue 8565024: base: Refactor PendingTask out of MessageLoop. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review fixes. Created 9 years, 1 month 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
« no previous file with comments | « base/pending_task.cc ('k') | base/threading/worker_pool_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 16 matching lines...) Expand all
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/callback.h"
33 #include "base/location.h" 33 #include "base/location.h"
34 #include "base/time.h" 34 #include "base/time.h"
35 #include "base/memory/ref_counted.h" 35 #include "base/memory/ref_counted.h"
36 #include "base/memory/scoped_ptr.h" 36 #include "base/memory/scoped_ptr.h"
37 #include "base/pending_task.h"
37 #include "base/synchronization/condition_variable.h" 38 #include "base/synchronization/condition_variable.h"
38 #include "base/synchronization/lock.h" 39 #include "base/synchronization/lock.h"
39 #include "base/threading/platform_thread.h" 40 #include "base/threading/platform_thread.h"
40 #include "base/tracked_objects.h" 41 #include "base/tracked_objects.h"
41 42
42 class Task; 43 class Task;
43 44
44 namespace base { 45 namespace base {
45 46
46 class BASE_EXPORT PosixDynamicThreadPool 47 class BASE_EXPORT PosixDynamicThreadPool
47 : public RefCountedThreadSafe<PosixDynamicThreadPool> { 48 : public RefCountedThreadSafe<PosixDynamicThreadPool> {
48 public: 49 public:
49 class PosixDynamicThreadPoolPeer; 50 class PosixDynamicThreadPoolPeer;
50 51
51 struct PendingTask {
52 PendingTask(const tracked_objects::Location& posted_from,
53 const base::Closure& task);
54 ~PendingTask();
55
56 // Counter for location where the Closure was posted from.
57 tracked_objects::Births* birth_tally;
58
59 // Time the task was posted.
60 tracked_objects::TrackedTime time_posted;
61
62 const tracked_objects::Location posted_from;
63
64 // The task to run.
65 base::Closure task;
66 };
67
68 // All worker threads will share the same |name_prefix|. They will exit after 52 // All worker threads will share the same |name_prefix|. They will exit after
69 // |idle_seconds_before_exit|. 53 // |idle_seconds_before_exit|.
70 PosixDynamicThreadPool(const std::string& name_prefix, 54 PosixDynamicThreadPool(const std::string& name_prefix,
71 int idle_seconds_before_exit); 55 int idle_seconds_before_exit);
72 ~PosixDynamicThreadPool(); 56 ~PosixDynamicThreadPool();
73 57
74 // Indicates that the thread pool is going away. Stops handing out tasks to 58 // Indicates that the thread pool is going away. Stops handing out tasks to
75 // worker threads. Wakes up all the idle threads to let them exit. 59 // worker threads. Wakes up all the idle threads to let them exit.
76 void Terminate(); 60 void Terminate();
77 61
78 // Adds |task| to the thread pool. PosixDynamicThreadPool assumes ownership 62 // Adds |task| to the thread pool. PosixDynamicThreadPool assumes ownership
79 // of |task|. 63 // of |task|.
80 // 64 //
81 // TODO(ajwong): Remove this compatibility API once the Task -> Closure 65 // TODO(ajwong): Remove this compatibility API once the Task -> Closure
82 // migration is finished. 66 // migration is finished.
83 void PostTask(const tracked_objects::Location& from_here, Task* task); 67 void PostTask(const tracked_objects::Location& from_here, Task* task);
84 68
85 // Adds |task| to the thread pool. 69 // Adds |task| to the thread pool.
86 void PostTask(const tracked_objects::Location& from_here, 70 void PostTask(const tracked_objects::Location& from_here,
87 const base::Closure& task); 71 const Closure& task);
88 72
89 // Worker thread method to wait for up to |idle_seconds_before_exit| for more 73 // Worker thread method to wait for up to |idle_seconds_before_exit| for more
90 // work from the thread pool. Returns NULL if no work is available. 74 // work from the thread pool. Returns NULL if no work is available.
91 PendingTask WaitForTask(); 75 PendingTask WaitForTask();
92 76
93 private: 77 private:
94 friend class PosixDynamicThreadPoolPeer; 78 friend class PosixDynamicThreadPoolPeer;
95 79
96 // Adds pending_task to the thread pool. This function will clear 80 // Adds pending_task to the thread pool. This function will clear
97 // |pending_task->task|. 81 // |pending_task->task|.
98 void AddTask(PendingTask* pending_task); 82 void AddTask(PendingTask* pending_task);
99 83
100 const std::string name_prefix_; 84 const std::string name_prefix_;
101 const int idle_seconds_before_exit_; 85 const int idle_seconds_before_exit_;
102 86
103 Lock lock_; // Protects all the variables below. 87 Lock lock_; // Protects all the variables below.
104 88
105 // Signal()s worker threads to let them know more tasks are available. 89 // Signal()s worker threads to let them know more tasks are available.
106 // Also used for Broadcast()'ing to worker threads to let them know the pool 90 // Also used for Broadcast()'ing to worker threads to let them know the pool
107 // is being deleted and they can exit. 91 // is being deleted and they can exit.
108 ConditionVariable pending_tasks_available_cv_; 92 ConditionVariable pending_tasks_available_cv_;
109 int num_idle_threads_; 93 int num_idle_threads_;
110 std::queue<PendingTask> pending_tasks_; 94 TaskQueue pending_tasks_;
111 bool terminated_; 95 bool terminated_;
112 // Only used for tests to ensure correct thread ordering. It will always be 96 // Only used for tests to ensure correct thread ordering. It will always be
113 // NULL in non-test code. 97 // NULL in non-test code.
114 scoped_ptr<ConditionVariable> num_idle_threads_cv_; 98 scoped_ptr<ConditionVariable> num_idle_threads_cv_;
115 99
116 DISALLOW_COPY_AND_ASSIGN(PosixDynamicThreadPool); 100 DISALLOW_COPY_AND_ASSIGN(PosixDynamicThreadPool);
117 }; 101 };
118 102
119 } // namespace base 103 } // namespace base
120 104
121 #endif // BASE_THREADING_WORKER_POOL_POSIX_H_ 105 #endif // BASE_THREADING_WORKER_POOL_POSIX_H_
OLDNEW
« no previous file with comments | « base/pending_task.cc ('k') | base/threading/worker_pool_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698