Scheduler.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. #include <AK/TemporaryChange.h>
  2. #include <Kernel/Arch/i386/PIT.h>
  3. #include <Kernel/FileSystem/FileDescription.h>
  4. #include <Kernel/Process.h>
  5. #include <Kernel/Profiling.h>
  6. #include <Kernel/RTC.h>
  7. #include <Kernel/Scheduler.h>
  8. //#define LOG_EVERY_CONTEXT_SWITCH
  9. //#define SCHEDULER_DEBUG
  10. //#define SCHEDULER_RUNNABLE_DEBUG
  11. SchedulerData* g_scheduler_data;
  12. void Scheduler::init_thread(Thread& thread)
  13. {
  14. g_scheduler_data->m_nonrunnable_threads.append(thread);
  15. }
  16. void Scheduler::update_state_for_thread(Thread& thread)
  17. {
  18. ASSERT_INTERRUPTS_DISABLED();
  19. auto& list = g_scheduler_data->thread_list_for_state_and_priority(thread.state(), thread.priority());
  20. if (list.contains(thread))
  21. return;
  22. list.append(thread);
  23. }
  24. template<typename Callback>
  25. static inline IterationDecision for_each_runnable_with_priority(ThreadPriority priority, Callback callback)
  26. {
  27. ASSERT_INTERRUPTS_DISABLED();
  28. auto& tl = g_scheduler_data->m_runnable_threads[(u8)priority - (u8)ThreadPriority::First];
  29. for (auto it = tl.begin(); it != tl.end();) {
  30. auto& thread = *it;
  31. it = ++it;
  32. if (callback(thread) == IterationDecision::Break)
  33. return IterationDecision::Break;
  34. }
  35. return IterationDecision::Continue;
  36. }
  37. static u32 time_slice_for(ThreadPriority priority)
  38. {
  39. // One time slice unit == 1ms
  40. switch (priority) {
  41. case ThreadPriority::High:
  42. return 20;
  43. case ThreadPriority::Normal:
  44. return 15;
  45. case ThreadPriority::Low:
  46. return 5;
  47. case ThreadPriority::Idle:
  48. return 1;
  49. }
  50. ASSERT_NOT_REACHED();
  51. }
  52. Thread* current;
  53. Thread* g_last_fpu_thread;
  54. Thread* g_finalizer;
  55. Thread* g_colonel;
  56. WaitQueue* g_finalizer_wait_queue;
  57. static Process* s_colonel_process;
  58. u64 g_uptime;
  59. struct TaskRedirectionData {
  60. u16 selector;
  61. TSS32 tss;
  62. };
  63. static TaskRedirectionData s_redirection;
  64. static bool s_active;
  65. bool Scheduler::is_active()
  66. {
  67. return s_active;
  68. }
  69. Thread::JoinBlocker::JoinBlocker(Thread& joinee, void*& joinee_exit_value)
  70. : m_joinee(joinee)
  71. , m_joinee_exit_value(joinee_exit_value)
  72. {
  73. ASSERT(m_joinee.m_joiner == nullptr);
  74. m_joinee.m_joiner = current;
  75. current->m_joinee = &joinee;
  76. }
  77. bool Thread::JoinBlocker::should_unblock(Thread& joiner, time_t, long)
  78. {
  79. return !joiner.m_joinee;
  80. }
  81. Thread::FileDescriptionBlocker::FileDescriptionBlocker(const FileDescription& description)
  82. : m_blocked_description(description)
  83. {
  84. }
  85. const FileDescription& Thread::FileDescriptionBlocker::blocked_description() const
  86. {
  87. return m_blocked_description;
  88. }
  89. Thread::AcceptBlocker::AcceptBlocker(const FileDescription& description)
  90. : FileDescriptionBlocker(description)
  91. {
  92. }
  93. bool Thread::AcceptBlocker::should_unblock(Thread&, time_t, long)
  94. {
  95. auto& socket = *blocked_description().socket();
  96. return socket.can_accept();
  97. }
  98. Thread::ReceiveBlocker::ReceiveBlocker(const FileDescription& description)
  99. : FileDescriptionBlocker(description)
  100. {
  101. }
  102. bool Thread::ReceiveBlocker::should_unblock(Thread&, time_t now_sec, long now_usec)
  103. {
  104. auto& socket = *blocked_description().socket();
  105. // FIXME: Block until the amount of data wanted is available.
  106. bool timed_out = now_sec > socket.receive_deadline().tv_sec || (now_sec == socket.receive_deadline().tv_sec && now_usec >= socket.receive_deadline().tv_usec);
  107. if (timed_out || blocked_description().can_read())
  108. return true;
  109. return false;
  110. }
  111. Thread::ConnectBlocker::ConnectBlocker(const FileDescription& description)
  112. : FileDescriptionBlocker(description)
  113. {
  114. }
  115. bool Thread::ConnectBlocker::should_unblock(Thread&, time_t, long)
  116. {
  117. auto& socket = *blocked_description().socket();
  118. return socket.setup_state() == Socket::SetupState::Completed;
  119. }
  120. Thread::WriteBlocker::WriteBlocker(const FileDescription& description)
  121. : FileDescriptionBlocker(description)
  122. {
  123. }
  124. bool Thread::WriteBlocker::should_unblock(Thread&, time_t, long)
  125. {
  126. return blocked_description().can_write();
  127. }
  128. Thread::ReadBlocker::ReadBlocker(const FileDescription& description)
  129. : FileDescriptionBlocker(description)
  130. {
  131. }
  132. bool Thread::ReadBlocker::should_unblock(Thread&, time_t, long)
  133. {
  134. // FIXME: Block until the amount of data wanted is available.
  135. return blocked_description().can_read();
  136. }
  137. Thread::ConditionBlocker::ConditionBlocker(const char* state_string, Function<bool()>&& condition)
  138. : m_block_until_condition(move(condition))
  139. , m_state_string(state_string)
  140. {
  141. ASSERT(m_block_until_condition);
  142. }
  143. bool Thread::ConditionBlocker::should_unblock(Thread&, time_t, long)
  144. {
  145. return m_block_until_condition();
  146. }
  147. Thread::SleepBlocker::SleepBlocker(u64 wakeup_time)
  148. : m_wakeup_time(wakeup_time)
  149. {
  150. }
  151. bool Thread::SleepBlocker::should_unblock(Thread&, time_t, long)
  152. {
  153. return m_wakeup_time <= g_uptime;
  154. }
  155. Thread::SelectBlocker::SelectBlocker(const timeval& tv, bool select_has_timeout, const FDVector& read_fds, const FDVector& write_fds, const FDVector& except_fds)
  156. : m_select_timeout(tv)
  157. , m_select_has_timeout(select_has_timeout)
  158. , m_select_read_fds(read_fds)
  159. , m_select_write_fds(write_fds)
  160. , m_select_exceptional_fds(except_fds)
  161. {
  162. }
  163. bool Thread::SelectBlocker::should_unblock(Thread& thread, time_t now_sec, long now_usec)
  164. {
  165. if (m_select_has_timeout) {
  166. if (now_sec > m_select_timeout.tv_sec || (now_sec == m_select_timeout.tv_sec && now_usec >= m_select_timeout.tv_usec))
  167. return true;
  168. }
  169. auto& process = thread.process();
  170. for (int fd : m_select_read_fds) {
  171. if (process.m_fds[fd].description->can_read())
  172. return true;
  173. }
  174. for (int fd : m_select_write_fds) {
  175. if (process.m_fds[fd].description->can_write())
  176. return true;
  177. }
  178. return false;
  179. }
  180. Thread::WaitBlocker::WaitBlocker(int wait_options, pid_t& waitee_pid)
  181. : m_wait_options(wait_options)
  182. , m_waitee_pid(waitee_pid)
  183. {
  184. }
  185. bool Thread::WaitBlocker::should_unblock(Thread& thread, time_t, long)
  186. {
  187. bool should_unblock = false;
  188. if (m_waitee_pid != -1) {
  189. auto* peer = Process::from_pid(m_waitee_pid);
  190. if (!peer)
  191. return true;
  192. }
  193. thread.process().for_each_child([&](Process& child) {
  194. if (m_waitee_pid != -1 && m_waitee_pid != child.pid())
  195. return IterationDecision::Continue;
  196. bool child_exited = child.is_dead();
  197. bool child_stopped = child.thread_count() && child.any_thread().state() == Thread::State::Stopped;
  198. bool wait_finished = ((m_wait_options & WEXITED) && child_exited)
  199. || ((m_wait_options & WSTOPPED) && child_stopped);
  200. if (!wait_finished)
  201. return IterationDecision::Continue;
  202. m_waitee_pid = child.pid();
  203. should_unblock = true;
  204. return IterationDecision::Break;
  205. });
  206. return should_unblock;
  207. }
  208. Thread::SemiPermanentBlocker::SemiPermanentBlocker(Reason reason)
  209. : m_reason(reason)
  210. {
  211. }
  212. bool Thread::SemiPermanentBlocker::should_unblock(Thread&, time_t, long)
  213. {
  214. // someone else has to unblock us
  215. return false;
  216. }
  217. // Called by the scheduler on threads that are blocked for some reason.
  218. // Make a decision as to whether to unblock them or not.
  219. void Thread::consider_unblock(time_t now_sec, long now_usec)
  220. {
  221. switch (state()) {
  222. case Thread::Invalid:
  223. case Thread::Runnable:
  224. case Thread::Running:
  225. case Thread::Dead:
  226. case Thread::Stopped:
  227. case Thread::Queued:
  228. case Thread::Dying:
  229. /* don't know, don't care */
  230. return;
  231. case Thread::Blocked:
  232. ASSERT(m_blocker != nullptr);
  233. if (m_blocker->should_unblock(*this, now_sec, now_usec))
  234. unblock();
  235. return;
  236. case Thread::Skip1SchedulerPass:
  237. set_state(Thread::Skip0SchedulerPasses);
  238. return;
  239. case Thread::Skip0SchedulerPasses:
  240. set_state(Thread::Runnable);
  241. return;
  242. }
  243. }
  244. bool Scheduler::pick_next()
  245. {
  246. ASSERT_INTERRUPTS_DISABLED();
  247. ASSERT(!s_active);
  248. TemporaryChange<bool> change(s_active, true);
  249. ASSERT(s_active);
  250. if (!current) {
  251. // XXX: The first ever context_switch() goes to the idle process.
  252. // This to setup a reliable place we can return to.
  253. return context_switch(*g_colonel);
  254. }
  255. struct timeval now;
  256. kgettimeofday(now);
  257. auto now_sec = now.tv_sec;
  258. auto now_usec = now.tv_usec;
  259. // Check and unblock threads whose wait conditions have been met.
  260. Scheduler::for_each_nonrunnable([&](Thread& thread) {
  261. thread.consider_unblock(now_sec, now_usec);
  262. return IterationDecision::Continue;
  263. });
  264. Process::for_each([&](Process& process) {
  265. if (process.is_dead()) {
  266. if (current->pid() != process.pid() && (!process.ppid() || !Process::from_pid(process.ppid()))) {
  267. auto name = process.name();
  268. auto pid = process.pid();
  269. auto exit_status = Process::reap(process);
  270. dbgprintf("reaped unparented process %s(%u), exit status: %u\n", name.characters(), pid, exit_status);
  271. }
  272. return IterationDecision::Continue;
  273. }
  274. if (process.m_alarm_deadline && g_uptime > process.m_alarm_deadline) {
  275. process.m_alarm_deadline = 0;
  276. process.send_signal(SIGALRM, nullptr);
  277. }
  278. return IterationDecision::Continue;
  279. });
  280. // Dispatch any pending signals.
  281. // FIXME: Do we really need this to be a separate pass over the process list?
  282. Thread::for_each_living([](Thread& thread) -> IterationDecision {
  283. if (!thread.has_unmasked_pending_signals())
  284. return IterationDecision::Continue;
  285. // FIXME: It would be nice if the Scheduler didn't have to worry about who is "current"
  286. // For now, avoid dispatching signals to "current" and do it in a scheduling pass
  287. // while some other process is interrupted. Otherwise a mess will be made.
  288. if (&thread == current)
  289. return IterationDecision::Continue;
  290. // We know how to interrupt blocked processes, but if they are just executing
  291. // at some random point in the kernel, let them continue. They'll be in userspace
  292. // sooner or later and we can deliver the signal then.
  293. // FIXME: Maybe we could check when returning from a syscall if there's a pending
  294. // signal and dispatch it then and there? Would that be doable without the
  295. // syscall effectively being "interrupted" despite having completed?
  296. if (thread.in_kernel() && !thread.is_blocked() && !thread.is_stopped())
  297. return IterationDecision::Continue;
  298. // NOTE: dispatch_one_pending_signal() may unblock the process.
  299. bool was_blocked = thread.is_blocked();
  300. if (thread.dispatch_one_pending_signal() == ShouldUnblockThread::No)
  301. return IterationDecision::Continue;
  302. if (was_blocked) {
  303. dbgprintf("Unblock %s(%u) due to signal\n", thread.process().name().characters(), thread.pid());
  304. ASSERT(thread.m_blocker != nullptr);
  305. thread.m_blocker->set_interrupted_by_signal();
  306. thread.unblock();
  307. }
  308. return IterationDecision::Continue;
  309. });
  310. #ifdef SCHEDULER_RUNNABLE_DEBUG
  311. dbgprintf("Non-runnables:\n");
  312. Scheduler::for_each_nonrunnable([](Thread& thread) -> IterationDecision {
  313. auto& process = thread.process();
  314. dbgprintf("[K%x] %-12s %s(%u:%u) @ %w:%x\n", &process, thread.state_string(), process.name().characters(), process.pid(), thread.tid(), thread.tss().cs, thread.tss().eip);
  315. return IterationDecision::Continue;
  316. });
  317. for (u8 priority = (u8)ThreadPriority::Last; priority >= (u8)ThreadPriority::First; --priority) {
  318. dbgprintf("Runnables (%s):\n", to_string((ThreadPriority)priority));
  319. for_each_runnable_with_priority((ThreadPriority)priority, [](Thread& thread) -> IterationDecision {
  320. auto& process = thread.process();
  321. dbgprintf("[K%x] %-12s %s(%u:%u) @ %w:%x\n", &process, thread.state_string(), process.name().characters(), process.pid(), thread.tid(), thread.tss().cs, thread.tss().eip);
  322. return IterationDecision::Continue;
  323. });
  324. }
  325. #endif
  326. for (u8 priority = (u8)ThreadPriority::Last; priority >= (u8)ThreadPriority::First; --priority) {
  327. auto& runnable_list = g_scheduler_data->m_runnable_threads[priority - (u8)ThreadPriority::First];
  328. if (runnable_list.is_empty())
  329. continue;
  330. auto* previous_head = runnable_list.first();
  331. for (;;) {
  332. // Move head to tail.
  333. runnable_list.append(*runnable_list.first());
  334. auto* thread = runnable_list.first();
  335. if (!thread->process().is_being_inspected() && (thread->state() == Thread::Runnable || thread->state() == Thread::Running)) {
  336. #ifdef SCHEDULER_DEBUG
  337. dbgprintf("switch to %s(%u:%u) @ %w:%x\n", thread->process().name().characters(), thread->process().pid(), thread->tid(), thread->tss().cs, thread->tss().eip);
  338. #endif
  339. return context_switch(*thread);
  340. }
  341. if (thread == previous_head)
  342. break;
  343. }
  344. }
  345. // Nothing wants to run. Send in the colonel!
  346. return context_switch(*g_colonel);
  347. }
  348. bool Scheduler::donate_to(Thread* beneficiary, const char* reason)
  349. {
  350. InterruptDisabler disabler;
  351. if (!Thread::is_thread(beneficiary))
  352. return false;
  353. (void)reason;
  354. unsigned ticks_left = current->ticks_left();
  355. if (!beneficiary || beneficiary->state() != Thread::Runnable || ticks_left <= 1)
  356. return yield();
  357. unsigned ticks_to_donate = min(ticks_left - 1, time_slice_for(beneficiary->priority()));
  358. #ifdef SCHEDULER_DEBUG
  359. dbgprintf("%s(%u:%u) donating %u ticks to %s(%u:%u), reason=%s\n", current->process().name().characters(), current->pid(), current->tid(), ticks_to_donate, beneficiary->process().name().characters(), beneficiary->pid(), beneficiary->tid(), reason);
  360. #endif
  361. context_switch(*beneficiary);
  362. beneficiary->set_ticks_left(ticks_to_donate);
  363. switch_now();
  364. return false;
  365. }
  366. bool Scheduler::yield()
  367. {
  368. InterruptDisabler disabler;
  369. ASSERT(current);
  370. // dbgprintf("%s(%u:%u) yield()\n", current->process().name().characters(), current->pid(), current->tid());
  371. if (!pick_next())
  372. return false;
  373. // dbgprintf("yield() jumping to new process: sel=%x, %s(%u:%u)\n", current->far_ptr().selector, current->process().name().characters(), current->pid(), current->tid());
  374. switch_now();
  375. return true;
  376. }
  377. void Scheduler::pick_next_and_switch_now()
  378. {
  379. bool someone_wants_to_run = pick_next();
  380. ASSERT(someone_wants_to_run);
  381. switch_now();
  382. }
  383. void Scheduler::switch_now()
  384. {
  385. Descriptor& descriptor = get_gdt_entry(current->selector());
  386. descriptor.type = 9;
  387. flush_gdt();
  388. asm("sti\n"
  389. "ljmp *(%%eax)\n" ::"a"(&current->far_ptr()));
  390. }
  391. bool Scheduler::context_switch(Thread& thread)
  392. {
  393. thread.set_ticks_left(time_slice_for(thread.priority()));
  394. thread.did_schedule();
  395. if (current == &thread)
  396. return false;
  397. if (current) {
  398. // If the last process hasn't blocked (still marked as running),
  399. // mark it as runnable for the next round.
  400. if (current->state() == Thread::Running)
  401. current->set_state(Thread::Runnable);
  402. #ifdef LOG_EVERY_CONTEXT_SWITCH
  403. dbgprintf("Scheduler: %s(%u:%u) -> %s(%u:%u) [%s] %w:%x\n",
  404. current->process().name().characters(), current->process().pid(), current->tid(),
  405. thread.process().name().characters(), thread.process().pid(), thread.tid(),
  406. to_string(thread.priority()),
  407. thread.tss().cs, thread.tss().eip);
  408. #endif
  409. }
  410. current = &thread;
  411. thread.set_state(Thread::Running);
  412. if (!thread.selector()) {
  413. thread.set_selector(gdt_alloc_entry());
  414. auto& descriptor = get_gdt_entry(thread.selector());
  415. descriptor.set_base(&thread.tss());
  416. descriptor.set_limit(0xffff);
  417. descriptor.dpl = 0;
  418. descriptor.segment_present = 1;
  419. descriptor.granularity = 1;
  420. descriptor.zero = 0;
  421. descriptor.operation_size = 1;
  422. descriptor.descriptor_type = 0;
  423. }
  424. if (!thread.thread_specific_data().is_null()) {
  425. auto& descriptor = thread_specific_descriptor();
  426. descriptor.set_base(thread.thread_specific_data().as_ptr());
  427. descriptor.set_limit(sizeof(ThreadSpecificData*));
  428. }
  429. auto& descriptor = get_gdt_entry(thread.selector());
  430. descriptor.type = 11; // Busy TSS
  431. flush_gdt();
  432. return true;
  433. }
  434. static void initialize_redirection()
  435. {
  436. auto& descriptor = get_gdt_entry(s_redirection.selector);
  437. descriptor.set_base(&s_redirection.tss);
  438. descriptor.set_limit(0xffff);
  439. descriptor.dpl = 0;
  440. descriptor.segment_present = 1;
  441. descriptor.granularity = 1;
  442. descriptor.zero = 0;
  443. descriptor.operation_size = 1;
  444. descriptor.descriptor_type = 0;
  445. descriptor.type = 9;
  446. flush_gdt();
  447. }
  448. void Scheduler::prepare_for_iret_to_new_process()
  449. {
  450. auto& descriptor = get_gdt_entry(s_redirection.selector);
  451. descriptor.type = 9;
  452. s_redirection.tss.backlink = current->selector();
  453. load_task_register(s_redirection.selector);
  454. }
  455. void Scheduler::prepare_to_modify_tss(Thread& thread)
  456. {
  457. // This ensures that a currently running process modifying its own TSS
  458. // in order to yield() and end up somewhere else doesn't just end up
  459. // right after the yield().
  460. if (current == &thread)
  461. load_task_register(s_redirection.selector);
  462. }
  463. Process* Scheduler::colonel()
  464. {
  465. return s_colonel_process;
  466. }
  467. void Scheduler::initialize()
  468. {
  469. g_scheduler_data = new SchedulerData;
  470. g_finalizer_wait_queue = new WaitQueue;
  471. s_redirection.selector = gdt_alloc_entry();
  472. initialize_redirection();
  473. s_colonel_process = Process::create_kernel_process(g_colonel, "colonel", nullptr);
  474. // Make sure the colonel uses a smallish time slice.
  475. g_colonel->set_priority(ThreadPriority::Idle);
  476. load_task_register(s_redirection.selector);
  477. }
  478. void Scheduler::timer_tick(RegisterDump& regs)
  479. {
  480. if (!current)
  481. return;
  482. ++g_uptime;
  483. timeval tv;
  484. tv.tv_sec = RTC::boot_time() + PIT::seconds_since_boot();
  485. tv.tv_usec = PIT::ticks_this_second() * 1000;
  486. Process::update_info_page_timestamp(tv);
  487. if (current->process().is_profiling()) {
  488. auto backtrace = current->raw_backtrace(regs.ebp);
  489. auto& sample = Profiling::next_sample_slot();
  490. sample.pid = current->pid();
  491. sample.tid = current->tid();
  492. sample.timestamp = g_uptime;
  493. for (size_t i = 0; i < min((size_t)backtrace.size(), Profiling::max_stack_frame_count); ++i) {
  494. sample.frames[i] = backtrace[i];
  495. }
  496. }
  497. if (current->tick())
  498. return;
  499. auto& outgoing_tss = current->tss();
  500. if (!pick_next())
  501. return;
  502. outgoing_tss.gs = regs.gs;
  503. outgoing_tss.fs = regs.fs;
  504. outgoing_tss.es = regs.es;
  505. outgoing_tss.ds = regs.ds;
  506. outgoing_tss.edi = regs.edi;
  507. outgoing_tss.esi = regs.esi;
  508. outgoing_tss.ebp = regs.ebp;
  509. outgoing_tss.ebx = regs.ebx;
  510. outgoing_tss.edx = regs.edx;
  511. outgoing_tss.ecx = regs.ecx;
  512. outgoing_tss.eax = regs.eax;
  513. outgoing_tss.eip = regs.eip;
  514. outgoing_tss.cs = regs.cs;
  515. outgoing_tss.eflags = regs.eflags;
  516. // Compute process stack pointer.
  517. // Add 16 for CS, EIP, EFLAGS, exception code (interrupt mechanic)
  518. outgoing_tss.esp = regs.esp + 16;
  519. outgoing_tss.ss = regs.ss;
  520. if ((outgoing_tss.cs & 3) != 0) {
  521. outgoing_tss.ss = regs.ss_if_crossRing;
  522. outgoing_tss.esp = regs.esp_if_crossRing;
  523. }
  524. prepare_for_iret_to_new_process();
  525. // Set the NT (nested task) flag.
  526. asm(
  527. "pushf\n"
  528. "orl $0x00004000, (%esp)\n"
  529. "popf\n");
  530. }
  531. static bool s_should_stop_idling = false;
  532. void Scheduler::stop_idling()
  533. {
  534. if (current != g_colonel)
  535. return;
  536. s_should_stop_idling = true;
  537. }
  538. void Scheduler::idle_loop()
  539. {
  540. for (;;) {
  541. asm("hlt");
  542. if (s_should_stop_idling) {
  543. s_should_stop_idling = false;
  544. yield();
  545. }
  546. }
  547. }