summaryrefslogtreecommitdiffstats
path: root/chromium/base/mac/libdispatch_task_runner.h
blob: b479bc7aa2bd58481beee854fa6453ccfc18085e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_MAC_LIBDISPATCH_TASK_RUNNER_H_
#define BASE_MAC_LIBDISPATCH_TASK_RUNNER_H_

#include <dispatch/dispatch.h>

#include "base/single_thread_task_runner.h"
#include "base/synchronization/waitable_event.h"

namespace base {
namespace mac {

// This is an implementation of the TaskRunner interface that runs closures on
// a thread managed by Apple's libdispatch. This has the benefit of being able
// to PostTask() and friends to a dispatch queue, while being reusable as a
// dispatch_queue_t.
//
// One would use this class if an object lives exclusively on one thread but
// needs a dispatch_queue_t for use in a system API. This ensures all dispatch
// callbacks happen on the same thread as Closure tasks.
//
// A LibDispatchTaskRunner will continue to run until all references to the
// underlying dispatch queue are released.
//
// Important Notes:
//   - There is no MessageLoop running on this thread, and ::current() returns
//     NULL.
//   - No nested loops can be run, and all tasks are run non-nested.
//   - Work scheduled via libdispatch runs at the same priority as and is
//     interleaved with posted tasks, though FIFO order is guaranteed.
//
class BASE_EXPORT LibDispatchTaskRunner : public base::SingleThreadTaskRunner {
 public:
  // Starts a new serial dispatch queue with a given name.
  explicit LibDispatchTaskRunner(const char* name);

  // base::TaskRunner:
  bool PostDelayedTask(const tracked_objects::Location& from_here,
                       const Closure& task,
                       base::TimeDelta delay) override;
  bool RunsTasksOnCurrentThread() const override;

  // base::SequencedTaskRunner:
  bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
                                  const Closure& task,
                                  base::TimeDelta delay) override;

  // This blocks the calling thread until all work on the dispatch queue has
  // been run and the queue has been destroyed. Destroying a queue requires
  // ALL retained references to it to be released. Any new tasks posted to
  // this thread after shutdown are dropped.
  void Shutdown();

  // Returns the dispatch queue associated with this task runner, for use with
  // system APIs that take dispatch queues. The caller is responsible for
  // retaining the result.
  //
  // All properties (context, finalizer, etc.) are managed by this class, and
  // clients should only use the result of this for dispatch_async().
  dispatch_queue_t GetDispatchQueue() const;

 protected:
  ~LibDispatchTaskRunner() override;

 private:
  static void Finalizer(void* context);

  dispatch_queue_t queue_;

  // The event on which Shutdown waits until Finalizer runs.
  base::WaitableEvent queue_finalized_;
};

}  // namespace mac
}  // namespace base

#endif  // BASE_MAC_LIBDISPATCH_TASK_RUNNER_H_