Skip to content

Commit

Permalink
Yield request event is sent regularly to processes
Browse files Browse the repository at this point in the history
  • Loading branch information
coderarjob committed Oct 17, 2024
1 parent a544309 commit c3f45eb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
7 changes: 7 additions & 0 deletions include/applib/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ typedef enum IProcessFlags {
IPROCESS_FLAGS_KERNEL_PROCESS = (1 << 0),
IPROCESS_FLAGS_THREAD = (1 << 1),
} IProcessFlags;

// Represents the 'KernelEvents' struct defined in 'kernel.h', but with some changes.
typedef enum IAppEvents {
APP_EVENT_NONE = 0,
APP_EVENT_TICK = 1,
APP_EVENT_PROCCESS_YIELD_REQ = 2
} IAppEvents;
// =============================================================================

typedef enum SYSCALLS {
Expand Down
7 changes: 7 additions & 0 deletions include/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ typedef struct KernelStateInfo {

extern volatile KernelStateInfo g_kstate;

typedef enum KernelEvents {
KERNEL_EVENT_NONE = 0,
KERNEL_EVENT_TICK = 1,
KERNEL_EVENT_PROCCESS_YIELD_REQ = 2
} KernelEvents;

#define KERNEL_PHASE_SET(p) \
do { \
k_assert (p == 0 || (g_kstate.phase + 1) == p, "Current state is unsuitable for " #p); \
Expand All @@ -42,3 +48,4 @@ extern volatile KernelStateInfo g_kstate;
#define KERNEL_TICK_COUNT_TO_MICROSEC(tick) ((tick)*CONFIG_TICK_PERIOD_MICROSEC)

void k_delay (UINT ms);
void keventmanager_invoke();
17 changes: 14 additions & 3 deletions src/apps/mpdemo.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ static void s_progressbar (UINT iterPerStep, char* title, UINT row, UINT color);
static void thread0();
static void thread1();

static bool shouldYield()
{
UINT pid = sys_process_get_pid();
IProcessEvent e;
sys_process_pop_event (pid, &e);
return (e.event == APP_EVENT_PROCCESS_YIELD_REQ);
}

static void s_printString (U32 row, U32 col, U32 bgcolor, U32 fgcolor, char* text)
{
syscall (SYSCALL_CONSOLE_SETCOLOR, bgcolor, fgcolor, 0, 0, 0);
Expand All @@ -27,10 +35,10 @@ void s_progressbar (UINT iterPerStep, char* title, UINT row, UINT color)
if (i % iterPerStep == 0) {
column = (column + 1) % MAX_VGA_COLUMNS;
}
sys_yield();
if (shouldYield()) {
sys_yield();
}
}

sys_process_kill();
}

void proc_main()
Expand All @@ -50,6 +58,7 @@ void proc_main()

s_progressbar (iterPerStep, "Process 0:\n", 25, RED);

k_halt();
sys_process_kill();
s_printString (37, 0, BLACK, WHITE,
"Cannot kill process 0. Make kernel thread slower for demo.");
Expand All @@ -60,11 +69,13 @@ void thread0()
UINT iterPerStep = 10;

s_progressbar (iterPerStep, "Thread 0:\n", 29, GREEN);
sys_process_kill();
}

void thread1()
{
UINT iterPerStep = 7;

s_progressbar (iterPerStep, "Thread 1:\n", 33, YELLOW);
sys_process_kill();
}
1 change: 1 addition & 0 deletions src/kernel/x86/interrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void timer_interrupt_handler (InterruptFrame* frame)
"Interrupt timer period != tick period");

g_kstate.tick_count++;
keventmanager_invoke();

pic_send_eoi (PIC_IRQ_TIMER);
}
Expand Down

0 comments on commit c3f45eb

Please sign in to comment.