forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
thread.h
74 lines (52 loc) · 1.8 KB
/
thread.h
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
// Copyright 2013 The Flutter 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 FLUTTER_FML_THREAD_H_
#define FLUTTER_FML_THREAD_H_
#include <atomic>
#include <functional>
#include <memory>
#include <string>
#include <thread>
#include "flutter/fml/macros.h"
#include "flutter/fml/task_runner.h"
namespace fml {
class Thread {
public:
/// Valid values for priority of Thread.
enum class ThreadPriority : int {
/// Suitable for threads that shouldn't disrupt high priority work.
BACKGROUND,
/// Default priority level.
NORMAL,
/// Suitable for threads which generate data for the display.
DISPLAY,
/// Suitable for thread which raster data.
RASTER,
};
/// The ThreadConfig is the thread info include thread name, thread priority.
struct ThreadConfig {
ThreadConfig(const std::string& name, ThreadPriority priority)
: name(name), priority(priority) {}
explicit ThreadConfig(const std::string& name)
: ThreadConfig(name, ThreadPriority::NORMAL) {}
ThreadConfig() : ThreadConfig("", ThreadPriority::NORMAL) {}
std::string name;
ThreadPriority priority;
};
using ThreadConfigSetter = std::function<void(const ThreadConfig&)>;
explicit Thread(const std::string& name = "");
explicit Thread(const ThreadConfigSetter& setter,
const ThreadConfig& config = ThreadConfig());
~Thread();
fml::RefPtr<fml::TaskRunner> GetTaskRunner() const;
void Join();
static void SetCurrentThreadName(const ThreadConfig& config);
private:
std::unique_ptr<std::thread> thread_;
fml::RefPtr<fml::TaskRunner> task_runner_;
std::atomic_bool joined_;
FML_DISALLOW_COPY_AND_ASSIGN(Thread);
};
} // namespace fml
#endif // FLUTTER_FML_THREAD_H_