Class: Event::Selector::EPoll

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

Instance Method Summary collapse

Constructor Details

#initialize(loop) ⇒ Object



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

VALUE Event_Selector_EPoll_initialize(VALUE self, VALUE loop) {
  struct Event_Selector_EPoll *data = NULL;
  TypedData_Get_Struct(self, struct Event_Selector_EPoll, &Event_Selector_EPoll_Type, data);
  
  Event_Selector_initialize(&data->backend, 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



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

VALUE Event_Selector_EPoll_close(VALUE self) {
  struct Event_Selector_EPoll *data = NULL;
  TypedData_Get_Struct(self, struct Event_Selector_EPoll, &Event_Selector_EPoll_Type, data);
  
  close_internal(data);
  
  return Qnil;
}

#io_read(fiber, io, buffer, _length) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'ext/event/selector/epoll.c', line 375

VALUE Event_Selector_EPoll_io_read(VALUE self, VALUE fiber, VALUE io, VALUE buffer, VALUE _length) {
  int descriptor = Event_Selector_io_descriptor(io);
  
  size_t length = NUM2SIZET(_length);
  
  struct io_read_arguments io_read_arguments = {
    .self = self,
    .fiber = fiber,
    .io = io,
    
    .flags = Event_Selector_nonblock_set(descriptor),
    .descriptor = descriptor,
    .buffer = buffer,
    .length = length,
  };
  
  return rb_ensure(io_read_loop, (VALUE)&io_read_arguments, io_read_ensure, (VALUE)&io_read_arguments);
}

#io_wait(fiber, io, events) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'ext/event/selector/epoll.c', line 279

VALUE Event_Selector_EPoll_io_wait(VALUE self, VALUE fiber, VALUE io, VALUE events) {
  struct Event_Selector_EPoll *data = NULL;
  TypedData_Get_Struct(self, struct Event_Selector_EPoll, &Event_Selector_EPoll_Type, data);
  
  struct epoll_event event = {0};
  
  int descriptor = Event_Selector_io_descriptor(io);
  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);
}

#io_write(fiber, io, buffer, _length) ⇒ Object



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'ext/event/selector/epoll.c', line 447

VALUE Event_Selector_EPoll_io_write(VALUE self, VALUE fiber, VALUE io, VALUE buffer, VALUE _length) {
  int descriptor = Event_Selector_io_descriptor(io);
  
  size_t length = NUM2SIZET(_length);
  
  struct io_write_arguments io_write_arguments = {
    .self = self,
    .fiber = fiber,
    .io = io,
    
    .flags = Event_Selector_nonblock_set(descriptor),
    .descriptor = descriptor,
    .buffer = buffer,
    .length = length,
  };
  
  return rb_ensure(io_write_loop, (VALUE)&io_write_arguments, io_write_ensure, (VALUE)&io_write_arguments);
}

#process_wait(fiber, pid, flags) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'ext/event/selector/epoll.c', line 197

VALUE Event_Selector_EPoll_process_wait(VALUE self, VALUE fiber, VALUE pid, VALUE flags) {
  struct Event_Selector_EPoll *data = NULL;
  TypedData_Get_Struct(self, struct Event_Selector_EPoll, &Event_Selector_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);
}

#push(fiber) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'ext/event/selector/epoll.c', line 145

VALUE Event_Selector_EPoll_push(VALUE self, VALUE fiber)
{
  struct Event_Selector_EPoll *data = NULL;
  TypedData_Get_Struct(self, struct Event_Selector_EPoll, &Event_Selector_EPoll_Type, data);
  
  Event_Selector_queue_push(&data->backend, fiber);
  
  return Qnil;
}

#raise(*args) ⇒ Object



155
156
157
158
159
160
161
# File 'ext/event/selector/epoll.c', line 155

VALUE Event_Selector_EPoll_raise(int argc, VALUE *argv, VALUE self)
{
  struct Event_Selector_EPoll *data = NULL;
  TypedData_Get_Struct(self, struct Event_Selector_EPoll, &Event_Selector_EPoll_Type, data);
  
  return Event_Selector_wait_and_raise(&data->backend, argc, argv);
}

#ready?Boolean

Returns:

  • (Boolean)


163
164
165
166
167
168
# File 'ext/event/selector/epoll.c', line 163

VALUE Event_Selector_EPoll_ready_p(VALUE self) {
  struct Event_Selector_EPoll *data = NULL;
  TypedData_Get_Struct(self, struct Event_Selector_EPoll, &Event_Selector_EPoll_Type, data);
  
  return data->backend.ready ? Qtrue : Qfalse;
}

#resume(*args) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'ext/event/selector/epoll.c', line 125

VALUE Event_Selector_EPoll_resume(int argc, VALUE *argv, VALUE self)
{
  struct Event_Selector_EPoll *data = NULL;
  TypedData_Get_Struct(self, struct Event_Selector_EPoll, &Event_Selector_EPoll_Type, data);
  
  Event_Selector_resume(&data->backend, argc, argv);
  
  return Qnil;
}

#select(duration) ⇒ Object



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'ext/event/selector/epoll.c', line 523

VALUE Event_Selector_EPoll_select(VALUE self, VALUE duration) {
  struct Event_Selector_EPoll *data = NULL;
  TypedData_Get_Struct(self, struct Event_Selector_EPoll, &Event_Selector_EPoll_Type, data);
  
  int ready = Event_Selector_queue_flush(&data->backend);
  
  struct select_arguments arguments = {
    .data = data,
    .timeout = 0
  };
  
  select_internal_with_gvl(&arguments);

  // If the ready list was empty and no events were processed: 
  if (!ready && 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_Selector_fiber_transfer(fiber, 1, &result);
  }
  
  return INT2NUM(arguments.count);
}

#transferObject



115
116
117
118
119
120
121
122
123
# File 'ext/event/selector/epoll.c', line 115

VALUE Event_Selector_EPoll_transfer(VALUE self)
{
  struct Event_Selector_EPoll *data = NULL;
  TypedData_Get_Struct(self, struct Event_Selector_EPoll, &Event_Selector_EPoll_Type, data);
  
  Event_Selector_fiber_transfer(data->backend.loop, 0, NULL);
  
  return Qnil;
}

#yieldObject



135
136
137
138
139
140
141
142
143
# File 'ext/event/selector/epoll.c', line 135

VALUE Event_Selector_EPoll_yield(VALUE self)
{
  struct Event_Selector_EPoll *data = NULL;
  TypedData_Get_Struct(self, struct Event_Selector_EPoll, &Event_Selector_EPoll_Type, data);
  
  Event_Selector_yield(&data->backend);
  
  return Qnil;
}