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

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: fix typo 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
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 #if defined(TRACK_ALL_TASK_OBJECTS)
53 // Counter for location where the Closure was posted from.
54 tracked_objects::Births* post_births;
55
56 // Time the task was posted.
57 TimeTicks time_posted;
58 #endif // defined(TRACK_ALL_TASK_OBJECTS)
59
60 // The task to run.
61 base::Closure task;
62 };
63
47 // All worker threads will share the same |name_prefix|. They will exit after 64 // All worker threads will share the same |name_prefix|. They will exit after
48 // |idle_seconds_before_exit|. 65 // |idle_seconds_before_exit|.
49 PosixDynamicThreadPool(const std::string& name_prefix, 66 PosixDynamicThreadPool(const std::string& name_prefix,
50 int idle_seconds_before_exit); 67 int idle_seconds_before_exit);
51 ~PosixDynamicThreadPool(); 68 ~PosixDynamicThreadPool();
52 69
53 // Indicates that the thread pool is going away. Stops handing out tasks to 70 // 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. 71 // worker threads. Wakes up all the idle threads to let them exit.
55 void Terminate(); 72 void Terminate();
56 73
57 // Adds |task| to the thread pool. PosixDynamicThreadPool assumes ownership 74 // Adds |task| to the thread pool. PosixDynamicThreadPool assumes ownership
58 // of |task|. 75 // of |task|.
59 void PostTask(Task* task); 76 //
77 // TODO(ajwong): Remove this compatibility API once the Task -> Closure
78 // migration is finished.
79 void PostTask(const tracked_objects::Location& from_here, Task* task);
60 80
81 // Adds |task| to the thread pool.
82 void PostTask(const tracked_objects::Location& from_here,
83 const base::Closure& task);
61 // Worker thread method to wait for up to |idle_seconds_before_exit| for more 84 // 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. 85 // work from the thread pool. Returns NULL if no work is available.
63 Task* WaitForTask(); 86 PendingTask WaitForTask();
64 87
65 private: 88 private:
66 friend class PosixDynamicThreadPoolPeer; 89 friend class PosixDynamicThreadPoolPeer;
67 90
91 // Adds pending_task to the thread pool. This function will clear
92 // |pending_task->task|.
93 void AddTask(PendingTask* pending_task);
94
68 const std::string name_prefix_; 95 const std::string name_prefix_;
69 const int idle_seconds_before_exit_; 96 const int idle_seconds_before_exit_;
70 97
71 Lock lock_; // Protects all the variables below. 98 Lock lock_; // Protects all the variables below.
72 99
73 // Signal()s worker threads to let them know more tasks are available. 100 // 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 101 // Also used for Broadcast()'ing to worker threads to let them know the pool
75 // is being deleted and they can exit. 102 // is being deleted and they can exit.
76 ConditionVariable tasks_available_cv_; 103 ConditionVariable pending_tasks_available_cv_;
77 int num_idle_threads_; 104 int num_idle_threads_;
78 std::queue<Task*> tasks_; 105 std::queue<PendingTask> pending_tasks_;
79 bool terminated_; 106 bool terminated_;
80 // Only used for tests to ensure correct thread ordering. It will always be 107 // Only used for tests to ensure correct thread ordering. It will always be
81 // NULL in non-test code. 108 // NULL in non-test code.
82 scoped_ptr<ConditionVariable> num_idle_threads_cv_; 109 scoped_ptr<ConditionVariable> num_idle_threads_cv_;
83 110
84 DISALLOW_COPY_AND_ASSIGN(PosixDynamicThreadPool); 111 DISALLOW_COPY_AND_ASSIGN(PosixDynamicThreadPool);
85 }; 112 };
86 113
87 } // namespace base 114 } // namespace base
88 115
89 #endif // BASE_THREADING_WORKER_POOL_POSIX_H_ 116 #endif // BASE_THREADING_WORKER_POOL_POSIX_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698