From 06ad0b0c9d70c707dd0e765ee8387115806cc322 Mon Sep 17 00:00:00 2001 From: khromenokroman Date: Mon, 23 Dec 2024 22:29:19 +0200 Subject: [PATCH] replace in clixon_event_loop select to poll --- configure.ac | 8 +++++ lib/src/clixon_event.c | 70 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/configure.ac b/configure.ac index 6202520d..4b970593 100644 --- a/configure.ac +++ b/configure.ac @@ -472,6 +472,14 @@ AC_ARG_ENABLE([event-poll], fi] ) +AC_ARG_WITH([max-events], + AS_HELP_STRING([--with-max-events=VALUE], [Set the maximum number of events (default: 1024)]), + [MAX_EVENTS=$withval], + [MAX_EVENTS=1024] # Значение по умолчанию, если не указано +) + +AC_DEFINE_UNQUOTED([MAX_EVENTS], [$MAX_EVENTS], [Maximum number of events]) + AC_CONFIG_FILES([Makefile lib/Makefile lib/src/Makefile diff --git a/lib/src/clixon_event.c b/lib/src/clixon_event.c index 691954a0..e029a481 100644 --- a/lib/src/clixon_event.c +++ b/lib/src/clixon_event.c @@ -399,6 +399,75 @@ clixon_event_poll(int fd) * Currently a socket that is not read/emptied properly starve timeouts. * One could try to poll the file descriptors after a timeout? */ +#ifdef CLIXON_EVENT_POLL +int +clixon_event_loop(clixon_handle h) +{ + struct event_data *e = NULL; + struct pollfd fds[MAX_EVENTS]; + int retval = -1; + int nfds = 0; + + while (clixon_exit_get() != 1) { + nfds = 0; + for (e = ee; e; e = e->e_next) { + if (e->e_type == EVENT_FD) { + fds[nfds].fd = e->e_fd; + fds[nfds].events = POLLIN; + nfds++; + } + } + + int timeout = -1; + if (ee_timers != NULL) { + struct timeval t0, t; + gettimeofday(&t0, NULL); + timersub(&ee_timers->e_time, &t0, &t); + timeout = t.tv_sec * 1000 + t.tv_usec / 1000; + } + + int n = poll(fds, nfds, timeout); + + if (n == -1) { + if (errno == EINTR) { + continue; + } + clixon_err(OE_EVENTS, errno, "poll"); + goto err; + } + + if (n == 0) { + // Таймаут + e = ee_timers; + ee_timers = ee_timers->e_next; + if ((*e->e_fn)(0, e->e_arg) < 0) { + free(e); + goto err; + } + free(e); + continue; + } + + for (int i = 0; i < nfds; i++) { + if (fds[i].revents & POLLIN) { + for (e = ee; e; e = e->e_next) { + if (e->e_type == EVENT_FD && e->e_fd == fds[i].fd) { + if ((*e->e_fn)(e->e_fd, e->e_arg) < 0) { + goto err; + } + break; + } + } + } + } + } + + retval = 0; + + err: + return retval; +} +#else int clixon_event_loop(clixon_handle h) { @@ -531,6 +600,7 @@ clixon_event_loop(clixon_handle h) clixon_debug(CLIXON_DBG_EVENT, "retval:%d", retval); return retval; } +#endif int clixon_event_exit(void)