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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'ext/event/backend/epoll.c', line 89

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

#closeObject



107
108
109
110
111
112
113
114
# File 'ext/event/backend/epoll.c', line 107

VALUE Event_Backend_EPoll_close(VALUE self) {
  struct Event_Backend_EPoll *data = NULL;
  TypedData_Get_Struct(self, struct Event_Backend_EPoll, &Event_Backend_EPoll_Type, data);
  
  close_internal(data);
  
  return Qnil;
}

#io_wait(fiber, io, events) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'ext/event/backend/epoll.c', line 225

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;
  
  event.events = epoll_flags_from_events(NUM2INT(events));
  event.data.ptr = (void*)fiber;
  
  // fprintf(stderr, "<- fiber=%p descriptor=%d\n", (void*)fiber, descriptor);
  
  // 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");
  }
  
  struct io_wait_arguments io_wait_arguments = {
    .data = data,
    .descriptor = descriptor,
    .duplicate = duplicate
  };
  
  return rb_ensure(io_wait_transfer, (VALUE)&io_wait_arguments, io_wait_ensure, (VALUE)&io_wait_arguments);
}

#process_wait(fiber, pid, flags) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'ext/event/backend/epoll.c', line 143

VALUE Event_Backend_EPoll_process_wait(VALUE self, VALUE fiber, VALUE pid, VALUE flags) {
  struct Event_Backend_EPoll *data = NULL;
  TypedData_Get_Struct(self, struct Event_Backend_EPoll, &Event_Backend_EPoll_Type, data);

  struct process_wait_arguments process_wait_arguments = {
    .data = data,
    .pid = NUM2PIDT(pid),
    .flags = NUM2INT(flags),
  };
  
  process_wait_arguments.descriptor = pidfd_open(process_wait_arguments.pid, 0);
  rb_update_max_fd(process_wait_arguments.descriptor);
  
  struct epoll_event event = {
    .events = EPOLLIN|EPOLLRDHUP|EPOLLONESHOT,
    .data = {.ptr = (void*)fiber},
  };
  
  int result = epoll_ctl(data->descriptor, EPOLL_CTL_ADD, process_wait_arguments.descriptor, &event);
  
  if (result == -1) {
    rb_sys_fail("epoll_ctl(process_wait)");
  }
  
  return rb_ensure(process_wait_transfer, (VALUE)&process_wait_arguments, process_wait_ensure, (VALUE)&process_wait_arguments);
}

#select(duration) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'ext/event/backend/epoll.c', line 322

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 select_arguments arguments = {
    .data = data,
    .timeout = 0
  };
  
  select_internal_with_gvl(&arguments);
  
  if (arguments.count == 0) {
    arguments.timeout = make_timeout(duration);
    
    if (arguments.timeout != 0) {
      select_internal_without_gvl(&arguments);
    }
  }
  
  for (int i = 0; i < arguments.count; i += 1) {
    VALUE fiber = (VALUE)arguments.events[i].data.ptr;
    VALUE result = INT2NUM(arguments.events[i].events);
    
    // fprintf(stderr, "-> fiber=%p descriptor=%d\n", (void*)fiber, events[i].data.fd);
    
    Event_Backend_transfer_result(fiber, result);
  }
  
  return INT2NUM(arguments.count);
}