summaryrefslogtreecommitdiffstats
path: root/src/msg/async/EventSelect.cc
blob: 35000ccea5c8afca6392707e4a4345287ae7cf17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// -*- 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"

#include <unistd.h>
#include <sys/select.h>
#define dout_subsys ceph_subsys_ms

#undef dout_prefix
#define dout_prefix *_dout << "SelectDriver."

int SelectDriver::init(EventCenter *c, int nevent)
{
  #ifndef _WIN32
  ldout(cct, 0) << "Select isn't suitable for production env, just avoid "
                << "compiling error or special purpose" << dendl;
  #endif
  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;

  int mask = cur_mask | add_mask;
  if (mask & EVENT_READABLE)
    FD_SET(fd, &rfds);
  if (mask & EVENT_WRITABLE)
    FD_SET(fd, &wfds);
  if (fd > max_fd)
      max_fd = fd;

  return 0;
}

int 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);
  return 0;
}

int SelectDriver::resize_events(int newsize)
{
  return 0;
}

int SelectDriver::event_wait(std::vector<FiredFileEvent> &fired_events, struct timeval *tvp)
{
  int retval, 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 (int 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;
      if (mask) {
        fe.fd = j;
        fe.mask = mask;
        fired_events.push_back(fe);
        numevents++;
      }
    }
  }
  return numevents;
}