Thread.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. #include "il2cpp-config.h"
  2. #include "os/Mutex.h"
  3. #include "os/Thread.h"
  4. #include "os/ThreadLocalValue.h"
  5. #include "os/Time.h"
  6. #include "os/Semaphore.h"
  7. #include "vm/Domain.h"
  8. #include "vm/Exception.h"
  9. #include "vm/Object.h"
  10. #include "vm/Profiler.h"
  11. #include "vm/Runtime.h"
  12. #include "vm/StackTrace.h"
  13. #include "vm/Thread.h"
  14. #include "vm/String.h"
  15. #include "gc/Allocator.h"
  16. #include "gc/GarbageCollector.h"
  17. #include "gc/GCHandle.h"
  18. #include "gc/WriteBarrier.h"
  19. #include "utils/Memory.h"
  20. #include "utils/StringUtils.h"
  21. #include "vm-utils/Debugger.h"
  22. #include "il2cpp-class-internals.h"
  23. #include "il2cpp-object-internals.h"
  24. #include <algorithm>
  25. #include "Baselib.h"
  26. #include "Cpp/Atomic.h"
  27. #include "Cpp/ReentrantLock.h"
  28. #include "hybridclr/interpreter/InterpreterModule.h"
  29. namespace il2cpp
  30. {
  31. namespace vm
  32. {
  33. Il2CppThread* Thread::s_MainThread = NULL;
  34. typedef std::vector<Il2CppThread*, il2cpp::gc::Allocator<Il2CppThread*> > GCTrackedThreadVector;
  35. // we need to allocate this ourselves so the CRT does not initialize it and try to allocate GC memory on startup before the GC is initialized
  36. static GCTrackedThreadVector* s_AttachedThreads;
  37. static bool s_BlockNewThreads = false;
  38. #define AUTO_LOCK_THREADS() il2cpp::os::FastAutoLock lock(&s_ThreadMutex)
  39. static baselib::ReentrantLock s_ThreadMutex;
  40. static std::vector<int32_t> s_ThreadStaticSizes;
  41. static il2cpp::os::ThreadLocalValue s_CurrentThread;
  42. static il2cpp::os::ThreadLocalValue s_StaticData; // Cache the static thread data in a local TLS slot for faster lookup
  43. static baselib::atomic<int32_t> s_NextManagedThreadId = {0};
  44. /*
  45. Thread static data is stored in a two level lookup so we can grow the size at runtime
  46. without requiring a lock on looking up static data
  47. We pre-allocate a fixed number of slot pointers - kMaxThreadStaticSlots at startup.
  48. Each of these slots can hold kMaxThreadStaticDataPointers data pointer. These slots
  49. are allocated as needed.
  50. */
  51. const int32_t kMaxThreadStaticSlots = 1024;
  52. const int32_t kMaxThreadStaticDataPointers = 1024;
  53. struct ThreadStaticOffset
  54. {
  55. uint32_t slot;
  56. uint32_t index;
  57. };
  58. static ThreadStaticOffset IndexToStaticFieldOffset(int32_t index)
  59. {
  60. static_assert(kMaxThreadStaticSlots <= 0xFFFF, "Only 65535 base thread static slots are supported");
  61. static_assert(kMaxThreadStaticDataPointers <= 0xFFFF, "Only 65535 thread static slots are supported");
  62. uint32_t value = (uint32_t)index;
  63. ThreadStaticOffset offset;
  64. offset.slot = value >> 16;
  65. offset.index = value & 0xFFFF;
  66. return offset;
  67. }
  68. struct ThreadStaticDataSlot
  69. {
  70. void* data[kMaxThreadStaticSlots];
  71. };
  72. struct ThreadStaticData
  73. {
  74. ThreadStaticDataSlot* slots[kMaxThreadStaticSlots];
  75. };
  76. static void
  77. set_wbarrier_for_attached_threads()
  78. {
  79. gc::GarbageCollector::SetWriteBarrier((void**)s_AttachedThreads->data(), sizeof(Il2CppThread*) * s_AttachedThreads->size());
  80. }
  81. static void
  82. thread_cleanup_on_cancel(void* arg)
  83. {
  84. Thread::Detach((Il2CppThread*)arg, true);
  85. #if IL2CPP_HAS_NATIVE_THREAD_CLEANUP
  86. il2cpp::os::Thread* osThread = ((Il2CppThread*)arg)->GetInternalThread()->handle;
  87. osThread->SignalExited();
  88. #endif
  89. }
  90. void Thread::Initialize()
  91. {
  92. #if IL2CPP_HAS_NATIVE_THREAD_CLEANUP
  93. os::Thread::SetNativeThreadCleanup(&thread_cleanup_on_cancel);
  94. #endif
  95. #if IL2CPP_ENABLE_RELOAD
  96. s_BlockNewThreads = false;
  97. #endif
  98. s_AttachedThreads = new GCTrackedThreadVector();
  99. }
  100. void Thread::Uninitialize()
  101. {
  102. IL2CPP_ASSERT(Current() == Main());
  103. #if IL2CPP_HAS_NATIVE_THREAD_CLEANUP
  104. os::Thread::SetNativeThreadCleanup(NULL);
  105. #endif
  106. delete s_AttachedThreads;
  107. s_AttachedThreads = NULL;
  108. s_MainThread = NULL;
  109. }
  110. Il2CppThread* Thread::Attach(Il2CppDomain *domain)
  111. {
  112. Il2CppThread* managedThread = Current();
  113. if (managedThread != NULL)
  114. return managedThread;
  115. gc::GarbageCollector::RegisterThread();
  116. StackTrace::InitializeStackTracesForCurrentThread();
  117. // Get/create OS thread representing the current thread. For pre-existing threads such as
  118. // the main thread, this will create an OS thread instance on demand. For threads that have
  119. // been started through our OS layer, there will already be an instance.
  120. os::Thread* osThread = os::Thread::GetOrCreateCurrentThread();
  121. // Create managed object representing the current thread.
  122. managedThread = (Il2CppThread*)Object::New(il2cpp_defaults.thread_class);
  123. SetupInternalManagedThread(managedThread, osThread);
  124. managedThread->GetInternalThread()->state = kThreadStateRunning;
  125. InitializeManagedThread(managedThread, domain);
  126. return managedThread;
  127. }
  128. void Thread::SetupInternalManagedThread(Il2CppThread* thread, os::Thread* osThread)
  129. {
  130. Il2CppInternalThread* internalManagedThread = (Il2CppInternalThread*)Object::New(il2cpp_defaults.internal_thread_class);
  131. internalManagedThread->handle = osThread;
  132. internalManagedThread->tid = osThread->Id();
  133. internalManagedThread->managed_id = GetNewManagedId();
  134. // The synch_cs object is deallocated in the InternalThread::Thread_free_internal icall, which
  135. // is called from the managed thread finalizer.
  136. internalManagedThread->longlived = (Il2CppLongLivedThreadData*)IL2CPP_MALLOC(sizeof(Il2CppLongLivedThreadData), IL2CPP_MEM_THREAD);
  137. internalManagedThread->longlived->synch_cs = new baselib::ReentrantLock;
  138. internalManagedThread->apartment_state = il2cpp::os::kApartmentStateUnknown;
  139. gc::WriteBarrier::GenericStore(&thread->internal_thread, internalManagedThread);
  140. }
  141. void Thread::InitializeManagedThread(Il2CppThread* thread, Il2CppDomain* domain)
  142. {
  143. #if IL2CPP_SUPPORT_THREADS
  144. IL2CPP_ASSERT(thread->GetInternalThread()->handle != NULL);
  145. IL2CPP_ASSERT(thread->GetInternalThread()->longlived->synch_cs != NULL);
  146. #endif
  147. #if IL2CPP_MONO_DEBUGGER
  148. utils::Debugger::AllocateThreadLocalData();
  149. #endif
  150. s_CurrentThread.SetValue(thread);
  151. Domain::ContextSet(domain->default_context);
  152. Register(thread);
  153. AllocateStaticDataForCurrentThread();
  154. #if IL2CPP_MONO_DEBUGGER
  155. utils::Debugger::ThreadStarted((uintptr_t)thread->GetInternalThread()->tid);
  156. #endif
  157. #if IL2CPP_ENABLE_PROFILER
  158. vm::Profiler::ThreadStart(((unsigned long)thread->GetInternalThread()->tid));
  159. #endif
  160. // Sync thread name.
  161. if (thread->GetInternalThread()->name.chars)
  162. {
  163. std::string utf8Name = il2cpp::utils::StringUtils::Utf16ToUtf8(thread->GetInternalThread()->name.chars);
  164. thread->GetInternalThread()->handle->SetName(utf8Name.c_str());
  165. }
  166. // Sync thread apartment state.
  167. thread->GetInternalThread()->apartment_state = thread->GetInternalThread()->handle->GetApartment();
  168. #if IL2CPP_HAS_NATIVE_THREAD_CLEANUP
  169. // register us for platform specific cleanup attempt in case thread is not exited cleanly
  170. os::Thread::RegisterCurrentThreadForCleanup(thread);
  171. #endif
  172. // If an interrupt has been requested before the thread was started, re-request
  173. // the interrupt now.
  174. if (thread->GetInternalThread()->interruption_requested)
  175. RequestInterrupt(thread);
  176. }
  177. void Thread::UninitializeManagedThread(Il2CppThread* thread)
  178. {
  179. Thread::UninitializeManagedThread(thread, false);
  180. }
  181. void Thread::UninitializeManagedThread(Il2CppThread *thread, bool inNativeThreadCleanup)
  182. {
  183. // This method is only valid to call from the current thread
  184. // But we can't safely check the Current() in native thread shutdown
  185. // because we can't rely on TLS values being valid
  186. IL2CPP_ASSERT(inNativeThreadCleanup || thread == Current());
  187. #if IL2CPP_HAS_NATIVE_THREAD_CLEANUP
  188. // unregister from special cleanup since we are doing it now
  189. os::Thread::UnregisterCurrentThreadForCleanup();
  190. #endif
  191. if (!gc::GarbageCollector::UnregisterThread())
  192. IL2CPP_ASSERT(0 && "gc::GarbageCollector::UnregisterThread failed");
  193. #if IL2CPP_ENABLE_PROFILER
  194. vm::Profiler::ThreadEnd(((unsigned long)thread->GetInternalThread()->tid));
  195. #endif
  196. #if IL2CPP_MONO_DEBUGGER
  197. // Only raise the event for the debugger if there is a current thread at the OS thread level.
  198. // The debugger code will try to take a lock, which requires a current thread. If this
  199. // thread is being detached by a call from thread_cleanup_on_cancel, then there might
  200. // not be a current thread, as pthreads does not privide TLS entries in thread destructors.
  201. if (os::Thread::HasCurrentThread())
  202. utils::Debugger::ThreadStopped((uintptr_t)thread->GetInternalThread()->tid);
  203. #endif
  204. FreeCurrentThreadStaticData(thread, inNativeThreadCleanup);
  205. // Call Unregister after all access to managed objects (Il2CppThread and Il2CppInternalThread)
  206. // is complete. Unregister will remove the managed thread object from the GC tracked vector of
  207. // attached threads, and allow it to be finalized and re-used. If runtime code accesses it
  208. // after a call to Unregister, there will be a race condition between the GC and the runtime
  209. // code for access to that object.
  210. Unregister(thread);
  211. #if IL2CPP_MONO_DEBUGGER
  212. utils::Debugger::FreeThreadLocalData();
  213. #endif
  214. hybridclr::interpreter::InterpreterModule::FreeThreadLocalMachineState();
  215. os::Thread::DetachCurrentThread();
  216. s_CurrentThread.SetValue(NULL);
  217. }
  218. Il2CppThread* Thread::Current()
  219. {
  220. void* value = NULL;
  221. s_CurrentThread.GetValue(&value);
  222. return (Il2CppThread*)value;
  223. }
  224. Il2CppThread** Thread::GetAllAttachedThreads(size_t &size)
  225. {
  226. size = s_AttachedThreads->size();
  227. return &(*s_AttachedThreads)[0];
  228. }
  229. static void STDCALL TerminateThread(void* context)
  230. {
  231. // We throw a dummy exception to make sure things clean up properly
  232. // and we don't leave any locks behind (such as global locks in the allocator which
  233. // would then deadlock other threads). This could work off ThreadAbortException
  234. // but we don't want to deal with a managed exception here. So we use a C++ exception.
  235. throw Thread::NativeThreadAbortException();
  236. }
  237. static bool IsDebuggerThread(os::Thread* thread)
  238. {
  239. #if IL2CPP_MONO_DEBUGGER
  240. return utils::Debugger::IsDebuggerThread(thread);
  241. #else
  242. return false;
  243. #endif
  244. }
  245. // This function requests that all threads exit
  246. // If a thread is in a non-alertable wait it may not have exited when this method exits
  247. void Thread::AbortAllThreads()
  248. {
  249. #if IL2CPP_SUPPORT_THREADS
  250. Il2CppThread* gcFinalizerThread = NULL;
  251. Il2CppThread* currentThread = Current();
  252. IL2CPP_ASSERT(currentThread != NULL && "No current thread!");
  253. s_ThreadMutex.Acquire();
  254. s_BlockNewThreads = true;
  255. GCTrackedThreadVector attachedThreadsCopy = *s_AttachedThreads;
  256. // In theory, we don't need a write barrier here for Boehm, because we keep a
  257. // reference to the object on the stack during it's lifetime. But for validation
  258. // tests, we turn off GC, and thus we need it to pass.
  259. gc::GarbageCollector::SetWriteBarrier((void**)attachedThreadsCopy.data(), sizeof(Il2CppThread*) * attachedThreadsCopy.size());
  260. s_ThreadMutex.Release();
  261. std::vector<os::Thread*> activeThreads;
  262. // Kill all threads but the finalizer and current one. We temporarily flush out
  263. // the entire list and then just put the two threads back.
  264. while (attachedThreadsCopy.size())
  265. {
  266. Il2CppThread* thread = attachedThreadsCopy.back();
  267. os::Thread* osThread = thread->GetInternalThread()->handle;
  268. if (gc::GarbageCollector::IsFinalizerThread(thread))
  269. {
  270. IL2CPP_ASSERT(gcFinalizerThread == NULL && "There seems to be more than one finalizer thread!");
  271. gcFinalizerThread = thread;
  272. }
  273. else if (thread != currentThread && !IsDebuggerThread(osThread))
  274. {
  275. ////TODO: use Thread.Abort() instead
  276. osThread->QueueUserAPC(TerminateThread, NULL);
  277. activeThreads.push_back(osThread);
  278. }
  279. attachedThreadsCopy.pop_back();
  280. }
  281. // In theory, we don't need a write barrier here for Boehm, because we keep a
  282. // reference to the object on the stack during it's lifetime. But for validation
  283. // tests, we turn off GC, and thus we need it to pass.
  284. gc::GarbageCollector::SetWriteBarrier((void**)attachedThreadsCopy.data(), sizeof(Il2CppThread*) * attachedThreadsCopy.size());
  285. ////FIXME: While we don't have stable thread abortion in place yet, work around problems in
  286. //// the current implementation by repeatedly requesting threads to terminate. This works around
  287. //// race condition to some extent.
  288. while (activeThreads.size())
  289. {
  290. os::Thread* osThread = activeThreads.back();
  291. // Wait for the thread.
  292. if (osThread->Join(10) == kWaitStatusSuccess)
  293. activeThreads.pop_back();
  294. else
  295. {
  296. ////TODO: use Thread.Abort() instead
  297. osThread->QueueUserAPC(TerminateThread, NULL);
  298. }
  299. }
  300. AUTO_LOCK_THREADS();
  301. s_AttachedThreads->clear();
  302. // Put finalizer and current thread back in list.
  303. IL2CPP_ASSERT(gcFinalizerThread != NULL && "GC finalizer thread was not found in list of attached threads!");
  304. if (gcFinalizerThread)
  305. s_AttachedThreads->push_back(gcFinalizerThread);
  306. if (currentThread)
  307. s_AttachedThreads->push_back(currentThread);
  308. set_wbarrier_for_attached_threads();
  309. #endif
  310. }
  311. void Thread::Detach(Il2CppThread* thread)
  312. {
  313. Thread::Detach(thread, false);
  314. }
  315. void Thread::Detach(Il2CppThread *thread, bool inNativeThreadCleanup)
  316. {
  317. IL2CPP_ASSERT(thread != NULL && "Cannot detach a NULL thread");
  318. UninitializeManagedThread(thread, inNativeThreadCleanup);
  319. il2cpp::vm::StackTrace::CleanupStackTracesForCurrentThread();
  320. }
  321. Il2CppThread* Thread::Main()
  322. {
  323. return s_MainThread;
  324. }
  325. void Thread::SetMain(Il2CppThread* thread)
  326. {
  327. IL2CPP_ASSERT(s_MainThread == NULL);
  328. s_MainThread = thread;
  329. }
  330. void Thread::SetState(Il2CppThread *thread, ThreadState value)
  331. {
  332. il2cpp::os::FastAutoLock lock(thread->GetInternalThread()->longlived->synch_cs);
  333. thread->GetInternalThread()->state |= value;
  334. }
  335. void Thread::ClrState(Il2CppInternalThread* thread, ThreadState clr)
  336. {
  337. il2cpp::os::FastAutoLock lock(thread->longlived->synch_cs);
  338. thread->state &= ~clr;
  339. }
  340. void Thread::SetState(Il2CppInternalThread *thread, ThreadState value)
  341. {
  342. il2cpp::os::FastAutoLock lock(thread->longlived->synch_cs);
  343. thread->state |= value;
  344. }
  345. ThreadState Thread::GetState(Il2CppInternalThread *thread)
  346. {
  347. il2cpp::os::FastAutoLock lock(thread->longlived->synch_cs);
  348. return (ThreadState)thread->state;
  349. }
  350. bool Thread::TestState(Il2CppInternalThread* thread, ThreadState value)
  351. {
  352. il2cpp::os::FastAutoLock lock(thread->longlived->synch_cs);
  353. return (thread->state & value) != 0;
  354. }
  355. Il2CppInternalThread* Thread::CurrentInternal()
  356. {
  357. return Current()->GetInternalThread();
  358. }
  359. ThreadState Thread::GetState(Il2CppThread *thread)
  360. {
  361. il2cpp::os::FastAutoLock lock(thread->GetInternalThread()->longlived->synch_cs);
  362. return (ThreadState)thread->GetInternalThread()->state;
  363. }
  364. void Thread::ClrState(Il2CppThread* thread, ThreadState state)
  365. {
  366. il2cpp::os::FastAutoLock lock(thread->GetInternalThread()->longlived->synch_cs);
  367. thread->GetInternalThread()->state &= ~state;
  368. }
  369. static void AllocThreadDataSlot(ThreadStaticData* staticData, ThreadStaticOffset offset, int32_t size)
  370. {
  371. if (staticData->slots[offset.slot] == NULL)
  372. staticData->slots[offset.slot] = (ThreadStaticDataSlot*)IL2CPP_CALLOC(1, sizeof(ThreadStaticDataSlot), IL2CPP_MEM_THREAD);
  373. if (staticData->slots[offset.slot]->data[offset.index] == NULL)
  374. staticData->slots[offset.slot]->data[offset.index] = gc::GarbageCollector::AllocateFixed(size, NULL);
  375. }
  376. void Thread::AllocateStaticDataForCurrentThread()
  377. {
  378. AUTO_LOCK_THREADS();
  379. int32_t index = 0;
  380. // Alloc the slotData along with the first slots at once
  381. ThreadStaticData* staticData = (ThreadStaticData*)IL2CPP_CALLOC(1, sizeof(ThreadStaticData) + sizeof(ThreadStaticDataSlot), IL2CPP_MEM_THREAD);
  382. staticData->slots[0] = (ThreadStaticDataSlot*)(staticData + 1);
  383. Il2CppThread* thread = Current();
  384. IL2CPP_ASSERT(!thread->GetInternalThread()->static_data);
  385. thread->GetInternalThread()->static_data = staticData;
  386. s_StaticData.SetValue(staticData);
  387. for (std::vector<int32_t>::const_iterator iter = s_ThreadStaticSizes.begin(); iter != s_ThreadStaticSizes.end(); ++iter)
  388. {
  389. AllocThreadDataSlot(staticData, IndexToStaticFieldOffset(index), *iter);
  390. index++;
  391. }
  392. }
  393. int32_t Thread::AllocThreadStaticData(int32_t size)
  394. {
  395. AUTO_LOCK_THREADS();
  396. IL2CPP_ASSERT(size > 0);
  397. int32_t index = (int32_t)s_ThreadStaticSizes.size();
  398. IL2CPP_ASSERT(index < kMaxThreadStaticSlots * kMaxThreadStaticDataPointers);
  399. if (index >= kMaxThreadStaticSlots * kMaxThreadStaticDataPointers)
  400. il2cpp::vm::Exception::Raise(Exception::GetExecutionEngineException("Out of thread static storage slots"));
  401. s_ThreadStaticSizes.push_back(size);
  402. ThreadStaticOffset offset = IndexToStaticFieldOffset(index);
  403. for (GCTrackedThreadVector::const_iterator iter = s_AttachedThreads->begin(); iter != s_AttachedThreads->end(); ++iter)
  404. {
  405. Il2CppThread* thread = *iter;
  406. ThreadStaticData* staticData = reinterpret_cast<ThreadStaticData*>(thread->GetInternalThread()->static_data);
  407. if (staticData == NULL)
  408. {
  409. // There is a race on staticData for a thread could be NULL here in two cases
  410. // 1. The thread hasn't entered AllocateStaticDataForCurrentThread yet
  411. // 2. The thread has exited FreeCurrentThreadStaticData but hasn't been remove from the s_AttachedThreads yet
  412. // In both cases we can just continue and in 1. the data will be allocated in AllocateStaticDataForCurrentThread
  413. // and in 2. we don't want to allocate anything
  414. continue;
  415. }
  416. AllocThreadDataSlot(staticData, offset, size);
  417. }
  418. return index;
  419. }
  420. void Thread::FreeCurrentThreadStaticData(Il2CppThread *thread, bool inNativeThreadCleanup)
  421. {
  422. // This method is only valid to call from the current thread
  423. // But we can't safely check the Current() in native thread shutdown
  424. // because we can't rely on TLS values being valid
  425. IL2CPP_ASSERT(inNativeThreadCleanup || thread == Current());
  426. AUTO_LOCK_THREADS();
  427. ThreadStaticData* staticData = reinterpret_cast<ThreadStaticData*>(thread->GetInternalThread()->static_data);
  428. thread->GetInternalThread()->static_data = NULL;
  429. s_StaticData.SetValue(NULL);
  430. // This shouldn't happen unless we call this twice, but there's no reason to crash here
  431. IL2CPP_ASSERT(staticData);
  432. if (staticData == NULL)
  433. return;
  434. for (int slot = 0; slot < kMaxThreadStaticSlots; slot++)
  435. {
  436. if (!staticData->slots[slot])
  437. break;
  438. for (int i = 0; i < kMaxThreadStaticDataPointers; i++)
  439. {
  440. if (!staticData->slots[slot]->data[i])
  441. break;
  442. gc::GarbageCollector::FreeFixed(staticData->slots[slot]->data[i]);
  443. }
  444. // Don't free the first slot because we allocate the first slot along with the root slots
  445. if (slot > 0)
  446. IL2CPP_FREE(staticData->slots[slot], IL2CPP_MEM_THREAD);
  447. }
  448. IL2CPP_FREE(staticData, IL2CPP_MEM_THREAD);
  449. }
  450. void* Thread::GetThreadStaticData(int32_t offset)
  451. {
  452. // No lock. We allocate static_data once with a fixed size so we can read it
  453. // safely without a lock here.
  454. IL2CPP_ASSERT(offset >= 0 && static_cast<uint32_t>(offset) < s_ThreadStaticSizes.size());
  455. ThreadStaticOffset staticOffset = IndexToStaticFieldOffset(offset);
  456. ThreadStaticData* staticData;
  457. s_StaticData.GetValue((void**)&staticData);
  458. IL2CPP_ASSERT(staticData != NULL);
  459. return staticData->slots[staticOffset.slot]->data[staticOffset.index];
  460. }
  461. void* Thread::GetThreadStaticDataForThread(int32_t offset, Il2CppInternalThread* thread)
  462. {
  463. // No lock. We allocate static_data once with a fixed size so we can read it
  464. // safely without a lock here.
  465. IL2CPP_ASSERT(offset >= 0 && static_cast<uint32_t>(offset) < s_ThreadStaticSizes.size());
  466. IL2CPP_ASSERT(thread->static_data != NULL);
  467. ThreadStaticOffset staticOffset = IndexToStaticFieldOffset(offset);
  468. return reinterpret_cast<ThreadStaticData*>(thread->static_data)->slots[staticOffset.slot]->data[staticOffset.index];
  469. }
  470. void Thread::Register(Il2CppThread *thread)
  471. {
  472. AUTO_LOCK_THREADS();
  473. if (s_BlockNewThreads)
  474. TerminateThread(NULL);
  475. else
  476. {
  477. s_AttachedThreads->push_back(thread);
  478. set_wbarrier_for_attached_threads();
  479. }
  480. }
  481. void Thread::Unregister(Il2CppThread *thread)
  482. {
  483. AUTO_LOCK_THREADS();
  484. GCTrackedThreadVector::iterator it = std::find(s_AttachedThreads->begin(), s_AttachedThreads->end(), thread);
  485. #if IL2CPP_MONO_DEBUGGER
  486. if (it == s_AttachedThreads->end() && thread->internal_thread && il2cpp::utils::Debugger::IsDebuggerThread(thread->internal_thread->handle))
  487. return;
  488. #endif
  489. IL2CPP_ASSERT(it != s_AttachedThreads->end() && "Vm thread not found in list of attached threads.");
  490. s_AttachedThreads->erase(it);
  491. set_wbarrier_for_attached_threads();
  492. }
  493. bool Thread::IsVmThread(Il2CppThread *thread)
  494. {
  495. return !gc::GarbageCollector::IsFinalizerThread(thread);
  496. }
  497. std::string Thread::GetName(Il2CppInternalThread* thread)
  498. {
  499. if (thread->name.chars == NULL)
  500. return std::string();
  501. return utils::StringUtils::Utf16ToUtf8(thread->name.chars);
  502. }
  503. void Thread::SetName(Il2CppThread* thread, Il2CppString* name)
  504. {
  505. il2cpp::os::FastAutoLock lock(thread->GetInternalThread()->longlived->synch_cs);
  506. // Throw if already set.
  507. if (thread->GetInternalThread()->name.length != 0)
  508. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetInvalidOperationException("Thread name can only be set once."));
  509. // Store name.
  510. thread->GetInternalThread()->name.length = utils::StringUtils::GetLength(name);
  511. thread->GetInternalThread()->name.chars = il2cpp::utils::StringUtils::StringDuplicate(utils::StringUtils::GetChars(name), thread->GetInternalThread()->name.length);
  512. // Hand over to OS layer, if thread has been started already.
  513. if (thread->GetInternalThread()->handle)
  514. {
  515. std::string utf8Name = il2cpp::utils::StringUtils::Utf16ToUtf8(thread->GetInternalThread()->name.chars);
  516. thread->GetInternalThread()->handle->SetName(utf8Name.c_str());
  517. }
  518. }
  519. void Thread::SetName(Il2CppInternalThread* thread, Il2CppString* name)
  520. {
  521. il2cpp::os::FastAutoLock lock(thread->longlived->synch_cs);
  522. // Throw if already set.
  523. if (thread->name.length != 0)
  524. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetInvalidOperationException("Thread name can only be set once."));
  525. // Store name.
  526. thread->name.length = utils::StringUtils::GetLength(name);
  527. thread->name.chars = il2cpp::utils::StringUtils::StringDuplicate(utils::StringUtils::GetChars(name), thread->name.length);
  528. // Hand over to OS layer, if thread has been started already.
  529. if (thread->handle)
  530. {
  531. std::string utf8Name = il2cpp::utils::StringUtils::Utf16ToUtf8(thread->name.chars);
  532. thread->handle->SetName(utf8Name.c_str());
  533. }
  534. }
  535. static void STDCALL CheckCurrentThreadForInterruptCallback(void* context)
  536. {
  537. Thread::CheckCurrentThreadForInterruptAndThrowIfNecessary();
  538. }
  539. void Thread::RequestInterrupt(Il2CppThread* thread)
  540. {
  541. il2cpp::os::FastAutoLock lock(thread->GetInternalThread()->longlived->synch_cs);
  542. thread->GetInternalThread()->interruption_requested = true;
  543. // If thread has already been started, queue an interrupt now.
  544. il2cpp::os::Thread* osThread = thread->GetInternalThread()->handle;
  545. if (osThread)
  546. osThread->QueueUserAPC(CheckCurrentThreadForInterruptCallback, NULL);
  547. }
  548. void Thread::CheckCurrentThreadForInterruptAndThrowIfNecessary()
  549. {
  550. Il2CppThread* currentThread = il2cpp::vm::Thread::Current();
  551. if (!currentThread)
  552. return;
  553. il2cpp::os::FastAutoLock lock(currentThread->GetInternalThread()->longlived->synch_cs);
  554. // Don't throw if thread is not currently in waiting state or if there's
  555. // no pending interrupt.
  556. if (!currentThread->GetInternalThread()->interruption_requested
  557. || !(il2cpp::vm::Thread::GetState(currentThread) & il2cpp::vm::kThreadStateWaitSleepJoin))
  558. return;
  559. // Mark the current thread as being unblocked.
  560. currentThread->GetInternalThread()->interruption_requested = false;
  561. il2cpp::vm::Thread::ClrState(currentThread, il2cpp::vm::kThreadStateWaitSleepJoin);
  562. // Throw interrupt exception.
  563. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetThreadInterruptedException());
  564. }
  565. static void STDCALL CheckCurrentThreadForAbortCallback(void* context)
  566. {
  567. Thread::CheckCurrentThreadForAbortAndThrowIfNecessary();
  568. }
  569. bool Thread::RequestAbort(Il2CppThread* thread)
  570. {
  571. il2cpp::os::FastAutoLock lock(thread->GetInternalThread()->longlived->synch_cs);
  572. ThreadState state = il2cpp::vm::Thread::GetState(thread);
  573. if (state & kThreadStateAbortRequested || state & kThreadStateStopped || state & kThreadStateStopRequested)
  574. return false;
  575. il2cpp::os::Thread* osThread = thread->GetInternalThread()->handle;
  576. if (osThread)
  577. {
  578. // If thread has already been started, queue an abort now.
  579. Thread::SetState(thread, kThreadStateAbortRequested);
  580. osThread->QueueUserAPC(CheckCurrentThreadForAbortCallback, NULL);
  581. }
  582. else
  583. {
  584. // If thread has not started, put it in the aborted state.
  585. Thread::SetState(thread, kThreadStateAborted);
  586. }
  587. return true;
  588. }
  589. bool Thread::RequestAbort(Il2CppInternalThread* thread)
  590. {
  591. il2cpp::os::FastAutoLock lock(thread->longlived->synch_cs);
  592. ThreadState state = il2cpp::vm::Thread::GetState(thread);
  593. if (state & kThreadStateAbortRequested || state & kThreadStateStopped || state & kThreadStateStopRequested)
  594. return false;
  595. il2cpp::os::Thread* osThread = thread->handle;
  596. if (osThread)
  597. {
  598. // If thread has already been started, queue an abort now.
  599. Thread::SetState(thread, kThreadStateAbortRequested);
  600. osThread->QueueUserAPC(CheckCurrentThreadForAbortCallback, NULL);
  601. }
  602. else
  603. {
  604. // If thread has not started, put it in the aborted state.
  605. Thread::SetState(thread, kThreadStateAborted);
  606. }
  607. return true;
  608. }
  609. void Thread::SetPriority(Il2CppThread* thread, int32_t priority)
  610. {
  611. Il2CppInternalThread* internalThread = thread->GetInternalThread();
  612. il2cpp::os::FastAutoLock lock(internalThread->longlived->synch_cs);
  613. internalThread->handle->SetPriority((il2cpp::os::ThreadPriority)priority);
  614. }
  615. int32_t Thread::GetPriority(Il2CppThread* thread)
  616. {
  617. Il2CppInternalThread* internalThread = thread->GetInternalThread();
  618. il2cpp::os::FastAutoLock lock(internalThread->longlived->synch_cs);
  619. return internalThread->handle->GetPriority();
  620. }
  621. struct StartDataInternal
  622. {
  623. Il2CppThread* m_Thread;
  624. Il2CppDomain* m_Domain;
  625. void* m_Delegate;
  626. void* m_StartArg;
  627. il2cpp::os::Semaphore* m_Semaphore;
  628. };
  629. static void ThreadStart(void* arg)
  630. {
  631. StartDataInternal* startData = (StartDataInternal*)arg;
  632. startData->m_Semaphore->Wait();
  633. {
  634. gc::GarbageCollector::RegisterThread();
  635. il2cpp::vm::StackTrace::InitializeStackTracesForCurrentThread();
  636. bool attachSuccessful = false;
  637. try
  638. {
  639. il2cpp::vm::Thread::InitializeManagedThread(startData->m_Thread, startData->m_Domain);
  640. il2cpp::vm::Thread::SetState(startData->m_Thread, kThreadStateRunning);
  641. attachSuccessful = true;
  642. try
  643. {
  644. ((void(*)(void*))startData->m_Delegate)(startData->m_StartArg);
  645. }
  646. catch (Il2CppExceptionWrapper& ex)
  647. {
  648. // Only deal with the unhandled exception if the runtime is not
  649. // shutting down. Otherwise, the code to process the unhandled
  650. // exception might fail in unexpected ways, because it needs
  651. // the full runtime available. We've seen this cause crashes
  652. // that are difficult to reproduce locally.
  653. if (!il2cpp::vm::Runtime::IsShuttingDown())
  654. Runtime::UnhandledException(ex.ex);
  655. }
  656. }
  657. catch (il2cpp::vm::Thread::NativeThreadAbortException)
  658. {
  659. // Nothing to do. We've successfully aborted the thread.
  660. il2cpp::vm::Thread::SetState(startData->m_Thread, kThreadStateAborted);
  661. }
  662. il2cpp::vm::Thread::ClrState(startData->m_Thread, kThreadStateRunning);
  663. il2cpp::vm::Thread::SetState(startData->m_Thread, kThreadStateStopped);
  664. if (attachSuccessful)
  665. il2cpp::vm::Thread::UninitializeManagedThread(startData->m_Thread, false);
  666. il2cpp::vm::StackTrace::CleanupStackTracesForCurrentThread();
  667. }
  668. delete startData->m_Semaphore;
  669. gc::GarbageCollector::FreeFixed(startData);
  670. }
  671. Il2CppInternalThread* Thread::CreateInternal(void(*func)(void*), void* arg, bool threadpool_thread, uint32_t stack_size)
  672. {
  673. // The os::Thread object is deallocated in the InternalThread::Thread_free_internal icall, which
  674. // is called from the managed thread finalizer.
  675. os::Thread* osThread = new os::Thread();
  676. Il2CppThread* managedThread = (Il2CppThread*)Object::New(il2cpp_defaults.thread_class);
  677. SetupInternalManagedThread(managedThread, osThread);
  678. Il2CppInternalThread* internalManagedThread = managedThread->GetInternalThread();
  679. internalManagedThread->state = kThreadStateUnstarted;
  680. internalManagedThread->threadpool_thread = threadpool_thread;
  681. // use fixed GC memory since we are storing managed object pointers
  682. StartDataInternal* startData = (StartDataInternal*)gc::GarbageCollector::AllocateFixed(sizeof(StartDataInternal), NULL);
  683. gc::WriteBarrier::GenericStore(&startData->m_Thread, managedThread);
  684. gc::WriteBarrier::GenericStore(&startData->m_Domain, Domain::GetCurrent());
  685. startData->m_Delegate = (void*)func;
  686. startData->m_StartArg = arg;
  687. startData->m_Semaphore = new il2cpp::os::Semaphore(0);
  688. osThread->SetStackSize(stack_size);
  689. osThread->SetExplicitApartment(static_cast<il2cpp::os::ApartmentState>(managedThread->GetInternalThread()->apartment_state));
  690. il2cpp::os::ErrorCode status = osThread->Run(&ThreadStart, startData);
  691. if (status != il2cpp::os::kErrorCodeSuccess)
  692. {
  693. delete osThread;
  694. return NULL;
  695. }
  696. internalManagedThread->state &= ~kThreadStateUnstarted;
  697. startData->m_Semaphore->Post(1, NULL);
  698. return internalManagedThread;
  699. }
  700. void Thread::Stop(Il2CppInternalThread* thread)
  701. {
  702. IL2CPP_ASSERT(thread != CurrentInternal());
  703. if (!RequestAbort(thread))
  704. return;
  705. os::Thread* osThread = thread->handle;
  706. ////FIXME: While we don't have stable thread abortion in place yet, work around problems in
  707. //// the current implementation by repeatedly requesting threads to terminate. This works around
  708. //// race condition to some extent.
  709. while (true)
  710. {
  711. // If it's a background thread, request it to kill itself.
  712. if (GetState(thread) & kThreadStateBackground)
  713. {
  714. ////TODO: use Thread.Abort() instead
  715. osThread->QueueUserAPC(TerminateThread, NULL);
  716. }
  717. // Wait for the thread.
  718. if (osThread->Join(10) == kWaitStatusSuccess)
  719. break;
  720. }
  721. }
  722. void Thread::Sleep(uint32_t ms)
  723. {
  724. CurrentInternal()->handle->Sleep(ms);
  725. }
  726. bool Thread::YieldInternal()
  727. {
  728. return os::Thread::YieldInternal();
  729. }
  730. void Thread::SetDefaultAffinityMask(int64_t affinityMask)
  731. {
  732. #if defined(IL2CPP_ENABLE_PLATFORM_THREAD_AFFINTY)
  733. os::Thread::SetDefaultAffinityMask(affinityMask);
  734. #endif
  735. }
  736. void Thread::CheckCurrentThreadForAbortAndThrowIfNecessary()
  737. {
  738. Il2CppThread* currentThread = il2cpp::vm::Thread::Current();
  739. if (!currentThread)
  740. return;
  741. il2cpp::os::FastAutoLock lock(currentThread->GetInternalThread()->longlived->synch_cs);
  742. ThreadState state = il2cpp::vm::Thread::GetState(currentThread);
  743. if (!(state & kThreadStateAbortRequested))
  744. return;
  745. // Throw interrupt exception.
  746. Il2CppException* abortException = il2cpp::vm::Exception::GetThreadAbortException();
  747. IL2CPP_OBJECT_SETREF(currentThread->GetInternalThread(), abort_exc, abortException);
  748. il2cpp::vm::Exception::Raise(abortException);
  749. }
  750. void Thread::ResetAbort(Il2CppThread* thread)
  751. {
  752. il2cpp::vm::Thread::ClrState(thread, kThreadStateAbortRequested);
  753. if (thread->GetInternalThread()->abort_exc == NULL)
  754. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetThreadStateException("Unable to reset abort because no abort was requested."));
  755. }
  756. void Thread::ResetAbort(Il2CppInternalThread* thread)
  757. {
  758. il2cpp::vm::Thread::ClrState(thread, kThreadStateAbortRequested);
  759. if (thread->abort_exc == NULL)
  760. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetThreadStateException("Unable to reset abort because no abort was requested."));
  761. }
  762. void Thread::FullMemoryBarrier()
  763. {
  764. os::Atomic::FullMemoryBarrier();
  765. }
  766. int32_t Thread::GetNewManagedId()
  767. {
  768. return ++s_NextManagedThreadId;
  769. }
  770. uint64_t Thread::GetId(Il2CppThread* thread)
  771. {
  772. return thread->GetInternalThread()->tid;
  773. }
  774. uint64_t Thread::GetId(Il2CppInternalThread* thread)
  775. {
  776. return thread->tid;
  777. }
  778. } /* namespace vm */
  779. } /* namespace il2cpp */