]>
git.ipfire.org Git - people/ms/mstpd.git/blob - epoll_loop.c
1 /*****************************************************************************
2 Copyright (c) 2006 EMC Corporation.
3 Copyright (c) 2011 Factor-SPE
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2 of the License, or (at your option)
10 This program is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc., 59
17 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 The full GNU General Public License is included in this distribution in the
22 Authors: Srinivas Aji <Aji_Srinivas@emc.com>
23 Authors: Vitalii Demianets <vitas@nppfactor.kiev.ua>
25 ******************************************************************************/
30 #include "epoll_loop.h"
31 #include "bridge_ctl.h"
34 static int epoll_fd
= -1;
35 static struct timeval nexttimeout
;
39 int r
= epoll_create(128);
42 fprintf(stderr
, "epoll_create failed: %m\n");
49 int add_epoll(struct epoll_event_handler
*h
)
51 struct epoll_event ev
=
57 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
);
71 fprintf(stderr
, "epoll_ctl_del: %m\n");
74 if(h
->ref_ev
&& h
->ref_ev
->data
.ptr
== h
)
76 h
->ref_ev
->data
.ptr
= NULL
;
82 void clear_epoll(void)
88 static inline int time_diff(struct timeval
*second
, struct timeval
*first
)
90 return (second
->tv_sec
- first
->tv_sec
) * 1000
91 + (second
->tv_usec
- first
->tv_usec
) / 1000;
94 static inline void run_timeouts(void)
97 ++(nexttimeout
.tv_sec
);
100 int epoll_main_loop(void)
102 gettimeofday(&nexttimeout
, NULL
);
103 ++(nexttimeout
.tv_sec
);
105 struct epoll_event ev
[EV_SIZE
];
113 gettimeofday(&tv
, NULL
);
114 timeout
= time_diff(&nexttimeout
, &tv
);
121 r
= epoll_wait(epoll_fd
, ev
, EV_SIZE
, timeout
);
122 if(r
< 0 && errno
!= EINTR
)
124 fprintf(stderr
, "epoll_wait: %m\n");
127 for(i
= 0; i
< r
; ++i
)
129 struct epoll_event_handler
*p
= ev
[i
].data
.ptr
;
133 for (i
= 0; i
< r
; ++i
)
135 struct epoll_event_handler
*p
= ev
[i
].data
.ptr
;
137 p
->handler(ev
[i
].events
, p
);
139 for (i
= 0; i
< r
; ++i
)
141 struct epoll_event_handler
*p
= ev
[i
].data
.ptr
;