5#define FML_USED_ON_EMBEDDER
7#include "flutter/fml/thread.h"
13#include "flutter/fml/build_config.h"
14#include "flutter/fml/message_loop.h"
15#include "flutter/fml/synchronization/waitable_event.h"
17#if defined(FML_OS_WIN)
19#elif defined(OS_FUCHSIA)
20#include <lib/zx/thread.h>
37#if defined(FML_OS_WIN)
44#if defined(FML_OS_WIN)
46 thread_ = (
HANDLE*)_beginthreadex(
48 [](
void* arg) ->
unsigned {
49 std::unique_ptr<ThreadFunction>
function(
59 WaitForSingleObjectEx(thread_, INFINITE,
FALSE);
68 pthread_attr_init(&attr);
73 [](
void* arg) ->
void* {
74 std::unique_ptr<ThreadFunction>
function(
81 result = pthread_attr_destroy(&attr);
86 pthread_join(thread_,
nullptr);
92#if defined(FML_OS_WIN)
95const DWORD kVCThreadNameException = 0x406D1388;
96typedef struct tagTHREADNAME_INFO {
108#if defined(FML_OS_MACOSX)
109 pthread_setname_np(
name.c_str());
110#elif defined(FML_OS_LINUX) || defined(FML_OS_ANDROID)
113 constexpr std::string::size_type kLinuxMaxThreadNameLen = 15;
114 pthread_setname_np(pthread_self(),
115 name.substr(0, kLinuxMaxThreadNameLen).c_str());
116#elif defined(FML_OS_WIN)
117 THREADNAME_INFO
info;
118 info.dwType = 0x1000;
120 info.dwThreadID = GetCurrentThreadId();
123 RaiseException(kVCThreadNameException, 0,
sizeof(
info) /
sizeof(
DWORD),
124 reinterpret_cast<DWORD_PTR*
>(&
info));
125 } __except (EXCEPTION_CONTINUE_EXECUTION) {
127#elif defined(OS_FUCHSIA)
130 FML_DLOG(INFO) <<
"Could not set the thread name to '" <<
name
131 <<
"' on this platform.";
147 thread_ = std::make_unique<ThreadHandle>(
148 [&latch, &runner, setter, config]() ->
void {
152 runner = loop.GetTaskRunner();
157 task_runner_ = runner;
178 return 1024 * 1024 * 2;
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
static void EnsureInitializedForCurrentThread()
static FML_EMBEDDER_ONLY MessageLoop & GetCurrent()
virtual void PostTask(const fml::closure &task) override
ThreadHandle(ThreadFunction &&function)
fml::RefPtr< fml::TaskRunner > GetTaskRunner() const
static size_t GetDefaultStackSize()
std::function< void(const ThreadConfig &)> ThreadConfigSetter
Thread(const std::string &name="")
static void SetCurrentThreadName(const ThreadConfig &config)
#define FML_DLOG(severity)
#define FML_CHECK(condition)
Dart_NativeFunction function
DEF_SWITCHES_START aot vmservice shared library name
std::function< void()> ThreadFunction
void SetThreadName(const std::string &name)
The ThreadConfig is the thread info include thread name, thread priority.