Implement streams IPC mechanism
All checks were successful
Build documentation / build-and-deploy (push) Successful in 3m47s
All checks were successful
Build documentation / build-and-deploy (push) Successful in 3m47s
This commit is contained in:
58
kernel/proc/stream.c
Normal file
58
kernel/proc/stream.c
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user