Home | History | Annotate | Download | only in threading
      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.
      4 
      5 #ifndef BASE_THREADING_WORKER_POOL_H_
      6 #define BASE_THREADING_WORKER_POOL_H_
      7 
      8 #include "base/base_export.h"
      9 #include "base/callback.h"
     10 #include "base/memory/ref_counted.h"
     11 
     12 namespace tracked_objects {
     13 class Location;
     14 }  // namespace tracked_objects
     15 
     16 namespace base {
     17 
     18 class TaskRunner;
     19 
     20 // This is a facility that runs tasks that don't require a specific thread or
     21 // a message loop.
     22 //
     23 // 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
     25 // inside the pool must be extremely careful about other objects they access
     26 // (MessageLoops, Singletons, etc). During shutdown these object may no longer
     27 // exist.
     28 class BASE_EXPORT WorkerPool {
     29  public:
     30   // 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
     32   // 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.
     34   static bool PostTask(const tracked_objects::Location& from_here,
     35                        OnceClosure task,
     36                        bool task_is_slow);
     37 
     38   // Just like TaskRunner::PostTaskAndReply, except the destination
     39   // for |task| is a worker thread and you can specify |task_is_slow| just
     40   // like you can for PostTask above.
     41   static bool PostTaskAndReply(const tracked_objects::Location& from_here,
     42                                OnceClosure task,
     43                                OnceClosure reply,
     44                                bool task_is_slow);
     45 
     46   // Return true if the current thread is one that this WorkerPool runs tasks
     47   // on.  (Note that if the Windows worker pool is used without going through
     48   // this WorkerPool interface, RunsTasksOnCurrentThread would return false on
     49   // those threads.)
     50   static bool RunsTasksOnCurrentThread();
     51 
     52   // Get a TaskRunner wrapper which posts to the WorkerPool using the given
     53   // |task_is_slow| behavior.
     54   static const scoped_refptr<TaskRunner>& GetTaskRunner(bool task_is_slow);
     55 };
     56 
     57 }  // namespace base
     58 
     59 #endif  // BASE_THREADING_WORKER_POOL_H_
     60