]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
crimson/thread: add a condition var impl
authorKefu Chai <kchai@redhat.com>
Thu, 7 Jun 2018 12:22:32 +0000 (20:22 +0800)
committerKefu Chai <kchai@redhat.com>
Wed, 13 Jun 2018 16:13:58 +0000 (00:13 +0800)
Signed-off-by: Kefu Chai <kchai@redhat.com>
src/crimson/thread/Condition.h [new file with mode: 0644]

diff --git a/src/crimson/thread/Condition.h b/src/crimson/thread/Condition.h
new file mode 100644 (file)
index 0000000..d5d1f36
--- /dev/null
@@ -0,0 +1,36 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#pragma once
+
+#include <core/reactor.hh>
+#include <sys/eventfd.h>
+
+namespace ceph::thread {
+
+/// a synchronization primitive can be used to block a seastar thread, until
+/// another thread notifies it.
+class Condition {
+  seastar::file_desc file_desc;
+  int fd;
+  seastar::pollable_fd_state fd_state;
+  eventfd_t event = 0;
+public:
+  Condition()
+    : file_desc{seastar::file_desc::eventfd(0, 0)},
+      fd(file_desc.get()),
+      fd_state{std::move(file_desc)}
+  {}
+  seastar::future<> wait() {
+    return seastar::engine().read_some(fd_state, &event, sizeof(event))
+      .then([](size_t) {
+         return seastar::now();
+       });
+  }
+  void notify() {
+    eventfd_t result = 1;
+    ::eventfd_write(fd, result);
+  }
+};
+
+} // namespace ceph::thread