thread.cpp 21 KB

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