[ovs-dev] [threads v2 06/13] latch: New module for a thread-safe, signal-safe, pollable doorbell.

Ben Pfaff blp at nicira.com
Fri Jul 12 21:54:33 UTC 2013


Signed-off-by: Ben Pfaff <blp at nicira.com>
---
 lib/automake.mk |    2 +
 lib/latch.c     |   72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/latch.h     |   37 ++++++++++++++++++++++++++++
 3 files changed, 111 insertions(+), 0 deletions(-)
 create mode 100644 lib/latch.c
 create mode 100644 lib/latch.h

diff --git a/lib/automake.mk b/lib/automake.mk
index 280fc8b..6b0972b 100644
--- a/lib/automake.mk
+++ b/lib/automake.mk
@@ -71,6 +71,8 @@ lib_libopenvswitch_a_SOURCES = \
 	lib/jsonrpc.h \
 	lib/lacp.c \
 	lib/lacp.h \
+	lib/latch.c \
+	lib/latch.h \
 	lib/learn.c \
 	lib/learn.h \
 	lib/learning-switch.c \
diff --git a/lib/latch.c b/lib/latch.c
new file mode 100644
index 0000000..90df61a
--- /dev/null
+++ b/lib/latch.c
@@ -0,0 +1,72 @@
+/*
+ * 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"
+
+void
+latch_init(struct latch *latch)
+{
+    xpipe_nonblocking(latch->fds);
+}
+
+void
+latch_destroy(struct latch *latch)
+{
+    close(latch->fds[0]);
+    close(latch->fds[1]);
+}
+
+bool
+latch_poll(struct latch *latch)
+{
+    char buffer[_POSIX_PIPE_BUF];
+
+    return read(latch->fds[0], buffer, sizeof buffer) > 0;
+}
+
+void
+latch_set(struct latch *latch)
+{
+    ignore(write(latch->fds[1], "", 1));
+}
+
+bool
+latch_is_set(const struct latch *latch)
+{
+    struct pollfd pfd;
+    int retval;
+
+    pfd.fd = latch->fds[0];
+    pfd.events = POLLIN;
+    do {
+        retval = poll(&pfd, 1, 0);
+    } while (retval < 0 && errno == EINTR);
+
+    return pfd.revents & POLLIN;
+}
+
+void
+(latch_wait)(const struct latch *latch, const char *where)
+{
+    (poll_fd_wait)(latch->fds[0], POLLIN, where);
+}
diff --git a/lib/latch.h b/lib/latch.h
new file mode 100644
index 0000000..7e916e7
--- /dev/null
+++ b/lib/latch.h
@@ -0,0 +1,37 @@
+/*
+ * 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.
+ */
+
+#ifndef LATCH_H
+#define LATCH_H 1
+
+#include <stdbool.h>
+#include "util.h"
+
+struct latch {
+    int fds[2];
+};
+
+void latch_init(struct latch *);
+void latch_destroy(struct latch *);
+
+bool latch_poll(struct latch *);
+void latch_set(struct latch *);
+
+bool latch_is_set(const struct latch *);
+void latch_wait(const struct latch *, const char *where);
+#define latch_wait(latch) latch_wait(latch, SOURCE_LOCATOR)
+
+#endif /* latch.h */
-- 
1.7.2.5




More information about the dev mailing list