From: Haomai Wang Date: Wed, 12 Nov 2014 09:26:03 +0000 (+0800) Subject: AsyncMessenger: Support select for other OS such as Windows X-Git-Tag: v0.90~97^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=ebc8875a92915f10753b3f4cb7c3e4e2906ab1cd;p=ceph.git AsyncMessenger: Support select for other OS such as Windows Signed-off-by: Haomai Wang --- diff --git a/src/msg/Makefile.am b/src/msg/Makefile.am index 68f69bf02892..ea44ac1c59c4 100644 --- a/src/msg/Makefile.am +++ b/src/msg/Makefile.am @@ -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 index 000000000000..9698dd1328e0 --- /dev/null +++ b/src/msg/async/EventSelect.cc @@ -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 + * + * Author: Haomai Wang + * + * 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 &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 index 000000000000..39160b26f68d --- /dev/null +++ b/src/msg/async/EventSelect.h @@ -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 + * + * Author: Haomai Wang + * + * 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 +#include + +#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 &fired_events, struct timeval *tp); +}; + +#endif