1 /* kludge up some limited epoll semantics using select for 2.4 kernels */
2 /* $Id: fake_epoll.h,v 1.2 2007-06-28 07:22:50 bodea Exp $ */
4 #ifndef __FAKE_EPOLL_H__
5 #define __FAKE_EPOLL_H__
12 #define EPOLL_CTL_ADD 1
13 #define EPOLL_CTL_DEL 2
14 #define EPOLL_CTL_MOD 3
26 int epoll_ctl(int epfd
, int op
, int fd
, struct epoll_event
*event
);
28 #ifdef FAKE_EPOLL_IMPLEMENTATION
30 #include <sys/select.h>
32 static fd_set _epoll_read_set
;
33 static fd_set _epoll_write_set
;
34 static int _epoll_fds
;
35 static struct epoll_event
*_epoll_data
[128];
37 static int epoll_create(int size
__attribute__ ((unused
)))
42 errno
= ENFILE
; /* only support one instance */
46 FD_ZERO(&_epoll_read_set
);
47 FD_ZERO(&_epoll_write_set
);
50 memset(_epoll_data
, 0, sizeof(_epoll_data
));
52 return 1; /* "descriptor" */
55 int epoll_ctl(int epfd
__attribute__ ((unused
)), int op
, int fd
,
56 struct epoll_event
*event
)
58 if (fd
> (sizeof(_epoll_data
)/sizeof(*_epoll_data
)) - 1)
67 if (event
->events
& EPOLLIN
)
68 FD_SET(fd
, &_epoll_read_set
);
70 if (event
->events
& EPOLLOUT
)
71 FD_SET(fd
, &_epoll_write_set
);
77 free(_epoll_data
[fd
]);
79 if (!(_epoll_data
[fd
] = malloc(sizeof(*_epoll_data
))))
85 memcpy(_epoll_data
[fd
], &event
->data
, sizeof(*_epoll_data
));
89 if (event
->events
& EPOLLIN
)
90 FD_SET(fd
, &_epoll_read_set
);
92 FD_CLR(fd
, &_epoll_read_set
);
94 if (event
->events
& EPOLLOUT
)
95 FD_SET(fd
, &_epoll_write_set
);
97 FD_CLR(fd
, &_epoll_write_set
);
99 memcpy(_epoll_data
[fd
], &event
->data
, sizeof(*_epoll_data
));
103 FD_CLR(fd
, &_epoll_read_set
);
104 FD_CLR(fd
, &_epoll_write_set
);
106 free(_epoll_data
[fd
]);
109 if (fd
== _epoll_fds
- 1)
114 if (FD_ISSET(fd
, &_epoll_read_set
) ||
115 FD_ISSET(fd
, &_epoll_write_set
))
129 static int epoll_wait(int epfd
__attribute__ ((unused
)),
130 struct epoll_event
*events
, int maxevents
, int timout
)
140 memcpy(&r
, &_epoll_read_set
, sizeof(r
));
141 memcpy(&w
, &_epoll_write_set
, sizeof(w
));
146 t
.tv_usec
= timout
* 1000;
152 n
= select(_epoll_fds
, &r
, &w
, 0, tp
);
159 for (i
= e
= 0; n
> 0 && i
< _epoll_fds
; i
++)
164 events
[e
].events
= 0;
166 events
[e
].events
|= EPOLLIN
;
169 events
[e
].events
|= EPOLLOUT
;
171 if (events
[e
].events
)
173 memcpy(&events
[e
++].data
, _epoll_data
[i
], sizeof(events
[0].data
));
181 #endif /* FAKE_EPOLL_IMPLEMENTATION */
182 #endif /* __FAKE_EPOLL_H__ */