1 /*
2 * Copyright (c) 2004 Andrey Panin <pazke@donpac.ru>
3 *
4 * This software is released under the MIT license.
5 */
6
7 #include "lib.h"
8 #include "ioloop-private.h"
9 #include "ioloop-iolist.h"
10
ioloop_iolist_add(struct io_list * list,struct io_file * io)11 bool ioloop_iolist_add(struct io_list *list, struct io_file *io)
12 {
13 int i, idx;
14
15 if ((io->io.condition & IO_READ) != 0)
16 idx = IOLOOP_IOLIST_INPUT;
17 else if ((io->io.condition & IO_WRITE) != 0)
18 idx = IOLOOP_IOLIST_OUTPUT;
19 else if ((io->io.condition & IO_ERROR) != 0)
20 idx = IOLOOP_IOLIST_ERROR;
21 else {
22 i_unreached();
23 }
24
25 if (list->ios[idx] != NULL) {
26 i_panic("io_add(0x%x) called twice fd=%d, callback=%p -> %p",
27 io->io.condition, io->fd, list->ios[idx]->io.callback,
28 io->io.callback);
29 }
30 i_assert(list->ios[idx] == NULL);
31 list->ios[idx] = io;
32
33 /* check if this was the first one */
34 for (i = 0; i < IOLOOP_IOLIST_IOS_PER_FD; i++) {
35 if (i != idx && list->ios[i] != NULL)
36 return FALSE;
37 }
38
39 return TRUE;
40 }
41
ioloop_iolist_del(struct io_list * list,struct io_file * io)42 bool ioloop_iolist_del(struct io_list *list, struct io_file *io)
43 {
44 bool last = TRUE;
45 int i;
46
47 for (i = 0; i < IOLOOP_IOLIST_IOS_PER_FD; i++) {
48 if (list->ios[i] != NULL) {
49 if (list->ios[i] == io)
50 list->ios[i] = NULL;
51 else
52 last = FALSE;
53 }
54 }
55 return last;
56 }
57