Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::processes::Scheduler Class Reference

Schedules processes to run on the core via their threads. More...

#include <scheduler.h>

Public Member Functions

 Scheduler ()
 Constructs a new Scheduler object and creates the idle process.
 
system::cpu_status_tschedule (system::cpu_status_t *cpu_state)
 Schedules the next thread to run.
 
system::cpu_status_tschedule_next (system::cpu_status_t *status)
 Schedules the next thread to run.
 
system::cpu_status_tyield ()
 Pass execution to the next thread.
 
uint64_t add_process (Process *process)
 Adds a process to the scheduler.
 
uint64_t remove_process (Process *process)
 Removes a process from the scheduler if the process has no threads, if it does then the threads are stopped but the process is not removed (this will be done automatically when all threads are stopped)
 
system::cpu_status_tforce_remove_process (Process *process)
 Removes a process from the scheduler and deletes all threads, begins running the next process.
 
uint64_t add_thread (Thread *thread)
 Adds a thread to the scheduler.
 
Processcurrent_process ()
 Gets the current process.
 
Processget_process (uint64_t pid)
 Gets a process by its PID.
 
uint64_t process_amount ()
 Gets how processes are running on this scheduler.
 
Threadcurrent_thread ()
 Gets the currently executing thread.
 
Threadget_thread (uint64_t tid)
 Gets a thread by its TID.
 
uint64_t thread_amount ()
 Gets how many threads are running on this scheduler.
 
uint64_t ticks () const
 Gets how long the system has been running for.
 
void activate ()
 Activates the scheduler.
 
void deactivate ()
 Deactivates the scheduler.
 

Detailed Description

Schedules processes to run on the core via their threads.

Definition at line 85 of file scheduler.h.

Constructor & Destructor Documentation

◆ Scheduler()

Scheduler::Scheduler ( )

Constructs a new Scheduler object and creates the idle process.

Definition at line 365 of file scheduler.cpp.

366: m_next_thread_index(0),
367 m_active(false),
368 m_ticks(0)
369{
370
371 // Create this idle process
372 auto* idle = new Process("Idle", nullptr, nullptr, 0, true);
373 idle->memory_manager = MemoryManager::s_kernel_memory_manager;
374 idle->set_pid(0);
375 add_process(idle);
376}
static MemoryManager * s_kernel_memory_manager
The memory manager for any kernel processes and all kernel allocations.
A process that can be scheduled by the Scheduler, wraps & manages threads as well as its own address ...
Definition process.h:85
uint64_t add_process(Process *process)
Adds a process to the scheduler.

References add_process(), and MaxOS::memory::MemoryManager::s_kernel_memory_manager.

Member Function Documentation

◆ activate()

void Scheduler::activate ( )

Activates the scheduler.

Definition at line 573 of file scheduler.cpp.

573 {
574
575 m_active = true;
576}

◆ add_process()

uint64_t Scheduler::add_process ( Process process)

Adds a process to the scheduler.

Parameters
processThe process to add
Returns
The process ID (will be assigned during add)

Definition at line 505 of file scheduler.cpp.

505 {
506
507 // Get the next process ID
508 auto pid = GlobalScheduler::next_pid();
509 process->set_pid(pid);
510
511 // Add the process to the list
512 m_processes.push_back(process);
513
514 // Add the threads to the list
515 for(const auto& thread : process->threads())
516 thread->tid = add_thread(thread);
517
518 return pid;
519}
static uint64_t next_pid()
Gets the next usable global PID.
void set_pid(uint64_t pid)
Sets the pid of the process once added to the queue.
Definition process.cpp:296
uint64_t add_thread(Thread *thread)
Adds a thread to the scheduler.

References add_thread(), MaxOS::processes::GlobalScheduler::next_pid(), MaxOS::processes::Process::set_pid(), MaxOS::processes::Process::threads(), and MaxOS::processes::Thread::tid.

