port test/unit/test_ccc.rb to Perl 5
[unicorn.git] / ext / unicorn_http / epollexclusive.h
blobc74a779969b8b3bb35ef7a8e4c17b816f48b2187
1 /*
2 * This is only intended for use inside a unicorn worker, nowhere else.
3 * EPOLLEXCLUSIVE somewhat mitigates the thundering herd problem for
4 * mostly idle processes since we can't use blocking accept4.
5 * This is NOT intended for use with multi-threaded servers, nor
6 * single-threaded multi-client ("C10K") servers or anything advanced
7 * like that. This use of epoll is only appropriate for a primitive,
8 * single-client, single-threaded servers like unicorn that need to
9 * support SIGKILL timeouts and parent death detection.
11 #if defined(HAVE_EPOLL_CREATE1)
12 # include <sys/epoll.h>
13 # include <errno.h>
14 # include <ruby/io.h>
15 # include <ruby/thread.h>
16 #endif /* __linux__ */
18 #if defined(EPOLLEXCLUSIVE) && defined(HAVE_EPOLL_CREATE1)
19 # define USE_EPOLL (1)
20 #else
21 # define USE_EPOLL (0)
22 #endif
24 #if USE_EPOLL
25 #if defined(HAVE_RB_IO_DESCRIPTOR) /* Ruby 3.1+ */
26 # define my_fileno(io) rb_io_descriptor(io)
27 #else /* Ruby <3.1 */
28 static int my_fileno(VALUE io)
30 rb_io_t *fptr;
31 GetOpenFile(io, fptr);
32 rb_io_check_closed(fptr);
33 return fptr->fd;
35 #endif /* Ruby <3.1 */
38 * :nodoc:
39 * returns IO object if EPOLLEXCLUSIVE works and arms readers
41 static VALUE prep_readers(VALUE cls, VALUE readers)
43 long i;
44 int epfd = epoll_create1(EPOLL_CLOEXEC);
45 VALUE epio;
47 if (epfd < 0) rb_sys_fail("epoll_create1");
49 epio = rb_funcall(cls, rb_intern("for_fd"), 1, INT2NUM(epfd));
51 Check_Type(readers, T_ARRAY);
52 for (i = 0; i < RARRAY_LEN(readers); i++) {
53 int rc, fd;
54 struct epoll_event e;
55 VALUE io = rb_ary_entry(readers, i);
57 e.data.u64 = i; /* the reason readers shouldn't change */
60 * I wanted to use EPOLLET here, but maintaining our own
61 * equivalent of ep->rdllist in Ruby-space doesn't fit
62 * our design at all (and the kernel already has it's own
63 * code path for doing it). So let the kernel spend
64 * cycles on maintaining level-triggering.
66 e.events = EPOLLEXCLUSIVE | EPOLLIN;
67 fd = my_fileno(rb_io_get_io(io));
68 rc = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &e);
69 if (rc < 0) rb_sys_fail("epoll_ctl");
71 return epio;
73 #endif /* USE_EPOLL */
75 #if USE_EPOLL
76 struct ep_wait {
77 struct epoll_event event;
78 int epfd;
79 int timeout_msec;
82 static void *do_wait(void *ptr) /* runs w/o GVL */
84 struct ep_wait *epw = ptr;
86 * Linux delivers epoll events in the order received, and using
87 * maxevents=1 ensures we pluck one item off ep->rdllist
88 * at-a-time (c.f. fs/eventpoll.c in linux.git, it's quite
89 * easy-to-understand for anybody familiar with Ruby C).
91 return (void *)(long)epoll_wait(epw->epfd, &epw->event, 1,
92 epw->timeout_msec);
95 /* :nodoc: */
96 /* readers must not change between prepare_readers and get_readers */
97 static VALUE
98 get_readers(VALUE epio, VALUE ready, VALUE readers, VALUE timeout_msec)
100 struct ep_wait epw;
101 long n;
103 Check_Type(ready, T_ARRAY);
104 Check_Type(readers, T_ARRAY);
106 epw.epfd = my_fileno(epio);
107 epw.timeout_msec = NUM2INT(timeout_msec);
108 n = (long)rb_thread_call_without_gvl(do_wait, &epw, RUBY_UBF_IO, NULL);
109 if (n < 0) {
110 if (errno != EINTR) rb_sys_fail("epoll_wait");
111 } else if (n > 0) { /* maxevents is hardcoded to 1 */
112 VALUE obj = rb_ary_entry(readers, epw.event.data.u64);
114 if (RTEST(obj))
115 rb_ary_push(ready, obj);
116 } /* n == 0 : timeout */
117 return Qfalse;
119 #endif /* USE_EPOLL */
121 static void init_epollexclusive(VALUE mUnicorn)
123 #if USE_EPOLL
124 VALUE cWaiter = rb_define_class_under(mUnicorn, "Waiter", rb_cIO);
125 rb_define_singleton_method(cWaiter, "prep_readers", prep_readers, 1);
126 rb_define_method(cWaiter, "get_readers", get_readers, 3);
127 #endif