feat: introduced stream to separate actual io and other tasks
This commit is contained in:
@@ -2,9 +2,19 @@
|
||||
// Created by rick on 28-01-21.
|
||||
//
|
||||
|
||||
#include <util/stream.h>
|
||||
#include <libc/string.h>
|
||||
#include <stdbool.h>
|
||||
#include <attributes.h>
|
||||
#include <tasks/task.h>
|
||||
#include <libc/libc.h>
|
||||
#include "kprint.h"
|
||||
|
||||
#define MAX_HANDLERS 8
|
||||
#define STREAM_SIZE 512
|
||||
#define PRINT_BUFFER_SIZE 64
|
||||
|
||||
stream_t *kprint_stream;
|
||||
|
||||
kprint_handler handlers[MAX_HANDLERS] = {
|
||||
NULL,
|
||||
@@ -28,10 +38,36 @@ void kprint_register(kprint_handler handler) {
|
||||
}
|
||||
|
||||
void kprint(const char *msg) {
|
||||
stream_write(kprint_stream, (const uint8_t *) msg, strlen(msg));
|
||||
}
|
||||
|
||||
void kprint_internal(const char *msg) {
|
||||
for (int i = 0; i < MAX_HANDLERS; ++i) {
|
||||
if (handlers[i] == NULL) {
|
||||
continue;
|
||||
}
|
||||
handlers[i](msg);
|
||||
}
|
||||
}
|
||||
|
||||
void kprint_init() {
|
||||
kprint_stream = stream_create(STREAM_SIZE);
|
||||
}
|
||||
|
||||
void noreturn kprint_task(void *_) {
|
||||
uint32_t last_read = 0;
|
||||
uint8_t data[PRINT_BUFFER_SIZE + 1] = {0};
|
||||
while (true) {
|
||||
stream_wait(kprint_stream);
|
||||
while (true) {
|
||||
last_read = stream_read(kprint_stream, data, PRINT_BUFFER_SIZE);
|
||||
if (last_read == 0) break;
|
||||
data[last_read] = 0;
|
||||
kprint_internal((const char *) data);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
void kprint_start_task() {
|
||||
task_spawn(kprint_task, NULL);
|
||||
}
|
||||
Reference in New Issue
Block a user