Referenced by MaxOS::processes::GlobalScheduler::add_process(), and Scheduler().

◆ add_thread()

uint64_t Scheduler::add_thread ( Thread thread)

Adds a thread to the scheduler.

Parameters
threadThe thread to add
Returns
The thread ID

Definition at line 527 of file scheduler.cpp.

527 {
528
529 // Get the next thread ID
530 auto tid = GlobalScheduler::next_tid();
531 thread->tid = tid;
532
533 // Add the thread to the list
534 m_threads.push_back(thread);
535
536 // Return the thread ID
537 return tid;
538}
static uint64_t next_tid()
Gets the next usable global TID.
uint64_t tid
The thread ID.
Definition process.h:64

References MaxOS::processes::GlobalScheduler::next_tid(), and MaxOS::processes::Thread::tid.

Referenced by add_process(), and MaxOS::processes::GlobalScheduler::add_thread().

◆ current_process()

Process * Scheduler::current_process ( )

Gets the current process.

Returns
The current process, or nullptr if not found

Definition at line 647 of file scheduler.cpp.

647 {
648
649 Process* current_process = nullptr;
650
651 // Find the process that has the thread being executed
652 for (auto process: m_processes)
653 if (process->pid() == current_thread()->parent_pid) {
654 current_process = process;
655 break;
656 }
657
658 return current_process;
659}
Thread * current_thread()
Gets the currently executing thread.
Process * current_process()
Gets the current process.

References current_process(), current_thread(), MaxOS::processes::Thread::parent_pid, and MaxOS::processes::Process::pid().

Referenced by MaxOS::processes::GlobalScheduler::current_process(), current_process(), and schedule_next().

◆ current_thread()

Thread * Scheduler::current_thread ( )

Gets the currently executing thread.

Returns
The currently executing thread

Definition at line 691 of file scheduler.cpp.

691 {
692
693 return m_threads[m_next_thread_index];
694}

Referenced by current_process(), MaxOS::processes::GlobalScheduler::current_thread(), force_remove_process(), schedule(), schedule_next(), and yield().

◆ deactivate()

void Scheduler::deactivate ( )

Deactivates the scheduler.

Definition at line 707 of file scheduler.cpp.

707 {
708
709 m_active = false;
710}

◆ force_remove_process()

cpu_status_t * Scheduler::force_remove_process ( Process process)

Removes a process from the scheduler and deletes all threads, begins running the next process.

Parameters
processThe process to remove
Returns
The status of the CPU for the next process to run or nullptr if the process was not found

Definition at line 619 of file scheduler.cpp.

619 {
620
621 // If there is no process, fail
622 if (!process)
623 return nullptr;
624
625 // Remove all the threads
626 for (auto thread: process->threads()) {
627
628 // Remove the thread from the scheduler
629 size_t index = m_threads.find(thread) - m_threads.begin();
630 m_threads.erase(m_threads.begin() + index);
631
632 // Delete the thread
633 process->remove_thread(thread->tid);
634
635 }
636
637 // Process will be dead now so run the next process (don't care about the execution state being outdated as it is being
638 // removed regardless)
639 return schedule_next(&current_thread()->execution_state);
640}
void remove_thread(uint64_t tid)
Remove a thread by its tid (NOTE: this will not remove it from the scheduler)
Definition process.cpp:272
system::cpu_status_t * schedule_next(system::cpu_status_t *status)
Schedules the next thread to run.

References current_thread(), MaxOS::processes::Process::remove_thread(), schedule_next(), MaxOS::processes::Process::threads(), and MaxOS::processes::Thread::tid.

◆ get_process()

Process * Scheduler::get_process ( uint64_t  pid)

Gets a process by its PID.

Parameters
pidThe process ID
Returns
The process or nullptr if not found

Definition at line 667 of file scheduler.cpp.

