57 lines
1.3 KiB
C
57 lines
1.3 KiB
C
#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_stream* stream, 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_stream* stream, 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);
|
|
}
|