Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / msg / async / EventSelect.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2014 UnitedStack <haomai@unitedstack.com>
7  *
8  * Author: Haomai Wang <haomaiwang@gmail.com>
9  *
10  * This is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License version 2.1, as published by the Free Software
13  * Foundation.  See file COPYING.
14  *
15  */
16
17 #include "common/errno.h"
18 #include "EventSelect.h"
19
20 #include <unistd.h>
21 #include <sys/select.h>
22 #define dout_subsys ceph_subsys_ms
23
24 #undef dout_prefix
25 #define dout_prefix *_dout << "SelectDriver."
26
27 int SelectDriver::init(EventCenter *c, int nevent)
28 {
29   ldout(cct, 0) << "Select isn't suitable for production env, just avoid "
30                 << "compiling error or special purpose" << dendl;
31   FD_ZERO(&rfds);
32   FD_ZERO(&wfds);
33   max_fd = 0;
34   return 0;
35 }
36
37 int SelectDriver::add_event(int fd, int cur_mask, int add_mask)
38 {
39   ldout(cct, 10) << __func__ << " add event to fd=" << fd << " mask=" << add_mask
40                  << dendl;
41
42   int mask = cur_mask | add_mask;
43   if (mask & EVENT_READABLE)
44     FD_SET(fd, &rfds);
45   if (mask & EVENT_WRITABLE)
46     FD_SET(fd, &wfds);
47   if (fd > max_fd)
48       max_fd = fd;
49
50   return 0;
51 }
52
53 int SelectDriver::del_event(int fd, int cur_mask, int delmask)
54 {
55   ldout(cct, 10) << __func__ << " del event fd=" << fd << " cur mask=" << cur_mask
56                  << dendl;
57
58   if (delmask & EVENT_READABLE)
59     FD_CLR(fd, &rfds);
60   if (delmask & EVENT_WRITABLE)
61     FD_CLR(fd, &wfds);
62   return 0;
63 }
64
65 int SelectDriver::resize_events(int newsize)
66 {
67   return 0;
68 }
69
70 int SelectDriver::event_wait(vector<FiredFileEvent> &fired_events, struct timeval *tvp)
71 {
72   int retval, numevents = 0;
73
74   memcpy(&_rfds, &rfds, sizeof(fd_set));
75   memcpy(&_wfds, &wfds, sizeof(fd_set));
76
77   retval = select(max_fd+1, &_rfds, &_wfds, NULL, tvp);
78   if (retval > 0) {
79     for (int j = 0; j <= max_fd; j++) {
80       int mask = 0;
81       struct FiredFileEvent fe;
82       if (FD_ISSET(j, &_rfds))
83           mask |= EVENT_READABLE;
84       if (FD_ISSET(j, &_wfds))
85           mask |= EVENT_WRITABLE;
86       if (mask) {
87         fe.fd = j;
88         fe.mask = mask;
89         fired_events.push_back(fe);
90         numevents++;
91       }
92     }
93   }
94   return numevents;
95 }