667 {
668
669 // Try to find the process
670 for (auto process: m_processes)
671 if (process->pid() == pid)
672 return process;
673
674 // Not found
675 return nullptr;
676}

References MaxOS::processes::Process::pid().

◆ get_thread()

Thread * Scheduler::get_thread ( uint64_t  tid)

Gets a thread by its TID.

Parameters
tidThe thread ID
Returns
The thread or nullptr if not found

Definition at line 718 of file scheduler.cpp.

718 {
719
720 // Try to find the thread
721 for (auto thread: m_threads)
722 if (thread->tid == tid)
723 return thread;
724
725 return nullptr;
726}

References MaxOS::processes::Thread::tid.

◆ process_amount()

uint64_t Scheduler::process_amount ( )

Gets how processes are running on this scheduler.

Returns
The amount

Definition at line 682 of file scheduler.cpp.

682 {
683 return m_processes.size();
684}

Referenced by MaxOS::processes::GlobalScheduler::add_process().

◆ remove_process()

uint64_t Scheduler::remove_process ( Process process)

Removes a process from the scheduler if the process has no threads, if it does then the threads are stopped but the process is not removed (this will be done automatically when all threads are stopped)

Parameters
processThe process to remove
Returns
-1 if the process has threads, 0 otherwise

Definition at line 584 of file scheduler.cpp.

584 {
585
586 // Check if the process has no threads
587 if (!process->threads().empty()) {
588
589 // Set the threads to stopped or remove them if forced
590 for (auto thread: process->threads())
591 thread->thread_state = ThreadState::STOPPED;
592
593 // Need to wait until the threads are stopped before removing the process (this will be called again when all threads are stopped)
594 return -1;
595
596 }
597
598 // Remove the process
599 for (uint32_t i = 0; i < m_processes.size(); i++) {
600 if (m_processes[i] == process) {
601 m_processes.erase(m_processes.begin() + i);
602
603 // Delete the process mem
604 delete process;
605 return 0;
606 }
607 }
608
609 // Process not found
610 return -1;
611}
common::Vector< Thread * > threads()
Gets the threads of the process.
Definition process.cpp:316
ThreadState
The different operational states a thread can be in.
Definition process.h:33

References MaxOS::processes::Thread::thread_state, and MaxOS::processes::Process::threads().

Referenced by schedule_next().

◆ schedule()

cpu_status_t * Scheduler::schedule ( system::cpu_status_t cpu_state)

Schedules the next thread to run.

Parameters
cpu_stateThe current CPU state
Returns
The next CPU state

Definition at line 387 of file scheduler.cpp.

387 {
388
389 // Scheduler cant schedule anything
390 if (m_threads.empty() || !m_active)
391 return cpu_state;
392
393 // Thread that we are dealing with
394 Thread* current_thread = m_threads[m_next_thread_index];
395
396 // Ticked
397 m_ticks++;
399
400 // Thread hasn't used its time slot yet
401 if(current_thread->ticks % TICKS_PER_EVENT)
402 return cpu_state;
403
404 // Schedule the next thread
405 return schedule_next(cpu_state);
406}
The execution context of a sub-process thread.
Definition process.h:49
size_t ticks
The number of ticks the thread has run for.
Definition process.h:70

References current_thread(), schedule_next(), MaxOS::processes::Thread::ticks, and MaxOS::processes::TICKS_PER_EVENT.

Referenced by MaxOS::processes::GlobalScheduler::handle_interrupt().

◆ schedule_next()

cpu_status_t * Scheduler::schedule_next ( system::cpu_status_t status)

Schedules the next thread to run.

Parameters
statusThe current CPU status of the thread
Returns
The next CPU status
Todo:
Remove by reference where possible

Definition at line 416 of file scheduler.cpp.

