2
2
// Use of this source code is governed by a BSD-style license that can be
3
3
// found in the LICENSE file.
4
4
5
+ #include " flutter/fml/build_config.h"
5
6
#include " flutter/fml/thread.h"
6
7
7
8
#if defined(FML_OS_MACOSX) || defined(FML_OS_LINUX) || defined(FML_OS_ANDROID)
15
16
#else
16
17
#endif
17
18
19
+ #if defined(FML_OS_WIN)
20
+ #include < windows.h>
21
+ #endif
22
+
18
23
#include < memory>
19
24
#include " gtest/gtest.h"
20
25
@@ -37,6 +42,35 @@ TEST(Thread, HasARunningMessageLoop) {
37
42
ASSERT_TRUE (done);
38
43
}
39
44
45
+ TEST (Thread, HasExpectedStackSize) {
46
+ size_t stack_size = 0 ;
47
+ fml::Thread thread;
48
+
49
+ thread.GetTaskRunner ()->PostTask ([&stack_size]() {
50
+ #if defined(FML_OS_WIN)
51
+ ULONG_PTR low_limit;
52
+ ULONG_PTR high_limit;
53
+ GetCurrentThreadStackLimits (&low_limit, &high_limit);
54
+ stack_size = high_limit - low_limit;
55
+ #elif defined(FML_OS_MACOSX)
56
+ stack_size = pthread_get_stacksize_np (pthread_self ());
57
+ #else
58
+ pthread_attr_t attr;
59
+ pthread_getattr_np (pthread_self (), &attr);
60
+ pthread_attr_getstacksize (&attr, &stack_size);
61
+ pthread_attr_destroy (&attr);
62
+ #endif
63
+ });
64
+ thread.Join ();
65
+
66
+ // Actual stack size will be aligned to page size, this assumes no supported
67
+ // platform has a page size larger than 16k. On Linux reducing the default
68
+ // stack size (8MB) does not seem to have any effect.
69
+ const size_t kPageSize = 16384 ;
70
+ ASSERT_TRUE (stack_size / kPageSize >=
71
+ fml::Thread::GetDefaultStackSize () / kPageSize );
72
+ }
73
+
40
74
#if FLUTTER_PTHREAD_SUPPORTED
41
75
TEST (Thread, ThreadNameCreatedWithConfig) {
42
76
const std::string name = " Thread1" ;
@@ -45,15 +79,27 @@ TEST(Thread, ThreadNameCreatedWithConfig) {
45
79
bool done = false ;
46
80
thread.GetTaskRunner ()->PostTask ([&done, &name]() {
47
81
done = true ;
48
- char thread_name[8 ];
82
+ char thread_name[16 ];
49
83
pthread_t current_thread = pthread_self ();
50
- pthread_getname_np (current_thread, thread_name, 8 );
84
+ pthread_getname_np (current_thread, thread_name, 16 );
51
85
ASSERT_EQ (thread_name, name);
52
86
});
53
87
thread.Join ();
54
88
ASSERT_TRUE (done);
55
89
}
56
90
91
+ static int clamp_priority (int priority, int policy) {
92
+ int min = sched_get_priority_min (policy);
93
+ int max = sched_get_priority_max (policy);
94
+ if (priority < min) {
95
+ return min;
96
+ } else if (priority > max) {
97
+ return max;
98
+ } else {
99
+ return priority;
100
+ }
101
+ }
102
+
57
103
static void MockThreadConfigSetter (const fml::Thread::ThreadConfig& config) {
58
104
// set thread name
59
105
fml::Thread::SetCurrentThreadName (config);
@@ -63,10 +109,10 @@ static void MockThreadConfigSetter(const fml::Thread::ThreadConfig& config) {
63
109
int policy = SCHED_OTHER;
64
110
switch (config.priority ) {
65
111
case fml::Thread::ThreadPriority::kDisplay :
66
- param.sched_priority = 10 ;
112
+ param.sched_priority = clamp_priority ( 10 , policy) ;
67
113
break ;
68
114
default :
69
- param.sched_priority = 1 ;
115
+ param.sched_priority = clamp_priority ( 1 , policy) ;
70
116
}
71
117
pthread_setschedparam (tid, policy, ¶m);
72
118
}
@@ -84,27 +130,27 @@ TEST(Thread, ThreadPriorityCreatedWithConfig) {
84
130
int policy;
85
131
thread.GetTaskRunner ()->PostTask ([&]() {
86
132
done = true ;
87
- char thread_name[8 ];
133
+ char thread_name[16 ];
88
134
pthread_t current_thread = pthread_self ();
89
- pthread_getname_np (current_thread, thread_name, 8 );
135
+ pthread_getname_np (current_thread, thread_name, 16 );
90
136
pthread_getschedparam (current_thread, &policy, ¶m);
91
137
ASSERT_EQ (thread_name, thread1_name);
92
138
ASSERT_EQ (policy, SCHED_OTHER);
93
- ASSERT_EQ (param.sched_priority , 1 );
139
+ ASSERT_EQ (param.sched_priority , clamp_priority ( 1 , policy) );
94
140
});
95
141
96
142
fml::Thread thread2 (MockThreadConfigSetter,
97
143
fml::Thread::ThreadConfig (
98
144
thread2_name, fml::Thread::ThreadPriority::kDisplay ));
99
145
thread2.GetTaskRunner ()->PostTask ([&]() {
100
146
done = true ;
101
- char thread_name[8 ];
147
+ char thread_name[16 ];
102
148
pthread_t current_thread = pthread_self ();
103
- pthread_getname_np (current_thread, thread_name, 8 );
149
+ pthread_getname_np (current_thread, thread_name, 16 );
104
150
pthread_getschedparam (current_thread, &policy, ¶m);
105
151
ASSERT_EQ (thread_name, thread2_name);
106
152
ASSERT_EQ (policy, SCHED_OTHER);
107
- ASSERT_EQ (param.sched_priority , 10 );
153
+ ASSERT_EQ (param.sched_priority , clamp_priority ( 10 , policy) );
108
154
});
109
155
thread.Join ();
110
156
ASSERT_TRUE (done);
0 commit comments