mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-10-28 21:28:21 +00:00
Events: stub
This commit is contained in:
+101
@@ -0,0 +1,101 @@
|
||||
#include <kernel/event.h>
|
||||
#include <arch/interrupt.h>
|
||||
#include <arch/spinlock.h>
|
||||
#include <arch/cls.h>
|
||||
|
||||
static bool addtoevent(thread_t* thread, event_t* event){
|
||||
|
||||
spinlock_acquire(&event->lock);
|
||||
|
||||
uintmax_t i = 0;
|
||||
|
||||
for(; i < EVENT_MAX_THREADS; ++i){
|
||||
if(!event->threads[i]){
|
||||
event->threads[i] = arch_getcls()->thread;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
spinlock_release(&event->lock);
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void removefromevent(thread_t* thread, event_t* event){
|
||||
spinlock_acquire(&event->lock);
|
||||
|
||||
for(size_t i = 0; i < EVENT_MAX_THREADS; ++i){
|
||||
if(event->threads[i] == thread){
|
||||
event->threads[i] = NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
spinlock_release(&event->lock);
|
||||
}
|
||||
|
||||
int event_wait(event_t* event, bool interruptible){
|
||||
|
||||
|
||||
arch_interrupt_disable();
|
||||
|
||||
thread_t* thread = arch_getcls()->thread;
|
||||
|
||||
if(interruptible){
|
||||
addtoevent(thread, &thread->sigevent);
|
||||
}
|
||||
|
||||
addtoevent(thread, event);
|
||||
|
||||
|
||||
// ...
|
||||
|
||||
|
||||
int ret = 0;
|
||||
|
||||
event_t* awokenby = thread->awokenby;
|
||||
|
||||
if(awokenby == &thread->sigevent){
|
||||
ret = EINTR;
|
||||
removefromevent(thread, awokenby);
|
||||
awokenby = event;
|
||||
}
|
||||
|
||||
removefromevent(thread, awokenby);
|
||||
|
||||
spinlock_release(&event->lock);
|
||||
|
||||
arch_interrupt_enable();
|
||||
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
int event_signal(event_t* event, bool interruptson){
|
||||
|
||||
if(interruptson)
|
||||
arch_interrupt_disable();
|
||||
|
||||
spinlock_acquire(&event->lock);
|
||||
|
||||
|
||||
for(uintmax_t i = 0; i < EVENT_MAX_THREADS; ++i){
|
||||
|
||||
thread_t* thread = event->threads[i];
|
||||
|
||||
if(!thread)
|
||||
continue;
|
||||
|
||||
// ...
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
spinlock_release(&event->lock);
|
||||
|
||||
if(interruptson)
|
||||
arch_interrupt_enable();
|
||||
|
||||
}
|
||||
@@ -262,12 +262,35 @@ thread_t* sched_newkthread(void* ip, size_t stacksize, bool run, int prio){
|
||||
return thread;
|
||||
}
|
||||
|
||||
void sched_eventsignal(event_t* event, thread_t* thread){
|
||||
if((thread->state == THREAD_STATE_BLOCKED_INTR || (thread->state == THREAD_STATE_BLOCKED && event != &thread->sigevent)))
|
||||
return;
|
||||
|
||||
thread->state = THREAD_STATE_WAITING;
|
||||
thread->awokenby = event;
|
||||
queue_remove(&blocked, thread);
|
||||
queue_add(&queues[thread->priority], thread);
|
||||
|
||||
}
|
||||
|
||||
void sched_block(bool interruptible){
|
||||
|
||||
thread_t* thread = arch_getcls()->thread;
|
||||
|
||||
queue_remove(&queues[thread->priority], thread);
|
||||
queue_add(&blocked, thread);
|
||||
thread->state = interruptible ? THREAD_STATE_BLOCKED_INTR : THREAD_STATE_BLOCKED;
|
||||
|
||||
sched_yield();
|
||||
|
||||
}
|
||||
|
||||
void sched_init(){
|
||||
arch_getcls()->thread = allocthread(NULL, THREAD_STATE_RUNNING, 0, 0);
|
||||
arch_getcls()->thread->priority = THREAD_PRIORITY_KERNEL;
|
||||
|
||||
timer_req* req = &arch_getcls()->schedreq;
|
||||
|
||||
|
||||
req->func = sched_timerhook;
|
||||
|
||||
}
|
||||
@@ -327,14 +350,20 @@ void sched_runinit(){
|
||||
|
||||
char* argv[] = {"/usr/bin/bash", NULL};
|
||||
char* env[] = {NULL};
|
||||
|
||||
void *entry, *stack;
|
||||
|
||||
ret = elf_load(thread, node, argv, env);
|
||||
ret = elf_load(thread, node, argv, env, &stack, &entry);
|
||||
|
||||
if(ret){
|
||||
printf("ELF load error: %s\n", strerror(ret));
|
||||
_panic("Could not load init", 0);
|
||||
}
|
||||
|
||||
arch_regs_setupuser(thread->regs, entry, stack, true);
|
||||
|
||||
timer_add(&arch_getcls()->schedreq, THREAD_QUANTUM, true);
|
||||
|
||||
switch_thread(thread);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user