416 {
417
418 // Get the current state
419 Thread* current_thread = m_threads[m_next_thread_index];
420 Process* owner_process = current_process();
421 auto old_tid = m_next_thread_index;
422
423 // Save the executing thread state
424 current_thread->execution_state = *cpu_state;
426 if (current_thread->thread_state == ThreadState::RUNNING)
427 current_thread->thread_state = ThreadState::READY;
428
429 // Find a free thread to run
430 while ((++m_next_thread_index) != old_tid){
431 m_next_thread_index %= m_threads.size();
432
433 // Get the current thread
434 current_thread = m_threads[m_next_thread_index];
435 owner_process = current_process();
436
437 // Handle state changes
438 switch (current_thread->thread_state) {
439
440 case ThreadState::SLEEPING:
441
442 // If the wake-up time hasn't occurred yet, run the next thread
443 if (current_thread->wakeup_time > TICKS_PER_EVENT){
445 continue;
446 }
447
448 break;
449
450 case ThreadState::STOPPED:
451
452 // Find the process that has the thread and remove it
453 for (auto thread: owner_process->threads()) {
454 if (thread == current_thread) {
455 owner_process->remove_thread(thread->tid);
456 break;
457 }
458 }
459
460 // Remove the thread
461 m_threads.erase(m_threads.begin() + m_next_thread_index);
462 if(owner_process->threads().empty())
463 remove_process(owner_process);
464 continue;
465
466 case ThreadState::WAITING:
467 continue;
468
469 default:
470 break;
471 }
472 break;
473 }
474
475 // Load the thread's state
476 return load_process(owner_process, current_thread);
477}
uint64_t remove_process(Process *process)
Removes a process from the scheduler if the process has no threads, if it does then the threads are s...
thread_state_t thread_state
The current state of the thread.
Definition process.h:68
void save_sse_state()
Saves the SSE, x87 FPU, and MMX states from memory to the thread.
Definition process.cpp:92
size_t wakeup_time
The time at which the thread should wake up (if sleeping)
Definition process.h:71
system::cpu_status_t execution_state
The CPU state of the thread.
Definition process.h:67
constexpr size_t TICKS_PER_EVENT
Number of times the clock has to interrupt before switching to the next process/thread.
Definition scheduler.h:79

References current_process(), current_thread(), MaxOS::processes::Thread::execution_state, remove_process(), MaxOS::processes::Process::remove_thread(), MaxOS::processes::Thread::save_sse_state(), MaxOS::processes::Thread::thread_state, MaxOS::processes::Process::threads(), MaxOS::processes::TICKS_PER_EVENT, and MaxOS::processes::Thread::wakeup_time.

Referenced by force_remove_process(), schedule(), and yield().

◆ thread_amount()

uint64_t Scheduler::thread_amount ( )

Gets how many threads are running on this scheduler.

Returns
The amount

Definition at line 700 of file scheduler.cpp.

700 {
701 return m_threads.size();
702}

Referenced by MaxOS::processes::GlobalScheduler::add_thread().

◆ ticks()

uint64_t Scheduler::ticks ( ) const

Gets how long the system has been running for.

Returns
The number of ticks

Definition at line 545 of file scheduler.cpp.

545 {
546
547 return m_ticks;
548}

◆ yield()

cpu_status_t * Scheduler::yield ( )

Pass execution to the next thread.

Returns
The cpu state of the next thread to run

Definition at line 555 of file scheduler.cpp.

555 {
556
557 // If this is the only thread, can't yield
558 if (m_threads.size() <= 1)
560
561 // Set the current thread to waiting if running
562 auto thread = current_thread();
563 if (thread->thread_state == ThreadState::RUNNING)
564 thread->thread_state = ThreadState::READY;
565
566 // Schedule the next thread
567 return schedule_next(&thread->execution_state);
568}

References current_thread(), MaxOS::processes::Thread::execution_state, schedule_next(), and MaxOS::processes::Thread::thread_state.

Referenced by MaxOS::processes::GlobalScheduler::yield().


The documentation for this class was generated from the following files: