Commit | Line | Data |
---|---|---|
9fd92637 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> |
9fd92637 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
9fd92637 | 5 | * |
9fd92637 DG |
6 | */ |
7 | ||
8 | #ifndef LTTNG_PIPE_H | |
9 | #define LTTNG_PIPE_H | |
10 | ||
11 | #include <pthread.h> | |
a247c6e0 | 12 | #include <common/macros.h> |
40dde31f | 13 | #include <sys/types.h> |
9fd92637 | 14 | |
7966af57 SM |
15 | #ifdef __cplusplus |
16 | extern "C" { | |
17 | #endif | |
18 | ||
9fd92637 DG |
19 | enum lttng_pipe_state { |
20 | LTTNG_PIPE_STATE_OPENED = 1, | |
21 | LTTNG_PIPE_STATE_CLOSED = 2, | |
22 | }; | |
23 | ||
24 | struct lttng_pipe { | |
25 | /* Read: 0, Write: 1. */ | |
26 | int fd[2]; | |
27 | /* | |
28 | * Flags of the pipe once opened. pipe(2) specifies either O_NONBLOCK or | |
29 | * O_CLOEXEC can be used. Flags are set using fcntl(2) call. | |
30 | */ | |
31 | int flags; | |
32 | ||
33 | /* | |
34 | * These states are protected by the operation mutex below. | |
35 | */ | |
36 | enum lttng_pipe_state r_state; | |
37 | enum lttng_pipe_state w_state; | |
38 | ||
39 | /* Held for each read(2) operation. */ | |
40 | pthread_mutex_t read_mutex; | |
41 | /* Held for each write(2) operation. */ | |
42 | pthread_mutex_t write_mutex; | |
43 | }; | |
44 | ||
45 | /* | |
46 | * Return 1 if read side is open else 0. | |
47 | */ | |
40b66b26 | 48 | static inline int lttng_pipe_is_read_open(const struct lttng_pipe *pipe) |
9fd92637 DG |
49 | { |
50 | return pipe->r_state == LTTNG_PIPE_STATE_OPENED ? 1 : 0; | |
51 | } | |
52 | ||
53 | /* | |
54 | * Return 1 if write side is open else 0. | |
55 | */ | |
40b66b26 | 56 | static inline int lttng_pipe_is_write_open(const struct lttng_pipe *pipe) |
9fd92637 DG |
57 | { |
58 | return pipe->w_state == LTTNG_PIPE_STATE_OPENED ? 1 : 0; | |
59 | } | |
60 | ||
40b66b26 | 61 | static inline int lttng_pipe_get_readfd(const struct lttng_pipe *pipe) |
9fd92637 DG |
62 | { |
63 | return pipe->fd[0]; | |
64 | } | |
65 | ||
40b66b26 | 66 | static inline int lttng_pipe_get_writefd(const struct lttng_pipe *pipe) |
9fd92637 DG |
67 | { |
68 | return pipe->fd[1]; | |
69 | } | |
70 | ||
71 | struct lttng_pipe *lttng_pipe_open(int flags); | |
9c2bd8db JG |
72 | struct lttng_pipe *lttng_pipe_named_open(const char *path, mode_t mode, |
73 | int flags); | |
9fd92637 DG |
74 | int lttng_pipe_write_close(struct lttng_pipe *pipe); |
75 | int lttng_pipe_read_close(struct lttng_pipe *pipe); | |
76 | /* Close both side of pipe. */ | |
77 | int lttng_pipe_close(struct lttng_pipe *pipe); | |
78 | void lttng_pipe_destroy(struct lttng_pipe *pipe); | |
79 | ||
80 | ssize_t lttng_pipe_read(struct lttng_pipe *pipe, void *buf, size_t count); | |
81 | ssize_t lttng_pipe_write(struct lttng_pipe *pipe, const void *buf, | |
82 | size_t count); | |
55dfb029 | 83 | /* Returns and releases the read end of the pipe. */ |
55dfb029 JG |
84 | int lttng_pipe_release_readfd(struct lttng_pipe *pipe); |
85 | /* Returns and releases the write end of the pipe. */ | |
55dfb029 | 86 | int lttng_pipe_release_writefd(struct lttng_pipe *pipe); |
9fd92637 | 87 | |
7966af57 SM |
88 | #ifdef __cplusplus |
89 | } | |
90 | #endif | |
91 | ||
9fd92637 | 92 | #endif /* LTTNG_PIPE_H */ |