pipe.odin 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package os2
  2. /*
  3. Create an anonymous pipe.
  4. This procedure creates an anonymous pipe, returning two ends of the pipe, `r`
  5. and `w`. The file `r` is the readable end of the pipe. The file `w` is a
  6. writeable end of the pipe.
  7. Pipes are used as an inter-process communication mechanism, to communicate
  8. between a parent and a child process. The child uses one end of the pipe to
  9. write data, and the parent uses the other end to read from the pipe
  10. (or vice-versa). When a parent passes one of the ends of the pipe to the child
  11. process, that end of the pipe needs to be closed by the parent, before any data
  12. is attempted to be read.
  13. Although pipes look like files and is compatible with most file APIs in package
  14. os2, the way it's meant to be read is different. Due to asynchronous nature of
  15. the communication channel, the data may not be present at the time of a read
  16. request. The other scenario is when a pipe has no data because the other end
  17. of the pipe was closed by the child process.
  18. */
  19. @(require_results)
  20. pipe :: proc() -> (r, w: ^File, err: Error) {
  21. return _pipe()
  22. }
  23. /*
  24. Check if the pipe has any data.
  25. This procedure checks whether a read-end of the pipe has data that can be
  26. read, and returns `true`, if the pipe has readable data, and `false` if the
  27. pipe is empty. This procedure does not block the execution of the current
  28. thread.
  29. **Note**: If the other end of the pipe was closed by the child process, the
  30. `.Broken_Pipe`
  31. can be returned by this procedure. Handle these errors accordingly.
  32. */
  33. @(require_results)
  34. pipe_has_data :: proc(r: ^File) -> (ok: bool, err: Error) {
  35. return _pipe_has_data(r)
  36. }