[email protected] | e7b3a61 | 2012-01-05 02:18:18 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | ac9ba8fe | 2010-12-30 18:08:36 | [diff] [blame] | 5 | #ifndef BASE_THREADING_WORKER_POOL_H_ |
| 6 | #define BASE_THREADING_WORKER_POOL_H_ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 7 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 8 | #include "base/base_export.h" |
[email protected] | 2d71566 | 2011-11-28 22:00:29 | [diff] [blame] | 9 | #include "base/callback_forward.h" |
[email protected] | 8e1946f9 | 2012-04-25 03:15:05 | [diff] [blame] | 10 | #include "base/memory/ref_counted.h" |
[email protected] | 43a973a | 2008-07-30 04:38:38 | [diff] [blame] | 11 | |
| 12 | class Task; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 13 | |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 14 | namespace tracked_objects { |
| 15 | class Location; |
| 16 | } // namespace tracked_objects |
| 17 | |
[email protected] | ac9ba8fe | 2010-12-30 18:08:36 | [diff] [blame] | 18 | namespace base { |
| 19 | |
[email protected] | 8e1946f9 | 2012-04-25 03:15:05 | [diff] [blame] | 20 | class TaskRunner; |
| 21 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 22 | // This is a facility that runs tasks that don't require a specific thread or |
| 23 | // a message loop. |
[email protected] | 6c710ee | 2010-05-07 07:51:16 | [diff] [blame] | 24 | // |
| 25 | // WARNING: This shouldn't be used unless absolutely necessary. We don't wait |
| 26 | // for the worker pool threads to finish on shutdown, so the tasks running |
| 27 | // inside the pool must be extremely careful about other objects they access |
| 28 | // (MessageLoops, Singletons, etc). During shutdown these object may no longer |
| 29 | // exist. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 30 | class BASE_EXPORT WorkerPool { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 31 | public: |
[email protected] | 43a973a | 2008-07-30 04:38:38 | [diff] [blame] | 32 | // This function posts |task| to run on a worker thread. |task_is_slow| |
| 33 | // should be used for tasks that will take a long time to execute. Returns |
| 34 | // false if |task| could not be posted to a worker thread. Regardless of |
| 35 | // return value, ownership of |task| is transferred to the worker pool. |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 36 | static bool PostTask(const tracked_objects::Location& from_here, |
| 37 | const base::Closure& task, bool task_is_slow); |
[email protected] | 2837654 | 2011-10-14 17:30:37 | [diff] [blame] | 38 | |
| 39 | // Just like MessageLoopProxy::PostTaskAndReply, except the destination |
| 40 | // for |task| is a worker thread and you can specify |task_is_slow| just |
| 41 | // like you can for PostTask above. |
| 42 | static bool PostTaskAndReply(const tracked_objects::Location& from_here, |
| 43 | const Closure& task, |
| 44 | const Closure& reply, |
| 45 | bool task_is_slow); |
[email protected] | 8e1946f9 | 2012-04-25 03:15:05 | [diff] [blame] | 46 | |
| 47 | // Return true if the current thread is one that this WorkerPool runs tasks |
| 48 | // on. (Note that if the Windows worker pool is used without going through |
| 49 | // this WorkerPool interface, RunsTasksOnCurrentThread would return false on |
| 50 | // those threads.) |
| 51 | static bool RunsTasksOnCurrentThread(); |
| 52 | |
| 53 | // Get a TaskRunner wrapper which posts to the WorkerPool using the given |
| 54 | // |task_is_slow| behavior. |
| 55 | static const scoped_refptr<TaskRunner>& GetTaskRunner(bool task_is_slow); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 56 | }; |
| 57 | |
[email protected] | ac9ba8fe | 2010-12-30 18:08:36 | [diff] [blame] | 58 | } // namespace base |
| 59 | |
| 60 | #endif // BASE_THREADING_WORKER_POOL_H_ |