]> git.ozlabs.org Git - ccan/blob - ccan/io/io_plan.h
ccan/io: add examples.
[ccan] / ccan / io / io_plan.h
1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #ifndef CCAN_IO_PLAN_H
3 #define CCAN_IO_PLAN_H
4 struct io_conn;
5
6 /**
7  * struct io_plan - a plan of what I/O to do.
8  * @pollflag: POLLIN or POLLOUT.
9  * @io: function to call when fd is available for @pollflag.
10  * @next: function to call after @io returns true.
11  * @next_arg: argument to @next.
12  * @u: scratch area for I/O.
13  *
14  * When the fd is POLLIN or POLLOUT (according to @pollflag), @io is
15  * called.  If it returns -1, io_close() becomed the new plan (and errno
16  * is saved).  If it returns 1, @next is called, otherwise @io is
17  * called again when @pollflag is available.
18  *
19  * You can use this to write your own io_plan functions.
20  */
21 struct io_plan {
22         int pollflag;
23         /* Only NULL if idle. */
24         int (*io)(int fd, struct io_plan *plan);
25         /* Only NULL if closing. */
26         struct io_plan (*next)(struct io_conn *, void *arg);
27         void *next_arg;
28
29         union {
30                 struct {
31                         char *buf;
32                         size_t len;
33                 } read;
34                 struct {
35                         const char *buf;
36                         size_t len;
37                 } write;
38                 struct {
39                         char *buf;
40                         size_t *lenp;
41                 } readpart;
42                 struct {
43                         const char *buf;
44                         size_t *lenp;
45                 } writepart;
46                 struct {
47                         int saved_errno;
48                 } close;
49                 struct {
50                         void *p;
51                         size_t len;
52                 } ptr_len;
53                 struct {
54                         void *p1;
55                         void *p2;
56                 } ptr_ptr;
57                 struct {
58                         size_t len1;
59                         size_t len2;
60                 } len_len;
61         } u;
62 };
63
64 #ifdef DEBUG
65 /**
66  * io_debug_conn - routine to select connection(s) to debug.
67  *
68  * If this is set, the routine should return true if the connection is a
69  * debugging candidate.  If so, the callchain for I/O operations on this
70  * connection will be linear, for easier use of a debugger.
71  *
72  * You will also see calls to any callbacks which wake the connection
73  * which is being debugged.
74  *
75  * Example:
76  *      static bool debug_all(struct io_conn *conn)
77  *      {
78  *              return true();
79  *      }
80  *      ...
81  *      io_debug_conn = debug_all;
82  */
83 extern bool (*io_debug_conn)(struct io_conn *conn);
84
85 /**
86  * io_debug - if we're debugging the current connection, call immediately.
87  *
88  * This determines if we are debugging the current connection: if so,
89  * it immediately applies the plan and calls back into io_loop() to
90  * create a linear call chain.
91  *
92  * Example:
93  *      #define io_idle() io_debug(io_idle_())
94  *      struct io_plan io_idle_(void);
95  */
96 struct io_plan io_debug(struct io_plan plan);
97
98 /**
99  * io_debug_io - return from function which actually does I/O.
100  *
101  * This determines if we are debugging the current connection: if so,
102  * it immediately sets the next function and calls into io_loop() to
103  * create a linear call chain.
104  *
105  * Example:
106  *
107  * static int do_write(int fd, struct io_plan *plan)
108  * {
109  *      ssize_t ret = write(fd, plan->u.write.buf, plan->u.write.len);
110  *      if (ret < 0)
111  *              return io_debug_io(-1);
112  *
113  *      plan->u.write.buf += ret;
114  *      plan->u.write.len -= ret;
115  *      return io_debug_io(plan->u.write.len == 0);
116  * }
117  */
118 int io_debug_io(int ret);
119
120 /**
121  * io_plan_no_debug - mark the next plan not to be called immediately.
122  *
123  * Most routines which take a plan are about to apply it to the current
124  * connection.  We (ab)use this pattern for debugging: as soon as such a
125  * plan is created it is called, to create a linear call chain.
126  *
127  * Some routines, like io_break(), io_duplex() and io_wake() take an
128  * io_plan, but they must not be applied immediately to the current
129  * connection, so we call this first.
130  *
131  * Example:
132  * #define io_break(ret, plan) (io_plan_no_debug(), io_break_((ret), (plan)))
133  * struct io_plan io_break_(void *ret, struct io_plan plan);
134  */
135 #define io_plan_no_debug() ((io_plan_nodebug = true))
136
137 extern bool io_plan_nodebug;
138 #else
139 static inline struct io_plan io_debug(struct io_plan plan)
140 {
141         return plan;
142 }
143 static inline int io_debug_io(int ret)
144 {
145         return ret;
146 }
147 #define io_plan_no_debug() (void)0
148 #endif
149
150 #endif /* CCAN_IO_PLAN_H */