Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace in clixon_event_loop select to poll #589

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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] # Значение по умолчанию, если не указано
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use english (more places)

)

AC_DEFINE_UNQUOTED([MAX_EVENTS], [$MAX_EVENTS], [Maximum number of events])

AC_CONFIG_FILES([Makefile
lib/Makefile
lib/src/Makefile
Expand Down
70 changes: 70 additions & 0 deletions lib/src/clixon_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: struct event_data *e = NULL; (indentation on the variable "e", not on the struct name "event_data"

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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "continue" statement may cause starvation so that only timeouts run. This can happen for example if a timeout registers itself, and then causes a loop that only serves timeouts, causing a deadlock.
Typically if the new timeout due to overload has already timed out.
This is the way it works in select at least, I am not sure if poll is different.
Removing continue will first serve one timeout, then serve file descriptors.
It is not 100% fair either but at least interleaving timeouts with file input removes some deadlocks.


for (int i = 0; i < nfds; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also removed the primitive CLICON_SOCK_PRIO algorithm.
It is somewhat primitive but at least avoided the same kind of deadlock as timeouts could cause. That is, if there is a prioritized file, then at most one un-prioritized file is served.

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)
{
Expand Down Expand Up @@ -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)
Expand Down
Loading