Class: Event::Backend::EPoll

Inherits:
Object
  • Object
show all
Defined in:
ext/event/backend/epoll.c

Instance Method Summary collapse

Constructor Details

#initialize(loop) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'ext/event/backend/epoll.c', line 82

VALUE Event_Backend_EPoll_initialize(VALUE self, VALUE loop) {
  struct Event_Backend_EPoll *data = NULL;
  TypedData_Get_Struct(self, struct Event_Backend_EPoll, &Event_Backend_EPoll_Type, data);
  
  data->loop = loop;
  int result = epoll_create1(EPOLL_CLOEXEC);
  
  if (result == -1) {
    rb_sys_fail("epoll_create");
  } else {
    data->descriptor = result;
    
    rb_update_max_fd(data->descriptor);
  }
  
  return self;
}

Instance Method Details

#io_wait(fiber, io, events) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'ext/event/backend/epoll.c', line 100

VALUE Event_Backend_EPoll_io_wait(VALUE self, VALUE fiber, VALUE io, VALUE events) {
  struct Event_Backend_EPoll *data = NULL;
  TypedData_Get_Struct(self, struct Event_Backend_EPoll, &Event_Backend_EPoll_Type, data);
  
  struct epoll_event event = {0};
  
  int descriptor = NUM2INT(rb_funcall(io, id_fileno, 0));
  int duplicate = -1;
  
  int mask = NUM2INT(events);
  
  if (mask & READABLE) {
    event.events |= EPOLLIN;
  }
  
  if (mask & PRIORITY) {
    event.events |= EPOLLPRI;
  }
  
  if (mask & WRITABLE) {
    event.events |= EPOLLOUT;
  }
  
  event.events |= EPOLLRDHUP;
  event.events |= EPOLLONESHOT;
  
  event.data.ptr = (void*)fiber;
  
  // A better approach is to batch all changes:
  int result = epoll_ctl(data->descriptor, EPOLL_CTL_ADD, descriptor, &event);;
  
  if (result == -1 && errno == EEXIST) {
    // The file descriptor was already inserted into epoll.
    duplicate = descriptor = dup(descriptor);
    
    rb_update_max_fd(duplicate);
    
    if (descriptor == -1)
      rb_sys_fail("dup");
    
    result = epoll_ctl(data->descriptor, EPOLL_CTL_ADD, descriptor, &event);;
  }
  
  if (result == -1) {
    rb_sys_fail("epoll_ctl");
  }
  
  rb_funcall(data->loop, id_transfer, 0);
  
  if (duplicate >= 0) {
    close(duplicate);
  }
  
  return Qnil;
}

#select(duration) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'ext/event/backend/epoll.c', line 175

VALUE Event_Backend_EPoll_select(VALUE self, VALUE duration) {
  struct Event_Backend_EPoll *data = NULL;
  TypedData_Get_Struct(self, struct Event_Backend_EPoll, &Event_Backend_EPoll_Type, data);
  
  struct epoll_event events[EPOLL_MAX_EVENTS];
  
  int count = epoll_wait(data->descriptor, events, EPOLL_MAX_EVENTS, make_timeout(duration));
  
  if (count == -1) {
    rb_sys_fail("epoll_wait");
  }
  
  for (int i = 0; i < count; i += 1) {
    VALUE fiber = (VALUE)events[i].data.ptr;
    rb_funcall(fiber, id_transfer, 0);
  }
  
  return INT2NUM(count);
}