@@ -352,8 +352,14 @@ extension AsyncIO {
352
352
)
353
353
var readLength : Int = 0
354
354
let signalStream = self . registerFileDescriptor ( fileDescriptor, for: . read)
355
+ /// Outer loop: every iteration signals we are ready to read more data
355
356
for try await _ in signalStream {
356
- // Every iteration signals we are ready to read more data
357
+ /// Inner loop: repeatedly call `.read()` and read more data until:
358
+ /// 1. We reached EOF (read length is 0), in which case return the result
359
+ /// 2. We read `maxLength` bytes, in which case return the result
360
+ /// 3. `read()` returns -1 and sets `errno` to `EAGAIN` or `EWOULDBLOCK`. In
361
+ /// this case we `break` out of the inner loop and wait `.read()` to be
362
+ /// ready by `await`ing the next signal in the outer loop.
357
363
while true {
358
364
let bytesRead = resultBuffer. withUnsafeMutableBufferPointer { bufferPointer in
359
365
// Get a pointer to the memory at the specified offset
@@ -417,7 +423,13 @@ extension AsyncIO {
417
423
let fileDescriptor = diskIO. fileDescriptor
418
424
let signalStream = self . registerFileDescriptor ( fileDescriptor, for: . write)
419
425
var writtenLength : Int = 0
426
+ /// Outer loop: every iteration signals we are ready to read more data
420
427
for try await _ in signalStream {
428
+ /// Inner loop: repeatedly call `.write()` and write more data until:
429
+ /// 1. We've written bytes.count bytes.
430
+ /// 3. `.write()` returns -1 and sets `errno` to `EAGAIN` or `EWOULDBLOCK`. In
431
+ /// this case we `break` out of the inner loop and wait `.write()` to be
432
+ /// ready by `await`ing the next signal in the outer loop.
421
433
while true {
422
434
let written = bytes. withUnsafeBytes { ptr in
423
435
let remainingLength = ptr. count - writtenLength
@@ -454,7 +466,13 @@ extension AsyncIO {
454
466
let fileDescriptor = diskIO. fileDescriptor
455
467
let signalStream = self . registerFileDescriptor ( fileDescriptor, for: . write)
456
468
var writtenLength : Int = 0
469
+ /// Outer loop: every iteration signals we are ready to read more data
457
470
for try await _ in signalStream {
471
+ /// Inner loop: repeatedly call `.write()` and write more data until:
472
+ /// 1. We've written bytes.count bytes.
473
+ /// 3. `.write()` returns -1 and sets `errno` to `EAGAIN` or `EWOULDBLOCK`. In
474
+ /// this case we `break` out of the inner loop and wait `.write()` to be
475
+ /// ready by `await`ing the next signal in the outer loop.
458
476
while true {
459
477
let written = span. withUnsafeBytes { ptr in
460
478
let remainingLength = ptr. count - writtenLength
0 commit comments