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

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: addressed nit and rebased. 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
« no previous file with comments | « base/threading/worker_pool.h ('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 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
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);
78
79 // Adds |task| to the thread pool.
80 void PostTask(const tracked_objects::Location& from_here,
81 const base::Closure& task);
60 82
61 // Worker thread method to wait for up to |idle_seconds_before_exit| for more 83 // Worker thread method to wait for up to |idle_seconds_before_exit| for more
62 // work from the thread pool. Returns NULL if no work is available. 84 // work from the thread pool. Returns NULL if no work is available.
63 Task* WaitForTask(); 85 PendingTask WaitForTask();
64 86
65 private: 87 private:
66 friend class PosixDynamicThreadPoolPeer; 88 friend class PosixDynamicThreadPoolPeer;
67 89
90 // Adds pending_task to the thread pool. This function will clear
91 // |pending_task->task|.
92 void AddTask(PendingTask* pending_task);
93
68 const std::string name_prefix_; 94 const std::string name_prefix_;
69 const int idle_seconds_before_exit_; 95 const int idle_seconds_before_exit_;
70 96
71 Lock lock_; // Protects all the variables below. 97 Lock lock_; // Protects all the variables below.
72 98
73 // Signal()s worker threads to let them know more tasks are available. 99 // 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 100 // Also used for Broadcast()'ing to worker threads to let them know the pool
75 // is being deleted and they can exit. 101 // is being deleted and they can exit.
76 ConditionVariable tasks_available_cv_; 102 ConditionVariable pending_tasks_available_cv_;
77 int num_idle_threads_; 103 int num_idle_threads_;
78 std::queue<Task*> tasks_; 104 std::queue<PendingTask> pending_tasks_;
79 bool terminated_; 105 bool terminated_;
80 // Only used for tests to ensure correct thread ordering. It will always be 106 // Only used for tests to ensure correct thread ordering. It will always be
81 // NULL in non-test code. 107 // NULL in non-test code.
82 scoped_ptr<ConditionVariable> num_idle_threads_cv_; 108 scoped_ptr<ConditionVariable> num_idle_threads_cv_;
83 109
84 DISALLOW_COPY_AND_ASSIGN(PosixDynamicThreadPool); 110 DISALLOW_COPY_AND_ASSIGN(PosixDynamicThreadPool);
85 }; 111 };
86 112
87 } // namespace base 113 } // namespace base
88 114
89 #endif // BASE_THREADING_WORKER_POOL_POSIX_H_ 115 #endif // BASE_THREADING_WORKER_POOL_POSIX_H_
OLDNEW
« no previous file with comments | « base/threading/worker_pool.h ('k') | base/threading/worker_pool_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698