Flutter Engine
fml::Thread Class Reference

#include <thread.h>

Public Member Functions

 Thread (const std::string &name="")
 
 ~Thread ()
 
fml::RefPtr< fml::TaskRunnerGetTaskRunner () const
 
void Join ()
 

Static Public Member Functions

static void SetCurrentThreadName (const std::string &name)
 

Detailed Description

Definition at line 17 of file thread.h.

Constructor & Destructor Documentation

◆ Thread()

fml::Thread::Thread ( const std::string &  name = "")
explicit

Definition at line 26 of file thread.cc.

References fml::MessageLoop::EnsureInitializedForCurrentThread(), fml::MessageLoop::GetCurrent(), name, and SetCurrentThreadName().

26  : joined_(false) {
29  thread_ = std::make_unique<std::thread>([&latch, &runner, name]() -> void {
32  auto& loop = MessageLoop::GetCurrent();
33  runner = loop.GetTaskRunner();
34  latch.Signal();
35  loop.Run();
36  });
37  latch.Wait();
38  task_runner_ = runner;
39 }
static FML_EMBEDDER_ONLY MessageLoop & GetCurrent()
Definition: message_loop.cc:19
static void EnsureInitializedForCurrentThread()
Definition: message_loop.cc:27
const char * name
Definition: fuchsia.cc:50
static void SetCurrentThreadName(const std::string &name)
Definition: thread.cc:70

◆ ~Thread()

fml::Thread::~Thread ( )

Definition at line 41 of file thread.cc.

References Join().

41  {
42  Join();
43 }
void Join()
Definition: thread.cc:49

Member Function Documentation

◆ GetTaskRunner()

fml::RefPtr< fml::TaskRunner > fml::Thread::GetTaskRunner ( ) const

Definition at line 45 of file thread.cc.

Referenced by TEST(), fml::TEST(), and flutter::testing::TEST_F().

45  {
46  return task_runner_;
47 }

◆ Join()

void fml::Thread::Join ( )

Definition at line 49 of file thread.cc.

References fml::MessageLoop::GetCurrent(), fml::TaskRunner::PostTask(), and fml::MessageLoop::Terminate().

Referenced by TEST(), and ~Thread().

49  {
50  if (joined_) {
51  return;
52  }
53  joined_ = true;
54  task_runner_->PostTask([]() { MessageLoop::GetCurrent().Terminate(); });
55  thread_->join();
56 }
static FML_EMBEDDER_ONLY MessageLoop & GetCurrent()
Definition: message_loop.cc:19
virtual void PostTask(const fml::closure &task)
Definition: task_runner.cc:24

◆ SetCurrentThreadName()

void fml::Thread::SetCurrentThreadName ( const std::string &  name)
static

Definition at line 70 of file thread.cc.

References FML_DLOG.

Referenced by fml::ConcurrentMessageLoop::Create(), and Thread().

70  {
71  if (name == "") {
72  return;
73  }
74 #if defined(OS_MACOSX)
75  pthread_setname_np(name.c_str());
76 #elif defined(OS_LINUX) || defined(OS_ANDROID)
77  pthread_setname_np(pthread_self(), name.c_str());
78 #elif defined(OS_WIN)
79  THREADNAME_INFO info;
80  info.dwType = 0x1000;
81  info.szName = name.c_str();
82  info.dwThreadID = GetCurrentThreadId();
83  info.dwFlags = 0;
84  __try {
85  RaiseException(kVCThreadNameException, 0, sizeof(info) / sizeof(DWORD),
86  reinterpret_cast<DWORD_PTR*>(&info));
87  } __except (EXCEPTION_CONTINUE_EXECUTION) {
88  }
89 #elif defined(OS_FUCHSIA)
90  zx::thread::self()->set_property(ZX_PROP_NAME, name.c_str(), name.size());
91 #else
92  FML_DLOG(INFO) << "Could not set the thread name to '" << name
93  << "' on this platform.";
94 #endif
95 }
const char * name
Definition: fuchsia.cc:50
#define FML_DLOG(severity)
Definition: logging.h:85

The documentation for this class was generated from the following files: