Flutter Engine
The Flutter Engine
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Friends | List of all members
dart::OSThread Class Reference

#include <os_thread.h>

Inheritance diagram for dart::OSThread:
dart::BaseThread

Public Types

typedef void(* ThreadStartFunction) (uword parameter)
 
typedef void(* ThreadDestructor) (void *parameter)
 

Public Member Functions

 ~OSThread ()
 
ThreadId id () const
 
const char * name () const
 
void SetName (const char *name)
 
Mutextimeline_block_lock () const
 
TimelineEventBlock * TimelineBlockLocked () const
 
void SetTimelineBlockLocked (TimelineEventBlock *block)
 
Loglog () const
 
uword stack_base () const
 
uword stack_limit () const
 
uword overflow_stack_limit () const
 
bool HasStackHeadroom ()
 
bool HasStackHeadroom (intptr_t headroom)
 
void DisableThreadInterrupts ()
 
void EnableThreadInterrupts ()
 
bool ThreadInterruptsEnabled ()
 
- Public Member Functions inherited from dart::BaseThread
bool is_os_thread () const
 

Static Public Member Functions

static OSThreadCreateOSThread ()
 
static bool GetCurrentStackBounds (uword *lower, uword *upper)
 
static uword GetCurrentStackPointer ()
 
static OSThreadTryCurrent ()
 
static OSThreadCurrent ()
 
static void SetCurrent (OSThread *current)
 
static ThreadStateCurrentVMThread ()
 
static uword GetSpecifiedStackSize ()
 
static BaseThreadGetCurrentTLS ()
 
static void SetCurrentTLS (BaseThread *value)
 
static int Start (const char *name, ThreadStartFunction function, uword parameter)
 
static ThreadLocalKey CreateThreadLocal (ThreadDestructor destructor=nullptr)
 
static void DeleteThreadLocal (ThreadLocalKey key)
 
static uword GetThreadLocal (ThreadLocalKey key)
 
static ThreadId GetCurrentThreadId ()
 
static void SetThreadLocal (ThreadLocalKey key, uword value)
 
static intptr_t GetMaxStackSize ()
 
static void Join (ThreadJoinId id)
 
static intptr_t ThreadIdToIntPtr (ThreadId id)
 
static ThreadId ThreadIdFromIntPtr (intptr_t id)
 
static bool Compare (ThreadId a, ThreadId b)
 
static ThreadJoinId GetCurrentThreadJoinId (OSThread *thread)
 
static void Init ()
 
static bool IsThreadInList (ThreadId id)
 
static void DisableOSThreadCreation ()
 
static void EnableOSThreadCreation ()
 

Static Public Attributes

static const uword kInvalidStackLimit = ~static_cast<uword>(0)
 
static constexpr intptr_t kStackSizeBufferMax = (16 * KB * kWordSize)
 
static constexpr float kStackSizeBufferFraction = 0.5
 
static const ThreadId kInvalidThreadId
 
static const ThreadJoinId kInvalidThreadJoinId
 

Friends

class Thread
 
class OSThreadIterator
 
class ThreadInterrupterFuchsia
 
class ThreadInterrupterMacOS
 
class ThreadInterrupterWin
 
class ThreadPool
 

Detailed Description

Definition at line 86 of file os_thread.h.

Member Typedef Documentation

◆ ThreadDestructor

typedef void(* dart::OSThread::ThreadDestructor) (void *parameter)

Definition at line 210 of file os_thread.h.

◆ ThreadStartFunction

typedef void(* dart::OSThread::ThreadStartFunction) (uword parameter)

Definition at line 209 of file os_thread.h.

Constructor & Destructor Documentation

◆ ~OSThread()

dart::OSThread::~OSThread ( )

Definition at line 77 of file os_thread.cc.

77 {
78 if (!is_os_thread()) {
79 // If the embedder enters an isolate on this thread and does not exit the
80 // isolate, the thread local at thread_key_, which we are destructing here,
81 // will contain a dart::Thread instead of a dart::OSThread.
82 FATAL("Thread exited without calling Dart_ExitIsolate");
83 }
84 RemoveThreadFromList(this);
85 delete log_;
86 log_ = nullptr;
87#if defined(SUPPORT_TIMELINE)
88 RecorderSynchronizationLockScope ls;
89 TimelineEventRecorder* recorder = Timeline::recorder();
90 if (recorder != nullptr && !ls.IsShuttingDown()) {
91 // Acquire the recorder's lock so that |timeline_block_| cannot be given to
92 // another thread until the call to |TimelineEventRecorder::FinishBlock| is
93 // complete. This is guarded by a check ensuring that the recorder has not
94 // yet been deleted, and thus ensuring that the recorder's lock can be
95 // acquired. It is fine to skip the call to
96 // |TimelineEventRecorder::FinishBlock| if the recorder has already been
97 // deleted, because only the recorder cares about whether blocks have been
98 // marked as finished.
99 MutexLocker recorder_lock_locker(&Timeline::recorder()->lock_);
100 MutexLocker timeline_block_lock_locker(timeline_block_lock());
101 Timeline::recorder()->FinishBlock(timeline_block_);
102 }
103#endif
104 timeline_block_ = nullptr;
105 free(name_);
106#if !defined(PRODUCT)
107 if (prepared_for_interrupts_) {
108 ThreadInterrupter::CleanupCurrentThreadState(thread_interrupter_state_);
109 thread_interrupter_state_ = nullptr;
110 prepared_for_interrupts_ = false;
111 }
112#endif // !defined(PRODUCT)
113}
bool is_os_thread() const
Definition: os_thread.h:71
Mutex * timeline_block_lock() const
Definition: os_thread.h:112
static void CleanupCurrentThreadState(void *state)
#define FATAL(error)

Member Function Documentation

◆ Compare()

static bool dart::OSThread::Compare ( ThreadId  a,
ThreadId  b 
)
static

◆ CreateOSThread()

OSThread * dart::OSThread::CreateOSThread ( )
static

Definition at line 66 of file os_thread.cc.

66 {
67 ASSERT(thread_list_lock_ != nullptr);
68 MutexLocker ml(thread_list_lock_);
69 if (!creation_enabled_) {
70 return nullptr;
71 }
72 OSThread* os_thread = new OSThread();
73 AddThreadToListLocked(os_thread);
74 return os_thread;
75}
#define ASSERT(E)

◆ CreateThreadLocal()

static ThreadLocalKey dart::OSThread::CreateThreadLocal ( ThreadDestructor  destructor = nullptr)
static

◆ Current()

static OSThread * dart::OSThread::Current ( )
inlinestatic

Definition at line 179 of file os_thread.h.

179 {
180 OSThread* os_thread = TryCurrent();
181 if (os_thread == nullptr) {
182 os_thread = CreateAndSetUnknownThread();
183 }
184 return os_thread;
185 }
static OSThread * TryCurrent()
Definition: os_thread.h:163

◆ CurrentVMThread()

static ThreadState * dart::OSThread::CurrentVMThread ( )
inlinestatic

Definition at line 188 of file os_thread.h.

188{ return current_vm_thread_; }

◆ DeleteThreadLocal()

static void dart::OSThread::DeleteThreadLocal ( ThreadLocalKey  key)
static

◆ DisableOSThreadCreation()

void dart::OSThread::DisableOSThreadCreation ( )
static

Definition at line 247 of file os_thread.cc.

247 {
248 MutexLocker ml(thread_list_lock_);
249 creation_enabled_ = false;
250}

◆ DisableThreadInterrupts()

void dart::OSThread::DisableThreadInterrupts ( )

Definition at line 143 of file os_thread.cc.

143 {
144 ASSERT(OSThread::Current() == this);
145 thread_interrupt_disabled_.fetch_add(1u);
146}
static OSThread * Current()
Definition: os_thread.h:179
T fetch_add(T arg, std::memory_order order=std::memory_order_relaxed)
Definition: atomic.h:35

◆ EnableOSThreadCreation()

void dart::OSThread::EnableOSThreadCreation ( )
static

Definition at line 252 of file os_thread.cc.

252 {
253 MutexLocker ml(thread_list_lock_);
254 creation_enabled_ = true;
255}

◆ EnableThreadInterrupts()

void dart::OSThread::EnableThreadInterrupts ( )

Definition at line 148 of file os_thread.cc.

148 {
149 ASSERT(OSThread::Current() == this);
150 uintptr_t old = thread_interrupt_disabled_.fetch_sub(1u);
151 if (FLAG_profiler && (old == 1)) {
152 // We just decremented from 1 to 0.
153 // Make sure the thread interrupter is awake.
154 if (!prepared_for_interrupts_) {
155 thread_interrupter_state_ = ThreadInterrupter::PrepareCurrentThread();
156 prepared_for_interrupts_ = true;
157 }
159 }
160 if (old == 0) {
161 // We just decremented from 0, this means we've got a mismatched pair
162 // of calls to EnableThreadInterrupts and DisableThreadInterrupts.
163 FATAL("Invalid call to OSThread::EnableThreadInterrupts()");
164 }
165}
T fetch_sub(T arg, std::memory_order order=std::memory_order_relaxed)
Definition: atomic.h:38
static void * PrepareCurrentThread()

◆ GetCurrentStackBounds()

static bool dart::OSThread::GetCurrentStackBounds ( uword lower,
uword upper 
)
static

◆ GetCurrentStackPointer()

DART_NOINLINE uword dart::OSThread::GetCurrentStackPointer ( )
static

Definition at line 132 of file os_thread.cc.

132 {
133#ifdef _MSC_VER
134 return reinterpret_cast<uword>(_AddressOfReturnAddress());
135#elif __GNUC__
136 return reinterpret_cast<uword>(__builtin_frame_address(0));
137#else
138#error Unimplemented
139#endif
140}
uintptr_t uword
Definition: globals.h:501

◆ GetCurrentThreadId()

static ThreadId dart::OSThread::GetCurrentThreadId ( )
static

◆ GetCurrentThreadJoinId()

static ThreadJoinId dart::OSThread::GetCurrentThreadJoinId ( OSThread thread)
static

◆ GetCurrentTLS()

static BaseThread * dart::OSThread::GetCurrentTLS ( )
inlinestatic

Definition at line 204 of file os_thread.h.

204 {
205 return reinterpret_cast<BaseThread*>(OSThread::GetThreadLocal(thread_key_));
206 }
static uword GetThreadLocal(ThreadLocalKey key)
Definition: os_thread.h:222

◆ GetMaxStackSize()

static intptr_t dart::OSThread::GetMaxStackSize ( )
static

◆ GetSpecifiedStackSize()

static uword dart::OSThread::GetSpecifiedStackSize ( )
inlinestatic

Definition at line 197 of file os_thread.h.

197 {
198 intptr_t headroom =
199 OSThread::CalculateHeadroom(OSThread::GetMaxStackSize());
200 ASSERT(headroom < OSThread::GetMaxStackSize());
201 uword stack_size = OSThread::GetMaxStackSize() - headroom;
202 return stack_size;
203 }
static intptr_t GetMaxStackSize()

◆ GetThreadLocal()

static uword dart::OSThread::GetThreadLocal ( ThreadLocalKey  key)
inlinestatic

Definition at line 222 of file os_thread.h.

222 {
223 return ThreadInlineImpl::GetThreadLocal(key);
224 }

◆ HasStackHeadroom() [1/2]

bool dart::OSThread::HasStackHeadroom ( )
inline

Definition at line 132 of file os_thread.h.

132{ return HasStackHeadroom(stack_headroom_); }
bool HasStackHeadroom()
Definition: os_thread.h:132

◆ HasStackHeadroom() [2/2]

bool dart::OSThread::HasStackHeadroom ( intptr_t  headroom)
inline

Definition at line 133 of file os_thread.h.

133 {
134 return GetCurrentStackPointer() > (stack_limit_ + headroom);
135 }
static uword GetCurrentStackPointer()
Definition: os_thread.cc:132

◆ id()

ThreadId dart::OSThread::id ( ) const
inline

Definition at line 96 of file os_thread.h.

96 {
98 return id_;
99 }
static const ThreadId kInvalidThreadId
Definition: os_thread.h:248

◆ Init()

void dart::OSThread::Init ( )
static

Definition at line 177 of file os_thread.cc.

