summaryrefslogtreecommitdiff
path: root/gn/util/msg_loop.h
blob: 267d2a9f681c986a8595d07e7c7fa7b632d6f415 (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
// Copyright 2018 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 UTIL_RUN_LOOP_H_
#define UTIL_RUN_LOOP_H_

#include "base/macros.h"
#include "util/task.h"

#include <condition_variable>
#include <mutex>
#include <queue>

class MsgLoop {
 public:
  MsgLoop();
  ~MsgLoop();

  // Blocks until PostQuit() is called, processing work items posted via
  void Run();

  // Schedules Run() to exit, but will not happen until other outstanding tasks
  // complete. Can be called from any thread.
  void PostQuit();

  // Posts a work item to this queue. All items will be run on the thread from
  // which Run() was called. Can be called from any thread.
  void PostTask(Task task);

  // Run()s until the queue is empty. Should only be used (carefully) in tests.
  void RunUntilIdleForTesting();

  // Gets the MsgLoop for the thread from which it's called, or nullptr if
  // there's no MsgLoop for the current thread.
  static MsgLoop* Current();

 private:
  std::mutex queue_mutex_;
  std::queue<Task> task_queue_;
  std::condition_variable notifier_;
  bool should_quit_ = false;

  DISALLOW_COPY_AND_ASSIGN(MsgLoop);
};

#endif  // UTIL_RUN_LOOP_H_