]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
AsyncMessenger: Support select for other OS such as Windows
authorHaomai Wang <haomaiwang@gmail.com>
Wed, 12 Nov 2014 09:26:03 +0000 (17:26 +0800)
committerHaomai Wang <haomaiwang@gmail.com>
Thu, 13 Nov 2014 07:35:47 +0000 (15:35 +0800)
Signed-off-by: Haomai Wang <haomaiwang@gmail.com>
src/msg/Makefile.am
src/msg/async/EventSelect.cc [new file with mode: 0644]
src/msg/async/EventSelect.h [new file with mode: 0644]

index 68f69bf0289225b686c4da1d668b4557a3499136..ea44ac1c59c4ceb3e91c4e7fe82d0ffe3d058d7b 100644 (file)
@@ -20,7 +20,8 @@ libmsg_la_SOURCES += \
        msg/async/AsyncConnection.cc \
        msg/async/AsyncMessenger.cc \
        msg/async/Event.cc \
-       msg/async/net_handler.cc
+       msg/async/net_handler.cc \
+       msg/async/EventSelect.cc
 
 if LINUX
 libmsg_la_SOURCES += msg/async/EventEpoll.cc
@@ -44,7 +45,8 @@ noinst_HEADERS += \
        msg/async/AsyncConnection.h \
        msg/async/AsyncMessenger.h \
        msg/async/Event.h \
-       msg/async/net_handler.h
+       msg/async/net_handler.h \
+       msg/async/EventEpoll.h
 
 if LINUX
 libmsg_la_SOURCES += msg/async/EventEpoll.h
diff --git a/src/msg/async/EventSelect.cc b/src/msg/async/EventSelect.cc
new file mode 100644 (file)
index 0000000..9698dd1
--- /dev/null
@@ -0,0 +1,89 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2014 UnitedStack <haomai@unitedstack.com>
+ *
+ * Author: Haomai Wang <haomaiwang@gmail.com>
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation.  See file COPYING.
+ *
+ */
+
+#include "common/errno.h"
+#include "EventSelect.h"
+
+#define dout_subsys ceph_subsys_ms
+
+#undef dout_prefix
+#define dout_prefix *_dout << "SelectDriver."
+
+int SelectDriver::init(int nevent)
+{
+  ldout(cct, 0) << "Select isn't suitable for production env, just avoid "
+                << "compiling error or special purpose" << dendl;
+  FD_ZERO(&rfds);
+  FD_ZERO(&wfds);
+  max_fd = 0;
+  return 0;
+}
+
+int SelectDriver::add_event(int fd, int cur_mask, int add_mask)
+{
+  ldout(cct, 10) << __func__ << " add event to fd=" << fd << " mask=" << add_mask
+                 << dendl;
+
+  if (add_mask & EVENT_READABLE)
+    FD_SET(fd, &rfds);
+  if (add_mask & EVENT_WRITABLE)
+    FD_SET(fd, &wfds);
+  if (fd > max_fd)
+      max_fd = fd;
+
+  return 0;
+}
+
+void SelectDriver::del_event(int fd, int cur_mask, int delmask)
+{
+  ldout(cct, 10) << __func__ << " del event fd=" << fd << " cur mask=" << cur_mask
+                 << dendl;
+
+  if (delmask & EVENT_READABLE)
+    FD_CLR(fd, &rfds);
+  if (delmask & EVENT_WRITABLE)
+    FD_CLR(fd, &wfds);
+}
+
+int SelectDriver::resize_events(int newsize)
+{
+  return 0;
+}
+
+int SelectDriver::event_wait(vector<FiredFileEvent> &fired_events, struct timeval *tvp)
+{
+  int retval, j, numevents = 0;
+
+  memcpy(&_rfds, &rfds, sizeof(fd_set));
+  memcpy(&_wfds, &wfds, sizeof(fd_set));
+
+  retval = select(max_fd+1, &_rfds, &_wfds, NULL, tvp);
+  if (retval > 0) {
+    for (j = 0; j <= max_fd; j++) {
+      int mask = 0;
+      struct FiredFileEvent fe;
+      if (FD_ISSET(j, &_rfds))
+          mask |= EVENT_READABLE;
+      if (FD_ISSET(j, &_wfds))
+          mask |= EVENT_WRITABLE;
+      fe.fd = j;
+      fe.mask = mask;
+      fired_events.push_back(fe);
+      numevents++;
+    }
+  }
+  return numevents;
+}
diff --git a/src/msg/async/EventSelect.h b/src/msg/async/EventSelect.h
new file mode 100644 (file)
index 0000000..39160b2
--- /dev/null
@@ -0,0 +1,44 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2014 UnitedStack <haomai@unitedstack.com>
+ *
+ * Author: Haomai Wang <haomaiwang@gmail.com>
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation.  See file COPYING.
+ *
+ */
+
+#ifndef CEPH_MSG_EVENTSELECT_H
+#define CEPH_MSG_EVENTSELECT_H
+
+#include <unistd.h>
+#include <sys/select.h>
+
+#include "Event.h"
+
+class SelectDriver : public EventDriver {
+  fd_set rfds, wfds;
+  /* We need to have a copy of the fd sets as it's not safe to reuse
+   * FD sets after select(). */
+  fd_set _rfds, _wfds;
+  int max_fd;
+  CephContext *cct;
+
+ public:
+  SelectDriver(CephContext *c): cct(c) {}
+  virtual ~SelectDriver() {}
+
+  int init(int nevent);
+  int add_event(int fd, int cur_mask, int add_mask);
+  void del_event(int fd, int cur_mask, int del_mask);
+  int resize_events(int newsize);
+  int event_wait(vector<FiredFileEvent> &fired_events, struct timeval *tp);
+};
+
+#endif