Make pipes blocking
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
#include "src/kernel/keyboard.h"
|
#include "src/kernel/keyboard.h"
|
||||||
#include "src/kernel/idt.h"
|
#include "src/kernel/idt.h"
|
||||||
|
#include "src/kernel/process.h"
|
||||||
#include "src/kernel/stream.h"
|
#include "src/kernel/stream.h"
|
||||||
#include "src/kernel/util.h"
|
#include "src/kernel/util.h"
|
||||||
#include "src/lib/memory.h"
|
#include "src/lib/memory.h"
|
||||||
@@ -56,8 +57,10 @@ static uint64_t stream_write(__attribute__((unused)) const stream_t *self, __att
|
|||||||
}
|
}
|
||||||
|
|
||||||
static uint64_t stream_read(__attribute__((unused)) const stream_t *self, uint64_t max, char *to) {
|
static uint64_t stream_read(__attribute__((unused)) const stream_t *self, uint64_t max, char *to) {
|
||||||
if (max == 0 || buffer_length == 0) {
|
while (!buffer_length) {
|
||||||
return 0;
|
__asm__ volatile("sti");
|
||||||
|
process_next();
|
||||||
|
__asm__ volatile("cli");
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t size = buffer_length > max ? max : buffer_length;
|
uint64_t size = buffer_length > max ? max : buffer_length;
|
||||||
|
|||||||
+35
-27
@@ -1,4 +1,5 @@
|
|||||||
#include "src/kernel/pipe.h"
|
#include "src/kernel/pipe.h"
|
||||||
|
#include "src/kernel/process.h"
|
||||||
#include "src/kernel/stream.h"
|
#include "src/kernel/stream.h"
|
||||||
#include "src/lib/memory.h"
|
#include "src/lib/memory.h"
|
||||||
|
|
||||||
@@ -35,17 +36,18 @@ static uint64_t null_write(__attribute__((unused)) const stream_t *self, __attri
|
|||||||
static uint64_t pipe_read(const stream_t *self, uint64_t max, char *to) {
|
static uint64_t pipe_read(const stream_t *self, uint64_t max, char *to) {
|
||||||
pipe_t *pipe = ((pipe_read_stream_t *)self)->pipe;
|
pipe_t *pipe = ((pipe_read_stream_t *)self)->pipe;
|
||||||
|
|
||||||
|
while (pipe->begin == pipe->end) {
|
||||||
|
if (pipe->write_closed) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
__asm__ volatile("sti");
|
||||||
|
process_next();
|
||||||
|
__asm__ volatile("cli");
|
||||||
|
}
|
||||||
|
|
||||||
uint64_t available = (pipe->end + PIPE_BUFFER_SIZE - pipe->begin) % PIPE_BUFFER_SIZE;
|
uint64_t available = (pipe->end + PIPE_BUFFER_SIZE - pipe->begin) % PIPE_BUFFER_SIZE;
|
||||||
uint64_t to_read = available < max ? available : max;
|
uint64_t to_read = available < max ? available : max;
|
||||||
|
|
||||||
if (pipe->write_closed && !to_read) {
|
|
||||||
return (uint64_t)-1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!to_read) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pipe->begin + to_read <= PIPE_BUFFER_SIZE) {
|
if (pipe->begin + to_read <= PIPE_BUFFER_SIZE) {
|
||||||
memory_copy(pipe->buffer + pipe->begin, to_read, to);
|
memory_copy(pipe->buffer + pipe->begin, to_read, to);
|
||||||
} else {
|
} else {
|
||||||
@@ -61,27 +63,33 @@ static uint64_t pipe_read(const stream_t *self, uint64_t max, char *to) {
|
|||||||
static uint64_t pipe_write(const stream_t *self, const char *from, uint64_t bytes) {
|
static uint64_t pipe_write(const stream_t *self, const char *from, uint64_t bytes) {
|
||||||
pipe_t *pipe = ((pipe_read_stream_t *)self)->pipe;
|
pipe_t *pipe = ((pipe_read_stream_t *)self)->pipe;
|
||||||
|
|
||||||
if (pipe->read_closed) {
|
uint64_t written = 0;
|
||||||
return (uint64_t)-1;
|
while (written < bytes) {
|
||||||
|
while (((pipe->end + 1) % PIPE_BUFFER_SIZE) == pipe->begin) {
|
||||||
|
if (pipe->read_closed) {
|
||||||
|
return written;
|
||||||
|
}
|
||||||
|
__asm__ volatile("sti");
|
||||||
|
process_next();
|
||||||
|
__asm__ volatile("cli");
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t available = PIPE_BUFFER_SIZE - (pipe->end + PIPE_BUFFER_SIZE - pipe->begin) % PIPE_BUFFER_SIZE - 1;
|
||||||
|
uint64_t to_write = available < bytes - written ? available : bytes - written;
|
||||||
|
|
||||||
|
if (pipe->end + to_write <= PIPE_BUFFER_SIZE) {
|
||||||
|
memory_copy(from, to_write, pipe->buffer + pipe->end);
|
||||||
|
} else {
|
||||||
|
uint64_t chunk = PIPE_BUFFER_SIZE - pipe->end;
|
||||||
|
memory_copy(from, chunk, pipe->buffer + pipe->end);
|
||||||
|
memory_copy(from + chunk, to_write - chunk, pipe->buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
pipe->end = (pipe->end + to_write) % PIPE_BUFFER_SIZE;
|
||||||
|
written += to_write;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t available = PIPE_BUFFER_SIZE - (pipe->end + PIPE_BUFFER_SIZE - pipe->begin) % PIPE_BUFFER_SIZE;
|
return written;
|
||||||
uint64_t to_write = available < bytes ? available : bytes;
|
|
||||||
|
|
||||||
if (to_write == 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pipe->end + to_write <= PIPE_BUFFER_SIZE) {
|
|
||||||
memory_copy(from, to_write, pipe->buffer + pipe->end);
|
|
||||||
} else {
|
|
||||||
uint64_t chunk = PIPE_BUFFER_SIZE - pipe->end;
|
|
||||||
memory_copy(from, chunk, pipe->buffer + pipe->end);
|
|
||||||
memory_copy(from + chunk, to_write - chunk, pipe->buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
pipe->end = (pipe->end + to_write) % PIPE_BUFFER_SIZE;
|
|
||||||
return to_write;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void read_close(stream_t *self) {
|
static void read_close(stream_t *self) {
|
||||||
|
|||||||
@@ -126,7 +126,9 @@ void process_next() {
|
|||||||
process_t *prev = current_process;
|
process_t *prev = current_process;
|
||||||
tss.rsp0 = (uint64_t)next->kernel_stack;
|
tss.rsp0 = (uint64_t)next->kernel_stack;
|
||||||
current_process = next;
|
current_process = next;
|
||||||
|
__asm__ volatile("cli");
|
||||||
process_switch_to(&prev->kernel_rsp, next->kernel_rsp, VIRT_TO_PHYS(next->pml4));
|
process_switch_to(&prev->kernel_rsp, next->kernel_rsp, VIRT_TO_PHYS(next->pml4));
|
||||||
|
__asm__ volatile("sti");
|
||||||
}
|
}
|
||||||
|
|
||||||
void process_destroy(process_t *proc) {
|
void process_destroy(process_t *proc) {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
#include "src/kernel/path.h"
|
#include "src/kernel/path.h"
|
||||||
#include "src/kernel/process.h"
|
#include "src/kernel/process.h"
|
||||||
#include "src/kernel/stream.h"
|
#include "src/kernel/stream.h"
|
||||||
#include "src/kernel/tss.h"
|
|
||||||
#include "src/lib/layout.h"
|
#include "src/lib/layout.h"
|
||||||
#include "src/lib/memory.h"
|
#include "src/lib/memory.h"
|
||||||
#include "src/lib/string.h"
|
#include "src/lib/string.h"
|
||||||
|
|||||||
@@ -108,16 +108,14 @@ uint64_t main() {
|
|||||||
char chunk[64];
|
char chunk[64];
|
||||||
uint64_t received;
|
uint64_t received;
|
||||||
|
|
||||||
while (1) {
|
while ((received = read(0, sizeof(chunk), chunk))) {
|
||||||
if ((received = read(0, sizeof(chunk), chunk))) {
|
for (uint64_t i = 0; i < received; i++) {
|
||||||
for (uint64_t i = 0; i < received; i++) {
|
if (chunk[i] == '\n') {
|
||||||
if (chunk[i] == '\n') {
|
line[offset] = '\0';
|
||||||
line[offset] = '\0';
|
execute(line);
|
||||||
execute(line);
|
offset = 0;
|
||||||
offset = 0;
|
} else if (offset < 255) {
|
||||||
} else if (offset < 255) {
|
line[offset++] = chunk[i];
|
||||||
line[offset++] = chunk[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -196,11 +196,9 @@ uint64_t main() {
|
|||||||
memory_set('\b', PROMPT_LENGTH, (char *)bs);
|
memory_set('\b', PROMPT_LENGTH, (char *)bs);
|
||||||
memory_set(' ', PROMPT_LENGTH, (char *)ws);
|
memory_set(' ', PROMPT_LENGTH, (char *)ws);
|
||||||
|
|
||||||
while (1) {
|
char c;
|
||||||
char c;
|
while (read(0, 1, &c)) {
|
||||||
if (read(0, 1, &c)) {
|
on_char_received(c);
|
||||||
on_char_received(c);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user