]>
git.ipfire.org Git - people/ms/rstp.git/blob - epoll_loop.c
1 /*****************************************************************************
2 Copyright (c) 2006 EMC Corporation.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2 of the License, or (at your option)
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 You should have received a copy of the GNU General Public License along with
15 this program; if not, write to the Free Software Foundation, Inc., 59
16 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 The full GNU General Public License is included in this distribution in the
21 Authors: Srinivas Aji <Aji_Srinivas@emc.com>
23 ******************************************************************************/
25 #include "epoll_loop.h"
27 #include <sys/epoll.h>
34 #include "bridge_ctl.h"
38 struct timeval nexttimeout
;
42 int r
= epoll_create(128);
44 fprintf(stderr
, "epoll_create failed: %m\n");
51 int add_epoll(struct epoll_event_handler
*h
)
53 struct epoll_event ev
= {
58 int r
= epoll_ctl(epoll_fd
, EPOLL_CTL_ADD
, h
->fd
, &ev
);
60 fprintf(stderr
, "epoll_ctl_add: %m\n");
66 int remove_epoll(struct epoll_event_handler
*h
)
68 int r
= epoll_ctl(epoll_fd
, EPOLL_CTL_DEL
, h
->fd
, NULL
);
70 fprintf(stderr
, "epoll_ctl_del: %m\n");
73 if (h
->ref_ev
&& h
->ref_ev
->data
.ptr
== h
) {
74 h
->ref_ev
->data
.ptr
= NULL
;
80 void clear_epoll(void)
86 int time_diff(struct timeval
*second
, struct timeval
*first
)
88 return (second
->tv_sec
- first
->tv_sec
) * 1000
89 + (second
->tv_usec
- first
->tv_usec
) / 1000;
92 void run_timeouts(void)
98 int epoll_main_loop(void)
100 gettimeofday(&nexttimeout
, NULL
);
101 nexttimeout
.tv_sec
++;
103 struct epoll_event ev
[EV_SIZE
];
110 gettimeofday(&tv
, NULL
);
111 timeout
= time_diff(&nexttimeout
, &tv
);
117 r
= epoll_wait(epoll_fd
, ev
, EV_SIZE
, timeout
);
118 if (r
< 0 && errno
!= EINTR
) {
119 fprintf(stderr
, "epoll_wait: %m\n");
122 for (i
= 0; i
< r
; i
++) {
123 struct epoll_event_handler
*p
= ev
[i
].data
.ptr
;
127 for (i
= 0; i
< r
; i
++) {
128 struct epoll_event_handler
*p
= ev
[i
].data
.ptr
;
130 p
->handler(ev
[i
].events
, p
);
132 for (i
= 0; i
< r
; i
++) {
133 struct epoll_event_handler
*p
= ev
[i
].data
.ptr
;