]>
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
=
59 int r
= epoll_ctl(epoll_fd
, EPOLL_CTL_ADD
, h
->fd
, &ev
);
61 fprintf(stderr
, "epoll_ctl_add: %m\n");
67 int remove_epoll(struct epoll_event_handler
*h
)
69 int r
= epoll_ctl(epoll_fd
, EPOLL_CTL_DEL
, h
->fd
, NULL
);
71 fprintf(stderr
, "epoll_ctl_del: %m\n");
74 if (h
->ref_ev
&& h
->ref_ev
->data
.ptr
== h
) {
75 h
->ref_ev
->data
.ptr
= NULL
;
81 void clear_epoll(void)
87 int time_diff(struct timeval
*second
, struct timeval
*first
)
89 return (second
->tv_sec
- first
->tv_sec
)*1000
90 + (second
->tv_usec
- first
->tv_usec
)/1000;
93 void run_timeouts(void)
99 int epoll_main_loop(void)
101 gettimeofday(&nexttimeout
, NULL
);
102 nexttimeout
.tv_sec
++;
104 struct epoll_event ev
[EV_SIZE
];
111 gettimeofday(&tv
, NULL
);
112 timeout
= time_diff(&nexttimeout
, &tv
);
118 r
= epoll_wait(epoll_fd
, ev
, EV_SIZE
, timeout
);
119 if (r
< 0 && errno
!= EINTR
) {
120 fprintf(stderr
, "epoll_wait: %m\n");
123 for (i
= 0; i
< r
; i
++) {
124 struct epoll_event_handler
*p
= ev
[i
].data
.ptr
;
128 for (i
= 0; i
< r
; i
++) {
129 struct epoll_event_handler
*p
= ev
[i
].data
.ptr
;
131 p
->handler(ev
[i
].events
, p
);
133 for (i
= 0; i
< r
; i
++) {
134 struct epoll_event_handler
*p
= ev
[i
].data
.ptr
;