QUIC - cleanup changes to sync chromium tree with internal source.
[chromium-blink-merge.git] / base / threading / worker_pool.h
blobf8c62357d93bf7439a76fbfa4324e601f052ebc8
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
3 // found in the LICENSE file.
5 #ifndef BASE_THREADING_WORKER_POOL_H_
6 #define BASE_THREADING_WORKER_POOL_H_
8 #include "base/base_export.h"
9 #include "base/callback_forward.h"
10 #include "base/memory/ref_counted.h"
12 class Task;
14 namespace tracked_objects {
15 class Location;
16 } // namespace tracked_objects
18 namespace base {
20 class TaskRunner;
22 // This is a facility that runs tasks that don't require a specific thread or
23 // a message loop.
25 // WARNING: This shouldn't be used unless absolutely necessary. Typically
26 // (without calling ShutDownCleanly()), we don't wait for the worker pool
27 // threads to finish on shutdown, so the tasks running inside the pool must be
28 // extremely careful about other objects they access (MessageLoops, Singletons,
29 // etc). During shutdown these object may no longer exist.
30 class BASE_EXPORT WorkerPool {
31 public:
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.
36 static bool PostTask(const tracked_objects::Location& from_here,
37 const base::Closure& task, bool task_is_slow);
39 // Just like TaskRunner::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);
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();
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);
57 // Blocks until all worker threads quit cleanly. Please note that it ensures
58 // that no worker threads are running after the method returns, but it doesn't
59 // guarantee to process all queued pending tasks. This method may take a long
60 // time. Please don't use it unless absolutely necessary, e.g., when we want
61 // to unload the library containing the worker pool before process shutdown.
62 static void ShutDownCleanly();
65 } // namespace base
67 #endif // BASE_THREADING_WORKER_POOL_H_