[ovs-dev] [PATCH] poll-loop: windows poll_block implementation
Linda Sun
lsun at vmware.com
Mon Jan 6 22:47:33 UTC 2014
Use WaitForMultipleObjects for polling on windows. This works on all kinds
of objects, e.g. sockets, files, especially ioctl calls to the kernel.
poll_fd_wait_event() is used if events need to be passed to pollfds.
latch is signaled with event, to be waited/polled by WaitForMultipleObjects()
as well.
Changed array of fds to hmap to check for duplicate fds.
Signed-off-by: Linda Sun <lsun at vmware.com>
---
include/linux/types.h | 2 +
lib/automake.mk | 9 +++-
lib/latch-windows.c | 85 ++++++++++++++++++++++++++++++++++
lib/latch.c | 2 +-
lib/latch.h | 5 ++
lib/poll-loop.c | 121 ++++++++++++++++++++++++++++++++++++++-----------
lib/poll-loop.h | 5 +-
lib/timeval.c | 20 ++++++--
lib/timeval.h | 4 +-
9 files changed, 218 insertions(+), 35 deletions(-)
create mode 100644 lib/latch-windows.c
diff --git a/include/linux/types.h b/include/linux/types.h
index b88fb1c..4114b0f 100644
--- a/include/linux/types.h
+++ b/include/linux/types.h
@@ -55,4 +55,6 @@ typedef uint32_t __bitwise__ __be32;
typedef uint64_t __bitwise__ __be64;
#endif /* no <linux/types.h> */
+typedef __u32 HANDLE;
+
#endif /* <linux/types.h> */
diff --git a/lib/automake.mk b/lib/automake.mk
index 5eece1c..4767909 100644
--- a/lib/automake.mk
+++ b/lib/automake.mk
@@ -79,7 +79,6 @@ lib_libopenvswitch_la_SOURCES = \
lib/jsonrpc.h \
lib/lacp.c \
lib/lacp.h \
- lib/latch.c \
lib/latch.h \
lib/learn.c \
lib/learn.h \
@@ -234,6 +233,14 @@ lib_libopenvswitch_la_SOURCES = \
lib/vswitch-idl.h \
lib/vtep-idl.c \
lib/vtep-idl.h
+if WIN32
+lib_libopenvswitch_la_SOURCES += \
+ lib/latch-windows.c
+else
+lib_libopenvswitch_la_SOURCES += \
+ lib/latch.c
+endif
+
EXTRA_DIST += lib/string.h.in
nodist_lib_libopenvswitch_la_SOURCES = \
diff --git a/lib/latch-windows.c b/lib/latch-windows.c
new file mode 100644
index 0000000..41c9426
--- /dev/null
+++ b/lib/latch-windows.c
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2013 Nicira, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <config.h>
+
+#include "latch.h"
+#include <errno.h>
+#include <poll.h>
+#include <unistd.h>
+#include "poll-loop.h"
+#include "socket-util.h"
+
+/* Initializes 'latch' as initially unset. */
+void
+latch_init(struct latch *latch)
+{
+ latch->fds[0] = 0;
+ latch->is_set = FALSE;
+ latch->wevent = CreateEvent(NULL, TRUE, FALSE, NULL);
+}
+
+/* Destroys 'latch'. */
+void
+latch_destroy(struct latch *latch)
+{
+ latch->fds[0] = 0;
+ latch->is_set = FALSE;
+ CloseHandle(latch->wevent);
+}
+
+/* Resets 'latch' to the unset state. Returns true if 'latch' was previously
+ * set, false otherwise. */
+bool
+latch_poll(struct latch *latch)
+{
+ bool is_set;
+
+ is_set = latch->is_set;
+ latch->is_set = FALSE;
+ ResetEvent(latch->wevent);
+ return is_set;
+}
+
+/* Sets 'latch'.
+ *
+ * Calls are not additive: a single latch_poll() clears out any number of
+ * latch_set(). */
+void
+latch_set(struct latch *latch)
+{
+ latch->is_set = TRUE;
+ SetEvent(latch->wevent);
+}
+
+/* Returns true if 'latch' is set, false otherwise. Does not reset 'latch'
+ * to the unset state. */
+bool
+latch_is_set(const struct latch *latch)
+{
+ return latch->is_set;
+}
+
+/* Causes the next poll_block() to wake up when 'latch' is set.
+ *
+ * ('where' is used in debug logging. Commonly one would use latch_wait() to
+ * automatically provide the caller's source file and line number for
+ * 'where'.) */
+void
+latch_wait_at(const struct latch *latch, const char *where)
+{
+ poll_fd_wait_at(latch->fds[0], latch->wevent, POLLIN, where);
+}
diff --git a/lib/latch.c b/lib/latch.c
index bf518b9..20a6575 100644
--- a/lib/latch.c
+++ b/lib/latch.c
@@ -83,5 +83,5 @@ latch_is_set(const struct latch *latch)
void
latch_wait_at(const struct latch *latch, const char *where)
{
- poll_fd_wait_at(latch->fds[0], POLLIN, where);
+ poll_fd_wait_at(latch->fds[0], 0, POLLIN, where);
}
diff --git a/lib/latch.h b/lib/latch.h
index 0b6e8a3..52829b1 100644
--- a/lib/latch.h
+++ b/lib/latch.h
@@ -26,7 +26,12 @@
#include "util.h"
struct latch {
+#ifndef _WIN32
int fds[2];
+#else
+ HANDLE wevent;
+ bool is_set;
+#endif
};
void latch_init(struct latch *);
diff --git a/lib/poll-loop.c b/lib/poll-loop.c
index 5e3618b..815d2c2 100644
--- a/lib/poll-loop.c
+++ b/lib/poll-loop.c
@@ -30,18 +30,24 @@
#include "socket-util.h"
#include "timeval.h"
#include "vlog.h"
+#include "hmap.h"
+#include "hash.h"
VLOG_DEFINE_THIS_MODULE(poll_loop);
COVERAGE_DEFINE(poll_fd_wait);
COVERAGE_DEFINE(poll_zero_timeout);
+struct poll_node {
+ struct hmap_node hmap_node;
+ struct pollfd poll_fd; /* Events to pass to time_poll() */
+ HANDLE wevent; /* events for waitformultipleobjects */
+ const char *where; /* where each pollfd was created */
+};
+
struct poll_loop {
/* All active poll waiters. */
- struct pollfd *pollfds; /* Events to pass to poll(). */
- const char **where; /* Where each pollfd was created. */
- size_t n_waiters; /* Number of elems in 'where' and 'pollfds'. */
- size_t allocated_waiters; /* Allocated elems in 'where' and 'pollfds'. */
+ struct hmap poll_nodes;
/* Time at which to wake up the next call to poll_block(), LLONG_MIN to
* wake up immediately, or LLONG_MAX to wait forever. */
@@ -51,10 +57,30 @@ struct poll_loop {
static struct poll_loop *poll_loop(void);
-/* Registers 'fd' as waiting for the specified 'events' (which should be POLLIN
+/* Look up the node with same fd and wevent */
+static struct poll_node *
+poll_fd_node_find(struct poll_loop *loop, int fd, uint32_t wevent)
+{
+ struct poll_node *node;
+
+ HMAP_FOR_EACH_WITH_HASH (node, hmap_node, hash_2words(fd, wevent),
+ &loop->poll_nodes) {
+ if (node->poll_fd.fd == fd && node->wevent == wevent) {
+ return node;
+ }
+ }
+ return NULL;
+}
+
+/* On unix based systems:
+ * Registers 'fd' as waiting for the specified 'events' (which should be POLLIN
* or POLLOUT or POLLIN | POLLOUT). The following call to poll_block() will
* wake up when 'fd' becomes ready for one or more of the requested events.
+ * the 'fd's are given to poll() function later.
*
+ * On windows system:
+ * register 'wevent' handle for the specified 'events'. These wevents are given
+ * to the handleMultipleObjects() to be polled.
* The event registration is one-shot: only the following call to poll_block()
* is affected. The event will need to be re-registered after poll_block() is
* called if it is to persist.
@@ -63,23 +89,34 @@ static struct poll_loop *poll_loop(void);
* automatically provide the caller's source file and line number for
* 'where'.) */
void
-poll_fd_wait_at(int fd, short int events, const char *where)
+poll_fd_wait_at(int fd, HANDLE wevent, short int events, const char *where)
{
struct poll_loop *loop = poll_loop();
+ struct poll_node *node;
COVERAGE_INC(poll_fd_wait);
- if (loop->n_waiters >= loop->allocated_waiters) {
- loop->where = x2nrealloc(loop->where, &loop->allocated_waiters,
- sizeof *loop->where);
- loop->pollfds = xrealloc(loop->pollfds,
- (loop->allocated_waiters
- * sizeof *loop->pollfds));
+
+#ifdef _WIN32
+ /* null event cannot be polled */
+ if (wevent == 0) {
+ VLOG_ERR("No event to wait fd %d", fd);
+ return;
}
+#endif
- loop->where[loop->n_waiters] = where;
- loop->pollfds[loop->n_waiters].fd = fd;
- loop->pollfds[loop->n_waiters].events = events;
- loop->n_waiters++;
+ /* check for duplicate. If found, "or" the event */
+ node = poll_fd_node_find(loop, fd, wevent);
+ if (node) {
+ node->poll_fd.events |= events;
+ } else {
+ node = xzalloc(sizeof *node);
+ node->where = where;
+ node->poll_fd.fd = fd;
+ node->wevent = wevent;
+ node->poll_fd.events = events;
+ hmap_insert(&loop->poll_nodes, &node->hmap_node,
+ hash_2words(fd, wevent));
+ }
}
/* Causes the following call to poll_block() to block for no more than 'msec'
@@ -215,8 +252,12 @@ void
poll_block(void)
{
struct poll_loop *loop = poll_loop();
+ struct poll_node *node, *next;
+ struct pollfd *pollfds;
+ HANDLE *wevents = NULL;
int elapsed;
int retval;
+ int i = 0;
/* Register fatal signal events before actually doing any real work for
* poll_block. */
@@ -227,7 +268,22 @@ poll_block(void)
}
timewarp_wait();
- retval = time_poll(loop->pollfds, loop->n_waiters,
+ pollfds = xmalloc(hmap_count(&loop->poll_nodes) * sizeof *pollfds);
+
+#ifdef _WIN32
+ wevents = xmalloc(hmap_count(&loop->poll_nodes) * sizeof *wevents);
+#endif
+
+ /* populate with all the fds and events */
+ HMAP_FOR_EACH (node, hmap_node, &loop->poll_nodes) {
+ memcpy(&pollfds[i], &node->poll_fd, sizeof node->poll_fd);
+#ifdef _WIN32
+ wevents[i] = node->wevent;
+#endif
+ i++;
+ }
+
+ retval = time_poll(pollfds, hmap_count(&loop->poll_nodes), wevents,
loop->timeout_when, &elapsed);
if (retval < 0) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
@@ -235,19 +291,26 @@ poll_block(void)
} else if (!retval) {
log_wakeup(loop->timeout_where, NULL, elapsed);
} else if (get_cpu_usage() > 50 || VLOG_IS_DBG_ENABLED()) {
- size_t i;
-
- for (i = 0; i < loop->n_waiters; i++) {
- if (loop->pollfds[i].revents) {
- log_wakeup(loop->where[i], &loop->pollfds[i], 0);
+ i = 0;
+ HMAP_FOR_EACH (node, hmap_node, &loop->poll_nodes) {
+ if (pollfds[i].revents) {
+ log_wakeup(node->where, &node->poll_fd, 0);
}
+ i++;
}
}
+ HMAP_FOR_EACH_SAFE (node, next, hmap_node, &loop->poll_nodes) {
+ hmap_remove(&loop->poll_nodes, &node->hmap_node);
+ free(node);
+ }
+
loop->timeout_when = LLONG_MAX;
loop->timeout_where = NULL;
- loop->n_waiters = 0;
-
+ free(pollfds);
+ if (wevents) {
+ free(wevents);
+ }
/* Handle any pending signals before doing anything else. */
fatal_signal_run();
@@ -258,9 +321,14 @@ static void
free_poll_loop(void *loop_)
{
struct poll_loop *loop = loop_;
+ struct poll_node *node, *next;
+
+ HMAP_FOR_EACH_SAFE (node, next, hmap_node, &loop->poll_nodes) {
+ hmap_remove(&loop->poll_nodes, &node->hmap_node);
+ free(node);
+ }
- free(loop->pollfds);
- free(loop->where);
+ hmap_destroy(&loop->poll_nodes);
free(loop);
}
@@ -279,6 +347,7 @@ poll_loop(void)
loop = pthread_getspecific(key);
if (!loop) {
loop = xzalloc(sizeof *loop);
+ hmap_init(&loop->poll_nodes);
xpthread_setspecific(key, loop);
}
return loop;
diff --git a/lib/poll-loop.h b/lib/poll-loop.h
index 0397853..79700b4 100644
--- a/lib/poll-loop.h
+++ b/lib/poll-loop.h
@@ -50,8 +50,9 @@ extern "C" {
* caller to supply a location explicitly, which is useful if the caller's own
* caller would be more useful in log output. See timer_wait_at() for an
* example. */
-void poll_fd_wait_at(int fd, short int events, const char *where);
-#define poll_fd_wait(fd, events) poll_fd_wait_at(fd, events, SOURCE_LOCATOR)
+void poll_fd_wait_at(int fd, HANDLE wevent, short int events, const char *where);
+#define poll_fd_wait(fd, events) poll_fd_wait_at(fd, 0, events, SOURCE_LOCATOR)
+#define poll_fd_wait_event(fd, wevent, events) poll_fd_wait_at(fd, wevent, events, SOURCE_LOCATOR)
void poll_timer_wait_at(long long int msec, const char *where);
#define poll_timer_wait(msec) poll_timer_wait_at(msec, SOURCE_LOCATOR)
diff --git a/lib/timeval.c b/lib/timeval.c
index 2ce45fc..23e3169 100644
--- a/lib/timeval.c
+++ b/lib/timeval.c
@@ -232,12 +232,12 @@ time_alarm(unsigned int secs)
*
* Stores the number of milliseconds elapsed during poll in '*elapsed'. */
int
-time_poll(struct pollfd *pollfds, int n_pollfds, long long int timeout_when,
- int *elapsed)
+time_poll(struct pollfd *pollfds, int n_pollfds, HANDLE *handles,
+ long long int timeout_when, int *elapsed)
{
long long int *last_wakeup = last_wakeup_get();
long long int start;
- int retval;
+ int retval = 0;
time_init();
coverage_clear();
@@ -261,10 +261,24 @@ time_poll(struct pollfd *pollfds, int n_pollfds, long long int timeout_when,
time_left = timeout_when - now;
}
+#ifndef _WIN32
+ if (handles) {
+ /* non-windows platform shouldn't really create these handles */
+ }
retval = poll(pollfds, n_pollfds, time_left);
if (retval < 0) {
retval = -errno;
}
+#else
+ if (n_pollfds != 0) {
+ retval = WaitForMultipleObjects(n_pollfds, handles, FALSE, time_left);
+ }
+ if (retval < 0) {
+ /* XXX This will be replace by a win error to errno conversion function */
+ retval = -WSAGetLastError();
+ retval = -EINVAL;
+ }
+#endif
if (deadline <= time_msec()) {
fatal_signal_handler(SIGALRM);
diff --git a/lib/timeval.h b/lib/timeval.h
index 1bbfd5c..c207f23 100644
--- a/lib/timeval.h
+++ b/lib/timeval.h
@@ -52,8 +52,8 @@ long long int time_wall_msec(void);
void time_timespec(struct timespec *);
void time_wall_timespec(struct timespec *);
void time_alarm(unsigned int secs);
-int time_poll(struct pollfd *, int n_pollfds, long long int timeout_when,
- int *elapsed);
+int time_poll(struct pollfd *, int n_pollfds, HANDLE *handles,
+ long long int timeout_when, int *elapsed);
long long int timespec_to_msec(const struct timespec *);
long long int timeval_to_msec(const struct timeval *);
--
1.7.9.5
More information about the dev
mailing list