pipe_linux.odin 866 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #+private
  2. package os2
  3. import "core:sys/linux"
  4. _pipe :: proc() -> (r, w: ^File, err: Error) {
  5. fds: [2]linux.Fd
  6. errno := linux.pipe2(&fds, {.CLOEXEC})
  7. if errno != .NONE {
  8. return nil, nil,_get_platform_error(errno)
  9. }
  10. r = _new_file(uintptr(fds[0])) or_return
  11. w = _new_file(uintptr(fds[1])) or_return
  12. return
  13. }
  14. @(require_results)
  15. _pipe_has_data :: proc(r: ^File) -> (ok: bool, err: Error) {
  16. if r == nil || r.impl == nil {
  17. return false, nil
  18. }
  19. fd := linux.Fd((^File_Impl)(r.impl).fd)
  20. poll_fds := []linux.Poll_Fd {
  21. linux.Poll_Fd {
  22. fd = fd,
  23. events = {.IN, .HUP},
  24. },
  25. }
  26. n, errno := linux.poll(poll_fds, 0)
  27. if n != 1 || errno != nil {
  28. return false, _get_platform_error(errno)
  29. }
  30. pipe_events := poll_fds[0].revents
  31. if pipe_events >= {.IN} {
  32. return true, nil
  33. }
  34. if pipe_events >= {.HUP} {
  35. return false, .Broken_Pipe
  36. }
  37. return false, nil
  38. }