blob: c8e58935f0977259349857bf91d71c63bf734567 [file] [log] [blame]
gab0ac3fff2016-05-10 23:17:011// Copyright 2016 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_THREAD_TASK_RUNNER_HANDLE_H_
6#define BASE_THREADING_THREAD_TASK_RUNNER_HANDLE_H_
7
8#include "base/base_export.h"
9#include "base/macros.h"
10#include "base/memory/ref_counted.h"
fdoray1d08c582016-06-09 22:55:0611#include "base/single_thread_task_runner.h"
gab0ac3fff2016-05-10 23:17:0112
13namespace base {
14
gab0ac3fff2016-05-10 23:17:0115// ThreadTaskRunnerHandle stores a reference to a thread's TaskRunner
16// in thread-local storage. Callers can then retrieve the TaskRunner
17// for the current thread by calling ThreadTaskRunnerHandle::Get().
18// At most one TaskRunner may be bound to each thread at a time.
19// Prefer SequenceTaskRunnerHandle to this unless thread affinity is required.
20class BASE_EXPORT ThreadTaskRunnerHandle {
21 public:
22 // Gets the SingleThreadTaskRunner for the current thread.
23 static scoped_refptr<SingleThreadTaskRunner> Get();
24
25 // Returns true if the SingleThreadTaskRunner is already created for
26 // the current thread.
27 static bool IsSet();
28
29 // Binds |task_runner| to the current thread. |task_runner| must belong
30 // to the current thread for this to succeed.
31 explicit ThreadTaskRunnerHandle(
32 scoped_refptr<SingleThreadTaskRunner> task_runner);
33 ~ThreadTaskRunnerHandle();
34
35 private:
36 scoped_refptr<SingleThreadTaskRunner> task_runner_;
37
38 DISALLOW_COPY_AND_ASSIGN(ThreadTaskRunnerHandle);
39};
40
41} // namespace base
42
43#endif // BASE_THREADING_THREAD_TASK_RUNNER_HANDLE_H_