summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorratchov <ratchov@openbsd.org>2015-08-01 10:47:30 +0000
committerratchov <ratchov@openbsd.org>2015-08-01 10:47:30 +0000
commit633fcf5d7e5730b762c487a9e11051a8670aaff3 (patch)
treeedf9806d24eb45e4060ad27082f4cb0a3c8707fb
parentUse "Ev" macro for AUCAT_COOKIE environment variable. From Michael (diff)
downloadwireguard-openbsd-633fcf5d7e5730b762c487a9e11051a8670aaff3.tar.xz
wireguard-openbsd-633fcf5d7e5730b762c487a9e11051a8670aaff3.zip
Move processing of polled files in its own function, and call it
twice: once for files that need immediate handling, and once for files that would block. No behavior change.
-rw-r--r--usr.bin/sndiod/file.c118
-rw-r--r--usr.bin/sndiod/file.h4
2 files changed, 76 insertions, 46 deletions
diff --git a/usr.bin/sndiod/file.c b/usr.bin/sndiod/file.c
index 18dc7611f85..3f8fe5b45fd 100644
--- a/usr.bin/sndiod/file.c
+++ b/usr.bin/sndiod/file.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: file.c,v 1.11 2015/07/17 10:15:24 ratchov Exp $ */
+/* $OpenBSD: file.c,v 1.12 2015/08/01 10:47:30 ratchov Exp $ */
/*
* Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
*
@@ -269,6 +269,42 @@ file_del(struct file *f)
#endif
}
+void
+file_process(struct file *f, struct pollfd *pfd)
+{
+ int revents;
+#ifdef DEBUG
+ struct timespec ts0, ts1;
+ long us;
+#endif
+
+#ifdef DEBUG
+ if (log_level >= 3)
+ clock_gettime(CLOCK_MONOTONIC, &ts0);
+#endif
+ revents = (f->state != FILE_ZOMB) ?
+ f->ops->revents(f->arg, pfd) : 0;
+ if ((revents & POLLHUP) && (f->state != FILE_ZOMB))
+ f->ops->hup(f->arg);
+ if ((revents & POLLIN) && (f->state != FILE_ZOMB))
+ f->ops->in(f->arg);
+ if ((revents & POLLOUT) && (f->state != FILE_ZOMB))
+ f->ops->out(f->arg);
+#ifdef DEBUG
+ if (log_level >= 3) {
+ clock_gettime(CLOCK_MONOTONIC, &ts1);
+ us = 1000000L * (ts1.tv_sec - ts0.tv_sec);
+ us += (ts1.tv_nsec - ts0.tv_nsec) / 1000;
+ if (log_level >= 4 || us >= 5000) {
+ file_log(f);
+ log_puts(": processed in ");
+ log_putu(us);
+ log_puts("us\n");
+ }
+ }
+#endif
+}
+
int
file_poll(void)
{
@@ -277,12 +313,12 @@ file_poll(void)
struct timespec ts;
#ifdef DEBUG
struct timespec sleepts;
- struct timespec ts0, ts1;
- long us;
int i;
#endif
long long delta_nsec;
- int nfds, revents, res, immed;
+ int nfds, res;
+
+ log_flush();
/*
* cleanup zombies
@@ -304,17 +340,14 @@ file_poll(void)
return 0;
}
- log_flush();
+ /*
+ * fill pollfd structures
+ */
nfds = 0;
- immed = 0;
for (f = file_list; f != NULL; f = f->next) {
f->nfds = f->ops->pollfd(f->arg, pfds + nfds);
if (f->nfds == 0)
continue;
- if (f->nfds < 0) {
- immed = 1;
- continue;
- }
nfds += f->nfds;
}
#ifdef DEBUG
@@ -322,7 +355,7 @@ file_poll(void)
log_puts("poll:");
pfd = pfds;
for (f = file_list; f != NULL; f = f->next) {
- if (f->nfds <= 0)
+ if (f->nfds == 0)
continue;
log_puts(" ");
log_puts(f->ops->name);
@@ -335,16 +368,35 @@ file_poll(void)
}
log_puts("\n");
}
+#endif
+
+ /*
+ * process files that do not rely on poll
+ */
+ for (f = file_list; f != NULL; f = f->next) {
+ if (f->nfds > 0)
+ continue;
+ file_process(f, NULL);
+ }
+
+ /*
+ * sleep
+ */
+#ifdef DEBUG
clock_gettime(CLOCK_MONOTONIC, &sleepts);
file_utime += 1000000000LL * (sleepts.tv_sec - file_ts.tv_sec);
file_utime += sleepts.tv_nsec - file_ts.tv_nsec;
#endif
- if (!immed) {
- res = poll(pfds, nfds, TIMER_MSEC);
- if (res < 0 && errno != EINTR)
+ res = poll(pfds, nfds, TIMER_MSEC);
+ if (res < 0) {
+ if (errno != EINTR)
err(1, "poll");
- } else
- res = 0;
+ return 1;
+ }
+
+ /*
+ * run timeouts
+ */
clock_gettime(CLOCK_MONOTONIC, &ts);
#ifdef DEBUG
file_wtime += 1000000000LL * (ts.tv_sec - sleepts.tv_sec);
@@ -363,37 +415,15 @@ file_poll(void)
if (log_level >= 2)
log_puts("ignored huge clock delta\n");
}
- if (!immed && res <= 0)
- return 1;
+
+ /*
+ * process files that rely on poll
+ */
pfd = pfds;
for (f = file_list; f != NULL; f = f->next) {
- if (f->nfds <= 0)
+ if (f->nfds == 0)
continue;
-#ifdef DEBUG
- if (log_level >= 3)
- clock_gettime(CLOCK_MONOTONIC, &ts0);
-#endif
- revents = (f->state != FILE_ZOMB) ?
- f->ops->revents(f->arg, pfd) : 0;
- if ((revents & POLLHUP) && (f->state != FILE_ZOMB))
- f->ops->hup(f->arg);
- if ((revents & POLLIN) && (f->state != FILE_ZOMB))
- f->ops->in(f->arg);
- if ((revents & POLLOUT) && (f->state != FILE_ZOMB))
- f->ops->out(f->arg);
-#ifdef DEBUG
- if (log_level >= 3) {
- clock_gettime(CLOCK_MONOTONIC, &ts1);
- us = 1000000L * (ts1.tv_sec - ts0.tv_sec);
- us += (ts1.tv_nsec - ts0.tv_nsec) / 1000;
- if (log_level >= 4 || us >= 5000) {
- file_log(f);
- log_puts(": processed in ");
- log_putu(us);
- log_puts("us\n");
- }
- }
-#endif
+ file_process(f, pfd);
pfd += f->nfds;
}
return 1;
diff --git a/usr.bin/sndiod/file.h b/usr.bin/sndiod/file.h
index b9b39c7bb04..b0d625f5a12 100644
--- a/usr.bin/sndiod/file.h
+++ b/usr.bin/sndiod/file.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: file.h,v 1.3 2015/07/17 10:15:24 ratchov Exp $ */
+/* $OpenBSD: file.h,v 1.4 2015/08/01 10:47:30 ratchov Exp $ */
/*
* Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
*
@@ -53,7 +53,7 @@ struct file {
#define FILE_ZOMB 1 /* closed, but not free()d yet */
unsigned int state; /* one of above */
unsigned int max_nfds; /* max number of descriptors */
- int nfds; /* number of descriptors polled */
+ unsigned int nfds; /* number of descriptors polled */
char *name; /* for debug purposes */
};