OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef BASE_THREADING_WORKER_POOL_H_ | 5 #ifndef BASE_THREADING_WORKER_POOL_H_ |
6 #define BASE_THREADING_WORKER_POOL_H_ | 6 #define BASE_THREADING_WORKER_POOL_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/base_export.h" | 9 #include "base/base_export.h" |
10 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
11 #include "base/memory/ref_counted.h" | |
11 | 12 |
12 class Task; | 13 class Task; |
13 | 14 |
14 namespace tracked_objects { | 15 namespace tracked_objects { |
15 class Location; | 16 class Location; |
16 } // namespace tracked_objects | 17 } // namespace tracked_objects |
17 | 18 |
18 namespace base { | 19 namespace base { |
19 | 20 |
21 class TaskRunner; | |
22 | |
20 // This is a facility that runs tasks that don't require a specific thread or | 23 // This is a facility that runs tasks that don't require a specific thread or |
21 // a message loop. | 24 // a message loop. |
22 // | 25 // |
23 // WARNING: This shouldn't be used unless absolutely necessary. We don't wait | 26 // WARNING: This shouldn't be used unless absolutely necessary. We don't wait |
24 // for the worker pool threads to finish on shutdown, so the tasks running | 27 // for the worker pool threads to finish on shutdown, so the tasks running |
25 // inside the pool must be extremely careful about other objects they access | 28 // inside the pool must be extremely careful about other objects they access |
26 // (MessageLoops, Singletons, etc). During shutdown these object may no longer | 29 // (MessageLoops, Singletons, etc). During shutdown these object may no longer |
27 // exist. | 30 // exist. |
28 class BASE_EXPORT WorkerPool { | 31 class BASE_EXPORT WorkerPool { |
29 public: | 32 public: |
30 // This function posts |task| to run on a worker thread. |task_is_slow| | 33 // This function posts |task| to run on a worker thread. |task_is_slow| |
31 // should be used for tasks that will take a long time to execute. Returns | 34 // should be used for tasks that will take a long time to execute. Returns |
32 // false if |task| could not be posted to a worker thread. Regardless of | 35 // false if |task| could not be posted to a worker thread. Regardless of |
33 // return value, ownership of |task| is transferred to the worker pool. | 36 // return value, ownership of |task| is transferred to the worker pool. |
34 static bool PostTask(const tracked_objects::Location& from_here, | 37 static bool PostTask(const tracked_objects::Location& from_here, |
35 const base::Closure& task, bool task_is_slow); | 38 const base::Closure& task, bool task_is_slow); |
36 | 39 |
37 // Just like MessageLoopProxy::PostTaskAndReply, except the destination | 40 // Just like MessageLoopProxy::PostTaskAndReply, except the destination |
38 // for |task| is a worker thread and you can specify |task_is_slow| just | 41 // for |task| is a worker thread and you can specify |task_is_slow| just |
39 // like you can for PostTask above. | 42 // like you can for PostTask above. |
40 static bool PostTaskAndReply(const tracked_objects::Location& from_here, | 43 static bool PostTaskAndReply(const tracked_objects::Location& from_here, |
41 const Closure& task, | 44 const Closure& task, |
42 const Closure& reply, | 45 const Closure& reply, |
43 bool task_is_slow); | 46 bool task_is_slow); |
47 | |
48 // Return true if the current thread is one that this WorkerPool runs tasks | |
49 // on. (Note that if the Windows worker pool is used without going through | |
50 // this WorkerPool interface, RunsTasksOnCurrentThread would return false on | |
51 // those threads.) | |
52 static bool RunsTasksOnCurrentThread(); | |
53 | |
54 // Get a TaskRunner wrapper which posts to the WorkerPool using the given | |
55 // |task_is_slow| behavior. | |
56 static scoped_refptr<TaskRunner> GetTaskRunner(bool task_is_slow); | |
willchan no longer on Chromium
2012/04/24 23:41:50
How about you also store a leaky LazyInstance for
mattm
2012/04/25 00:57:35
Done. Had to make a holder class to initialize th
| |
44 }; | 57 }; |
45 | 58 |
46 } // namespace base | 59 } // namespace base |
47 | 60 |
48 #endif // BASE_THREADING_WORKER_POOL_H_ | 61 #endif // BASE_THREADING_WORKER_POOL_H_ |
OLD | NEW |