Implement proc_spawn_thread syscall, fix proc_resume and proc_suspend
All checks were successful
Build documentation / build-and-deploy (push) Successful in 35s

This commit is contained in:
2026-01-16 00:26:37 +01:00
parent ebd9f0cac6
commit 711da8aeab
12 changed files with 219 additions and 51 deletions

View File

@@ -7,36 +7,58 @@
#include <sys/debug.h>
void proc_mutex_lock (struct proc* proc, struct proc_mutex* mutex) {
while (atomic_flag_test_and_set_explicit (&mutex->flag, memory_order_acquire))
proc_suspend (proc, &mutex->suspension_q);
spin_lock_ctx_t ctxmt;
/* taken */
try:
spin_lock (&mutex->lock, &ctxmt);
mutex->owner = proc;
if (!mutex->locked || mutex->owner == proc) {
mutex->locked = true;
mutex->owner = proc;
spin_unlock (&mutex->lock, &ctxmt);
return;
}
spin_unlock (&mutex->lock, &ctxmt);
proc_suspend (proc, &mutex->suspension_q);
goto try;
}
bool proc_mutex_unlock (struct proc* proc, struct proc_mutex* mutex) {
if (mutex->owner != proc)
return false;
spin_lock_ctx_t ctxmt, ctxsq;
atomic_flag_clear_explicit (&mutex->flag, memory_order_release);
spin_lock (&mutex->lock, &ctxmt);
if (mutex->owner != proc) {
spin_unlock (&mutex->lock, &ctxmt);
return false;
}
spin_lock (&mutex->suspension_q.lock, &ctxsq);
struct proc* resumed_proc;
struct rb_node_link* node;
rbtree_first (&mutex->suspension_q.proc_tree, node);
while (node) {
struct rb_node_link* next;
rbtree_next (node, next);
if (node) {
resumed_proc = rbtree_entry (node, struct proc, suspension_link);
mutex->owner = resumed_proc;
spin_unlock (&mutex->suspension_q.lock, &ctxsq);
spin_unlock (&mutex->lock, &ctxmt);
proc_resume (resumed_proc);
node = next;
return true;
}
assert (mutex->suspension_q.proc_tree == NULL);
spin_unlock (&mutex->suspension_q.lock, &ctxsq);
mutex->locked = false;
mutex->owner = NULL;
spin_unlock (&mutex->lock, &ctxmt);
return true;
}

View File

@@ -8,7 +8,8 @@
struct proc;
struct proc_mutex {
atomic_flag flag;
spin_lock_t lock;
bool locked;
struct proc_suspension_q suspension_q;
struct proc* owner;
};

View File

@@ -197,7 +197,7 @@ static struct proc* proc_spawn_rd (char* name) {
return proc_from_elf (rd_file->content);
}
static void proc_register (struct proc* proc, struct cpu* cpu) {
void proc_register (struct proc* proc, struct cpu* cpu) {
spin_lock_ctx_t ctxcpu, ctxprtr;
proc->cpu = cpu;

View File

@@ -25,6 +25,8 @@
#define PROC_RESOURCES_MAX 1024
#define PROC_USTK_PREALLOC (1 << 0)
struct cpu;
struct proc_mapping {
@@ -35,6 +37,11 @@ struct proc_mapping {
size_t size;
};
struct proc_sys_rids {
atomic_int refs;
atomic_int counter;
};
struct proc {
int pid;
struct rb_node_link proc_tree_link;
@@ -44,11 +51,13 @@ struct proc {
struct list_node_link* mappings; /* pd.lock implicitly protects this field */
struct proc_platformdata pdata;
uint32_t flags;
struct pd* pd;
spin_lock_t lock;
struct cpu* cpu;
atomic_int state;
struct rb_node_link* resource_tree;
struct proc_sys_rids* sys_rids;
struct proc_suspension_q* suspension_q;
};
@@ -60,6 +69,7 @@ bool proc_map (struct proc* proc, uintptr_t start_paddr, uintptr_t start_vaddr,
uint32_t flags);
bool proc_unmap (struct proc* proc, uintptr_t start_vaddr, size_t pages);
struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf);
void proc_register (struct proc* proc, struct cpu* cpu);
void proc_init (void);
#endif // _KERNEL_PROC_PROC_H