Files
mop3/kernel/proc/resource.c
kamkow1 e5ebd7f3ba
All checks were successful
Build ISO image / build-and-deploy (push) Successful in 2m21s
Build documentation / build-and-deploy (push) Successful in 54s
Use a big-lock for kernel sychronization instead of fine-grained locking
2026-04-27 18:06:02 +02:00

119 lines
3.0 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) {
struct proc_resource* resource = NULL;
rbtree_find(struct proc_resource, &procgroup->resource_tree, rid, resource, resource_tree_link,
rid);
return resource;
}
struct proc_resource* proc_create_resource_mutex(struct procgroup* procgroup) {
struct proc_resource* resource;
resource = malloc(sizeof(*resource));
if (resource == NULL)
return NULL;
memset(resource, 0, sizeof(*resource));
resource->rid = id_alloc(&procgroup->rid_alloc);
if (resource->rid < 0) {
free(resource);
return NULL;
}
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);
return resource;
}
struct proc_resource* proc_create_resource_mail(struct procgroup* procgroup) {
struct proc_resource* resource;
resource = malloc(sizeof(*resource));
if (resource == NULL)
return NULL;
memset(resource, 0, sizeof(*resource));
resource->rid = id_alloc(&procgroup->rid_alloc);
if (resource->rid < 0) {
free(resource);
return NULL;
}
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);
return resource;
}
struct proc_resource* proc_create_resource_stream(struct procgroup* procgroup) {
struct proc_resource* resource;
resource = malloc(sizeof(*resource));
if (resource == NULL)
return NULL;
memset(resource, 0, sizeof(*resource));
resource->rid = id_alloc(&procgroup->rid_alloc);
if (resource->rid < 0) {
free(resource);
return NULL;
}
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);
return NULL;
}
rbtree_insert(struct proc_resource, &procgroup->resource_tree, &resource->resource_tree_link,
resource_tree_link, rid);
return resource;
}
void proc_delete_resource(struct procgroup* procgroup, struct proc_resource* resource,
struct reschedule_ctx* rctx) {
id_free(&procgroup->rid_alloc, resource->rid);
rbtree_delete(&procgroup->resource_tree, &resource->resource_tree_link);
resource->ops.cleanup(resource, rctx);
free(resource);
}