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
23 #include "multipoll.h"
26 #define INITIAL_N_AVAIL 16
28 int multipoll_init(struct mpentries
*ent
)
31 ent
->n_avail
= INITIAL_N_AVAIL
;
33 ent
->pollfds
= (struct pollfd
*) malloc(sizeof(struct pollfd
) * INITIAL_N_AVAIL
);
34 ent
->extras
= (struct pollfd_extra
*) malloc(sizeof(struct pollfd_extra
) * INITIAL_N_AVAIL
);
39 int multipoll_destroy(struct mpentries
*ent
)
43 for(i
=0; i
<ent
->n_used
; i
++) {
44 if(ent
->extras
[i
].destroy_priv
) {
45 ent
->extras
[i
].destroy_priv(ent
->extras
[i
].priv
);
55 int multipoll_add(struct mpentries
*ent
, int fd
, short events
, int (*func
)(void *priv
, int fd
, short events
), void *priv
, int (*destroy_priv
)(void *))
59 if(ent
->n_used
== ent
->n_avail
) {
61 ent
->pollfds
= (struct pollfd
*) realloc(ent
->pollfds
, sizeof(struct pollfd
) * ent
->n_avail
);
62 ent
->extras
= (struct pollfd_extra
*) realloc(ent
->extras
, sizeof(struct pollfd_extra
) * ent
->n_avail
);
68 ent
->pollfds
[cur
].fd
= fd
;
69 ent
->pollfds
[cur
].events
= events
;
70 ent
->extras
[cur
].func
= func
;
71 ent
->extras
[cur
].priv
= priv
;
72 ent
->extras
[cur
].destroy_priv
= destroy_priv
;
77 int multipoll_poll(struct mpentries
*ent
, int timeout
)
82 result
= poll(ent
->pollfds
, ent
->n_used
, timeout
);
88 for(i
=0; i
<ent
->n_used
; i
++) {
89 if(ent
->pollfds
[i
].revents
) {
90 ent
->extras
[i
].func(ent
->extras
[i
].priv
, ent
->pollfds
[i
].fd
, ent
->pollfds
[i
].revents
);
This page took 0.035311 seconds and 5 git commands to generate.