From 4c3572395959b452e1069ff76fa51e17fbdb80fb Mon Sep 17 00:00:00 2001 From: Max Wash Date: Thu, 19 Feb 2026 19:16:59 +0000 Subject: [PATCH] sched: add helper functions for opening and resolving handles for a task --- sched/task.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++- sched/thread.c | 5 +++ 2 files changed, 122 insertions(+), 2 deletions(-) diff --git a/sched/task.c b/sched/task.c index 185d711..2c5d8dc 100644 --- a/sched/task.c +++ b/sched/task.c @@ -45,6 +45,11 @@ struct task *idle_task(void) return __idle_task; } +struct task *task_cast(struct object *obj) +{ + return TASK_CAST(obj); +} + void idle(void) { while (1) { @@ -215,15 +220,125 @@ struct task *task_create(const char *name, size_t name_len) return task; } -struct task *task_from_pid(unsigned int pid) +kern_status_t task_add_child(struct task *parent, struct task *child) +{ + queue_push_back(&parent->t_children, &child->t_child_entry); + return KERN_OK; +} + +kern_status_t task_add_channel( + struct task *task, + struct channel *channel, + unsigned int id) +{ + channel->c_id = id; + + if (!task->b_channels.b_root) { + task->b_channels.b_root = &channel->c_node; + btree_insert_fixup(&task->b_channels, &channel->c_node); + return KERN_OK; + } + + struct btree_node *cur = task->b_channels.b_root; + while (1) { + struct channel *cur_node + = BTREE_CONTAINER(struct channel, c_node, cur); + struct btree_node *next = NULL; + + if (id > cur_node->c_id) { + next = btree_right(cur); + + if (!next) { + btree_put_right(cur, &channel->c_node); + break; + } + } else if (id < cur_node->c_id) { + next = btree_left(cur); + + if (!next) { + btree_put_left(cur, &channel->c_node); + break; + } + } else { + return KERN_NAME_EXISTS; + } + + cur = next; + } + + btree_insert_fixup(&task->b_channels, &channel->c_node); + return KERN_OK; +} + +BTREE_DEFINE_SIMPLE_GET( + struct channel, + unsigned int, + c_node, + c_id, + get_channel_with_id) + +struct channel *task_get_channel(struct task *task, unsigned int id) +{ + return get_channel_with_id(&task->b_channels, id); +} + +struct task *task_from_tid(tid_t id) { unsigned long flags; spin_lock_irqsave(&task_list_lock, &flags); - struct task *t = task_list_get(&task_list, pid); + struct task *t = task_list_get(&task_list, id); spin_unlock_irqrestore(&task_list_lock, flags); return t; } +kern_status_t task_open_handle( + struct task *task, + struct object *obj, + handle_flags_t flags, + kern_handle_t *out) +{ + struct handle *handle_data = NULL; + kern_status_t status + = handle_table_alloc_handle(task->t_handles, &handle_data, out); + if (status != KERN_OK) { + return status; + } + + object_add_handle(obj); + handle_data->h_object = obj; + handle_data->h_flags = flags; + + return KERN_OK; +} + +kern_status_t task_resolve_handle( + struct task *task, + kern_handle_t handle, + struct object **out_obj, + handle_flags_t *out_flags) +{ + struct handle *handle_data + = handle_table_get_handle(task->t_handles, handle); + if (!handle_data) { + return KERN_INVALID_ARGUMENT; + } + + if (out_obj) { + *out_obj = handle_data->h_object; + } + + if (out_flags) { + *out_flags = handle_data->h_flags; + } + + return KERN_OK; +} + +kern_status_t task_close_handle(struct task *task, kern_handle_t handle) +{ + return handle_table_free_handle(task->t_handles, handle); +} + struct thread *task_create_thread(struct task *parent) { struct thread *thread = thread_alloc(); diff --git a/sched/thread.c b/sched/thread.c index d4d240e..0b6505f 100644 --- a/sched/thread.c +++ b/sched/thread.c @@ -17,6 +17,11 @@ kern_status_t thread_object_type_init(void) return object_type_register(&thread_type); } +struct thread *thread_cast(struct object *obj) +{ + return THREAD_CAST(obj); +} + struct thread *thread_alloc(void) { struct object *thread_obj = object_create(&thread_type);