Scheduler.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/QuickSort.h>
  27. #include <AK/ScopeGuard.h>
  28. #include <AK/TemporaryChange.h>
  29. #include <Kernel/FileSystem/FileDescription.h>
  30. #include <Kernel/Net/Socket.h>
  31. #include <Kernel/Process.h>
  32. #include <Kernel/Profiling.h>
  33. #include <Kernel/RTC.h>
  34. #include <Kernel/Scheduler.h>
  35. #include <Kernel/Time/TimeManagement.h>
  36. #include <Kernel/TimerQueue.h>
  37. //#define LOG_EVERY_CONTEXT_SWITCH
  38. //#define SCHEDULER_DEBUG
  39. //#define SCHEDULER_RUNNABLE_DEBUG
  40. namespace Kernel {
  41. class SchedulerPerProcessorData {
  42. AK_MAKE_NONCOPYABLE(SchedulerPerProcessorData);
  43. AK_MAKE_NONMOVABLE(SchedulerPerProcessorData);
  44. public:
  45. SchedulerPerProcessorData() = default;
  46. bool m_in_scheduler { true };
  47. };
  48. SchedulerData* g_scheduler_data;
  49. timeval g_timeofday;
  50. RecursiveSpinLock g_scheduler_lock;
  51. void Scheduler::init_thread(Thread& thread)
  52. {
  53. ASSERT(g_scheduler_data);
  54. g_scheduler_data->m_nonrunnable_threads.append(thread);
  55. }
  56. static u32 time_slice_for(const Thread& thread)
  57. {
  58. // One time slice unit == 1ms
  59. if (&thread == Processor::current().idle_thread())
  60. return 1;
  61. return 10;
  62. }
  63. timeval Scheduler::time_since_boot()
  64. {
  65. return { TimeManagement::the().seconds_since_boot(), (suseconds_t)TimeManagement::the().ticks_this_second() * 1000 };
  66. }
  67. Thread* g_finalizer;
  68. WaitQueue* g_finalizer_wait_queue;
  69. Atomic<bool> g_finalizer_has_work { false };
  70. static Process* s_colonel_process;
  71. u64 g_uptime;
  72. Thread::JoinBlocker::JoinBlocker(Thread& joinee, void*& joinee_exit_value)
  73. : m_joinee(joinee)
  74. , m_joinee_exit_value(joinee_exit_value)
  75. {
  76. ASSERT(m_joinee.m_joiner == nullptr);
  77. auto current_thread = Thread::current();
  78. m_joinee.m_joiner = current_thread;
  79. current_thread->m_joinee = &joinee;
  80. }
  81. bool Thread::JoinBlocker::should_unblock(Thread& joiner)
  82. {
  83. return !joiner.m_joinee;
  84. }
  85. Thread::FileDescriptionBlocker::FileDescriptionBlocker(const FileDescription& description)
  86. : m_blocked_description(description)
  87. {
  88. }
  89. const FileDescription& Thread::FileDescriptionBlocker::blocked_description() const
  90. {
  91. return m_blocked_description;
  92. }
  93. Thread::AcceptBlocker::AcceptBlocker(const FileDescription& description)
  94. : FileDescriptionBlocker(description)
  95. {
  96. }
  97. bool Thread::AcceptBlocker::should_unblock(Thread&)
  98. {
  99. auto& socket = *blocked_description().socket();
  100. return socket.can_accept();
  101. }
  102. Thread::ConnectBlocker::ConnectBlocker(const FileDescription& description)
  103. : FileDescriptionBlocker(description)
  104. {
  105. }
  106. bool Thread::ConnectBlocker::should_unblock(Thread&)
  107. {
  108. auto& socket = *blocked_description().socket();
  109. return socket.setup_state() == Socket::SetupState::Completed;
  110. }
  111. Thread::WriteBlocker::WriteBlocker(const FileDescription& description)
  112. : FileDescriptionBlocker(description)
  113. {
  114. if (description.is_socket()) {
  115. auto& socket = *description.socket();
  116. if (socket.has_send_timeout()) {
  117. timeval deadline = Scheduler::time_since_boot();
  118. deadline.tv_sec += socket.send_timeout().tv_sec;
  119. deadline.tv_usec += socket.send_timeout().tv_usec;
  120. deadline.tv_sec += (socket.send_timeout().tv_usec / 1000000) * 1;
  121. deadline.tv_usec %= 1000000;
  122. m_deadline = deadline;
  123. }
  124. }
  125. }
  126. bool Thread::WriteBlocker::should_unblock(Thread& thread, time_t now_sec, long now_usec)
  127. {
  128. if (m_deadline.has_value()) {
  129. bool timed_out = now_sec > m_deadline.value().tv_sec || (now_sec == m_deadline.value().tv_sec && now_usec >= m_deadline.value().tv_usec);
  130. return timed_out || blocked_description().can_write();
  131. }
  132. return should_unblock(thread);
  133. }
  134. bool Thread::WriteBlocker::should_unblock(Thread&)
  135. {
  136. return blocked_description().can_write();
  137. }
  138. Thread::ReadBlocker::ReadBlocker(const FileDescription& description)
  139. : FileDescriptionBlocker(description)
  140. {
  141. if (description.is_socket()) {
  142. auto& socket = *description.socket();
  143. if (socket.has_receive_timeout()) {
  144. timeval deadline = Scheduler::time_since_boot();
  145. deadline.tv_sec += socket.receive_timeout().tv_sec;
  146. deadline.tv_usec += socket.receive_timeout().tv_usec;
  147. deadline.tv_sec += (socket.receive_timeout().tv_usec / 1000000) * 1;
  148. deadline.tv_usec %= 1000000;
  149. m_deadline = deadline;
  150. }
  151. }
  152. }
  153. bool Thread::ReadBlocker::should_unblock(Thread& thread, time_t now_sec, long now_usec)
  154. {
  155. if (m_deadline.has_value()) {
  156. bool timed_out = now_sec > m_deadline.value().tv_sec || (now_sec == m_deadline.value().tv_sec && now_usec >= m_deadline.value().tv_usec);
  157. return timed_out || blocked_description().can_read();
  158. }
  159. return should_unblock(thread);
  160. }
  161. bool Thread::ReadBlocker::should_unblock(Thread&)
  162. {
  163. return blocked_description().can_read();
  164. }
  165. Thread::ConditionBlocker::ConditionBlocker(const char* state_string, Function<bool()>&& condition)
  166. : m_block_until_condition(move(condition))
  167. , m_state_string(state_string)
  168. {
  169. ASSERT(m_block_until_condition);
  170. }
  171. bool Thread::ConditionBlocker::should_unblock(Thread&)
  172. {
  173. return m_block_until_condition();
  174. }
  175. Thread::SleepBlocker::SleepBlocker(u64 wakeup_time)
  176. : m_wakeup_time(wakeup_time)
  177. {
  178. }
  179. bool Thread::SleepBlocker::should_unblock(Thread&)
  180. {
  181. return m_wakeup_time <= g_uptime;
  182. }
  183. Thread::SelectBlocker::SelectBlocker(const timespec& ts, bool select_has_timeout, const FDVector& read_fds, const FDVector& write_fds, const FDVector& except_fds)
  184. : m_select_timeout(ts)
  185. , m_select_has_timeout(select_has_timeout)
  186. , m_select_read_fds(read_fds)
  187. , m_select_write_fds(write_fds)
  188. , m_select_exceptional_fds(except_fds)
  189. {
  190. }
  191. bool Thread::SelectBlocker::should_unblock(Thread& thread, time_t now_sec, long now_usec)
  192. {
  193. if (m_select_has_timeout) {
  194. if (now_sec > m_select_timeout.tv_sec || (now_sec == m_select_timeout.tv_sec && now_usec * 1000 >= m_select_timeout.tv_nsec))
  195. return true;
  196. }
  197. return should_unblock(thread);
  198. }
  199. bool Thread::SelectBlocker::should_unblock(Thread& thread)
  200. {
  201. auto& process = thread.process();
  202. for (int fd : m_select_read_fds) {
  203. if (!process.m_fds[fd])
  204. continue;
  205. if (process.m_fds[fd].description()->can_read())
  206. return true;
  207. }
  208. for (int fd : m_select_write_fds) {
  209. if (!process.m_fds[fd])
  210. continue;
  211. if (process.m_fds[fd].description()->can_write())
  212. return true;
  213. }
  214. return false;
  215. }
  216. Thread::WaitBlocker::WaitBlocker(int wait_options, pid_t& waitee_pid)
  217. : m_wait_options(wait_options)
  218. , m_waitee_pid(waitee_pid)
  219. {
  220. }
  221. bool Thread::WaitBlocker::should_unblock(Thread& thread)
  222. {
  223. bool should_unblock = m_wait_options & WNOHANG;
  224. if (m_waitee_pid != -1) {
  225. auto peer = Process::from_pid(m_waitee_pid);
  226. if (!peer)
  227. return true;
  228. }
  229. thread.process().for_each_child([&](Process& child) {
  230. if (m_waitee_pid != -1 && m_waitee_pid != child.pid())
  231. return IterationDecision::Continue;
  232. bool child_exited = child.is_dead();
  233. bool child_stopped = false;
  234. if (child.thread_count()) {
  235. child.for_each_thread([&](auto& child_thread) {
  236. if (child_thread.state() == Thread::State::Stopped && !child_thread.has_pending_signal(SIGCONT)) {
  237. child_stopped = true;
  238. return IterationDecision::Break;
  239. }
  240. return IterationDecision::Continue;
  241. });
  242. }
  243. bool fits_the_spec = ((m_wait_options & WEXITED) && child_exited)
  244. || ((m_wait_options & WSTOPPED) && child_stopped);
  245. if (!fits_the_spec)
  246. return IterationDecision::Continue;
  247. m_waitee_pid = child.pid();
  248. should_unblock = true;
  249. return IterationDecision::Break;
  250. });
  251. return should_unblock;
  252. }
  253. Thread::SemiPermanentBlocker::SemiPermanentBlocker(Reason reason)
  254. : m_reason(reason)
  255. {
  256. }
  257. bool Thread::SemiPermanentBlocker::should_unblock(Thread&)
  258. {
  259. // someone else has to unblock us
  260. return false;
  261. }
  262. // Called by the scheduler on threads that are blocked for some reason.
  263. // Make a decision as to whether to unblock them or not.
  264. void Thread::consider_unblock(time_t now_sec, long now_usec)
  265. {
  266. ScopedSpinLock lock(m_lock);
  267. switch (state()) {
  268. case Thread::Invalid:
  269. case Thread::Runnable:
  270. case Thread::Running:
  271. case Thread::Dead:
  272. case Thread::Stopped:
  273. case Thread::Queued:
  274. case Thread::Dying:
  275. /* don't know, don't care */
  276. return;
  277. case Thread::Blocked:
  278. ASSERT(m_blocker != nullptr);
  279. if (m_blocker->should_unblock(*this, now_sec, now_usec))
  280. unblock();
  281. return;
  282. case Thread::Skip1SchedulerPass:
  283. set_state(Thread::Skip0SchedulerPasses);
  284. return;
  285. case Thread::Skip0SchedulerPasses:
  286. set_state(Thread::Runnable);
  287. return;
  288. }
  289. }
  290. void Scheduler::start()
  291. {
  292. ASSERT_INTERRUPTS_DISABLED();
  293. // We need to acquire our scheduler lock, which will be released
  294. // by the idle thread once control transferred there
  295. g_scheduler_lock.lock();
  296. auto& processor = Processor::current();
  297. processor.set_scheduler_data(*new SchedulerPerProcessorData());
  298. ASSERT(processor.is_initialized());
  299. auto& idle_thread = *processor.idle_thread();
  300. ASSERT(processor.current_thread() == &idle_thread);
  301. ASSERT(processor.idle_thread() == &idle_thread);
  302. idle_thread.set_ticks_left(time_slice_for(idle_thread));
  303. idle_thread.did_schedule();
  304. idle_thread.set_initialized(true);
  305. processor.init_context(idle_thread, false);
  306. idle_thread.set_state(Thread::Running);
  307. ASSERT(idle_thread.affinity() == (1u << processor.id()));
  308. processor.initialize_context_switching(idle_thread);
  309. ASSERT_NOT_REACHED();
  310. }
  311. bool Scheduler::pick_next()
  312. {
  313. ASSERT_INTERRUPTS_DISABLED();
  314. auto current_thread = Thread::current();
  315. auto now = time_since_boot();
  316. auto now_sec = now.tv_sec;
  317. auto now_usec = now.tv_usec;
  318. // Set the m_in_scheduler flag before acquiring the spinlock. This
  319. // prevents a recursive call into Scheduler::invoke_async upon
  320. // leaving the scheduler lock.
  321. ScopedCritical critical;
  322. Processor::current().get_scheduler_data().m_in_scheduler = true;
  323. ScopeGuard guard(
  324. []() {
  325. // We may be on a different processor after we got switched
  326. // back to this thread!
  327. auto& scheduler_data = Processor::current().get_scheduler_data();
  328. ASSERT(scheduler_data.m_in_scheduler);
  329. scheduler_data.m_in_scheduler = false;
  330. });
  331. ScopedSpinLock lock(g_scheduler_lock);
  332. // Check and unblock threads whose wait conditions have been met.
  333. Scheduler::for_each_nonrunnable([&](Thread& thread) {
  334. thread.consider_unblock(now_sec, now_usec);
  335. return IterationDecision::Continue;
  336. });
  337. Process::for_each([&](Process& process) {
  338. if (process.is_dead()) {
  339. if (current_thread->process().pid() != process.pid() && (!process.ppid() || !Process::from_pid(process.ppid()))) {
  340. auto name = process.name();
  341. auto pid = process.pid();
  342. auto exit_status = Process::reap(process);
  343. dbg() << "Scheduler[" << Processor::current().id() << "]: Reaped unparented process " << name << "(" << pid << "), exit status: " << exit_status.si_status;
  344. }
  345. return IterationDecision::Continue;
  346. }
  347. if (process.m_alarm_deadline && g_uptime > process.m_alarm_deadline) {
  348. process.m_alarm_deadline = 0;
  349. process.send_signal(SIGALRM, nullptr);
  350. }
  351. return IterationDecision::Continue;
  352. });
  353. // Dispatch any pending signals.
  354. Thread::for_each_living([&](Thread& thread) -> IterationDecision {
  355. ScopedSpinLock lock(thread.get_lock());
  356. if (!thread.has_unmasked_pending_signals())
  357. return IterationDecision::Continue;
  358. // NOTE: dispatch_one_pending_signal() may unblock the process.
  359. bool was_blocked = thread.is_blocked();
  360. if (thread.dispatch_one_pending_signal() == ShouldUnblockThread::No)
  361. return IterationDecision::Continue;
  362. if (was_blocked) {
  363. #ifdef SCHEDULER_DEBUG
  364. dbg() << "Scheduler[" << Processor::current().id() << "]:Unblock " << thread << " due to signal";
  365. #endif
  366. ASSERT(thread.m_blocker != nullptr);
  367. thread.m_blocker->set_interrupted_by_signal();
  368. thread.unblock();
  369. }
  370. return IterationDecision::Continue;
  371. });
  372. #ifdef SCHEDULER_RUNNABLE_DEBUG
  373. dbg() << "Non-runnables:";
  374. Scheduler::for_each_nonrunnable([](Thread& thread) -> IterationDecision {
  375. if (thread.state() == Thread::Queued)
  376. dbg() << " " << String::format("%-12s", thread.state_string()) << " " << thread << " @ " << String::format("%w", thread.tss().cs) << ":" << String::format("%x", thread.tss().eip) << " Reason: " << (thread.wait_reason() ? thread.wait_reason() : "none");
  377. else if (thread.state() == Thread::Dying)
  378. dbg() << " " << String::format("%-12s", thread.state_string()) << " " << thread << " @ " << String::format("%w", thread.tss().cs) << ":" << String::format("%x", thread.tss().eip) << " Finalizable: " << thread.is_finalizable();
  379. else
  380. dbg() << " " << String::format("%-12s", thread.state_string()) << " " << thread << " @ " << String::format("%w", thread.tss().cs) << ":" << String::format("%x", thread.tss().eip);
  381. return IterationDecision::Continue;
  382. });
  383. dbg() << "Runnables:";
  384. Scheduler::for_each_runnable([](Thread& thread) -> IterationDecision {
  385. dbg() << " " << String::format("%3u", thread.effective_priority()) << "/" << String::format("%2u", thread.priority()) << " " << String::format("%-12s", thread.state_string()) << " " << thread << " @ " << String::format("%w", thread.tss().cs) << ":" << String::format("%x", thread.tss().eip);
  386. return IterationDecision::Continue;
  387. });
  388. #endif
  389. Vector<Thread*, 128> sorted_runnables;
  390. for_each_runnable([&sorted_runnables](auto& thread) {
  391. if ((thread.affinity() & (1u << Processor::current().id())) != 0)
  392. sorted_runnables.append(&thread);
  393. return IterationDecision::Continue;
  394. });
  395. quick_sort(sorted_runnables, [](auto& a, auto& b) { return a->effective_priority() >= b->effective_priority(); });
  396. Thread* thread_to_schedule = nullptr;
  397. for (auto* thread : sorted_runnables) {
  398. if (thread->process().exec_tid() && thread->process().exec_tid() != thread->tid())
  399. continue;
  400. ASSERT(thread->state() == Thread::Runnable || thread->state() == Thread::Running);
  401. if (!thread_to_schedule) {
  402. thread->m_extra_priority = 0;
  403. thread_to_schedule = thread;
  404. } else {
  405. thread->m_extra_priority++;
  406. }
  407. }
  408. if (!thread_to_schedule)
  409. thread_to_schedule = Processor::current().idle_thread();
  410. #ifdef SCHEDULER_DEBUG
  411. dbg() << "Scheduler[" << Processor::current().id() << "]: Switch to " << *thread_to_schedule << " @ " << String::format("%04x:%08x", thread_to_schedule->tss().cs, thread_to_schedule->tss().eip);
  412. #endif
  413. // We need to leave our first critical section before switching context,
  414. // but since we're still holding the scheduler lock we're still in a critical section
  415. critical.leave();
  416. return context_switch(thread_to_schedule);
  417. }
  418. bool Scheduler::yield()
  419. {
  420. InterruptDisabler disabler;
  421. auto& proc = Processor::current();
  422. auto current_thread = Thread::current();
  423. #ifdef SCHEDULER_DEBUG
  424. dbg() << "Scheduler[" << proc.id() << "]: yielding thread " << *current_thread << " in_irq: " << proc.in_irq();
  425. #endif
  426. ASSERT(current_thread != nullptr);
  427. if (proc.in_irq() || proc.in_critical()) {
  428. // If we're handling an IRQ we can't switch context, or we're in
  429. // a critical section where we don't want to switch contexts, then
  430. // delay until exiting the trap or critical section
  431. proc.invoke_scheduler_async();
  432. return false;
  433. }
  434. if (!Scheduler::pick_next())
  435. return false;
  436. #ifdef SCHEDULER_DEBUG
  437. dbg() << "Scheduler[" << Processor::current().id() << "]: yield returns to thread " << *current_thread << " in_irq: " << Processor::current().in_irq();
  438. #endif
  439. return true;
  440. }
  441. bool Scheduler::donate_to(Thread* beneficiary, const char* reason)
  442. {
  443. ASSERT(beneficiary);
  444. // Set the m_in_scheduler flag before acquiring the spinlock. This
  445. // prevents a recursive call into Scheduler::invoke_async upon
  446. // leaving the scheduler lock.
  447. ScopedCritical critical;
  448. auto& proc = Processor::current();
  449. proc.get_scheduler_data().m_in_scheduler = true;
  450. ScopeGuard guard(
  451. []() {
  452. // We may be on a different processor after we got switched
  453. // back to this thread!
  454. auto& scheduler_data = Processor::current().get_scheduler_data();
  455. ASSERT(scheduler_data.m_in_scheduler);
  456. scheduler_data.m_in_scheduler = false;
  457. });
  458. ScopedSpinLock lock(g_scheduler_lock);
  459. ASSERT(!proc.in_irq());
  460. if (proc.in_critical()) {
  461. proc.invoke_scheduler_async();
  462. return false;
  463. }
  464. (void)reason;
  465. unsigned ticks_left = Thread::current()->ticks_left();
  466. if (!beneficiary || beneficiary->state() != Thread::Runnable || ticks_left <= 1)
  467. return Scheduler::yield();
  468. unsigned ticks_to_donate = min(ticks_left - 1, time_slice_for(*beneficiary));
  469. #ifdef SCHEDULER_DEBUG
  470. dbg() << "Scheduler[" << proc.id() << "]: Donating " << ticks_to_donate << " ticks to " << *beneficiary << ", reason=" << reason;
  471. #endif
  472. beneficiary->set_ticks_left(ticks_to_donate);
  473. Scheduler::context_switch(beneficiary);
  474. return false;
  475. }
  476. bool Scheduler::context_switch(Thread* thread)
  477. {
  478. thread->set_ticks_left(time_slice_for(*thread));
  479. thread->did_schedule();
  480. auto from_thread = Thread::current();
  481. if (from_thread == thread)
  482. return false;
  483. if (from_thread) {
  484. // If the last process hasn't blocked (still marked as running),
  485. // mark it as runnable for the next round.
  486. if (from_thread->state() == Thread::Running)
  487. from_thread->set_state(Thread::Runnable);
  488. #ifdef LOG_EVERY_CONTEXT_SWITCH
  489. dbg() << "Scheduler[" << Processor::current().id() << "]: " << *from_thread << " -> " << *thread << " [" << thread->priority() << "] " << String::format("%w", thread->tss().cs) << ":" << String::format("%x", thread->tss().eip);
  490. #endif
  491. }
  492. auto& proc = Processor::current();
  493. if (!thread->is_initialized()) {
  494. proc.init_context(*thread, false);
  495. thread->set_initialized(true);
  496. }
  497. thread->set_state(Thread::Running);
  498. // Mark it as active because we are using this thread. This is similar
  499. // to comparing it with Processor::current_thread, but when there are
  500. // multiple processors there's no easy way to check whether the thread
  501. // is actually still needed. This prevents accidental finalization when
  502. // a thread is no longer in Running state, but running on another core.
  503. thread->set_active(true);
  504. proc.switch_context(from_thread, thread);
  505. // NOTE: from_thread at this point reflects the thread we were
  506. // switched from, and thread reflects Thread::current()
  507. enter_current(*from_thread);
  508. ASSERT(thread == Thread::current());
  509. return true;
  510. }
  511. void Scheduler::enter_current(Thread& prev_thread)
  512. {
  513. ASSERT(g_scheduler_lock.is_locked());
  514. prev_thread.set_active(false);
  515. if (prev_thread.state() == Thread::Dying) {
  516. // If the thread we switched from is marked as dying, then notify
  517. // the finalizer. Note that as soon as we leave the scheduler lock
  518. // the finalizer may free from_thread!
  519. notify_finalizer();
  520. }
  521. }
  522. void Scheduler::leave_on_first_switch(u32 flags)
  523. {
  524. // This is called when a thread is swiched into for the first time.
  525. // At this point, enter_current has already be called, but because
  526. // Scheduler::context_switch is not in the call stack we need to
  527. // clean up and release locks manually here
  528. g_scheduler_lock.unlock(flags);
  529. auto& scheduler_data = Processor::current().get_scheduler_data();
  530. ASSERT(scheduler_data.m_in_scheduler);
  531. scheduler_data.m_in_scheduler = false;
  532. }
  533. void Scheduler::prepare_after_exec()
  534. {
  535. // This is called after exec() when doing a context "switch" into
  536. // the new process. This is called from Processor::assume_context
  537. ASSERT(g_scheduler_lock.own_lock());
  538. auto& scheduler_data = Processor::current().get_scheduler_data();
  539. ASSERT(!scheduler_data.m_in_scheduler);
  540. scheduler_data.m_in_scheduler = true;
  541. }
  542. void Scheduler::prepare_for_idle_loop()
  543. {
  544. // This is called when the CPU finished setting up the idle loop
  545. // and is about to run it. We need to acquire he scheduler lock
  546. ASSERT(!g_scheduler_lock.own_lock());
  547. g_scheduler_lock.lock();
  548. auto& scheduler_data = Processor::current().get_scheduler_data();
  549. ASSERT(!scheduler_data.m_in_scheduler);
  550. scheduler_data.m_in_scheduler = true;
  551. }
  552. Process* Scheduler::colonel()
  553. {
  554. ASSERT(s_colonel_process);
  555. return s_colonel_process;
  556. }
  557. void Scheduler::initialize()
  558. {
  559. ASSERT(&Processor::current() != nullptr); // sanity check
  560. Thread* idle_thread = nullptr;
  561. g_scheduler_data = new SchedulerData;
  562. g_finalizer_wait_queue = new WaitQueue;
  563. g_finalizer_has_work.store(false, AK::MemoryOrder::memory_order_release);
  564. s_colonel_process = &Process::create_kernel_process(idle_thread, "colonel", idle_loop, 1).leak_ref();
  565. ASSERT(s_colonel_process);
  566. ASSERT(idle_thread);
  567. idle_thread->set_priority(THREAD_PRIORITY_MIN);
  568. idle_thread->set_name("idle thread #0");
  569. set_idle_thread(idle_thread);
  570. }
  571. void Scheduler::set_idle_thread(Thread* idle_thread)
  572. {
  573. Processor::current().set_idle_thread(*idle_thread);
  574. Processor::current().set_current_thread(*idle_thread);
  575. }
  576. Thread* Scheduler::create_ap_idle_thread(u32 cpu)
  577. {
  578. ASSERT(cpu != 0);
  579. // This function is called on the bsp, but creates an idle thread for another AP
  580. ASSERT(Processor::current().id() == 0);
  581. ASSERT(s_colonel_process);
  582. Thread* idle_thread = s_colonel_process->create_kernel_thread(idle_loop, THREAD_PRIORITY_MIN, String::format("idle thread #%u", cpu), 1 << cpu, false);
  583. ASSERT(idle_thread);
  584. return idle_thread;
  585. }
  586. void Scheduler::timer_tick(const RegisterState& regs)
  587. {
  588. ASSERT_INTERRUPTS_DISABLED();
  589. ASSERT(Processor::current().in_irq());
  590. if (Processor::current().id() > 0)
  591. return;
  592. auto current_thread = Processor::current().current_thread();
  593. if (!current_thread)
  594. return;
  595. ++g_uptime;
  596. g_timeofday = TimeManagement::now_as_timeval();
  597. if (current_thread->process().is_profiling()) {
  598. SmapDisabler disabler;
  599. auto backtrace = current_thread->raw_backtrace(regs.ebp, regs.eip);
  600. auto& sample = Profiling::next_sample_slot();
  601. sample.pid = current_thread->process().pid();
  602. sample.tid = current_thread->tid();
  603. sample.timestamp = g_uptime;
  604. for (size_t i = 0; i < min(backtrace.size(), Profiling::max_stack_frame_count); ++i) {
  605. sample.frames[i] = backtrace[i];
  606. }
  607. }
  608. TimerQueue::the().fire();
  609. if (current_thread->tick())
  610. return;
  611. ASSERT_INTERRUPTS_DISABLED();
  612. ASSERT(Processor::current().in_irq());
  613. Processor::current().invoke_scheduler_async();
  614. }
  615. void Scheduler::invoke_async()
  616. {
  617. ASSERT_INTERRUPTS_DISABLED();
  618. auto& proc = Processor::current();
  619. ASSERT(!proc.in_irq());
  620. // Since this function is called when leaving critical sections (such
  621. // as a SpinLock), we need to check if we're not already doing this
  622. // to prevent recursion
  623. if (!proc.get_scheduler_data().m_in_scheduler)
  624. pick_next();
  625. }
  626. void Scheduler::notify_finalizer()
  627. {
  628. if (g_finalizer_has_work.exchange(true, AK::MemoryOrder::memory_order_acq_rel) == false)
  629. g_finalizer_wait_queue->wake_all();
  630. }
  631. void Scheduler::idle_loop()
  632. {
  633. dbg() << "Scheduler[" << Processor::current().id() << "]: idle loop running";
  634. ASSERT(are_interrupts_enabled());
  635. for (;;) {
  636. asm("hlt");
  637. if (Processor::current().id() == 0)
  638. yield();
  639. }
  640. }
  641. }