thread.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. // Copyright 2014 Citra Emulator Project / PPSSPP Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <list>
  6. #include <vector>
  7. #include "common/assert.h"
  8. #include "common/common_types.h"
  9. #include "common/logging/log.h"
  10. #include "common/math_util.h"
  11. #include "common/thread_queue_list.h"
  12. #include "core/arm/arm_interface.h"
  13. #include "core/arm/skyeye_common/armstate.h"
  14. #include "core/core.h"
  15. #include "core/core_timing.h"
  16. #include "core/hle/kernel/kernel.h"
  17. #include "core/hle/kernel/memory.h"
  18. #include "core/hle/kernel/mutex.h"
  19. #include "core/hle/kernel/process.h"
  20. #include "core/hle/kernel/thread.h"
  21. #include "core/hle/result.h"
  22. #include "core/memory.h"
  23. namespace Kernel {
  24. /// Event type for the thread wake up event
  25. static int ThreadWakeupEventType;
  26. bool Thread::ShouldWait(Thread* thread) const {
  27. return status != THREADSTATUS_DEAD;
  28. }
  29. void Thread::Acquire(Thread* thread) {
  30. ASSERT_MSG(!ShouldWait(thread), "object unavailable!");
  31. }
  32. // TODO(yuriks): This can be removed if Thread objects are explicitly pooled in the future, allowing
  33. // us to simply use a pool index or similar.
  34. static Kernel::HandleTable wakeup_callback_handle_table;
  35. // Lists all thread ids that aren't deleted/etc.
  36. static std::vector<SharedPtr<Thread>> thread_list;
  37. // Lists only ready thread ids.
  38. static Common::ThreadQueueList<Thread*, THREADPRIO_LOWEST + 1> ready_queue;
  39. static SharedPtr<Thread> current_thread;
  40. // The first available thread id at startup
  41. static u32 next_thread_id;
  42. /**
  43. * Creates a new thread ID
  44. * @return The new thread ID
  45. */
  46. inline static u32 const NewThreadId() {
  47. return next_thread_id++;
  48. }
  49. Thread::Thread() {}
  50. Thread::~Thread() {}
  51. Thread* GetCurrentThread() {
  52. return current_thread.get();
  53. }
  54. /**
  55. * Check if the specified thread is waiting on the specified address to be arbitrated
  56. * @param thread The thread to test
  57. * @param wait_address The address to test against
  58. * @return True if the thread is waiting, false otherwise
  59. */
  60. static bool CheckWait_AddressArbiter(const Thread* thread, VAddr wait_address) {
  61. return thread->status == THREADSTATUS_WAIT_ARB && wait_address == thread->wait_address;
  62. }
  63. void Thread::Stop() {
  64. // Cancel any outstanding wakeup events for this thread
  65. CoreTiming::UnscheduleEvent(ThreadWakeupEventType, callback_handle);
  66. wakeup_callback_handle_table.Close(callback_handle);
  67. callback_handle = 0;
  68. // Clean up thread from ready queue
  69. // This is only needed when the thread is termintated forcefully (SVC TerminateProcess)
  70. if (status == THREADSTATUS_READY) {
  71. ready_queue.remove(current_priority, this);
  72. }
  73. status = THREADSTATUS_DEAD;
  74. WakeupAllWaitingThreads();
  75. // Clean up any dangling references in objects that this thread was waiting for
  76. for (auto& wait_object : wait_objects) {
  77. wait_object->RemoveWaitingThread(this);
  78. }
  79. wait_objects.clear();
  80. // Release all the mutexes that this thread holds
  81. ReleaseThreadMutexes(this);
  82. // Mark the TLS slot in the thread's page as free.
  83. u32 tls_page = (tls_address - Memory::TLS_AREA_VADDR) / Memory::PAGE_SIZE;
  84. u32 tls_slot =
  85. ((tls_address - Memory::TLS_AREA_VADDR) % Memory::PAGE_SIZE) / Memory::TLS_ENTRY_SIZE;
  86. Kernel::g_current_process->tls_slots[tls_page].reset(tls_slot);
  87. }
  88. Thread* ArbitrateHighestPriorityThread(u32 address) {
  89. Thread* highest_priority_thread = nullptr;
  90. s32 priority = THREADPRIO_LOWEST;
  91. // Iterate through threads, find highest priority thread that is waiting to be arbitrated...
  92. for (auto& thread : thread_list) {
  93. if (!CheckWait_AddressArbiter(thread.get(), address))
  94. continue;
  95. if (thread == nullptr)
  96. continue;
  97. if (thread->current_priority <= priority) {
  98. highest_priority_thread = thread.get();
  99. priority = thread->current_priority;
  100. }
  101. }
  102. // If a thread was arbitrated, resume it
  103. if (nullptr != highest_priority_thread) {
  104. highest_priority_thread->ResumeFromWait();
  105. }
  106. return highest_priority_thread;
  107. }
  108. void ArbitrateAllThreads(u32 address) {
  109. // Resume all threads found to be waiting on the address
  110. for (auto& thread : thread_list) {
  111. if (CheckWait_AddressArbiter(thread.get(), address))
  112. thread->ResumeFromWait();
  113. }
  114. }
  115. /// Boost low priority threads (temporarily) that have been starved
  116. static void PriorityBoostStarvedThreads() {
  117. u64 current_ticks = CoreTiming::GetTicks();
  118. for (auto& thread : thread_list) {
  119. // TODO(bunnei): Threads that have been waiting to be scheduled for `boost_ticks` (or
  120. // longer) will have their priority temporarily adjusted to 1 higher than the highest
  121. // priority thread to prevent thread starvation. This general behavior has been verified
  122. // on hardware. However, this is almost certainly not perfect, and the real CTR OS scheduler
  123. // should probably be reversed to verify this.
  124. const u64 boost_timeout = 2000000; // Boost threads that have been ready for > this long
  125. u64 delta = current_ticks - thread->last_running_ticks;
  126. if (thread->status == THREADSTATUS_READY && delta > boost_timeout) {
  127. const s32 priority = std::max(ready_queue.get_first()->current_priority - 1, 0);
  128. thread->BoostPriority(priority);
  129. }
  130. }
  131. }
  132. /**
  133. * Switches the CPU's active thread context to that of the specified thread
  134. * @param new_thread The thread to switch to
  135. */
  136. static void SwitchContext(Thread* new_thread) {
  137. Thread* previous_thread = GetCurrentThread();
  138. // Save context for previous thread
  139. if (previous_thread) {
  140. previous_thread->last_running_ticks = CoreTiming::GetTicks();
  141. Core::CPU().SaveContext(previous_thread->context);
  142. if (previous_thread->status == THREADSTATUS_RUNNING) {
  143. // This is only the case when a reschedule is triggered without the current thread
  144. // yielding execution (i.e. an event triggered, system core time-sliced, etc)
  145. ready_queue.push_front(previous_thread->current_priority, previous_thread);
  146. previous_thread->status = THREADSTATUS_READY;
  147. }
  148. }
  149. // Load context of new thread
  150. if (new_thread) {
  151. ASSERT_MSG(new_thread->status == THREADSTATUS_READY,
  152. "Thread must be ready to become running.");
  153. // Cancel any outstanding wakeup events for this thread
  154. CoreTiming::UnscheduleEvent(ThreadWakeupEventType, new_thread->callback_handle);
  155. current_thread = new_thread;
  156. ready_queue.remove(new_thread->current_priority, new_thread);
  157. new_thread->status = THREADSTATUS_RUNNING;
  158. // Restores thread to its nominal priority if it has been temporarily changed
  159. new_thread->current_priority = new_thread->nominal_priority;
  160. Core::CPU().LoadContext(new_thread->context);
  161. Core::CPU().SetCP15Register(CP15_THREAD_URO, new_thread->GetTLSAddress());
  162. } else {
  163. current_thread = nullptr;
  164. }
  165. }
  166. /**
  167. * Pops and returns the next thread from the thread queue
  168. * @return A pointer to the next ready thread
  169. */
  170. static Thread* PopNextReadyThread() {
  171. Thread* next;
  172. Thread* thread = GetCurrentThread();
  173. if (thread && thread->status == THREADSTATUS_RUNNING) {
  174. // We have to do better than the current thread.
  175. // This call returns null when that's not possible.
  176. next = ready_queue.pop_first_better(thread->current_priority);
  177. if (!next) {
  178. // Otherwise just keep going with the current thread
  179. next = thread;
  180. }
  181. } else {
  182. next = ready_queue.pop_first();
  183. }
  184. return next;
  185. }
  186. void WaitCurrentThread_Sleep() {
  187. Thread* thread = GetCurrentThread();
  188. thread->status = THREADSTATUS_WAIT_SLEEP;
  189. }
  190. void WaitCurrentThread_ArbitrateAddress(VAddr wait_address) {
  191. Thread* thread = GetCurrentThread();
  192. thread->wait_address = wait_address;
  193. thread->status = THREADSTATUS_WAIT_ARB;
  194. }
  195. void ExitCurrentThread() {
  196. Thread* thread = GetCurrentThread();
  197. thread->Stop();
  198. thread_list.erase(std::remove(thread_list.begin(), thread_list.end(), thread),
  199. thread_list.end());
  200. }
  201. /**
  202. * Callback that will wake up the thread it was scheduled for
  203. * @param thread_handle The handle of the thread that's been awoken
  204. * @param cycles_late The number of CPU cycles that have passed since the desired wakeup time
  205. */
  206. static void ThreadWakeupCallback(u64 thread_handle, int cycles_late) {
  207. SharedPtr<Thread> thread = wakeup_callback_handle_table.Get<Thread>((Handle)thread_handle);
  208. if (thread == nullptr) {
  209. LOG_CRITICAL(Kernel, "Callback fired for invalid thread %08X", (Handle)thread_handle);
  210. return;
  211. }
  212. if (thread->status == THREADSTATUS_WAIT_SYNCH_ANY ||
  213. thread->status == THREADSTATUS_WAIT_SYNCH_ALL || thread->status == THREADSTATUS_WAIT_ARB) {
  214. thread->wait_set_output = false;
  215. // Remove the thread from each of its waiting objects' waitlists
  216. for (auto& object : thread->wait_objects)
  217. object->RemoveWaitingThread(thread.get());
  218. thread->wait_objects.clear();
  219. thread->SetWaitSynchronizationResult(ResultCode(ErrorDescription::Timeout, ErrorModule::OS,
  220. ErrorSummary::StatusChanged,
  221. ErrorLevel::Info));
  222. }
  223. thread->ResumeFromWait();
  224. }
  225. void Thread::WakeAfterDelay(s64 nanoseconds) {
  226. // Don't schedule a wakeup if the thread wants to wait forever
  227. if (nanoseconds == -1)
  228. return;
  229. u64 microseconds = nanoseconds / 1000;
  230. CoreTiming::ScheduleEvent(usToCycles(microseconds), ThreadWakeupEventType, callback_handle);
  231. }
  232. void Thread::ResumeFromWait() {
  233. ASSERT_MSG(wait_objects.empty(), "Thread is waking up while waiting for objects");
  234. switch (status) {
  235. case THREADSTATUS_WAIT_SYNCH_ALL:
  236. case THREADSTATUS_WAIT_SYNCH_ANY:
  237. case THREADSTATUS_WAIT_ARB:
  238. case THREADSTATUS_WAIT_SLEEP:
  239. break;
  240. case THREADSTATUS_READY:
  241. // If the thread is waiting on multiple wait objects, it might be awoken more than once
  242. // before actually resuming. We can ignore subsequent wakeups if the thread status has
  243. // already been set to THREADSTATUS_READY.
  244. return;
  245. case THREADSTATUS_RUNNING:
  246. DEBUG_ASSERT_MSG(false, "Thread with object id %u has already resumed.", GetObjectId());
  247. return;
  248. case THREADSTATUS_DEAD:
  249. // This should never happen, as threads must complete before being stopped.
  250. DEBUG_ASSERT_MSG(false, "Thread with object id %u cannot be resumed because it's DEAD.",
  251. GetObjectId());
  252. return;
  253. }
  254. ready_queue.push_back(current_priority, this);
  255. status = THREADSTATUS_READY;
  256. Core::System::GetInstance().PrepareReschedule();
  257. }
  258. /**
  259. * Prints the thread queue for debugging purposes
  260. */
  261. static void DebugThreadQueue() {
  262. Thread* thread = GetCurrentThread();
  263. if (!thread) {
  264. LOG_DEBUG(Kernel, "Current: NO CURRENT THREAD");
  265. } else {
  266. LOG_DEBUG(Kernel, "0x%02X %u (current)", thread->current_priority,
  267. GetCurrentThread()->GetObjectId());
  268. }
  269. for (auto& t : thread_list) {
  270. s32 priority = ready_queue.contains(t.get());
  271. if (priority != -1) {
  272. LOG_DEBUG(Kernel, "0x%02X %u", priority, t->GetObjectId());
  273. }
  274. }
  275. }
  276. /**
  277. * Finds a free location for the TLS section of a thread.
  278. * @param tls_slots The TLS page array of the thread's owner process.
  279. * Returns a tuple of (page, slot, alloc_needed) where:
  280. * page: The index of the first allocated TLS page that has free slots.
  281. * slot: The index of the first free slot in the indicated page.
  282. * alloc_needed: Whether there's a need to allocate a new TLS page (All pages are full).
  283. */
  284. std::tuple<u32, u32, bool> GetFreeThreadLocalSlot(std::vector<std::bitset<8>>& tls_slots) {
  285. // Iterate over all the allocated pages, and try to find one where not all slots are used.
  286. for (unsigned page = 0; page < tls_slots.size(); ++page) {
  287. const auto& page_tls_slots = tls_slots[page];
  288. if (!page_tls_slots.all()) {
  289. // We found a page with at least one free slot, find which slot it is
  290. for (unsigned slot = 0; slot < page_tls_slots.size(); ++slot) {
  291. if (!page_tls_slots.test(slot)) {
  292. return std::make_tuple(page, slot, false);
  293. }
  294. }
  295. }
  296. }
  297. return std::make_tuple(0, 0, true);
  298. }
  299. /**
  300. * Resets a thread context, making it ready to be scheduled and run by the CPU
  301. * @param context Thread context to reset
  302. * @param stack_top Address of the top of the stack
  303. * @param entry_point Address of entry point for execution
  304. * @param arg User argument for thread
  305. */
  306. static void ResetThreadContext(ARM_Interface::ThreadContext& context, u32 stack_top,
  307. u32 entry_point, u32 arg) {
  308. memset(&context, 0, sizeof(ARM_Interface::ThreadContext));
  309. context.cpu_registers[0] = arg;
  310. context.pc = entry_point;
  311. context.sp = stack_top;
  312. context.cpsr = USER32MODE | ((entry_point & 1) << 5); // Usermode and THUMB mode
  313. }
  314. ResultVal<SharedPtr<Thread>> Thread::Create(std::string name, VAddr entry_point, s32 priority,
  315. u32 arg, s32 processor_id, VAddr stack_top) {
  316. if (priority < THREADPRIO_HIGHEST || priority > THREADPRIO_LOWEST) {
  317. s32 new_priority = MathUtil::Clamp<s32>(priority, THREADPRIO_HIGHEST, THREADPRIO_LOWEST);
  318. LOG_WARNING(Kernel_SVC, "(name=%s): invalid priority=%d, clamping to %d", name.c_str(),
  319. priority, new_priority);
  320. // TODO(bunnei): Clamping to a valid priority is not necessarily correct behavior... Confirm
  321. // validity of this
  322. priority = new_priority;
  323. }
  324. if (!Memory::IsValidVirtualAddress(entry_point)) {
  325. LOG_ERROR(Kernel_SVC, "(name=%s): invalid entry %08x", name.c_str(), entry_point);
  326. // TODO: Verify error
  327. return ResultCode(ErrorDescription::InvalidAddress, ErrorModule::Kernel,
  328. ErrorSummary::InvalidArgument, ErrorLevel::Permanent);
  329. }
  330. SharedPtr<Thread> thread(new Thread);
  331. thread_list.push_back(thread);
  332. ready_queue.prepare(priority);
  333. thread->thread_id = NewThreadId();
  334. thread->status = THREADSTATUS_DORMANT;
  335. thread->entry_point = entry_point;
  336. thread->stack_top = stack_top;
  337. thread->nominal_priority = thread->current_priority = priority;
  338. thread->last_running_ticks = CoreTiming::GetTicks();
  339. thread->processor_id = processor_id;
  340. thread->wait_set_output = false;
  341. thread->wait_objects.clear();
  342. thread->wait_address = 0;
  343. thread->name = std::move(name);
  344. thread->callback_handle = wakeup_callback_handle_table.Create(thread).MoveFrom();
  345. thread->owner_process = g_current_process;
  346. // Find the next available TLS index, and mark it as used
  347. auto& tls_slots = Kernel::g_current_process->tls_slots;
  348. bool needs_allocation = true;
  349. u32 available_page; // Which allocated page has free space
  350. u32 available_slot; // Which slot within the page is free
  351. std::tie(available_page, available_slot, needs_allocation) = GetFreeThreadLocalSlot(tls_slots);
  352. if (needs_allocation) {
  353. // There are no already-allocated pages with free slots, lets allocate a new one.
  354. // TLS pages are allocated from the BASE region in the linear heap.
  355. MemoryRegionInfo* memory_region = GetMemoryRegion(MemoryRegion::BASE);
  356. auto& linheap_memory = memory_region->linear_heap_memory;
  357. if (linheap_memory->size() + Memory::PAGE_SIZE > memory_region->size) {
  358. LOG_ERROR(Kernel_SVC,
  359. "Not enough space in region to allocate a new TLS page for thread");
  360. return ResultCode(ErrorDescription::OutOfMemory, ErrorModule::Kernel,
  361. ErrorSummary::OutOfResource, ErrorLevel::Permanent);
  362. }
  363. u32 offset = linheap_memory->size();
  364. // Allocate some memory from the end of the linear heap for this region.
  365. linheap_memory->insert(linheap_memory->end(), Memory::PAGE_SIZE, 0);
  366. memory_region->used += Memory::PAGE_SIZE;
  367. Kernel::g_current_process->linear_heap_used += Memory::PAGE_SIZE;
  368. tls_slots.emplace_back(0); // The page is completely available at the start
  369. available_page = tls_slots.size() - 1;
  370. available_slot = 0; // Use the first slot in the new page
  371. auto& vm_manager = Kernel::g_current_process->vm_manager;
  372. vm_manager.RefreshMemoryBlockMappings(linheap_memory.get());
  373. // Map the page to the current process' address space.
  374. // TODO(Subv): Find the correct MemoryState for this region.
  375. vm_manager.MapMemoryBlock(Memory::TLS_AREA_VADDR + available_page * Memory::PAGE_SIZE,
  376. linheap_memory, offset, Memory::PAGE_SIZE, MemoryState::Private);
  377. }
  378. // Mark the slot as used
  379. tls_slots[available_page].set(available_slot);
  380. thread->tls_address = Memory::TLS_AREA_VADDR + available_page * Memory::PAGE_SIZE +
  381. available_slot * Memory::TLS_ENTRY_SIZE;
  382. // TODO(peachum): move to ScheduleThread() when scheduler is added so selected core is used
  383. // to initialize the context
  384. ResetThreadContext(thread->context, stack_top, entry_point, arg);
  385. ready_queue.push_back(thread->current_priority, thread.get());
  386. thread->status = THREADSTATUS_READY;
  387. return MakeResult<SharedPtr<Thread>>(std::move(thread));
  388. }
  389. // TODO(peachum): Remove this. Range checking should be done, and an appropriate error should be
  390. // returned.
  391. static void ClampPriority(const Thread* thread, s32* priority) {
  392. if (*priority < THREADPRIO_HIGHEST || *priority > THREADPRIO_LOWEST) {
  393. DEBUG_ASSERT_MSG(
  394. false, "Application passed an out of range priority. An error should be returned.");
  395. s32 new_priority = MathUtil::Clamp<s32>(*priority, THREADPRIO_HIGHEST, THREADPRIO_LOWEST);
  396. LOG_WARNING(Kernel_SVC, "(name=%s): invalid priority=%d, clamping to %d",
  397. thread->name.c_str(), *priority, new_priority);
  398. // TODO(bunnei): Clamping to a valid priority is not necessarily correct behavior... Confirm
  399. // validity of this
  400. *priority = new_priority;
  401. }
  402. }
  403. void Thread::SetPriority(s32 priority) {
  404. ClampPriority(this, &priority);
  405. // If thread was ready, adjust queues
  406. if (status == THREADSTATUS_READY)
  407. ready_queue.move(this, current_priority, priority);
  408. else
  409. ready_queue.prepare(priority);
  410. nominal_priority = current_priority = priority;
  411. }
  412. void Thread::UpdatePriority() {
  413. s32 best_priority = nominal_priority;
  414. for (auto& mutex : held_mutexes) {
  415. if (mutex->priority < best_priority)
  416. best_priority = mutex->priority;
  417. }
  418. BoostPriority(best_priority);
  419. }
  420. void Thread::BoostPriority(s32 priority) {
  421. // If thread was ready, adjust queues
  422. if (status == THREADSTATUS_READY)
  423. ready_queue.move(this, current_priority, priority);
  424. else
  425. ready_queue.prepare(priority);
  426. current_priority = priority;
  427. }
  428. SharedPtr<Thread> SetupMainThread(u32 entry_point, s32 priority) {
  429. DEBUG_ASSERT(!GetCurrentThread());
  430. // Initialize new "main" thread
  431. auto thread_res = Thread::Create("main", entry_point, priority, 0, THREADPROCESSORID_0,
  432. Memory::HEAP_VADDR_END);
  433. SharedPtr<Thread> thread = thread_res.MoveFrom();
  434. thread->context.fpscr =
  435. FPSCR_DEFAULT_NAN | FPSCR_FLUSH_TO_ZERO | FPSCR_ROUND_TOZERO | FPSCR_IXC; // 0x03C00010
  436. // Run new "main" thread
  437. SwitchContext(thread.get());
  438. return thread;
  439. }
  440. void Reschedule() {
  441. PriorityBoostStarvedThreads();
  442. Thread* cur = GetCurrentThread();
  443. Thread* next = PopNextReadyThread();
  444. if (cur && next) {
  445. LOG_TRACE(Kernel, "context switch %u -> %u", cur->GetObjectId(), next->GetObjectId());
  446. } else if (cur) {
  447. LOG_TRACE(Kernel, "context switch %u -> idle", cur->GetObjectId());
  448. } else if (next) {
  449. LOG_TRACE(Kernel, "context switch idle -> %u", next->GetObjectId());
  450. }
  451. SwitchContext(next);
  452. }
  453. void Thread::SetWaitSynchronizationResult(ResultCode result) {
  454. context.cpu_registers[0] = result.raw;
  455. }
  456. void Thread::SetWaitSynchronizationOutput(s32 output) {
  457. context.cpu_registers[1] = output;
  458. }
  459. s32 Thread::GetWaitObjectIndex(WaitObject* object) const {
  460. ASSERT_MSG(!wait_objects.empty(), "Thread is not waiting for anything");
  461. auto match = std::find(wait_objects.rbegin(), wait_objects.rend(), object);
  462. return std::distance(match, wait_objects.rend()) - 1;
  463. }
  464. ////////////////////////////////////////////////////////////////////////////////////////////////////
  465. void ThreadingInit() {
  466. ThreadWakeupEventType = CoreTiming::RegisterEvent("ThreadWakeupCallback", ThreadWakeupCallback);
  467. current_thread = nullptr;
  468. next_thread_id = 1;
  469. }
  470. void ThreadingShutdown() {
  471. current_thread = nullptr;
  472. for (auto& t : thread_list) {
  473. t->Stop();
  474. }
  475. thread_list.clear();
  476. ready_queue.clear();
  477. }
  478. const std::vector<SharedPtr<Thread>>& GetThreadList() {
  479. return thread_list;
  480. }
  481. } // namespace