1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
5#include "mos/io/io.h"
6#include "mos/tasks/wait.h"
7
8#include <mos/lib/structures/ring_buffer.h>
9
10typedef struct
11{
12 u32 magic;
13 waitlist_t waitlist; ///< for both reader and writer, only one party can wait on the pipe at a time
14 spinlock_t lock; ///< protects the buffer_pos (and thus the buffer)
15 bool other_closed; ///< true if the other end of the pipe has been closed
16 void *buffers;
17 ring_buffer_pos_t buffer_pos;
18} pipe_t;
19
20pipe_t *pipe_create(size_t bufsize);
21size_t pipe_read(pipe_t *pipe, void *buf, size_t size);
22size_t pipe_write(pipe_t *pipe, const void *buf, size_t size);
23
24/**
25 * @brief Close one end of the pipe, so that the other end will get EOF.
26 * @note The other end should also call this function to get the pipe correctly freed.
27 *
28 * @param pipe The pipe to close one end of.
29 * @return true if the pipe was fully closed, false if the other end is still open.
30 */
31__nodiscard bool pipe_close_one_end(pipe_t *pipe);
32
33typedef struct
34{
35 io_t io_r, io_w;
36 pipe_t *pipe;
37} pipeio_t;
38
39pipeio_t *pipeio_create(pipe_t *pipe);
40