wip: fork()
This commit is contained in:
@ -2,17 +2,18 @@
|
||||
#include "alloc.h"
|
||||
#include "assert.h"
|
||||
#include "interrupts.h"
|
||||
#include "list.h"
|
||||
#include "memory.h"
|
||||
#include "process.h"
|
||||
#include "string.h"
|
||||
#include "thread.h"
|
||||
|
||||
struct tcb *create_thread(struct pcb *process, void (*entry)(void))
|
||||
{
|
||||
static uint32_t tid = 1;
|
||||
struct tcb *new_tcb = vmalloc(sizeof(struct tcb));
|
||||
if (!new_tcb)
|
||||
return NULL;
|
||||
new_tcb->tid = tid++;
|
||||
new_tcb->tid = process->tid++;
|
||||
|
||||
new_tcb->esp0 = alloc_pages(STACK_SIZE, NULL);
|
||||
if (!new_tcb->esp0) {
|
||||
@ -40,18 +41,26 @@ struct tcb *create_thread(struct pcb *process, void (*entry)(void))
|
||||
*(--stack) = 0; // ESI
|
||||
*(--stack) = 0; // EDI
|
||||
*(--stack) = 0x10; // kernel DS
|
||||
|
||||
new_tcb->esp = stack;
|
||||
new_tcb->process = process;
|
||||
new_tcb->next = NULL;
|
||||
new_tcb->state = NEW;
|
||||
|
||||
struct list *new_node = vmalloc(sizeof(struct list));
|
||||
if (!new_node) {
|
||||
free_pages(new_tcb->esp0, STACK_SIZE);
|
||||
vfree(new_tcb);
|
||||
return NULL;
|
||||
}
|
||||
new_node->content = new_tcb;
|
||||
new_node->next = NULL;
|
||||
if (process->thread_list == NULL) {
|
||||
process->thread_list = new_tcb;
|
||||
process->thread_list = new_node;
|
||||
} else {
|
||||
struct tcb *it = process->thread_list;
|
||||
struct list *it = process->thread_list;
|
||||
while (it->next)
|
||||
it = it->next;
|
||||
it->next = new_tcb;
|
||||
it->next = new_node;
|
||||
}
|
||||
|
||||
return new_tcb;
|
||||
@ -60,10 +69,12 @@ struct tcb *create_thread(struct pcb *process, void (*entry)(void))
|
||||
void delete_thread(struct tcb *thread)
|
||||
{
|
||||
vfree(thread->esp0);
|
||||
struct tcb *it = thread->process->thread_list;
|
||||
struct list *it = thread->process->thread_list;
|
||||
assert(it);
|
||||
while (it->next != thread)
|
||||
while (it->next && it->next->content != thread)
|
||||
it = it->next;
|
||||
struct list *to_free = it;
|
||||
it->next = it->next->next;
|
||||
vfree(to_free);
|
||||
vfree(thread);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user