Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 1535f76

Browse files
committed
Split out the thread data and the thread data set operator
1 parent 9378b40 commit 1535f76

File tree

12 files changed

+219
-166
lines changed

12 files changed

+219
-166
lines changed

fml/thread.cc

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,22 @@
2424

2525
namespace fml {
2626

27-
Thread::ThreadConfig::ThreadConfig(const std::string& name,
28-
ThreadPriority priority)
29-
: thread_name_(name), thread_priority_(priority) {}
30-
31-
void Thread::ThreadConfig::SetCurrentThreadName() const {
32-
Thread::SetCurrentThreadName(thread_name_);
27+
void Thread::ThreadConfigSetter::set(const ThreadConfig& config) const {
28+
Thread::SetCurrentThreadName(config.name);
3329
}
3430

35-
void Thread::ThreadConfig::SetCurrentThreadPriority() const {}
36-
3731
Thread::Thread(const std::string& name)
38-
: Thread(ThreadConfig::MakeDefaultConfigure(name)) {}
32+
: Thread(Thread::ThreadConfigSetter::MakeDefaultConfigurerSetter(),
33+
ThreadConfig(name)) {}
3934

40-
Thread::Thread(std::unique_ptr<ThreadConfig> config) : joined_(false) {
35+
Thread::Thread(std::shared_ptr<ThreadConfigSetter> setter,
36+
const ThreadConfig& config)
37+
: joined_(false) {
4138
fml::AutoResetWaitableEvent latch;
4239
fml::RefPtr<fml::TaskRunner> runner;
4340
thread_ = std::make_unique<std::thread>(
44-
[&latch, &runner, threadConfig = std::move(config)]() -> void {
45-
threadConfig->SetCurrentThreadName();
46-
threadConfig->SetCurrentThreadPriority();
41+
[&latch, &runner, setter, config]() -> void {
42+
setter->set(config);
4743
fml::MessageLoop::EnsureInitializedForCurrentThread();
4844
auto& loop = MessageLoop::GetCurrent();
4945
runner = loop.GetTaskRunner();

fml/thread.h

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,40 +29,38 @@ class Thread {
2929
RASTER,
3030
};
3131

32-
/// The ThreadConfig is used for setting thread perorities.
33-
class ThreadConfig {
34-
public:
35-
explicit ThreadConfig(const std::string& name = "",
36-
ThreadPriority priority = ThreadPriority::NORMAL);
37-
38-
static std::unique_ptr<ThreadConfig> MakeDefaultConfigure(
39-
const std::string& name = "") {
40-
return std::make_unique<ThreadConfig>(name);
41-
}
32+
/// The ThreadConfig is the thread info include thread name, thread priority.
33+
struct ThreadConfig {
34+
ThreadConfig(const std::string& name, ThreadPriority priority)
35+
: name(name), priority(priority) {}
4236

43-
ThreadPriority GetThreadPriority() const { return thread_priority_; }
37+
explicit ThreadConfig(const std::string& name)
38+
: ThreadConfig(name, ThreadPriority::NORMAL) {}
4439

45-
const std::string& GetThreadName() const { return thread_name_; }
40+
ThreadConfig() : ThreadConfig("", ThreadPriority::NORMAL) {}
4641

47-
/// Set current thread name.
48-
virtual void SetCurrentThreadName() const;
42+
std::string name;
43+
ThreadPriority priority;
44+
};
4945

50-
/// default do nothing, which mean user can use platform api to set priority
51-
/// example: iOS might use pthread_qos set thread priority, Android might
52-
/// use ::setPriority set thread priority
53-
virtual void SetCurrentThreadPriority() const;
46+
class ThreadConfigSetter {
47+
public:
48+
/// Use the ThreadConfig info to set thread priorities.
49+
/// The default set method has implement set thread name logic.
50+
virtual void set(const ThreadConfig& config) const;
5451

55-
virtual ~ThreadConfig() = default;
52+
/// Make a default thread config setter, it can only set the thread name
53+
static std::shared_ptr<ThreadConfigSetter> MakeDefaultConfigurerSetter() {
54+
return std::make_shared<ThreadConfigSetter>();
55+
}
5656

57-
private:
58-
const std::string thread_name_;
59-
ThreadPriority thread_priority_;
57+
virtual ~ThreadConfigSetter() = default;
6058
};
6159

62-
explicit Thread(const std::string& name);
60+
explicit Thread(const std::string& name = "");
6361

64-
explicit Thread(std::unique_ptr<ThreadConfig> config =
65-
ThreadConfig::MakeDefaultConfigure());
62+
explicit Thread(std::shared_ptr<ThreadConfigSetter> setter,
63+
const ThreadConfig& config = ThreadConfig());
6664

6765
~Thread();
6866

@@ -74,7 +72,9 @@ class Thread {
7472

7573
private:
7674
std::unique_ptr<std::thread> thread_;
75+
7776
fml::RefPtr<fml::TaskRunner> task_runner_;
77+
7878
std::atomic_bool joined_;
7979

8080
FML_DISALLOW_COPY_AND_ASSIGN(Thread);

fml/thread_unittests.cc

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
#include "flutter/fml/thread.h"
66

7-
#define FLUTTER_PTHREAD_SUPPORTED \
8-
defined(OS_MACOSX) || defined(OS_LINUX) || defined(OS_ANDROID)
7+
#if defined(OS_MACOSX) || defined(OS_LINUX) || defined(OS_ANDROID)
8+
#define FLUTTER_PTHREAD_SUPPORTED 1
9+
#else
10+
#define FLUTTER_PTHREAD_SUPPORTED 0
11+
#endif
912

10-
#ifdef FLUTTER_PTHREAD_SUPPORTED
13+
#if FLUTTER_PTHREAD_SUPPORTED
1114
#include <pthread.h>
1215
#else
13-
#error "Doesn't has pthead.h"
1416
#endif
1517

1618
#include <memory>
@@ -35,33 +37,35 @@ TEST(Thread, HasARunningMessageLoop) {
3537
ASSERT_TRUE(done);
3638
}
3739

38-
#ifdef FLUTTER_PTHREAD_SUPPORTED
40+
#if FLUTTER_PTHREAD_SUPPORTED
3941
TEST(Thread, ThreadNameCreatedWithConfig) {
4042
const std::string name = "Thread1";
41-
fml::Thread thread(fml::Thread::ThreadConfig::MakeDefaultConfigure(name));
43+
fml::Thread thread(name);
4244

4345
bool done = false;
44-
constexpr int NAMELEN = 8;
4546
thread.GetTaskRunner()->PostTask([&done, &name]() {
4647
done = true;
47-
char thread_name[NAMELEN];
48+
char thread_name[8];
4849
pthread_t current_thread = pthread_self();
49-
pthread_getname_np(current_thread, thread_name, NAMELEN);
50+
pthread_getname_np(current_thread, thread_name, 8);
5051
ASSERT_EQ(thread_name, name);
5152
});
5253
thread.Join();
5354
ASSERT_TRUE(done);
5455
}
5556

56-
class MockThreadConfig : public fml::Thread::ThreadConfig {
57+
class MockThreadConfigSetter : public fml::Thread::ThreadConfigSetter {
5758
public:
58-
using fml::Thread::ThreadConfig::ThreadConfig;
59+
using fml::Thread::ThreadConfigSetter::ThreadConfigSetter;
60+
61+
void set(const fml::Thread::ThreadConfig& config) const override {
62+
// set thread name
63+
fml::Thread::ThreadConfigSetter::set(config);
5964

60-
void SetCurrentThreadPriority() const override {
6165
pthread_t tid = pthread_self();
6266
struct sched_param param;
6367
int policy = SCHED_OTHER;
64-
switch (GetThreadPriority()) {
68+
switch (config.priority) {
6569
case fml::Thread::ThreadPriority::DISPLAY:
6670
param.sched_priority = 10;
6771
break;
@@ -75,31 +79,33 @@ class MockThreadConfig : public fml::Thread::ThreadConfig {
7579
TEST(Thread, ThreadPriorityCreatedWithConfig) {
7680
const std::string thread1_name = "Thread1";
7781
const std::string thread2_name = "Thread2";
78-
fml::Thread thread(std::make_unique<MockThreadConfig>(
79-
thread1_name, fml::Thread::ThreadPriority::NORMAL));
8082

83+
fml::Thread thread(std::make_shared<MockThreadConfigSetter>(),
84+
fml::Thread::ThreadConfig(
85+
thread1_name, fml::Thread::ThreadPriority::NORMAL));
8186
bool done = false;
82-
constexpr int NAMELEN = 8;
87+
8388
struct sched_param param;
8489
int policy;
8590
thread.GetTaskRunner()->PostTask([&]() {
8691
done = true;
87-
char thread_name[NAMELEN];
92+
char thread_name[8];
8893
pthread_t current_thread = pthread_self();
89-
pthread_getname_np(current_thread, thread_name, NAMELEN);
94+
pthread_getname_np(current_thread, thread_name, 8);
9095
pthread_getschedparam(current_thread, &policy, &param);
9196
ASSERT_EQ(thread_name, thread1_name);
9297
ASSERT_EQ(policy, SCHED_OTHER);
9398
ASSERT_EQ(param.sched_priority, 1);
9499
});
95100

96-
fml::Thread thread2(std::make_unique<MockThreadConfig>(
97-
thread2_name, fml::Thread::ThreadPriority::DISPLAY));
101+
fml::Thread thread2(std::make_shared<MockThreadConfigSetter>(),
102+
fml::Thread::ThreadConfig(
103+
thread2_name, fml::Thread::ThreadPriority::DISPLAY));
98104
thread2.GetTaskRunner()->PostTask([&]() {
99105
done = true;
100-
char thread_name[NAMELEN];
106+
char thread_name[8];
101107
pthread_t current_thread = pthread_self();
102-
pthread_getname_np(current_thread, thread_name, NAMELEN);
108+
pthread_getname_np(current_thread, thread_name, 8);
103109
pthread_getschedparam(current_thread, &policy, &param);
104110
ASSERT_EQ(thread_name, thread2_name);
105111
ASSERT_EQ(policy, SCHED_OTHER);

lib/ui/ui_benchmarks.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class Fixture : public testing::FixtureTest {
2020
};
2121

2222
static void BM_PlatformMessageResponseDartComplete(benchmark::State& state) {
23-
ThreadHost thread_host("test",
24-
ThreadHost::Type::Platform | ThreadHost::Type::RASTER |
25-
ThreadHost::Type::IO | ThreadHost::Type::UI);
23+
ThreadHost thread_host(ThreadHost::ThreadHostConfig(
24+
"test", ThreadHost::Type::Platform | ThreadHost::Type::RASTER |
25+
ThreadHost::Type::IO | ThreadHost::Type::UI));
2626
TaskRunners task_runners("test", thread_host.platform_thread->GetTaskRunner(),
2727
thread_host.raster_thread->GetTaskRunner(),
2828
thread_host.ui_thread->GetTaskRunner(),
@@ -68,9 +68,9 @@ static void BM_PlatformMessageResponseDartComplete(benchmark::State& state) {
6868
}
6969

7070
static void BM_PathVolatilityTracker(benchmark::State& state) {
71-
ThreadHost thread_host("test",
72-
ThreadHost::Type::Platform | ThreadHost::Type::RASTER |
73-
ThreadHost::Type::IO | ThreadHost::Type::UI);
71+
ThreadHost thread_host(ThreadHost::ThreadHostConfig(
72+
"test", ThreadHost::Type::Platform | ThreadHost::Type::RASTER |
73+
ThreadHost::Type::IO | ThreadHost::Type::UI));
7474
TaskRunners task_runners("test", thread_host.platform_thread->GetTaskRunner(),
7575
thread_host.raster_thread->GetTaskRunner(),
7676
thread_host.ui_thread->GetTaskRunner(),

shell/common/shell_benchmarks.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ static void StartupAndShutdownShell(benchmark::State& state,
4343
};
4444
}
4545

46-
thread_host = std::make_unique<ThreadHost>(
46+
thread_host = std::make_unique<ThreadHost>(ThreadHost::ThreadHostConfig(
4747
"io.flutter.bench.", ThreadHost::Type::Platform |
4848
ThreadHost::Type::RASTER |
49-
ThreadHost::Type::IO | ThreadHost::Type::UI);
49+
ThreadHost::Type::IO | ThreadHost::Type::UI));
5050

5151
TaskRunners task_runners("test",
5252
thread_host->platform_thread->GetTaskRunner(),

shell/common/shell_unittests.cc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,10 @@ TEST_F(ShellTest, InitializeWithInvalidThreads) {
264264
TEST_F(ShellTest, InitializeWithDifferentThreads) {
265265
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
266266
Settings settings = CreateSettingsForFixture();
267-
ThreadHost thread_host("io.flutter.test." + GetCurrentTestName() + ".",
268-
ThreadHost::Type::Platform | ThreadHost::Type::RASTER |
269-
ThreadHost::Type::IO | ThreadHost::Type::UI);
267+
ThreadHost thread_host(ThreadHost::ThreadHostConfig(
268+
"io.flutter.test." + GetCurrentTestName() + ".",
269+
ThreadHost::Type::Platform | ThreadHost::Type::RASTER |
270+
ThreadHost::Type::IO | ThreadHost::Type::UI));
270271
TaskRunners task_runners("test", thread_host.platform_thread->GetTaskRunner(),
271272
thread_host.raster_thread->GetTaskRunner(),
272273
thread_host.ui_thread->GetTaskRunner(),
@@ -3112,8 +3113,9 @@ TEST_F(ShellTest, UpdateAssetResolverByTypeAppends) {
31123113
TEST_F(ShellTest, UpdateAssetResolverByTypeNull) {
31133114
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
31143115
Settings settings = CreateSettingsForFixture();
3115-
ThreadHost thread_host("io.flutter.test." + GetCurrentTestName() + ".",
3116-
ThreadHost::Type::Platform);
3116+
ThreadHost thread_host(ThreadHost::ThreadHostConfig(
3117+
"io.flutter.test." + GetCurrentTestName() + ".",
3118+
ThreadHost::Type::Platform));
31173119
auto task_runner = thread_host.platform_thread->GetTaskRunner();
31183120
TaskRunners task_runners("test", task_runner, task_runner, task_runner,
31193121
task_runner);
@@ -3149,8 +3151,9 @@ TEST_F(ShellTest, UpdateAssetResolverByTypeNull) {
31493151
TEST_F(ShellTest, UpdateAssetResolverByTypeDoesNotReplaceMismatchType) {
31503152
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
31513153
Settings settings = CreateSettingsForFixture();
3152-
ThreadHost thread_host("io.flutter.test." + GetCurrentTestName() + ".",
3153-
ThreadHost::Type::Platform);
3154+
ThreadHost thread_host(ThreadHost::ThreadHostConfig(
3155+
"io.flutter.test." + GetCurrentTestName() + ".",
3156+
ThreadHost::Type::Platform));
31543157
auto task_runner = thread_host.platform_thread->GetTaskRunner();
31553158
TaskRunners task_runners("test", task_runner, task_runner, task_runner,
31563159
task_runner);

shell/common/thread_host.cc

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <algorithm>
88
#include <memory>
9+
#include <optional>
910
#include <string>
1011
#include <utility>
1112

@@ -30,49 +31,46 @@ std::string ThreadHost::ThreadHostConfig::MakeThreadName(
3031

3132
std::unique_ptr<fml::Thread> ThreadHost::CreateThread(
3233
Type type,
33-
ThreadHost::ThreadConfig configure) {
34-
std::string name = ThreadHostConfig::MakeThreadName(type, name_prefix);
35-
if (configure != nullptr) {
36-
return std::make_unique<fml::Thread>(std::move(configure));
34+
std::optional<ThreadConfig> thread_config,
35+
const ThreadHostConfig& host_config) const {
36+
/// if not specified ThreadConfig, create a ThreadConfig.
37+
if (!thread_config.has_value()) {
38+
thread_config = ThreadConfig(
39+
ThreadHostConfig::MakeThreadName(type, host_config.name_prefix));
3740
}
38-
return std::make_unique<fml::Thread>(
39-
fml::Thread::ThreadConfig::MakeDefaultConfigure(name));
41+
return std::make_unique<fml::Thread>(host_config.config_setter,
42+
thread_config.value());
4043
}
4144

4245
ThreadHost::ThreadHost() = default;
4346

4447
ThreadHost::ThreadHost(ThreadHost&&) = default;
4548

46-
ThreadHost::ThreadHost(std::string name_prefix_arg,
47-
uint64_t mask,
48-
ThreadHostConfig configure_host)
49-
: name_prefix(name_prefix_arg) {
50-
if (mask & ThreadHost::Type::Platform) {
49+
ThreadHost::ThreadHost(const std::string name_prefix, uint64_t mask)
50+
: ThreadHost(ThreadHostConfig(name_prefix, mask)) {}
51+
52+
ThreadHost::ThreadHost(const ThreadHostConfig& host_config) {
53+
if (host_config.isNeedThread(ThreadHost::Type::Platform)) {
5154
platform_thread =
52-
CreateThread(ThreadHost::Type::Platform,
53-
std::move(configure_host.platform_configure));
55+
CreateThread(Type::Platform, host_config.platform_config, host_config);
5456
}
5557

56-
if (mask & ThreadHost::Type::UI) {
57-
ui_thread = CreateThread(ThreadHost::Type::UI,
58-
std::move(configure_host.ui_configure));
58+
if (host_config.isNeedThread(ThreadHost::Type::UI)) {
59+
ui_thread = CreateThread(Type::UI, host_config.ui_config, host_config);
5960
}
6061

61-
if (mask & ThreadHost::Type::RASTER) {
62-
raster_thread = CreateThread(ThreadHost::Type::RASTER,
63-
std::move(configure_host.raster_configure));
62+
if (host_config.isNeedThread(ThreadHost::Type::RASTER)) {
63+
raster_thread =
64+
CreateThread(Type::RASTER, host_config.raster_config, host_config);
6465
}
6566

66-
if (mask & ThreadHost::Type::IO) {
67-
io_thread = CreateThread(ThreadHost::Type::IO,
68-
std::move(configure_host.io_configure));
67+
if (host_config.isNeedThread(ThreadHost::Type::IO)) {
68+
io_thread = CreateThread(Type::IO, host_config.io_config, host_config);
6969
}
7070

71-
if (mask & ThreadHost::Type::Profiler) {
71+
if (host_config.isNeedThread(ThreadHost::Type::Profiler)) {
7272
profiler_thread =
73-
CreateThread(ThreadHost::Type::Profiler,
74-
std::move(configure_host.profiler_configure));
75-
;
73+
CreateThread(Type::Profiler, host_config.profiler_config, host_config);
7674
}
7775
}
7876

0 commit comments

Comments
 (0)