4 * Copyright (C) 2010 - Pierre-Marc Fournier (pierre-marc dot fournier at polymtl dot ca)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 /* Multipoll is a framework to poll on several file descriptors and to call
22 * a specific callback depending on the fd that had activity.
27 #include "multipoll.h"
30 #define INITIAL_N_AVAIL 16
34 * Initialize an mpentries struct, which is initially empty of any fd.
37 int multipoll_init(struct mpentries
*ent
)
40 ent
->n_avail
= INITIAL_N_AVAIL
;
42 ent
->pollfds
= (struct pollfd
*) malloc(sizeof(struct pollfd
) * INITIAL_N_AVAIL
);
43 ent
->extras
= (struct pollfd_extra
*) malloc(sizeof(struct pollfd_extra
) * INITIAL_N_AVAIL
);
48 /* multipoll_destroy: free a struct mpentries
51 int multipoll_destroy(struct mpentries
*ent
)
55 for(i
=0; i
<ent
->n_used
; i
++) {
56 if(ent
->extras
[i
].destroy_priv
) {
57 ent
->extras
[i
].destroy_priv(ent
->extras
[i
].priv
);
69 * Add a file descriptor to be waited on in a struct mpentries.
71 * @ent: the struct mpentries to add an fd to
72 * @fd: the fd to wait on
73 * @events: a mask of the types of events to wait on, see the poll(2) man page
74 * @func: the callback function to be called if there is activity on the fd
75 * @priv: the private pointer to pass to func
76 * @destroy_priv: a callback to destroy the priv pointer when the mpentries
77 is destroyed; may be NULL
80 int multipoll_add(struct mpentries
*ent
, int fd
, short events
, int (*func
)(void *priv
, int fd
, short events
), void *priv
, int (*destroy_priv
)(void *))
84 if(ent
->n_used
== ent
->n_avail
) {
86 ent
->pollfds
= (struct pollfd
*) realloc(ent
->pollfds
, sizeof(struct pollfd
) * ent
->n_avail
);
87 ent
->extras
= (struct pollfd_extra
*) realloc(ent
->extras
, sizeof(struct pollfd_extra
) * ent
->n_avail
);
93 ent
->pollfds
[cur
].fd
= fd
;
94 ent
->pollfds
[cur
].events
= events
;
95 ent
->extras
[cur
].func
= func
;
96 ent
->extras
[cur
].priv
= priv
;
97 ent
->extras
[cur
].destroy_priv
= destroy_priv
;
102 /* multipoll_poll: do the actual poll on a struct mpentries
104 * File descriptors should have been already added with multipoll_add().
106 * A struct mpentries may be reused for multiple multipoll_poll calls.
108 * @ent: the struct mpentries to poll on.
109 * @timeout: the timeout after which to return if there was no activity.
112 int multipoll_poll(struct mpentries
*ent
, int timeout
)
117 result
= poll(ent
->pollfds
, ent
->n_used
, timeout
);
123 for(i
=0; i
<ent
->n_used
; i
++) {
124 if(ent
->pollfds
[i
].revents
) {
125 ent
->extras
[i
].func(ent
->extras
[i
].priv
, ent
->pollfds
[i
].fd
, ent
->pollfds
[i
].revents
);
This page took 0.043134 seconds and 5 git commands to generate.