Files
mop3/kernel/proc/resource.c
kamkow1 217179c9a0
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m42s
Handle IRQs inside the kernel
2026-03-13 20:33:27 +01:00

122 lines
3.1 KiB
C

#include <id/id_alloc.h>
#include <libk/assert.h>
#include <libk/list.h>
#include <libk/rbtree.h>
#include <libk/std.h>
#include <libk/string.h>
#include <mm/malloc.h>
#include <mm/pmm.h>
#include <proc/mail.h>
#include <proc/mutex.h>
#include <proc/proc.h>
#include <proc/procgroup.h>
#include <proc/reschedule.h>
#include <proc/resource.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
struct proc_resource* proc_find_resource (struct procgroup* procgroup, int rid) {
uint64_t fr;
struct proc_resource* resource = NULL;
spin_lock (&procgroup->lock, &fr);
rbtree_find (struct proc_resource, &procgroup->resource_tree, rid, resource, resource_tree_link,
rid);
spin_unlock (&procgroup->lock, fr);
return resource;
}
struct proc_resource* proc_create_resource_mutex (struct procgroup* procgroup) {
uint64_t fpg;
struct proc_resource* resource;
resource = malloc (sizeof (*resource));
if (resource == NULL)
return NULL;
memset (resource, 0, sizeof (*resource));
spin_lock (&procgroup->lock, &fpg);
resource->rid = id_alloc (&procgroup->rid_alloc);
if (resource->rid < 0) {
free (resource);
spin_unlock (&procgroup->lock, fpg);
return NULL;
}
resource->lock = SPIN_LOCK_INIT;
resource->ops.cleanup = &proc_cleanup_resource_mutex;
resource->u.mutex.resource = resource;
resource->type = PR_MUTEX;
rbtree_insert (struct proc_resource, &procgroup->resource_tree, &resource->resource_tree_link,
resource_tree_link, rid);
spin_unlock (&procgroup->lock, fpg);
return resource;
}
struct proc_resource* proc_create_resource_mail (struct procgroup* procgroup) {
uint64_t fpg;
struct proc_resource* resource;
resource = malloc (sizeof (*resource));
if (resource == NULL)
return NULL;
memset (resource, 0, sizeof (*resource));
spin_lock (&procgroup->lock, &fpg);
resource->rid = id_alloc (&procgroup->rid_alloc);
if (resource->rid < 0) {
free (resource);
spin_unlock (&procgroup->lock, fpg);
return NULL;
}
resource->lock = SPIN_LOCK_INIT;
resource->ops.cleanup = &proc_cleanup_resource_mail;
resource->u.mail.resource = resource;
resource->type = PR_MAIL;
if (!ringbuffer_init (&resource->u.mail.ringbuffer, PROC_MAIL_MAX, sizeof (struct mail_packet))) {
id_free (&procgroup->rid_alloc, resource->rid);
free (resource);
spin_unlock (&procgroup->lock, fpg);
return NULL;
}
rbtree_insert (struct proc_resource, &procgroup->resource_tree, &resource->resource_tree_link,
resource_tree_link, rid);
spin_unlock (&procgroup->lock, fpg);
return resource;
}
void proc_delete_resource (struct procgroup* procgroup, struct proc_resource* resource,
struct reschedule_ctx* rctx) {
uint64_t fr, fpg;
spin_lock (&procgroup->lock, &fpg);
spin_lock (&resource->lock, &fr);
id_free (&procgroup->rid_alloc, resource->rid);
rbtree_delete (&procgroup->resource_tree, &resource->resource_tree_link);
spin_unlock (&resource->lock, fr);
spin_unlock (&procgroup->lock, fpg);
resource->ops.cleanup (resource, rctx);
free (resource);
}