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