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_THREAD_H_
6#define FLUTTER_FML_THREAD_H_
7
8#include <atomic>
9#include <functional>
10#include <memory>
11#include <string>
12#include <thread>
13
14#include "flutter/fml/macros.h"
15#include "flutter/fml/task_runner.h"
16
17namespace fml {
18
19class Thread {
20 public:
21 /// Valid values for priority of Thread.
22 enum class ThreadPriority : int {
23 /// Suitable for threads that shouldn't disrupt high priority work.
24 BACKGROUND,
25 /// Default priority level.
26 NORMAL,
27 /// Suitable for threads which generate data for the display.
28 DISPLAY,
29 /// Suitable for thread which raster data.
30 RASTER,
31 };
32
33 /// The ThreadConfig is the thread info include thread name, thread priority.
34 struct ThreadConfig {
35 ThreadConfig(const std::string& name, ThreadPriority priority)
36 : name(name), priority(priority) {}
37
38 explicit ThreadConfig(const std::string& name)
39 : ThreadConfig(name, ThreadPriority::NORMAL) {}
40
41 ThreadConfig() : ThreadConfig("", ThreadPriority::NORMAL) {}
42
43 std::string name;
44 ThreadPriority priority;
45 };
46
47 using ThreadConfigSetter = std::function<void(const ThreadConfig&)>;
48
49 explicit Thread(const std::string& name = "");
50
51 explicit Thread(const ThreadConfigSetter& setter,
52 const ThreadConfig& config = ThreadConfig());
53
54 ~Thread();
55
56 fml::RefPtr<fml::TaskRunner> GetTaskRunner() const;
57
58 void Join();
59
60 static void SetCurrentThreadName(const ThreadConfig& config);
61
62 private:
63 std::unique_ptr<std::thread> thread_;
64
65 fml::RefPtr<fml::TaskRunner> task_runner_;
66
67 std::atomic_bool joined_;
68
69 FML_DISALLOW_COPY_AND_ASSIGN(Thread);
70};
71
72} // namespace fml
73
74#endif // FLUTTER_FML_THREAD_H_
75

source code of flutter_engine/flutter/fml/thread.h