177 {
178 // Allocate the global OSThread lock.
179 if (thread_list_lock_ == nullptr) {
180 thread_list_lock_ = new Mutex();
181 }
182 ASSERT(thread_list_lock_ != nullptr);
183
184 // Create the thread local key.
185 if (thread_key_ == kUnsetThreadLocalKey) {
186 thread_key_ = CreateThreadLocal(DeleteThread);
187 }
188 ASSERT(thread_key_ != kUnsetThreadLocalKey);
189
190 // Enable creation of OSThread structures in the VM.
192
193 // Create a new OSThread structure and set it as the TLS.
194 OSThread* os_thread = CreateOSThread();
195 ASSERT(os_thread != nullptr);
196 OSThread::SetCurrent(os_thread);
197 os_thread->SetName("Dart_Initialize");
198}
static OSThread * CreateOSThread()
Definition: os_thread.cc:66
static void SetCurrent(OSThread *current)
Definition: os_thread.h:186
static ThreadLocalKey CreateThreadLocal(ThreadDestructor destructor=nullptr)
static void EnableOSThreadCreation()
Definition: os_thread.cc:252
static void DeleteThread(void *thread)
Definition: os_thread.cc:172
static const ThreadLocalKey kUnsetThreadLocalKey

◆ IsThreadInList()

bool dart::OSThread::IsThreadInList ( ThreadId  id)
static

Definition at line 230 of file os_thread.cc.

230 {
231 if (id == OSThread::kInvalidThreadId) {
232 return false;
233 }
235 while (it.HasNext()) {
236 ASSERT(OSThread::thread_list_lock_->IsOwnedByCurrentThread());
237 OSThread* t = it.Next();
238 // An address test is not sufficient because the allocator may recycle
239 // the address for another Thread. Test against the thread's id.
240 if (t->id() == id) {
241 return true;
242 }
243 }
244 return false;
245}
friend class OSThreadIterator
Definition: os_thread.h:333

◆ Join()

static void dart::OSThread::Join ( ThreadJoinId  id)
static

◆ log()

Log * dart::OSThread::log ( ) const
inline

Definition at line 126 of file os_thread.h.

126{ return log_; }

◆ name()

const char * dart::OSThread::name ( ) const
inline

Definition at line 108 of file os_thread.h.

108{ return name_; }

◆ overflow_stack_limit()

uword dart::OSThread::overflow_stack_limit ( ) const
inline

Definition at line 130 of file os_thread.h.

130{ return stack_limit_ + stack_headroom_; }

◆ SetCurrent()

static void dart::OSThread::SetCurrent ( OSThread current)
inlinestatic

Definition at line 186 of file os_thread.h.

186{ SetCurrentTLS(current); }
static void SetCurrentTLS(BaseThread *value)
Definition: os_thread.cc:318

◆ SetCurrentTLS()

void dart::OSThread::SetCurrentTLS ( BaseThread value)
static

Definition at line 318 of file os_thread.cc.

318 {
319 // Provides thread-local destructors.
320 SetThreadLocal(thread_key_, reinterpret_cast<uword>(value));
321
322 // Allows the C compiler more freedom to optimize.
323 if ((value != nullptr) && !value->is_os_thread()) {
324 current_vm_thread_ = static_cast<Thread*>(value);
325 } else {
326 current_vm_thread_ = nullptr;
327 }
328}
friend class Thread
Definition: os_thread.h:332
static void SetThreadLocal(ThreadLocalKey key, uword value)
uint8_t value

◆ SetName()

void dart::OSThread::SetName ( const char *  name)

Definition at line 115 of file os_thread.cc.

115 {
116 MutexLocker ml(thread_list_lock_);
117 // Clear the old thread name.
118 if (name_ != nullptr) {
119 free(name_);
120 name_ = nullptr;
121 }
122 ASSERT(OSThread::Current() == this);
123 ASSERT(name != nullptr);
124 name_ = Utils::StrDup(name);
125
126#if defined(SUPPORT_TIMELINE)
127 UpdateTimelineTrackMetadata(*this);
128#endif // defined(SUPPORT_TIMELINE)
129}
const char * name() const
Definition: os_thread.h:108
static char * StrDup(const char *s)

◆ SetThreadLocal()

static void dart::OSThread::SetThreadLocal ( ThreadLocalKey  key,
uword  value 
)
static

