sched: add helper functions for opening and resolving handles for a task

This commit is contained in:
2026-02-19 19:16:59 +00:00
parent 2b7e5368c9
commit 4c35723959
2 changed files with 122 additions and 2 deletions

View File

@@ -45,6 +45,11 @@ struct task *idle_task(void)
return __idle_task; return __idle_task;
} }
struct task *task_cast(struct object *obj)
{
return TASK_CAST(obj);
}
void idle(void) void idle(void)
{ {
while (1) { while (1) {
@@ -215,15 +220,125 @@ struct task *task_create(const char *name, size_t name_len)
return task; 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; unsigned long flags;
spin_lock_irqsave(&task_list_lock, &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); spin_unlock_irqrestore(&task_list_lock, flags);
return t; 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 *task_create_thread(struct task *parent)
{ {
struct thread *thread = thread_alloc(); struct thread *thread = thread_alloc();

View File

@@ -17,6 +17,11 @@ kern_status_t thread_object_type_init(void)
return object_type_register(&thread_type); return object_type_register(&thread_type);
} }
struct thread *thread_cast(struct object *obj)
{
return THREAD_CAST(obj);
}
struct thread *thread_alloc(void) struct thread *thread_alloc(void)
{ {
struct object *thread_obj = object_create(&thread_type); struct object *thread_obj = object_create(&thread_type);