Skip to content

Commit 614ec67

Browse files
authored
wasip3: Refactor handling of eof callback (#794)
Execute this in the wrapper functions around the "guts" instead of within the guts themselves where it's more easily forgotten.
1 parent 2f31cf1 commit 614ec67

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

libc-bottom-half/sources/file_utils.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ static ssize_t __wasilibc_write_without_offset_update(wasi_write_t *write,
371371
// If this stream is closed, for example with a TCP shutdown, then it's
372372
// closed and we're at EOF.
373373
if (state->stream == 0)
374-
return write->eof(write->eof_data);
374+
return 0;
375375

376376
// First resolve any pending I/O, should it exist.
377377
if (wasip3_write_resolve_pending(write) < 0)
@@ -386,7 +386,7 @@ static ssize_t __wasilibc_write_without_offset_update(wasi_write_t *write,
386386

387387
// If this stream is complete, then delegate to EOF.
388388
if (state->flags & WASIP3_IO_DONE)
389-
return write->eof(write->eof_data);
389+
return 0;
390390

391391
// For simplicity, handle blocking writes directly here. This involves
392392
// blocking with an optional timeout and handling the result.
@@ -401,8 +401,6 @@ static ssize_t __wasilibc_write_without_offset_update(wasi_write_t *write,
401401
state->flags |= WASIP3_IO_DONE;
402402
if (amount < 0)
403403
return -1;
404-
if (amount == 0 && done)
405-
return write->eof(write->eof_data);
406404
return amount;
407405
}
408406

@@ -435,7 +433,7 @@ static ssize_t __wasilibc_write_without_offset_update(wasi_write_t *write,
435433
if (amount > 0)
436434
return amount;
437435
if (state->flags & WASIP3_IO_DONE)
438-
return write->eof(write->eof_data);
436+
return 0;
439437
}
440438

441439
// If, at this point, we're still not forced to buffer then it means that a
@@ -448,7 +446,7 @@ static ssize_t __wasilibc_write_without_offset_update(wasi_write_t *write,
448446
return -1;
449447
}
450448
if (state->flags & WASIP3_IO_DONE)
451-
return write->eof(write->eof_data);
449+
return 0;
452450

453451
// The zero-length write succeeded, so turn the loop again to retry the
454452
// write of the user-supplied buffer. Note that the I/O "should be ready"
@@ -507,6 +505,11 @@ ssize_t __wasilibc_write(wasi_write_t *write, const void *buffer,
507505
__wasilibc_write_without_offset_update(write, buffer, length);
508506
if (result > 0 && write->offset)
509507
*write->offset += result;
508+
#ifndef __wasip2__
509+
if (result == 0 &&
510+
((write->state->flags & WASIP3_IO_DONE) || (write->state->stream == 0)))
511+
return write->eof(write->eof_data);
512+
#endif
510513
return result;
511514
}
512515

@@ -583,7 +586,7 @@ static ssize_t __wasilibc_read_without_offset_update(wasi_read_t *read,
583586
// If this stream is closed, for example with a TCP shutdown, then it's
584587
// closed and we're at EOF.
585588
if (state->stream == 0)
586-
return read->eof(read->eof_data);
589+
return 0;
587590

588591
// If there's active I/O in progress for this stream then this must wait for
589592
// it to complete.
@@ -619,7 +622,7 @@ static ssize_t __wasilibc_read_without_offset_update(wasi_read_t *read,
619622

620623
// If this stream has finished, then delegate to EOF.
621624
if (state->flags & WASIP3_IO_DONE)
622-
return read->eof(read->eof_data);
625+
return 0;
623626

624627
// For simplicity handle the blocking read case here.
625628
if (read->blocking) {
@@ -632,8 +635,6 @@ static ssize_t __wasilibc_read_without_offset_update(wasi_read_t *read,
632635
state->flags |= WASIP3_IO_DONE;
633636
if (amount < 0)
634637
return -1;
635-
if (amount == 0 && done)
636-
return read->eof(read->eof_data);
637638
return amount;
638639
}
639640

@@ -658,7 +659,7 @@ static ssize_t __wasilibc_read_without_offset_update(wasi_read_t *read,
658659
if (amount > 0)
659660
return amount;
660661
if (state->flags & WASIP3_IO_DONE)
661-
return read->eof(read->eof_data);
662+
return 0;
662663
}
663664

664665
if (!(state->flags & WASIP3_IO_MUST_BUFFER)) {
@@ -667,7 +668,7 @@ static ssize_t __wasilibc_read_without_offset_update(wasi_read_t *read,
667668
return -1;
668669
}
669670
if (state->flags & WASIP3_IO_DONE)
670-
return read->eof(read->eof_data);
671+
return 0;
671672

672673
assert(state->flags & WASIP3_IO_SHOULD_BE_READY);
673674
continue;
@@ -691,6 +692,11 @@ ssize_t __wasilibc_read(wasi_read_t *read, void *buffer, size_t length) {
691692
ssize_t result = __wasilibc_read_without_offset_update(read, buffer, length);
692693
if (result > 0 && read->offset)
693694
*read->offset += result;
695+
#ifndef __wasip2__
696+
if (result == 0 &&
697+
((read->state->flags & WASIP3_IO_DONE) || (read->state->stream == 0)))
698+
return read->eof(read->eof_data);
699+
#endif
694700
return result;
695701
}
696702

0 commit comments

Comments
 (0)