Skip to content

Commit

Permalink
add io flush
Browse files Browse the repository at this point in the history
  • Loading branch information
enricozb committed Jun 18, 2024
1 parent 13c8d18 commit 5938a0b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
25 changes: 23 additions & 2 deletions src/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ Port io_open(Net* net, Book* book, Port argm) {
Port io_close(Net* net, Book* book, Port argm) {
FILE* fp = readback_file(argm);
if (fp == NULL) {
fprintf(stderr, "io_close: failed to close\n");
fprintf(stderr, "io_close: invalid file descriptor\n");
return new_port(ERA, 0);
}

Expand Down Expand Up @@ -383,6 +383,23 @@ Port io_write(Net* net, Book* book, Port argm) {
return new_port(ERA, 0);
}

// Flushes an output stream.
Port io_flush(Net* net, Book* book, Port argm) {
FILE* fp = readback_file(argm);
if (fp == NULL) {
fprintf(stderr, "io_flush: invalid file descriptor\n");
return new_port(ERA, 0);
}

int err = fflush(fp) != 0;
if (err != 0) {
fprintf(stderr, "io_flush: failed to flush: %i\n", err);
return new_port(ERA, 0);
}

return new_port(ERA, 0);
}

// Seeks to a position in a file.
// `argm` is a 3-tuple (CON fd (CON offset whence)), where
// - fd is a file descriptor
Expand Down Expand Up @@ -473,6 +490,7 @@ void book_init(Book* book) {
book->ffns_buf[book->ffns_len++] = (FFn){"READ", io_read};
book->ffns_buf[book->ffns_len++] = (FFn){"OPEN", io_open};
book->ffns_buf[book->ffns_len++] = (FFn){"CLOSE", io_close};
book->ffns_buf[book->ffns_len++] = (FFn){"FLUSH", io_flush};
book->ffns_buf[book->ffns_len++] = (FFn){"WRITE", io_write};
book->ffns_buf[book->ffns_len++] = (FFn){"SEEK", io_seek};
book->ffns_buf[book->ffns_len++] = (FFn){"GET_TIME", io_get_time};
Expand All @@ -484,7 +502,10 @@ void book_init(Book* book) {

// Runs an IO computation.
void do_run_io(Net* net, Book* book, Port port) {
book_init(book);
book_init(book);

setlinebuf(stdout);
setlinebuf(stderr);

// IO loop
while (TRUE) {
Expand Down
25 changes: 23 additions & 2 deletions src/run.cu
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ Port io_open(GNet* gnet, Port argm) {
Port io_close(GNet* gnet, Port argm) {
FILE* fp = readback_file(argm);
if (fp == NULL) {
fprintf(stderr, "io_close: failed to close\n");
fprintf(stderr, "io_close: invalid file descriptor\n");
return new_port(ERA, 0);
}

Expand All @@ -392,6 +392,23 @@ Port io_close(GNet* gnet, Port argm) {
return new_port(ERA, 0);
}

// Flushes an output stream.
Port io_flush(GNet* gnet, Port argm) {
FILE* fp = readback_file(argm);
if (fp == NULL) {
fprintf(stderr, "io_flush: invalid file descriptor\n");
return new_port(ERA, 0);
}

int err = fflush(fp) != 0;
if (err != 0) {
fprintf(stderr, "io_flush: failed to flush: %i\n", err);
return new_port(ERA, 0);
}

return new_port(ERA, 0);
}

// Writes a list of bytes to a file.
// `argm` is a tuple (CON node) of the
// file descriptor and list of bytes to write.
Expand Down Expand Up @@ -502,6 +519,7 @@ void book_init(Book* book) {
book->ffns_buf[book->ffns_len++] = (FFn){"READ", io_read};
book->ffns_buf[book->ffns_len++] = (FFn){"OPEN", io_open};
book->ffns_buf[book->ffns_len++] = (FFn){"CLOSE", io_close};
book->ffns_buf[book->ffns_len++] = (FFn){"FLUSH", io_flush};
book->ffns_buf[book->ffns_len++] = (FFn){"WRITE", io_write};
book->ffns_buf[book->ffns_len++] = (FFn){"SEEK", io_seek};
book->ffns_buf[book->ffns_len++] = (FFn){"GET_TIME", io_get_time};
Expand All @@ -515,7 +533,10 @@ void book_init(Book* book) {

// Runs an IO computation.
void do_run_io(GNet* gnet, Book* book, Port port) {
book_init(book);
book_init(book);

setlinebuf(stdout);
setlinebuf(stderr);

// IO loop
while (TRUE) {
Expand Down

0 comments on commit 5938a0b

Please sign in to comment.