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
3941TEST (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 {
7579TEST (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, ¶m);
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, ¶m);
104110 ASSERT_EQ (thread_name, thread2_name);
105111 ASSERT_EQ (policy, SCHED_OTHER);
0 commit comments