◆ SetTimelineBlockLocked()

void dart::OSThread::SetTimelineBlockLocked ( TimelineEventBlock *  block)
inline

Definition at line 121 of file os_thread.h.

121 {
122 ASSERT(timeline_block_lock()->IsOwnedByCurrentThread());
123 timeline_block_ = block;
124 }

◆ stack_base()

uword dart::OSThread::stack_base ( ) const
inline

Definition at line 128 of file os_thread.h.

128{ return stack_base_; }

◆ stack_limit()

uword dart::OSThread::stack_limit ( ) const
inline

Definition at line 129 of file os_thread.h.

129{ return stack_limit_; }

◆ Start()

static int dart::OSThread::Start ( const char *  name,
ThreadStartFunction  function,
uword  parameter 
)
static

◆ ThreadIdFromIntPtr()

static ThreadId dart::OSThread::ThreadIdFromIntPtr ( intptr_t  id)
static

◆ ThreadIdToIntPtr()

static intptr_t dart::OSThread::ThreadIdToIntPtr ( ThreadId  id)
static

◆ ThreadInterruptsEnabled()

bool dart::OSThread::ThreadInterruptsEnabled ( )

Definition at line 167 of file os_thread.cc.

167 {
168 return thread_interrupt_disabled_ == 0;
169}

◆ timeline_block_lock()

Mutex * dart::OSThread::timeline_block_lock ( ) const
inline

Definition at line 112 of file os_thread.h.

112{ return &timeline_block_lock_; }

◆ TimelineBlockLocked()

TimelineEventBlock * dart::OSThread::TimelineBlockLocked ( ) const
inline

Definition at line 115 of file os_thread.h.

115 {
116 ASSERT(timeline_block_lock()->IsOwnedByCurrentThread());
117 return timeline_block_;
118 }

◆ TryCurrent()

static OSThread * dart::OSThread::TryCurrent ( )
inlinestatic

Definition at line 163 of file os_thread.h.

163 {
164 BaseThread* thread = GetCurrentTLS();
165 OSThread* os_thread = nullptr;
166 if (thread != nullptr) {
167 if (thread->is_os_thread()) {
168 os_thread = reinterpret_cast<OSThread*>(thread);
169 } else {
170 ThreadState* vm_thread = reinterpret_cast<ThreadState*>(thread);
171 os_thread = GetOSThreadFromThread(vm_thread);
172 }
173 }
174 return os_thread;
175 }
friend class ThreadState
Definition: os_thread.h:79
static BaseThread * GetCurrentTLS()
Definition: os_thread.h:204

Friends And Related Function Documentation

◆ OSThreadIterator

friend class OSThreadIterator
friend

Definition at line 333 of file os_thread.h.

◆ Thread

friend class Thread
friend

Definition at line 332 of file os_thread.h.

◆ ThreadInterrupterFuchsia

friend class ThreadInterrupterFuchsia
friend

Definition at line 334 of file os_thread.h.

◆ ThreadInterrupterMacOS

friend class ThreadInterrupterMacOS
friend

Definition at line 335 of file os_thread.h.

◆ ThreadInterrupterWin

friend class ThreadInterrupterWin
friend

Definition at line 336 of file os_thread.h.

◆ ThreadPool

friend class ThreadPool
friend

Definition at line 337 of file os_thread.h.

Member Data Documentation

◆ kInvalidStackLimit

const uword dart::OSThread::kInvalidStackLimit = ~static_cast<uword>(0)
static

Definition at line 88 of file os_thread.h.

◆ kInvalidThreadId

const ThreadId dart::OSThread::kInvalidThreadId
static

Definition at line 248 of file os_thread.h.

◆ kInvalidThreadJoinId

const ThreadJoinId dart::OSThread::kInvalidThreadJoinId
static

Definition at line 249 of file os_thread.h.

◆ kStackSizeBufferFraction

constexpr float dart::OSThread::kStackSizeBufferFraction = 0.5
staticconstexpr

Definition at line 246 of file os_thread.h.

◆ kStackSizeBufferMax

constexpr intptr_t dart::OSThread::kStackSizeBufferMax = (16 * KB * kWordSize)
staticconstexpr

Definition at line 245 of file os_thread.h.


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