1// Copyright 2013 The Flutter 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 FLUTTER_FML_CONCURRENT_MESSAGE_LOOP_H_
6#define FLUTTER_FML_CONCURRENT_MESSAGE_LOOP_H_
7
8#include <condition_variable>
9#include <map>
10#include <queue>
11#include <thread>
12
13#include "flutter/fml/closure.h"
14#include "flutter/fml/macros.h"
15
16namespace fml {
17
18class ConcurrentTaskRunner;
19
20class ConcurrentMessageLoop
21 : public std::enable_shared_from_this<ConcurrentMessageLoop> {
22 public:
23 static std::shared_ptr<ConcurrentMessageLoop> Create(
24 size_t worker_count = std::thread::hardware_concurrency());
25
26 ~ConcurrentMessageLoop();
27
28 size_t GetWorkerCount() const;
29
30 std::shared_ptr<ConcurrentTaskRunner> GetTaskRunner();
31
32 void Terminate();
33
34 void PostTaskToAllWorkers(fml::closure task);
35
36 private:
37 friend ConcurrentTaskRunner;
38
39 size_t worker_count_ = 0;
40 std::vector<std::thread> workers_;
41 std::mutex tasks_mutex_;
42 std::condition_variable tasks_condition_;
43 std::queue<fml::closure> tasks_;
44 std::vector<std::thread::id> worker_thread_ids_;
45 std::map<std::thread::id, std::vector<fml::closure>> thread_tasks_;
46 bool shutdown_ = false;
47
48 ConcurrentMessageLoop(size_t worker_count);
49
50 void WorkerMain();
51
52 void PostTask(const fml::closure& task);
53
54 bool HasThreadTasksLocked() const;
55
56 std::vector<fml::closure> GetThreadTasksLocked();
57
58 FML_DISALLOW_COPY_AND_ASSIGN(ConcurrentMessageLoop);
59};
60
61class ConcurrentTaskRunner {
62 public:
63 ConcurrentTaskRunner(std::weak_ptr<ConcurrentMessageLoop> weak_loop);
64
65 ~ConcurrentTaskRunner();
66
67 void PostTask(const fml::closure& task);
68
69 private:
70 friend ConcurrentMessageLoop;
71
72 std::weak_ptr<ConcurrentMessageLoop> weak_loop_;
73
74 FML_DISALLOW_COPY_AND_ASSIGN(ConcurrentTaskRunner);
75};
76
77} // namespace fml
78
79#endif // FLUTTER_FML_CONCURRENT_MESSAGE_LOOP_H_
80