aboutsummaryrefslogtreecommitdiff
path: root/kernel/proc/kthread.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/proc/kthread.c')
-rw-r--r--kernel/proc/kthread.c54
1 files changed, 50 insertions, 4 deletions
diff --git a/kernel/proc/kthread.c b/kernel/proc/kthread.c
index 1104b31..d066dac 100644
--- a/kernel/proc/kthread.c
+++ b/kernel/proc/kthread.c
@@ -68,8 +68,43 @@ void kthread_init()
kthread_t *kthread_create(proc_t *proc, kthread_func_t func, long arg1,
void *arg2)
{
- NOT_YET_IMPLEMENTED("PROCS: kthread_create");
- return NULL;
+ // NOT_YET_IMPLEMENTED("PROCS: kthread_create");
+ dbg(DBG_THR, "ATTEMPT to create a new thread with proc name=%s, id=%d\n", proc->p_name, proc->p_pid);
+
+ kthread_t *new_thread = slab_obj_alloc(kthread_allocator);
+ if (new_thread == NULL)
+ {
+ return NULL;
+ }
+ new_thread->kt_state = KT_NO_STATE;
+
+ new_thread->kt_kstack = alloc_stack();
+ if (new_thread->kt_kstack == NULL)
+ {
+ slab_obj_free(kthread_allocator, new_thread);
+ return NULL;
+ }
+
+ new_thread->kt_proc = proc;
+ context_setup(&new_thread->kt_ctx, func, arg1, arg2, new_thread->kt_kstack,
+ DEFAULT_STACK_SIZE, new_thread->kt_proc->p_pml4);
+
+ // give default values to rest of struct
+ new_thread->kt_retval = NULL;
+ new_thread->kt_errno = 0;
+ new_thread->kt_cancelled = 0;
+ new_thread->kt_wchan = NULL;
+ list_link_init(&new_thread->kt_plink);
+ list_link_init(&new_thread->kt_qlink);
+ list_init(&new_thread->kt_mutexes);
+ new_thread->kt_recent_core = 0;
+
+ // put this thread on the process's thread list
+ list_insert_tail(&proc->p_threads, &new_thread->kt_plink);
+
+ dbg(DBG_THR, "SUCCESFULLY created a new THREAD with proc name=%s, id=%d\n", proc->p_name, proc->p_pid);
+
+ return new_thread;
}
/*
@@ -124,7 +159,15 @@ void kthread_destroy(kthread_t *thr)
*/
void kthread_cancel(kthread_t *thr, void *retval)
{
- NOT_YET_IMPLEMENTED("PROCS: kthread_cancel");
+ // NOT_YET_IMPLEMENTED("PROCS: kthread_cancel");
+ KASSERT(thr != curthr);
+
+ // FIXME: ask about the use of check_curthr_cancelled() in syscall_handler()
+ int status = (int) retval;
+ dbg(DBG_THR, "Cancelling thread with proc name=%s, id=%d, status=%d\n",
+ thr->kt_proc->p_name, thr->kt_proc->p_pid, status);
+ thr->kt_retval = retval;
+ sched_cancel(thr);
}
/*
@@ -132,5 +175,8 @@ void kthread_cancel(kthread_t *thr, void *retval)
*/
void kthread_exit(void *retval)
{
- NOT_YET_IMPLEMENTED("PROCS: kthread_exit");
+ // NOT_YET_IMPLEMENTED("PROCS: kthread_exit");
+ dbg(DBG_THR, "Exiting thread with proc name=%s, id=%d, status=%d\n",
+ curthr->kt_proc->p_name, curthr->kt_proc->p_pid, (int) retval);
+ proc_thread_exiting(retval);
}