diff options
Diffstat (limited to 'src/kernel/core')
-rw-r--r-- | src/kernel/core/idt.c | 2 | ||||
-rw-r--r-- | src/kernel/core/kmalloc.c | 6 | ||||
-rw-r--r-- | src/kernel/core/paging.c | 8 | ||||
-rw-r--r-- | src/kernel/core/thread.c | 68 |
4 files changed, 37 insertions, 47 deletions
diff --git a/src/kernel/core/idt.c b/src/kernel/core/idt.c index f3dd63b..d963b4d 100644 --- a/src/kernel/core/idt.c +++ b/src/kernel/core/idt.c @@ -91,7 +91,7 @@ static isr_handler_t ex_handlers[32] = {0}; /* Called in interrupt.s when an exception fires (interrupt 0 to 31) */ void idt_ex_handler(registers_t *regs) { - dbg_printf("Ex handler: %d\n", regs->int_no); + dbg_printf("ex%d.", regs->int_no); if (ex_handlers[regs->int_no] != 0) { ex_handlers[regs->int_no](regs); } else { diff --git a/src/kernel/core/kmalloc.c b/src/kernel/core/kmalloc.c index c977e54..cc20487 100644 --- a/src/kernel/core/kmalloc.c +++ b/src/kernel/core/kmalloc.c @@ -16,7 +16,10 @@ static void* page_alloc_fun_for_kmalloc(size_t bytes) { for (void* i = addr; i < addr + bytes; i += PAGE_SIZE) { int f = frame_alloc(1); if (f == 0) goto failure; - if (!pd_map_page(i, f, true)) goto failure; + if (!pd_map_page(i, f, true)) { + frame_free(f, 1); + goto failure; + } } return addr; @@ -29,6 +32,7 @@ failure: frame_free(f, 1); } } + region_free(addr); return 0; } diff --git a/src/kernel/core/paging.c b/src/kernel/core/paging.c index 03a973f..cefb8c3 100644 --- a/src/kernel/core/paging.c +++ b/src/kernel/core/paging.c @@ -209,13 +209,7 @@ bool pd_map_page(void* vaddr, uint32_t frame_id, bool rw) { mutex_lock(&pdd->mutex); if (!(pd->page[pt] & PTE_PRESENT)) { - uint32_t new_pt_frame; - int tries = 0; - while ((new_pt_frame = frame_alloc(1)) == 0 && (tries++) < 3) { - mutex_unlock(&pdd->mutex); - free_some_memory(); - mutex_lock(&pdd->mutex); - } + uint32_t new_pt_frame = frame_alloc(1); if (new_pt_frame == 0) { mutex_unlock(&pdd->mutex); return false; diff --git a/src/kernel/core/thread.c b/src/kernel/core/thread.c index ed2c185..19ad6c8 100644 --- a/src/kernel/core/thread.c +++ b/src/kernel/core/thread.c @@ -18,7 +18,7 @@ void resume_context(saved_context_t *ctx); thread_t *current_thread = 0; -static hashtbl_t *waiters = 0; // threads waiting on a ressource +static thread_t *waiters = 0; // ====================== // // THE PROGRAMMABLE TIMER // @@ -235,9 +235,6 @@ void threading_irq0_handler() { } } void threading_setup(entry_t cont, void* arg) { - waiters = create_hashtbl(id_key_eq_fun, id_hash_fun, 0); - ASSERT(waiters != 0); - set_pit_frequency(TASK_SWITCH_FREQUENCY); idt_set_irq_handler(IRQ0, &irq0_handler); @@ -286,34 +283,20 @@ bool wait_on_many(void** x, size_t n) { int st = enter_critical(CL_NOINT); - // ---- Check we can wait on all the requested objects - bool ok = true; - for (size_t i = 0; ok && i < n; i++) { - void* prev_th = hashtbl_find(waiters, x[i]); - if (prev_th == 0) { - bool add_ok = hashtbl_add(waiters, x[i], (void*)1); - if (!add_ok) { - ok = false; - } - } else if (prev_th != (void*)1) { - ok = false; - break; - } - } - if (!ok) { - exit_critical(st); - return false; - } - // ---- Set ourselves as the waiting thread for all the requested objets - dbg_printf("Wait on many: "); + dbg_printf("Wait on many:"); for (size_t i = 0; i < n; i++) { - ASSERT(hashtbl_change(waiters, x[i], current_thread)); - dbg_printf("0x%p (0x%p) ", x[i], hashtbl_find(waiters, x[i])); + dbg_printf(" 0x%p", x[i]); } dbg_printf("\n"); + current_thread->waiting_on = x; + current_thread->n_waiting_on = n; + + current_thread->next_waiter = waiters; + waiters = current_thread; + // ---- Go to sleep current_thread->state = T_STATE_PAUSED; @@ -321,8 +304,15 @@ bool wait_on_many(void** x, size_t n) { // ---- Remove ourselves from the list - for (size_t i = 0; i < n; i++) { - ASSERT(hashtbl_change(waiters, x[i], (void*)1)); + current_thread->waiting_on = 0; + current_thread->n_waiting_on = 0; + + if (waiters == current_thread) { + waiters = current_thread->next_waiter; + } else { + for (thread_t *w = waiters; w->next_waiter != 0; w = w->next_waiter) { + if (w->next_waiter == current_thread) w->next_waiter = current_thread->next_waiter; + } } exit_critical(st); @@ -374,24 +364,26 @@ void exit() { } bool resume_on(void* x) { - - thread_t *thread; - bool ret = false; int st = enter_critical(CL_NOINT); - thread = hashtbl_find(waiters, x); + dbg_printf("Resume on 0x%p:", x); - dbg_printf("Resume on 0x%p : 0x%p\n", x, thread); + for (thread_t *t = waiters; t != 0; t = t->next_waiter) { + for (int i = 0; i < t->n_waiting_on; i++) { + if (t->waiting_on[i] == x) { + dbg_printf(" 0x%p", t); - if (thread != 0 && thread != (void*)1) { - if (thread->state == T_STATE_PAUSED) { - thread->state = T_STATE_RUNNING; + if (t->state == T_STATE_PAUSED) { + t->state = T_STATE_RUNNING; - enqueue_thread(thread, false); + enqueue_thread(t, false); - ret = true; + ret = true; + } + break; + } } } |