Add wrappers for pipe
[lttng-tools.git] / src / common / pipe.h
1 /*
2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #ifndef LTTNG_PIPE_H
19 #define LTTNG_PIPE_H
20
21 #include <pthread.h>
22
23 enum lttng_pipe_state {
24 LTTNG_PIPE_STATE_OPENED = 1,
25 LTTNG_PIPE_STATE_CLOSED = 2,
26 };
27
28 struct lttng_pipe {
29 /* Read: 0, Write: 1. */
30 int fd[2];
31 /*
32 * Flags of the pipe once opened. pipe(2) specifies either O_NONBLOCK or
33 * O_CLOEXEC can be used. Flags are set using fcntl(2) call.
34 */
35 int flags;
36
37 /*
38 * These states are protected by the operation mutex below.
39 */
40 enum lttng_pipe_state r_state;
41 enum lttng_pipe_state w_state;
42
43 /* Held for each read(2) operation. */
44 pthread_mutex_t read_mutex;
45 /* Held for each write(2) operation. */
46 pthread_mutex_t write_mutex;
47 };
48
49 /*
50 * Return 1 if read side is open else 0.
51 */
52 static inline int lttng_pipe_is_read_open(struct lttng_pipe *pipe)
53 {
54 return pipe->r_state == LTTNG_PIPE_STATE_OPENED ? 1 : 0;
55 }
56
57 /*
58 * Return 1 if write side is open else 0.
59 */
60 static inline int lttng_pipe_is_write_open(struct lttng_pipe *pipe)
61 {
62 return pipe->w_state == LTTNG_PIPE_STATE_OPENED ? 1 : 0;
63 }
64
65 static inline int lttng_pipe_get_readfd(struct lttng_pipe *pipe)
66 {
67 return pipe->fd[0];
68 }
69
70 static inline int lttng_pipe_get_writefd(struct lttng_pipe *pipe)
71 {
72 return pipe->fd[1];
73 }
74
75 struct lttng_pipe *lttng_pipe_open(int flags);
76 int lttng_pipe_write_close(struct lttng_pipe *pipe);
77 int lttng_pipe_read_close(struct lttng_pipe *pipe);
78 /* Close both side of pipe. */
79 int lttng_pipe_close(struct lttng_pipe *pipe);
80 void lttng_pipe_destroy(struct lttng_pipe *pipe);
81
82 ssize_t lttng_pipe_read(struct lttng_pipe *pipe, void *buf, size_t count);
83 ssize_t lttng_pipe_write(struct lttng_pipe *pipe, const void *buf,
84 size_t count);
85
86 #endif /* LTTNG_PIPE_H */
This page took 0.044902 seconds and 5 git commands to generate.