5#include "flutter/fml/build_config.h"
6#include "flutter/fml/thread.h"
8#if defined(FML_OS_MACOSX) || defined(FML_OS_LINUX) || defined(FML_OS_ANDROID)
9#define FLUTTER_PTHREAD_SUPPORTED 1
11#define FLUTTER_PTHREAD_SUPPORTED 0
14#if FLUTTER_PTHREAD_SUPPORTED
19#if defined(FML_OS_WIN)
25#include "gtest/gtest.h"
27TEST(Thread, CanStartAndEnd) {
32TEST(Thread, CanStartAndEndWithExplicitJoin) {
38TEST(Thread, HasARunningMessageLoop) {
46TEST(Thread, HasExpectedStackSize) {
47 size_t stack_size = 0;
51#if defined(FML_OS_WIN)
54 GetCurrentThreadStackLimits(&low_limit, &high_limit);
55 stack_size = high_limit - low_limit;
56#elif defined(FML_OS_MACOSX)
57 stack_size = pthread_get_stacksize_np(pthread_self());
60 pthread_getattr_np(pthread_self(), &attr);
61 pthread_attr_getstacksize(&attr, &stack_size);
62 pthread_attr_destroy(&attr);
75#if FLUTTER_PTHREAD_SUPPORTED
76TEST(Thread, ThreadNameCreatedWithConfig) {
77 const std::string
name =
"Thread1";
81 thread.GetTaskRunner()->PostTask([&
done, &
name]() {
84 pthread_t current_thread = pthread_self();
85 pthread_getname_np(current_thread, thread_name, 16);
86 ASSERT_EQ(thread_name,
name);
92static int clamp_priority(
int priority,
int policy) {
94 sched_get_priority_max(
policy));
101 pthread_t tid = pthread_self();
102 struct sched_param param;
106 param.sched_priority = clamp_priority(10,
policy);
109 param.sched_priority = clamp_priority(1,
policy);
111 pthread_setschedparam(tid,
policy, ¶m);
114TEST(Thread, ThreadPriorityCreatedWithConfig) {
115 const std::string thread1_name =
"Thread1";
116 const std::string thread2_name =
"Thread2";
123 struct sched_param param;
125 thread.GetTaskRunner()->PostTask([&]() {
127 char thread_name[16];
128 pthread_t current_thread = pthread_self();
129 pthread_getname_np(current_thread, thread_name, 16);
130 pthread_getschedparam(current_thread, &
policy, ¶m);
131 ASSERT_EQ(thread_name, thread1_name);
132 ASSERT_EQ(
policy, SCHED_OTHER);
133 ASSERT_EQ(param.sched_priority, clamp_priority(1,
policy));
139 thread2.GetTaskRunner()->PostTask([&]() {
141 char thread_name[16];
142 pthread_t current_thread = pthread_self();
143 pthread_getname_np(current_thread, thread_name, 16);
144 pthread_getschedparam(current_thread, &
policy, ¶m);
145 ASSERT_EQ(thread_name, thread2_name);
146 ASSERT_EQ(
policy, SCHED_OTHER);
147 ASSERT_EQ(param.sched_priority, clamp_priority(10,
policy));
154#if defined(FML_OS_LINUX)
155TEST(Thread, LinuxLongThreadNameTruncated) {
156 const std::string
name =
"VeryLongThreadNameTest";
159 thread.GetTaskRunner()->PostTask([&
name]() {
160 constexpr size_t kThreadNameLen = 16;
161 char thread_name[kThreadNameLen];
162 pthread_getname_np(pthread_self(), thread_name, kThreadNameLen);
163 ASSERT_EQ(thread_name,
name.substr(0, kThreadNameLen - 1));
static void done(const char *config, const char *src, const char *srcOptions, const char *name)
static unsigned clamp(SkFixed fx, int max)
virtual void PostTask(const fml::closure &task) override
@ kNormal
Default priority level.
@ kDisplay
Suitable for threads which generate data for the display.
fml::RefPtr< fml::TaskRunner > GetTaskRunner() const
static size_t GetDefaultStackSize()
static void SetCurrentThreadName(const ThreadConfig &config)
DEF_SWITCHES_START aot vmservice shared library name
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network policy
The ThreadConfig is the thread info include thread name, thread priority.
TEST(Thread, CanStartAndEnd)
__w64 unsigned long ULONG_PTR