Implement streams IPC mechanism
All checks were successful
Build documentation / build-and-deploy (push) Successful in 3m47s

This commit is contained in:
2026-03-18 22:27:56 +01:00
parent 77ab25bcee
commit 80a728f04b
22 changed files with 311 additions and 50 deletions

View File

@@ -183,6 +183,18 @@ struct procgroup* procgroup_create (void) {
return NULL;
}
if (proc_create_resource_stream (procgroup) == NULL) {
id_alloc_fini (&procgroup->rid_alloc);
free (procgroup);
return NULL;
}
if (proc_create_resource_stream (procgroup) == NULL) {
id_alloc_fini (&procgroup->rid_alloc);
free (procgroup);
return NULL;
}
spin_lock (&procgroup_tree_lock, &fpgt);
rbtree_insert (struct procgroup, &procgroup_tree, &procgroup->procgroup_tree_link,
procgroup_tree_link, pgid);

View File

@@ -12,6 +12,7 @@
#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>
@@ -96,6 +97,46 @@ struct proc_resource* proc_create_resource_mail (struct procgroup* procgroup) {
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.mail.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;

View File

@@ -6,10 +6,12 @@
#include <libk/std.h>
#include <proc/mail.h>
#include <proc/mutex.h>
#include <proc/stream.h>
#include <sync/spin_lock.h>
#define PR_MUTEX 0
#define PR_MAIL 1
#define PR_MUTEX 0
#define PR_MAIL 1
#define PR_STREAM 2
struct proc;
struct procgroup;
@@ -24,6 +26,7 @@ struct proc_resource {
union {
struct proc_mutex mutex;
struct proc_mail mail;
struct proc_stream stream;
} u;
struct {
void (*cleanup) (struct proc_resource* resource, struct reschedule_ctx* rctx);
@@ -36,6 +39,8 @@ struct proc_resource* proc_create_resource_mutex (struct procgroup* procgroup);
struct proc_resource* proc_create_resource_mail (struct procgroup* procgroup);
struct proc_resource* proc_create_resource_stream (struct procgroup* procgroup);
void proc_delete_resource (struct procgroup* procgroup, struct proc_resource* resource,
struct reschedule_ctx* rctx);

View File

@@ -4,7 +4,8 @@ c += proc/proc.c \
proc/procgroup.c \
proc/suspension_q.c \
proc/mail.c \
proc/env.c
proc/env.c \
proc/stream.c
o += proc/proc.o \
proc/resource.o \
@@ -12,4 +13,5 @@ o += proc/proc.o \
proc/procgroup.o \
proc/suspension_q.o \
proc/mail.o \
proc/env.o
proc/env.o \
proc/stream.o

58
kernel/proc/stream.c Normal file
View File

@@ -0,0 +1,58 @@
#include <libk/list.h>
#include <libk/ringbuffer.h>
#include <libk/std.h>
#include <proc/proc.h>
#include <proc/reschedule.h>
#include <proc/resource.h>
#include <proc/stream.h>
#include <proc/suspension_q.h>
void proc_stream_write (struct proc* proc, struct proc_stream* stream, struct reschedule_ctx* rctx,
void* data, size_t data_size) {
uint64_t fr, fsq;
spin_lock (&stream->resource->lock, &fr);
for (size_t i = 0; i < data_size; i++)
ringbuffer_push (uint8_t, &stream->ringbuffer, ((uint8_t*)data)[i]);
spin_unlock (&stream->resource->lock, fr);
}
size_t proc_stream_read (struct proc* proc, struct proc_stream* stream, struct reschedule_ctx* rctx,
void* out_data, size_t data_size) {
uint64_t fr;
size_t bytes = 0;
uint8_t* p = (uint8_t*)out_data;
spin_lock (&stream->resource->lock, &fr);
for (size_t i = 0; i < data_size; i++) {
if (stream->ringbuffer.count == 0) {
break;
}
ringbuffer_pop (uint8_t, &stream->ringbuffer, &p[i]);
bytes++;
}
spin_unlock (&stream->resource->lock, fr);
return bytes;
}
void proc_cleanup_resource_stream (struct proc_resource* resource, struct reschedule_ctx* rctx) {
(void)rctx;
uint64_t fr;
struct proc_stream* stream = &resource->u.stream;
spin_lock (&stream->resource->lock, &fr);
ringbuffer_fini (&stream->ringbuffer);
spin_unlock (&stream->resource->lock, fr);
}

29
kernel/proc/stream.h Normal file
View File

@@ -0,0 +1,29 @@
#ifndef _KERNEL_PROC_STREAM_H
#define _KERNEL_PROC_STREAM_H
#include <libk/list.h>
#include <libk/ringbuffer.h>
#include <libk/std.h>
#include <proc/suspension_q.h>
#define PROC_STREAM_MAX (1024 * 1024)
struct proc;
struct proc_resource;
struct cpu;
struct reschedule_ctx;
struct proc_stream {
struct proc_resource* resource;
struct ringbuffer ringbuffer;
};
void proc_cleanup_resource_stream (struct proc_resource* resource, struct reschedule_ctx* rctx);
void proc_stream_write (struct proc* proc, struct proc_stream* stream, struct reschedule_ctx* rctx,
void* data, size_t data_size);
size_t proc_stream_read (struct proc* proc, struct proc_stream* stream, struct reschedule_ctx* rctx,
void* out_data, size_t data_size);
#endif // _KERNEL_PROC_STREAM_H