event 0.1.0 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1b8d69bf47d4d3deb8ade5279d26b662bc2f83d330d6887eda18272501e11f38
4
- data.tar.gz: 25c43e30cd6135bcd4bc8481cc734894f35e4caa947eefb7b9c1af1a071fbcf6
3
+ metadata.gz: 9a363339a1d384ea4d844450be2ff46cbc091bc0aca240e869491bf6a5d61300
4
+ data.tar.gz: b1b3c431481f4499bf6605010392283acfb6f54a59aed232f25217d2cce4a221
5
5
  SHA512:
6
- metadata.gz: edc80ba4e1310eb3f0cdfde4e87d416c727aafeefc691ef63e4a90c179c66010cd984be75fcb6c8ece8bbf863fe8c83764f18c1b302b933bb936543a2b0136d2
7
- data.tar.gz: 5516ca2eb3119a1510e5910b75be8e529e05d726eee2842e3489d3c67a15adb24e054f5823f1ddce5e90c758172be9bce86830e02d6a9c5183d4d6f6ca816b4b
6
+ metadata.gz: 3321631a8be1353038cdc9c63a2b6b17f02ea66924b11f5fb9126f9db1072ea4ee59141858f26e8879e1b52fe265471a2552afcab56cddd84b1e28d1611235d9
7
+ data.tar.gz: 7196977bd770153c5be35d16701e32a635c84cf53edf2880b4c5f095b8644825c7dcd1254a4da8f72655fb48e3ac3ec529d2ae2496691fb9ead30bb595cae357
@@ -0,0 +1,267 @@
1
+
2
+ SHELL = /bin/sh
3
+
4
+ # V=0 quiet, V=1 verbose. other values don't work.
5
+ V = 0
6
+ Q1 = $(V:1=)
7
+ Q = $(Q1:0=@)
8
+ ECHO1 = $(V:1=@ :)
9
+ ECHO = $(ECHO1:0=@ echo)
10
+ NULLCMD = :
11
+
12
+ #### Start of system configuration section. ####
13
+
14
+ srcdir = .
15
+ topdir = /Users/samuel/.rubies/ruby-3.0.0/include/ruby-3.0.0
16
+ hdrdir = $(topdir)
17
+ arch_hdrdir = /Users/samuel/.rubies/ruby-3.0.0/include/ruby-3.0.0/x86_64-darwin20
18
+ PATH_SEPARATOR = :
19
+ VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby:$(srcdir)/backend
20
+ prefix = $(DESTDIR)/Users/samuel/.rubies/ruby-3.0.0
21
+ rubysitearchprefix = $(rubylibprefix)/$(sitearch)
22
+ rubyarchprefix = $(rubylibprefix)/$(arch)
23
+ rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
24
+ exec_prefix = $(prefix)
25
+ vendorarchhdrdir = $(vendorhdrdir)/$(sitearch)
26
+ sitearchhdrdir = $(sitehdrdir)/$(sitearch)
27
+ rubyarchhdrdir = $(rubyhdrdir)/$(arch)
28
+ vendorhdrdir = $(rubyhdrdir)/vendor_ruby
29
+ sitehdrdir = $(rubyhdrdir)/site_ruby
30
+ rubyhdrdir = $(includedir)/$(RUBY_VERSION_NAME)
31
+ vendorarchdir = $(vendorlibdir)/$(sitearch)
32
+ vendorlibdir = $(vendordir)/$(ruby_version)
33
+ vendordir = $(rubylibprefix)/vendor_ruby
34
+ sitearchdir = $(sitelibdir)/$(sitearch)
35
+ sitelibdir = $(sitedir)/$(ruby_version)
36
+ sitedir = $(rubylibprefix)/site_ruby
37
+ rubyarchdir = $(rubylibdir)/$(arch)
38
+ rubylibdir = $(rubylibprefix)/$(ruby_version)
39
+ sitearchincludedir = $(includedir)/$(sitearch)
40
+ archincludedir = $(includedir)/$(arch)
41
+ sitearchlibdir = $(libdir)/$(sitearch)
42
+ archlibdir = $(libdir)/$(arch)
43
+ ridir = $(datarootdir)/$(RI_BASE_NAME)
44
+ mandir = $(datarootdir)/man
45
+ localedir = $(datarootdir)/locale
46
+ libdir = $(exec_prefix)/lib
47
+ psdir = $(docdir)
48
+ pdfdir = $(docdir)
49
+ dvidir = $(docdir)
50
+ htmldir = $(docdir)
51
+ infodir = $(datarootdir)/info
52
+ docdir = $(datarootdir)/doc/$(PACKAGE)
53
+ oldincludedir = $(SDKROOT)/usr/include
54
+ includedir = $(prefix)/include
55
+ runstatedir = $(localstatedir)/run
56
+ localstatedir = $(prefix)/var
57
+ sharedstatedir = $(prefix)/com
58
+ sysconfdir = $(prefix)/etc
59
+ datadir = $(datarootdir)
60
+ datarootdir = $(prefix)/share
61
+ libexecdir = $(exec_prefix)/libexec
62
+ sbindir = $(exec_prefix)/sbin
63
+ bindir = $(exec_prefix)/bin
64
+ archdir = $(rubyarchdir)
65
+
66
+
67
+ CC_WRAPPER =
68
+ CC = clang -fdeclspec
69
+ CXX = clang++ -fdeclspec
70
+ LIBRUBY = $(LIBRUBY_A)
71
+ LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
72
+ LIBRUBYARG_SHARED =
73
+ LIBRUBYARG_STATIC = -l$(RUBY_SO_NAME)-static -framework Security -framework Foundation $(MAINLIBS)
74
+ empty =
75
+ OUTFLAG = -o $(empty)
76
+ COUTFLAG = -o $(empty)
77
+ CSRCFLAG = $(empty)
78
+
79
+ RUBY_EXTCONF_H = extconf.h
80
+ cflags = $(optflags) $(debugflags) $(warnflags)
81
+ cxxflags =
82
+ optflags = -O3
83
+ debugflags = -ggdb3
84
+ warnflags = -Wall -Wextra -Wdeprecated-declarations -Wdivision-by-zero -Wimplicit-function-declaration -Wimplicit-int -Wmisleading-indentation -Wpointer-arith -Wshorten-64-to-32 -Wwrite-strings -Wmissing-noreturn -Wno-constant-logical-operand -Wno-long-long -Wno-missing-field-initializers -Wno-overlength-strings -Wno-parentheses-equality -Wno-self-assign -Wno-tautological-compare -Wno-unused-parameter -Wno-unused-value -Wunused-variable -Wextra-tokens
85
+ cppflags =
86
+ CCDLFLAGS = -fno-common
87
+ CFLAGS = $(CCDLFLAGS) $(cflags) -pipe -Wall $(ARCH_FLAG)
88
+ INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir)/ruby/backward -I$(hdrdir) -I$(srcdir)
89
+ DEFS =
90
+ CPPFLAGS = -DRUBY_EXTCONF_H=\"$(RUBY_EXTCONF_H)\" -I/opt/local/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT $(DEFS) $(cppflags)
91
+ CXXFLAGS = $(CCDLFLAGS) $(ARCH_FLAG)
92
+ ldflags = -L. -fstack-protector-strong -L/opt/local/lib
93
+ dldflags = -Wl,-undefined,dynamic_lookup -Wl,-multiply_defined,suppress -L/opt/local/lib
94
+ ARCH_FLAG =
95
+ DLDFLAGS = $(ldflags) $(dldflags) $(ARCH_FLAG)
96
+ LDSHARED = $(CC) -dynamic -bundle
97
+ LDSHAREDXX = $(CXX) -dynamic -bundle
98
+ AR = ar
99
+ EXEEXT =
100
+
101
+ RUBY_INSTALL_NAME = $(RUBY_BASE_NAME)
102
+ RUBY_SO_NAME = ruby.3.0
103
+ RUBYW_INSTALL_NAME =
104
+ RUBY_VERSION_NAME = $(RUBY_BASE_NAME)-$(ruby_version)
105
+ RUBYW_BASE_NAME = rubyw
106
+ RUBY_BASE_NAME = ruby
107
+
108
+ arch = x86_64-darwin20
109
+ sitearch = $(arch)
110
+ ruby_version = 3.0.0
111
+ ruby = $(bindir)/$(RUBY_BASE_NAME)
112
+ RUBY = $(ruby)
113
+ ruby_headers = $(hdrdir)/ruby.h $(hdrdir)/ruby/backward.h $(hdrdir)/ruby/ruby.h $(hdrdir)/ruby/defines.h $(hdrdir)/ruby/missing.h $(hdrdir)/ruby/intern.h $(hdrdir)/ruby/st.h $(hdrdir)/ruby/subst.h $(arch_hdrdir)/ruby/config.h $(RUBY_EXTCONF_H)
114
+
115
+ RM = rm -f
116
+ RM_RF = $(RUBY) -run -e rm -- -rf
117
+ RMDIRS = rmdir -p
118
+ MAKEDIRS = /opt/local/bin/gmkdir -p
119
+ INSTALL = /usr/bin/install -c
120
+ INSTALL_PROG = $(INSTALL) -m 0755
121
+ INSTALL_DATA = $(INSTALL) -m 644
122
+ COPY = cp
123
+ TOUCH = exit >
124
+
125
+ #### End of system configuration section. ####
126
+
127
+ preload =
128
+ libpath = . $(libdir) /opt/local/lib
129
+ LIBPATH = -L. -L$(libdir) -L/opt/local/lib
130
+ DEFFILE =
131
+
132
+ CLEANFILES = mkmf.log
133
+ DISTCLEANFILES =
134
+ DISTCLEANDIRS =
135
+
136
+ extout =
137
+ extout_prefix =
138
+ target_prefix = /event
139
+ LOCAL_LIBS =
140
+ LIBS =
141
+ ORIG_SRCS = event.c
142
+ SRCS = $(ORIG_SRCS) event.c kqueue.c
143
+ OBJS = event.o kqueue.o
144
+ HDRS = $(srcdir)/event.h $(srcdir)/extconf.h
145
+ LOCAL_HDRS =
146
+ TARGET = event
147
+ TARGET_NAME = event
148
+ TARGET_ENTRY = Init_$(TARGET_NAME)
149
+ DLLIB = $(TARGET).bundle
150
+ EXTSTATIC =
151
+ STATIC_LIB =
152
+
153
+ TIMESTAMP_DIR = .
154
+ BINDIR = $(bindir)
155
+ RUBYCOMMONDIR = $(sitedir)$(target_prefix)
156
+ RUBYLIBDIR = $(sitelibdir)$(target_prefix)
157
+ RUBYARCHDIR = $(sitearchdir)$(target_prefix)
158
+ HDRDIR = $(rubyhdrdir)/ruby$(target_prefix)
159
+ ARCHHDRDIR = $(rubyhdrdir)/$(arch)/ruby$(target_prefix)
160
+ TARGET_SO_DIR =
161
+ TARGET_SO = $(TARGET_SO_DIR)$(DLLIB)
162
+ CLEANLIBS = $(TARGET_SO)
163
+ CLEANOBJS = *.o *.bak
164
+
165
+ all: $(DLLIB)
166
+ static: $(STATIC_LIB)
167
+ .PHONY: all install static install-so install-rb
168
+ .PHONY: clean clean-so clean-static clean-rb
169
+
170
+ clean-static::
171
+ clean-rb-default::
172
+ clean-rb::
173
+ clean-so::
174
+ clean: clean-so clean-static clean-rb-default clean-rb
175
+ -$(Q)$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES) .*.time
176
+
177
+ distclean-rb-default::
178
+ distclean-rb::
179
+ distclean-so::
180
+ distclean-static::
181
+ distclean: clean distclean-so distclean-static distclean-rb-default distclean-rb
182
+ -$(Q)$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
183
+ -$(Q)$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
184
+ -$(Q)$(RMDIRS) $(DISTCLEANDIRS) 2> /dev/null || true
185
+
186
+ realclean: distclean
187
+ install: install-so install-rb
188
+
189
+ install-so: $(DLLIB) $(TIMESTAMP_DIR)/.sitearchdir.-.event.time
190
+ $(INSTALL_PROG) $(DLLIB) $(RUBYARCHDIR)
191
+ clean-static::
192
+ -$(Q)$(RM) $(STATIC_LIB)
193
+ install-rb: pre-install-rb do-install-rb install-rb-default
194
+ install-rb-default: pre-install-rb-default do-install-rb-default
195
+ pre-install-rb: Makefile
196
+ pre-install-rb-default: Makefile
197
+ do-install-rb:
198
+ do-install-rb-default:
199
+ pre-install-rb-default:
200
+ @$(NULLCMD)
201
+ $(TIMESTAMP_DIR)/.sitearchdir.-.event.time:
202
+ $(Q) $(MAKEDIRS) $(@D) $(RUBYARCHDIR)
203
+ $(Q) $(TOUCH) $@
204
+
205
+ site-install: site-install-so site-install-rb
206
+ site-install-so: install-so
207
+ site-install-rb: install-rb
208
+
209
+ .SUFFIXES: .c .m .cc .mm .cxx .cpp .o .S
210
+
211
+ .cc.o:
212
+ $(ECHO) compiling $(<)
213
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
214
+
215
+ .cc.S:
216
+ $(ECHO) translating $(<)
217
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
218
+
219
+ .mm.o:
220
+ $(ECHO) compiling $(<)
221
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
222
+
223
+ .mm.S:
224
+ $(ECHO) translating $(<)
225
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
226
+
227
+ .cxx.o:
228
+ $(ECHO) compiling $(<)
229
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
230
+
231
+ .cxx.S:
232
+ $(ECHO) translating $(<)
233
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
234
+
235
+ .cpp.o:
236
+ $(ECHO) compiling $(<)
237
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
238
+
239
+ .cpp.S:
240
+ $(ECHO) translating $(<)
241
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
242
+
243
+ .c.o:
244
+ $(ECHO) compiling $(<)
245
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
246
+
247
+ .c.S:
248
+ $(ECHO) translating $(<)
249
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
250
+
251
+ .m.o:
252
+ $(ECHO) compiling $(<)
253
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
254
+
255
+ .m.S:
256
+ $(ECHO) translating $(<)
257
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
258
+
259
+ $(TARGET_SO): $(OBJS) Makefile
260
+ $(ECHO) linking shared-object event/$(DLLIB)
261
+ -$(Q)$(RM) $(@)
262
+ $(Q) $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
263
+ $(Q) $(POSTLINK)
264
+
265
+
266
+
267
+ $(OBJS): $(HDRS) $(ruby_headers)
@@ -0,0 +1,29 @@
1
+ // Copyright, 2021, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ // of this software and associated documentation files (the "Software"), to deal
5
+ // in the Software without restriction, including without limitation the rights
6
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ // copies of the Software, and to permit persons to whom the Software is
8
+ // furnished to do so, subject to the following conditions:
9
+ //
10
+ // The above copyright notice and this permission notice shall be included in
11
+ // all copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ // THE SOFTWARE.
20
+
21
+ enum Event {
22
+ READABLE = 1,
23
+ PRIORITY = 2,
24
+ WRITABLE = 4,
25
+ ERROR = 8,
26
+ HANGUP = 16
27
+ };
28
+
29
+ #include <ruby/thread.h>
@@ -0,0 +1,293 @@
1
+ // Copyright, 2021, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ // of this software and associated documentation files (the "Software"), to deal
5
+ // in the Software without restriction, including without limitation the rights
6
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ // copies of the Software, and to permit persons to whom the Software is
8
+ // furnished to do so, subject to the following conditions:
9
+ //
10
+ // The above copyright notice and this permission notice shall be included in
11
+ // all copies or substantial portions of the Software.
12
+ //
13
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ // THE SOFTWARE.
20
+
21
+ #include "kqueue.h"
22
+ #include "backend.h"
23
+
24
+ #include <sys/epoll.h>
25
+ #include <time.h>
26
+ #include <errno.h>
27
+
28
+ static VALUE Event_Backend_EPoll = Qnil;
29
+ static ID id_fileno, id_transfer;
30
+
31
+ enum {EPOLL_MAX_EVENTS = 64};
32
+
33
+ struct Event_Backend_EPoll {
34
+ VALUE loop;
35
+ int descriptor;
36
+ };
37
+
38
+ void Event_Backend_EPoll_Type_mark(void *_data)
39
+ {
40
+ struct Event_Backend_EPoll *data = _data;
41
+ rb_gc_mark(data->loop);
42
+ }
43
+
44
+ void Event_Backend_EPoll_Type_free(void *_data)
45
+ {
46
+ struct Event_Backend_EPoll *data = _data;
47
+
48
+ if (data->descriptor >= 0) {
49
+ close(data->descriptor);
50
+ }
51
+
52
+ free(data);
53
+ }
54
+
55
+ size_t Event_Backend_EPoll_Type_size(const void *data)
56
+ {
57
+ return sizeof(struct Event_Backend_EPoll);
58
+ }
59
+
60
+ static const rb_data_type_t Event_Backend_EPoll_Type = {
61
+ .wrap_struct_name = "Event::Backend::EPoll",
62
+ .function = {
63
+ .dmark = Event_Backend_EPoll_Type_mark,
64
+ .dfree = Event_Backend_EPoll_Type_free,
65
+ .dsize = Event_Backend_EPoll_Type_size,
66
+ },
67
+ .data = NULL,
68
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
69
+ };
70
+
71
+ VALUE Event_Backend_EPoll_allocate(VALUE self) {
72
+ struct Event_Backend_EPoll *data = NULL;
73
+ VALUE instance = TypedData_Make_Struct(self, struct Event_Backend_EPoll, &Event_Backend_EPoll_Type, data);
74
+
75
+ data->loop = Qnil;
76
+ data->descriptor = -1;
77
+
78
+ return instance;
79
+ }
80
+
81
+ VALUE Event_Backend_EPoll_initialize(VALUE self, VALUE loop) {
82
+ struct Event_Backend_EPoll *data = NULL;
83
+ TypedData_Get_Struct(self, struct Event_Backend_EPoll, &Event_Backend_EPoll_Type, data);
84
+
85
+ data->loop = loop;
86
+ int result = epoll_create1(EPOLL_CLOEXEC);
87
+
88
+ if (result == -1) {
89
+ rb_sys_fail("epoll_create");
90
+ } else {
91
+ data->descriptor = result;
92
+
93
+ rb_update_max_fd(data->descriptor);
94
+ }
95
+
96
+ return self;
97
+ }
98
+
99
+ static inline
100
+ uint32_t epoll_flags_from_events(int events) {
101
+ uint32_t flags = 0;
102
+
103
+ if (events & READABLE) flags |= EPOLLIN;
104
+ if (events & PRIORITY) flags |= EPOLLPRI;
105
+ if (events & WRITABLE) flags |= EPOLLOUT;
106
+
107
+ flags |= EPOLLRDHUP;
108
+ flags |= EPOLLONESHOT;
109
+
110
+ return flags;
111
+ }
112
+
113
+ static inline
114
+ int events_from_epoll_flags(uint32_t flags) {
115
+ int events = 0;
116
+
117
+ if (flags & EPOLLIN) events |= READABLE;
118
+ if (flags & EPOLLPRI) events |= PRIORITY;
119
+ if (flags & EPOLLOUT) events |= WRITABLE;
120
+
121
+ return events;
122
+ }
123
+
124
+ struct io_wait_arguments {
125
+ struct Event_Backend_EPoll *data;
126
+ int descriptor;
127
+ int duplicate;
128
+ };
129
+
130
+ static
131
+ VALUE io_wait_ensure(VALUE _arguments) {
132
+ struct io_wait_arguments *arguments = (struct io_wait_arguments *)_arguments;
133
+
134
+ if (arguments->duplicate >= 0) {
135
+ epoll_ctl(arguments->data->descriptor, EPOLL_CTL_DEL, arguments->duplicate, NULL);
136
+
137
+ close(arguments->duplicate);
138
+ } else {
139
+ epoll_ctl(arguments->data->descriptor, EPOLL_CTL_DEL, arguments->descriptor, NULL);
140
+ }
141
+
142
+ return Qnil;
143
+ };
144
+
145
+ static
146
+ VALUE io_wait_transfer(VALUE _arguments) {
147
+ struct io_wait_arguments *arguments = (struct io_wait_arguments *)_arguments;
148
+
149
+ VALUE result = rb_funcall(arguments->data->loop, id_transfer, 0);
150
+
151
+ return INT2NUM(events_from_epoll_flags(NUM2INT(result)));
152
+ };
153
+
154
+ VALUE Event_Backend_EPoll_io_wait(VALUE self, VALUE fiber, VALUE io, VALUE events) {
155
+ struct Event_Backend_EPoll *data = NULL;
156
+ TypedData_Get_Struct(self, struct Event_Backend_EPoll, &Event_Backend_EPoll_Type, data);
157
+
158
+ struct epoll_event event = {0};
159
+
160
+ int descriptor = NUM2INT(rb_funcall(io, id_fileno, 0));
161
+ int duplicate = -1;
162
+
163
+ event.events = epoll_flags_from_events(NUM2INT(events));
164
+ event.data.ptr = (void*)fiber;
165
+
166
+ // fprintf(stderr, "<- fiber=%p descriptor=%d\n", (void*)fiber, descriptor);
167
+
168
+ // A better approach is to batch all changes:
169
+ int result = epoll_ctl(data->descriptor, EPOLL_CTL_ADD, descriptor, &event);
170
+
171
+ if (result == -1 && errno == EEXIST) {
172
+ // The file descriptor was already inserted into epoll.
173
+ duplicate = descriptor = dup(descriptor);
174
+
175
+ rb_update_max_fd(duplicate);
176
+
177
+ if (descriptor == -1)
178
+ rb_sys_fail("dup");
179
+
180
+ result = epoll_ctl(data->descriptor, EPOLL_CTL_ADD, descriptor, &event);
181
+ }
182
+
183
+ if (result == -1) {
184
+ rb_sys_fail("epoll_ctl");
185
+ }
186
+
187
+ struct io_wait_arguments io_wait_arguments = {
188
+ .data = data,
189
+ .descriptor = descriptor,
190
+ .duplicate = duplicate
191
+ };
192
+
193
+ return rb_ensure(io_wait_transfer, (VALUE)&io_wait_arguments, io_wait_ensure, (VALUE)&io_wait_arguments);
194
+ }
195
+
196
+ static
197
+ int make_timeout(VALUE duration) {
198
+ if (duration == Qnil) {
199
+ return -1;
200
+ }
201
+
202
+ if (FIXNUM_P(duration)) {
203
+ return NUM2LONG(duration) * 1000L;
204
+ }
205
+
206
+ else if (RB_FLOAT_TYPE_P(duration)) {
207
+ double value = RFLOAT_VALUE(duration);
208
+
209
+ return value * 1000;
210
+ }
211
+
212
+ rb_raise(rb_eRuntimeError, "unable to convert timeout");
213
+ }
214
+
215
+ struct select_arguments {
216
+ struct Event_Backend_EPoll *data;
217
+
218
+ int count;
219
+ struct epoll_event events[EPOLL_MAX_EVENTS];
220
+
221
+ int timeout;
222
+ };
223
+
224
+ static
225
+ void * select_internal(void *_arguments) {
226
+ struct select_arguments * arguments = (struct select_arguments *)_arguments;
227
+
228
+ arguments->count = epoll_wait(arguments->data->descriptor, arguments->events, EPOLL_MAX_EVENTS, arguments->timeout);
229
+
230
+ return NULL;
231
+ }
232
+
233
+ static
234
+ void select_internal_without_gvl(struct select_arguments *arguments) {
235
+ rb_thread_call_without_gvl(select_internal, (void *)arguments, RUBY_UBF_IO, 0);
236
+
237
+ if (arguments->count == -1) {
238
+ rb_sys_fail("select_internal_without_gvl:epoll_wait");
239
+ }
240
+ }
241
+
242
+ static
243
+ void select_internal_with_gvl(struct select_arguments *arguments) {
244
+ select_internal((void *)arguments);
245
+
246
+ if (arguments->count == -1) {
247
+ rb_sys_fail("select_internal_with_gvl:epoll_wait");
248
+ }
249
+ }
250
+
251
+ VALUE Event_Backend_EPoll_select(VALUE self, VALUE duration) {
252
+ struct Event_Backend_EPoll *data = NULL;
253
+ TypedData_Get_Struct(self, struct Event_Backend_EPoll, &Event_Backend_EPoll_Type, data);
254
+
255
+ struct select_arguments arguments = {
256
+ .data = data,
257
+ .timeout = 0
258
+ };
259
+
260
+ select_internal_without_gvl(&arguments);
261
+
262
+ if (arguments.count == 0) {
263
+ arguments.timeout = make_timeout(duration);
264
+
265
+ if (arguments.timeout != 0) {
266
+ select_internal_with_gvl(&arguments);
267
+ }
268
+ }
269
+
270
+ for (int i = 0; i < arguments.count; i += 1) {
271
+ VALUE fiber = (VALUE)arguments.events[i].data.ptr;
272
+ VALUE result = INT2NUM(arguments.events[i].events);
273
+
274
+ // fprintf(stderr, "-> fiber=%p descriptor=%d\n", (void*)fiber, events[i].data.fd);
275
+
276
+ rb_funcall(fiber, id_transfer, 1, result);
277
+ }
278
+
279
+ return INT2NUM(arguments.count);
280
+ }
281
+
282
+ void Init_Event_Backend_EPoll(VALUE Event_Backend) {
283
+ id_fileno = rb_intern("fileno");
284
+ id_transfer = rb_intern("transfer");
285
+
286
+ Event_Backend_EPoll = rb_define_class_under(Event_Backend, "EPoll", rb_cObject);
287
+
288
+ rb_define_alloc_func(Event_Backend_EPoll, Event_Backend_EPoll_allocate);
289
+ rb_define_method(Event_Backend_EPoll, "initialize", Event_Backend_EPoll_initialize, 1);
290
+
291
+ rb_define_method(Event_Backend_EPoll, "io_wait", Event_Backend_EPoll_io_wait, 3);
292
+ rb_define_method(Event_Backend_EPoll, "select", Event_Backend_EPoll_select, 1);
293
+ }