UnnamedOS
schedule.c
Go to the documentation of this file.
1 
11 #include <common.h>
12 #include <tasks/schedule.h>
13 #include <tasks/elf.h>
14 #include <tasks/tss.h>
15 #include <mem/mmu.h>
16 #include <mem/vmm.h>
17 
19 static uint32_t ticks_per_time_slice = 1;
20 
27  task_pid_t next_task = schedule_get_next_task();
28  if (!next_task)
29  return cpu;
30  if (current_task) {
33  return cpu;
34  task_set_cpu(current_task, cpu); // save the current ESP / CPU state
35  }
36  if (current_task == next_task) // no switch is needed
37  return cpu;
39  return schedule_switch_task(next_task);
40 }
41 
48  if (current_task)
49  logln("SCHEDULE", "Task switch from task %d to task %d",
50  current_task, next_task);
51  else
52  logln("SCHEDULE", "Initial task switch to task %d", next_task);
55  // When the iret is done, the CPU state on the kernel stack is fully popped,
56  // so when doing the iret, ESP will be next_task->cpu + sizeof(cpu_state_t)
57  // or next_task->cpu + 1. We will use that as ESP when we are interrupted
58  // the next time. Note that this has no effect when we stay in the kernel.
59  tss_set_stack((uint32_t) (task_get_cpu(next_task) + 1));
63  io_set_logging(0);
65  io_set_logging(1);
66  current_task = next_task;
67  return task_get_cpu(next_task); // restore the CPU state / ESP saved earlier
68 }
69 
75  return current_task;
76 }
77 
83  return task_get_next_task_with_state(current_task, TASK_RUNNING);
84 }
85 
88  task_pid_t pid = 0;
89  while ((pid = task_get_next_task_with_state(pid, TASK_STOPPED)))
90  task_get_elf(pid) ? elf_destroy_task(pid) : task_destroy(pid);
91 }
92 
task_pid_t schedule_get_current_task()
Returns the current task&#39;s PID.
Definition: schedule.c:74
cpu_state_t * schedule(cpu_state_t *cpu)
Returns the next task to run.
Definition: schedule.c:26
void tss_set_stack(uint32_t stack_pointer)
Sets the TSS&#39;s kernel stack which is used to handle interrupts.
Definition: tss.c:48
void schedule_finalize_tasks()
Destroys tasks marked for removal.
Definition: schedule.c:87
The CPU&#39;s state when an interrupt occurs.
Definition: isr.h:58
void elf_destroy_task(task_pid_t pid)
Destroys a user task running the code of an ELF file.
Definition: elf.c:166
page_directory_t * vmm_load_page_directory(page_directory_t *new_directory)
Loads a new page directory.
Definition: vmm.c:129
void task_set_cpu(task_pid_t pid, cpu_state_t *cpu)
Sets a task&#39;s CPU state.
Definition: task.c:249
cpu_state_t * task_get_cpu(task_pid_t pid)
Returns a task&#39;s CPU state.
Definition: task.c:240
task_pid_t schedule_get_next_task()
Returns the next running task&#39;s PID.
Definition: schedule.c:82
uint32_t task_pid_t
unique process ID
Definition: task.h:17
task_pid_t task_get_next_task_with_state(task_pid_t pid, task_state_t state)
Returns the next task from the task list with a specified state.
Definition: task.c:200
static task_pid_t current_task
the currently running task pid
Definition: schedule.c:18
static uint32_t ticks_per_time_slice
1 tick = frequency of the PIT
Definition: schedule.c:19
page_directory_t * task_get_page_directory(task_pid_t pid)
Returns a task&#39;s page directory.
Definition: task.c:258
cpu_state_t * schedule_switch_task(task_pid_t next_task)
Switches to a given task.
Definition: schedule.c:47
void * task_get_elf(task_pid_t pid)
Returns a task&#39;s ELF file.
Definition: task.c:276
task_state_t task_get_ticks(task_pid_t pid)
Returns a task&#39;s number of remaining ticks.
Definition: task.c:218
void task_destroy(task_pid_t pid)
Destroys a task.
Definition: task.c:161
uint32_t task_set_ticks(task_pid_t pid, uint32_t ticks)
Sets a task&#39;s number of remaining ticks.
Definition: task.c:228