mirror of
https://github.com/osmarks/ngircd.git
synced 2025-06-29 08:32:53 +00:00
cleanup
This commit is contained in:
parent
e426c131c7
commit
f6e729443e
139
src/ngircd/io.c
139
src/ngircd/io.c
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
#include "portab.h"
|
#include "portab.h"
|
||||||
|
|
||||||
static char UNUSED id[] = "$Id: io.c,v 1.16 2006/07/23 23:11:44 alex Exp $";
|
static char UNUSED id[] = "$Id: io.c,v 1.17 2006/09/16 14:49:26 fw Exp $";
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -44,8 +44,8 @@ typedef struct {
|
|||||||
#define IO_USE_KQUEUE 1
|
#define IO_USE_KQUEUE 1
|
||||||
# else
|
# else
|
||||||
#define IO_USE_SELECT 1
|
#define IO_USE_SELECT 1
|
||||||
#endif
|
# endif /* HAVE_KQUEUE */
|
||||||
#endif
|
#endif /* HAVE_EPOLL_CREATE */
|
||||||
|
|
||||||
static bool library_initialized;
|
static bool library_initialized;
|
||||||
|
|
||||||
@ -98,40 +98,10 @@ io_event_get(int fd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool
|
#ifdef IO_USE_SELECT
|
||||||
io_library_init(unsigned int eventsize)
|
static bool
|
||||||
|
io_library_init_select(unsigned int eventsize)
|
||||||
{
|
{
|
||||||
#if defined(IO_USE_EPOLL) || defined(IO_USE_KQUEUE)
|
|
||||||
bool ret;
|
|
||||||
#endif
|
|
||||||
#ifdef IO_USE_EPOLL
|
|
||||||
int ecreate_hint = (int)eventsize;
|
|
||||||
if (ecreate_hint <= 0)
|
|
||||||
ecreate_hint = 128;
|
|
||||||
#endif
|
|
||||||
if (library_initialized)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
#ifdef IO_USE_SELECT
|
|
||||||
#ifdef FD_SETSIZE
|
|
||||||
if (eventsize >= FD_SETSIZE)
|
|
||||||
eventsize = FD_SETSIZE - 1;
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
|
|
||||||
eventsize = 0;
|
|
||||||
|
|
||||||
#ifdef IO_USE_EPOLL
|
|
||||||
io_masterfd = epoll_create(ecreate_hint);
|
|
||||||
Log(LOG_INFO,
|
|
||||||
"IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
|
|
||||||
ecreate_hint, eventsize, io_masterfd);
|
|
||||||
ret = io_masterfd >= 0;
|
|
||||||
if (ret) library_initialized = true;
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
#endif
|
|
||||||
#ifdef IO_USE_SELECT
|
|
||||||
Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
|
Log(LOG_INFO, "IO subsystem: select (initial maxfd %u).",
|
||||||
eventsize);
|
eventsize);
|
||||||
FD_ZERO(&readers);
|
FD_ZERO(&readers);
|
||||||
@ -144,14 +114,38 @@ io_library_init(unsigned int eventsize)
|
|||||||
|
|
||||||
Conf_MaxConnections = FD_SETSIZE - 1;
|
Conf_MaxConnections = FD_SETSIZE - 1;
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
Log(LOG_WARNING,
|
|
||||||
"FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
|
|
||||||
#endif /* FD_SETSIZE */
|
#endif /* FD_SETSIZE */
|
||||||
library_initialized = true;
|
library_initialized = true;
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
#endif /* SELECT */
|
#endif /* SELECT */
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef IO_USE_EPOLL
|
||||||
|
static bool
|
||||||
|
io_library_init_epoll(unsigned int eventsize)
|
||||||
|
{
|
||||||
|
bool ret;
|
||||||
|
int ecreate_hint = (int)eventsize;
|
||||||
|
if (ecreate_hint <= 0)
|
||||||
|
ecreate_hint = 128;
|
||||||
|
io_masterfd = epoll_create(ecreate_hint);
|
||||||
|
Log(LOG_INFO,
|
||||||
|
"IO subsystem: epoll (hint size %d, initial maxfd %u, masterfd %d).",
|
||||||
|
ecreate_hint, eventsize, io_masterfd);
|
||||||
|
ret = io_masterfd >= 0;
|
||||||
|
if (ret) library_initialized = true;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#ifdef IO_USE_KQUEUE
|
#ifdef IO_USE_KQUEUE
|
||||||
|
static bool
|
||||||
|
io_library_init_kqueue(unsigned int eventsize)
|
||||||
|
{
|
||||||
|
bool ret;
|
||||||
io_masterfd = kqueue();
|
io_masterfd = kqueue();
|
||||||
|
|
||||||
Log(LOG_INFO,
|
Log(LOG_INFO,
|
||||||
@ -159,8 +153,35 @@ io_library_init(unsigned int eventsize)
|
|||||||
eventsize, io_masterfd);
|
eventsize, io_masterfd);
|
||||||
ret = io_masterfd >= 0;
|
ret = io_masterfd >= 0;
|
||||||
if (ret) library_initialized = true;
|
if (ret) library_initialized = true;
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
bool
|
||||||
|
io_library_init(unsigned int eventsize)
|
||||||
|
{
|
||||||
|
if (library_initialized)
|
||||||
|
return true;
|
||||||
|
#ifdef IO_USE_SELECT
|
||||||
|
#ifndef FD_SETSIZE
|
||||||
|
Log(LOG_WARNING,
|
||||||
|
"FD_SETSIZE undefined, don't know how many descriptors select() can handle on your platform ...");
|
||||||
|
#else
|
||||||
|
if (eventsize >= FD_SETSIZE)
|
||||||
|
eventsize = FD_SETSIZE - 1;
|
||||||
|
#endif /* FD_SETSIZE */
|
||||||
|
#endif /* IO_USE_SELECT */
|
||||||
|
if ((eventsize > 0) && !array_alloc(&io_events, sizeof(io_event), (size_t)eventsize))
|
||||||
|
eventsize = 0;
|
||||||
|
#ifdef IO_USE_EPOLL
|
||||||
|
return io_library_init_epoll(eventsize);
|
||||||
|
#endif
|
||||||
|
#ifdef IO_USE_KQUEUE
|
||||||
|
return io_library_init_kqueue(eventsize);
|
||||||
|
#endif
|
||||||
|
#ifdef IO_USE_SELECT
|
||||||
|
return io_library_init_select(eventsize);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,11 +192,14 @@ io_library_shutdown(void)
|
|||||||
#ifdef IO_USE_SELECT
|
#ifdef IO_USE_SELECT
|
||||||
FD_ZERO(&readers);
|
FD_ZERO(&readers);
|
||||||
FD_ZERO(&writers);
|
FD_ZERO(&writers);
|
||||||
#else
|
#endif
|
||||||
close(io_masterfd); /* kqueue, epoll */
|
#ifdef IO_USE_EPOLL
|
||||||
|
close(io_masterfd);
|
||||||
io_masterfd = -1;
|
io_masterfd = -1;
|
||||||
#endif
|
#endif
|
||||||
#ifdef IO_USE_KQUEUE
|
#ifdef IO_USE_KQUEUE
|
||||||
|
close(io_masterfd);
|
||||||
|
io_masterfd = -1;
|
||||||
array_free(&io_evcache);
|
array_free(&io_evcache);
|
||||||
#endif
|
#endif
|
||||||
library_initialized = false;
|
library_initialized = false;
|
||||||
@ -201,18 +225,14 @@ io_event_create(int fd, short what, void (*cbfunc) (int, short))
|
|||||||
io_event *i;
|
io_event *i;
|
||||||
|
|
||||||
assert(fd >= 0);
|
assert(fd >= 0);
|
||||||
|
#if defined(IO_USE_SELECT) || defined(FD_SETSIZE)
|
||||||
#ifdef IO_USE_SELECT
|
|
||||||
#ifdef FD_SETSIZE
|
|
||||||
if (fd >= FD_SETSIZE) {
|
if (fd >= FD_SETSIZE) {
|
||||||
Log(LOG_ERR,
|
Log(LOG_ERR,
|
||||||
"fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
|
"fd %d exceeds FD_SETSIZE (%u) (select can't handle more file descriptors)",
|
||||||
fd, FD_SETSIZE);
|
fd, FD_SETSIZE);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#endif /* FD_SETSIZE */
|
#endif
|
||||||
#endif /* IO_USE_SELECT */
|
|
||||||
|
|
||||||
i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
|
i = (io_event *) array_alloc(&io_events, sizeof(io_event), (size_t) fd);
|
||||||
if (!i) {
|
if (!i) {
|
||||||
Log(LOG_WARNING,
|
Log(LOG_WARNING,
|
||||||
@ -327,7 +347,6 @@ io_event_add(int fd, short what)
|
|||||||
#ifdef IO_USE_KQUEUE
|
#ifdef IO_USE_KQUEUE
|
||||||
return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
|
return io_event_change_kqueue(fd, what, EV_ADD | EV_ENABLE);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef IO_USE_SELECT
|
#ifdef IO_USE_SELECT
|
||||||
if (fd > select_maxfd)
|
if (fd > select_maxfd)
|
||||||
select_maxfd = fd;
|
select_maxfd = fd;
|
||||||
@ -358,14 +377,17 @@ io_setnonblock(int fd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool
|
#ifdef IO_USE_SELECT
|
||||||
io_close(int fd)
|
static void
|
||||||
|
io_close_select(int fd)
|
||||||
{
|
{
|
||||||
io_event *i;
|
io_event *i;
|
||||||
#ifdef IO_USE_SELECT
|
|
||||||
FD_CLR(fd, &writers);
|
FD_CLR(fd, &writers);
|
||||||
FD_CLR(fd, &readers);
|
FD_CLR(fd, &readers);
|
||||||
|
|
||||||
|
i = io_event_get(fd);
|
||||||
|
if (!i) return;
|
||||||
|
|
||||||
if (fd == select_maxfd) {
|
if (fd == select_maxfd) {
|
||||||
while (select_maxfd>0) {
|
while (select_maxfd>0) {
|
||||||
--select_maxfd; /* find largest fd */
|
--select_maxfd; /* find largest fd */
|
||||||
@ -373,7 +395,17 @@ io_close(int fd)
|
|||||||
if (i && i->callback) break;
|
if (i && i->callback) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
static inline void io_close_select(int UNUSED x) { /* NOTHING */ }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
bool
|
||||||
|
io_close(int fd)
|
||||||
|
{
|
||||||
|
io_event *i;
|
||||||
|
|
||||||
i = io_event_get(fd);
|
i = io_event_get(fd);
|
||||||
#ifdef IO_USE_KQUEUE
|
#ifdef IO_USE_KQUEUE
|
||||||
if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
|
if (array_length(&io_evcache, sizeof (struct kevent))) /* pending data in cache? */
|
||||||
@ -387,6 +419,9 @@ io_close(int fd)
|
|||||||
io_event_kqueue_commit_cache();
|
io_event_kqueue_commit_cache();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
io_close_select(fd);
|
||||||
|
|
||||||
#ifdef IO_USE_EPOLL
|
#ifdef IO_USE_EPOLL
|
||||||
io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
|
io_event_change_epoll(fd, 0, EPOLL_CTL_DEL);
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user