Files
mop3/kernel/proc/resource.c
kamkow1 c8fb575bdd
All checks were successful
Build ISO image / build-and-deploy (push) Successful in 2m7s
Build documentation / build-and-deploy (push) Successful in 39s
Change formatting rules
2026-04-24 01:54:48 +02:00

156 lines
3.8 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 <proc/stream.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;
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_stream(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_stream;
resource->u.stream.resource = resource;
resource->type = PR_STREAM;
if (!ringbuffer_init(&resource->u.stream.ringbuffer, PROC_STREAM_MAX, 1)) {
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);
}