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,71 +37,70 @@ 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- public:
58- using fml::Thread::ThreadConfig::ThreadConfig ;
57+ static void MockThreadConfigSetter ( const fml::Thread::ThreadConfig& config) {
58+ // set thread name
59+ fml::Thread::SetCurrentThreadName (config) ;
5960
60- void SetCurrentThreadPriority () const override {
61- pthread_t tid = pthread_self ();
62- struct sched_param param;
63- int policy = SCHED_OTHER;
64- switch (GetThreadPriority ()) {
65- case fml::Thread::ThreadPriority::DISPLAY:
66- param.sched_priority = 10 ;
67- break ;
68- default :
69- param.sched_priority = 1 ;
70- }
71- pthread_setschedparam (tid, policy, ¶m);
61+ pthread_t tid = pthread_self ();
62+ struct sched_param param;
63+ int policy = SCHED_OTHER;
64+ switch (config.priority ) {
65+ case fml::Thread::ThreadPriority::DISPLAY:
66+ param.sched_priority = 10 ;
67+ break ;
68+ default :
69+ param.sched_priority = 1 ;
7270 }
73- };
71+ pthread_setschedparam (tid, policy, ¶m);
72+ }
7473
7574TEST (Thread, ThreadPriorityCreatedWithConfig) {
7675 const std::string thread1_name = " Thread1" ;
7776 const std::string thread2_name = " Thread2" ;
78- fml::Thread thread (std::make_unique<MockThreadConfig>(
79- thread1_name, fml::Thread::ThreadPriority::NORMAL));
8077
78+ fml::Thread thread (MockThreadConfigSetter,
79+ fml::Thread::ThreadConfig (
80+ thread1_name, fml::Thread::ThreadPriority::NORMAL));
8181 bool done = false ;
82- constexpr int NAMELEN = 8 ;
82+
8383 struct sched_param param;
8484 int policy;
8585 thread.GetTaskRunner ()->PostTask ([&]() {
8686 done = true ;
87- char thread_name[NAMELEN ];
87+ char thread_name[8 ];
8888 pthread_t current_thread = pthread_self ();
89- pthread_getname_np (current_thread, thread_name, NAMELEN );
89+ pthread_getname_np (current_thread, thread_name, 8 );
9090 pthread_getschedparam (current_thread, &policy, ¶m);
9191 ASSERT_EQ (thread_name, thread1_name);
9292 ASSERT_EQ (policy, SCHED_OTHER);
9393 ASSERT_EQ (param.sched_priority , 1 );
9494 });
9595
96- fml::Thread thread2 (std::make_unique<MockThreadConfig>(
97- thread2_name, fml::Thread::ThreadPriority::DISPLAY));
96+ fml::Thread thread2 (MockThreadConfigSetter,
97+ fml::Thread::ThreadConfig (
98+ thread2_name, fml::Thread::ThreadPriority::DISPLAY));
9899 thread2.GetTaskRunner ()->PostTask ([&]() {
99100 done = true ;
100- char thread_name[NAMELEN ];
101+ char thread_name[8 ];
101102 pthread_t current_thread = pthread_self ();
102- pthread_getname_np (current_thread, thread_name, NAMELEN );
103+ pthread_getname_np (current_thread, thread_name, 8 );
103104 pthread_getschedparam (current_thread, &policy, ¶m);
104105 ASSERT_EQ (thread_name, thread2_name);
105106 ASSERT_EQ (policy, SCHED_OTHER);
0 commit comments