X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Fpipe.h;fp=src%2Fcommon%2Fpipe.h;h=11f41cf1543b7dfe03aed2cb866c49ee2d76dbd0;hp=0000000000000000000000000000000000000000;hb=9fd926379bd4c6c17f2b3c39a5bbf9879bc74e14;hpb=b31398bb2b3fa91a53dea3b36fd693da4b50e0d3 diff --git a/src/common/pipe.h b/src/common/pipe.h new file mode 100644 index 000000000..11f41cf15 --- /dev/null +++ b/src/common/pipe.h @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2013 - David Goulet + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef LTTNG_PIPE_H +#define LTTNG_PIPE_H + +#include + +enum lttng_pipe_state { + LTTNG_PIPE_STATE_OPENED = 1, + LTTNG_PIPE_STATE_CLOSED = 2, +}; + +struct lttng_pipe { + /* Read: 0, Write: 1. */ + int fd[2]; + /* + * Flags of the pipe once opened. pipe(2) specifies either O_NONBLOCK or + * O_CLOEXEC can be used. Flags are set using fcntl(2) call. + */ + int flags; + + /* + * These states are protected by the operation mutex below. + */ + enum lttng_pipe_state r_state; + enum lttng_pipe_state w_state; + + /* Held for each read(2) operation. */ + pthread_mutex_t read_mutex; + /* Held for each write(2) operation. */ + pthread_mutex_t write_mutex; +}; + +/* + * Return 1 if read side is open else 0. + */ +static inline int lttng_pipe_is_read_open(struct lttng_pipe *pipe) +{ + return pipe->r_state == LTTNG_PIPE_STATE_OPENED ? 1 : 0; +} + +/* + * Return 1 if write side is open else 0. + */ +static inline int lttng_pipe_is_write_open(struct lttng_pipe *pipe) +{ + return pipe->w_state == LTTNG_PIPE_STATE_OPENED ? 1 : 0; +} + +static inline int lttng_pipe_get_readfd(struct lttng_pipe *pipe) +{ + return pipe->fd[0]; +} + +static inline int lttng_pipe_get_writefd(struct lttng_pipe *pipe) +{ + return pipe->fd[1]; +} + +struct lttng_pipe *lttng_pipe_open(int flags); +int lttng_pipe_write_close(struct lttng_pipe *pipe); +int lttng_pipe_read_close(struct lttng_pipe *pipe); +/* Close both side of pipe. */ +int lttng_pipe_close(struct lttng_pipe *pipe); +void lttng_pipe_destroy(struct lttng_pipe *pipe); + +ssize_t lttng_pipe_read(struct lttng_pipe *pipe, void *buf, size_t count); +ssize_t lttng_pipe_write(struct lttng_pipe *pipe, const void *buf, + size_t count); + +#endif /* LTTNG_PIPE_H */