event 0.1.0 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/event/Makefile +267 -0
- data/ext/event/backend/backend.h +29 -0
- data/ext/event/backend/epoll.c +293 -0
- data/ext/event/backend/epoll.h +25 -0
- data/ext/event/backend/kqueue.c +330 -0
- data/ext/event/backend/kqueue.h +27 -0
- data/ext/event/backend/uring.c +407 -0
- data/ext/event/backend/uring.h +28 -0
- data/ext/event/event.bundle +0 -0
- data/ext/event/event.c +46 -0
- data/ext/event/event.h +39 -0
- data/ext/event/event.o +0 -0
- data/ext/event/extconf.h +4 -0
- data/ext/event/extconf.rb +51 -0
- data/ext/event/kqueue.o +0 -0
- data/ext/event/mkmf.log +73 -0
- data/lib/event.rb +33 -3
- data/lib/event/backend/select.rb +72 -0
- data/lib/event/debug/selector.rb +99 -0
- data/lib/event/selector.rb +26 -0
- data/lib/event/version.rb +1 -1
- metadata +49 -3
@@ -0,0 +1,28 @@
|
|
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
|
+
#pragma once
|
22
|
+
|
23
|
+
#include <ruby.h>
|
24
|
+
|
25
|
+
#define EVENT_BACKEND_URING
|
26
|
+
|
27
|
+
void Init_Event_Backend_URing(VALUE Event_Backend);
|
28
|
+
VALUE Event_Backend_URing_select(VALUE self, VALUE duration);
|
Binary file
|
data/ext/event/event.c
ADDED
@@ -0,0 +1,46 @@
|
|
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 "event.h"
|
22
|
+
|
23
|
+
VALUE Event = Qnil;
|
24
|
+
VALUE Event_Backend = Qnil;
|
25
|
+
|
26
|
+
void Init_event()
|
27
|
+
{
|
28
|
+
#ifdef HAVE_RB_EXT_RACTOR_SAFE
|
29
|
+
rb_ext_ractor_safe(true);
|
30
|
+
#endif
|
31
|
+
|
32
|
+
Event = rb_define_module("Event");
|
33
|
+
Event_Backend = rb_define_module_under(Event, "Backend");
|
34
|
+
|
35
|
+
#ifdef EVENT_BACKEND_URING
|
36
|
+
Init_Event_Backend_URing(Event_Backend);
|
37
|
+
#endif
|
38
|
+
|
39
|
+
#ifdef EVENT_BACKEND_EPOLL
|
40
|
+
Init_Event_Backend_EPoll(Event_Backend);
|
41
|
+
#endif
|
42
|
+
|
43
|
+
#ifdef EVENT_BACKEND_KQUEUE
|
44
|
+
Init_Event_Backend_KQueue(Event_Backend);
|
45
|
+
#endif
|
46
|
+
}
|
data/ext/event/event.h
ADDED
@@ -0,0 +1,39 @@
|
|
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
|
+
#pragma once
|
22
|
+
|
23
|
+
#include <ruby.h>
|
24
|
+
|
25
|
+
#include "extconf.h"
|
26
|
+
|
27
|
+
void Init_event();
|
28
|
+
|
29
|
+
#if HAVE_LIBURING_H
|
30
|
+
#include "backend/uring.h"
|
31
|
+
#endif
|
32
|
+
|
33
|
+
#if HAVE_SYS_EPOLL_H
|
34
|
+
#include "backend/epoll.h"
|
35
|
+
#endif
|
36
|
+
|
37
|
+
#if HAVE_SYS_EVENT_H
|
38
|
+
#include "backend/kqueue.h"
|
39
|
+
#endif
|
data/ext/event/event.o
ADDED
Binary file
|
data/ext/event/extconf.h
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Copyright, 2021, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
|
23
|
+
require 'mkmf'
|
24
|
+
|
25
|
+
gem_name = File.basename(__dir__)
|
26
|
+
extension_name = 'event'
|
27
|
+
|
28
|
+
# The destination
|
29
|
+
dir_config(extension_name)
|
30
|
+
|
31
|
+
$CFLAGS << " -Wall"
|
32
|
+
|
33
|
+
$srcs = ["event.c"]
|
34
|
+
$VPATH << "$(srcdir)/backend"
|
35
|
+
|
36
|
+
if have_library('uring') and have_header('liburing.h')
|
37
|
+
$srcs << "backend/uring.c"
|
38
|
+
end
|
39
|
+
|
40
|
+
if have_header('sys/epoll.h')
|
41
|
+
$srcs << "backend/epoll.c"
|
42
|
+
end
|
43
|
+
|
44
|
+
if have_header('sys/event.h')
|
45
|
+
$srcs << "backend/kqueue.c"
|
46
|
+
end
|
47
|
+
|
48
|
+
create_header
|
49
|
+
|
50
|
+
# Generate the makefile to compile the native binary into `lib`:
|
51
|
+
create_makefile(File.join(gem_name, extension_name))
|
data/ext/event/kqueue.o
ADDED
Binary file
|
data/ext/event/mkmf.log
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
have_library: checking for -luring... -------------------- no
|
2
|
+
|
3
|
+
"clang -fdeclspec -o conftest -I/Users/samuel/.rubies/ruby-3.0.0/include/ruby-3.0.0/x86_64-darwin20 -I/Users/samuel/.rubies/ruby-3.0.0/include/ruby-3.0.0/ruby/backward -I/Users/samuel/.rubies/ruby-3.0.0/include/ruby-3.0.0 -I. -I/opt/local/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT -O3 -ggdb3 -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 -pipe -Wall conftest.c -L. -L/Users/samuel/.rubies/ruby-3.0.0/lib -L/opt/local/lib -L. -fstack-protector-strong -L/opt/local/lib -lruby.3.0-static -framework Security -framework Foundation -lpthread -lgmp -ldl -lobjc "
|
4
|
+
checked program was:
|
5
|
+
/* begin */
|
6
|
+
1: #include "ruby.h"
|
7
|
+
2:
|
8
|
+
3: int main(int argc, char **argv)
|
9
|
+
4: {
|
10
|
+
5: return !!argv[argc];
|
11
|
+
6: }
|
12
|
+
/* end */
|
13
|
+
|
14
|
+
"clang -fdeclspec -o conftest -I/Users/samuel/.rubies/ruby-3.0.0/include/ruby-3.0.0/x86_64-darwin20 -I/Users/samuel/.rubies/ruby-3.0.0/include/ruby-3.0.0/ruby/backward -I/Users/samuel/.rubies/ruby-3.0.0/include/ruby-3.0.0 -I. -I/opt/local/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT -O3 -ggdb3 -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 -pipe -Wall conftest.c -L. -L/Users/samuel/.rubies/ruby-3.0.0/lib -L/opt/local/lib -L. -fstack-protector-strong -L/opt/local/lib -lruby.3.0-static -framework Security -framework Foundation -lpthread -lgmp -ldl -lobjc -luring "
|
15
|
+
ld: library not found for -luring
|
16
|
+
clang: error: linker command failed with exit code 1 (use -v to see invocation)
|
17
|
+
checked program was:
|
18
|
+
/* begin */
|
19
|
+
1: #include "ruby.h"
|
20
|
+
2:
|
21
|
+
3: /*top*/
|
22
|
+
4: extern int t(void);
|
23
|
+
5: int main(int argc, char **argv)
|
24
|
+
6: {
|
25
|
+
7: if (argc > 1000000) {
|
26
|
+
8: int (* volatile tp)(void)=(int (*)(void))&t;
|
27
|
+
9: printf("%d", (*tp)());
|
28
|
+
10: }
|
29
|
+
11:
|
30
|
+
12: return !!argv[argc];
|
31
|
+
13: }
|
32
|
+
14:
|
33
|
+
15: int t(void) { ; return 0; }
|
34
|
+
/* end */
|
35
|
+
|
36
|
+
--------------------
|
37
|
+
|
38
|
+
have_header: checking for sys/epoll.h... -------------------- no
|
39
|
+
|
40
|
+
"clang -E -I/Users/samuel/.rubies/ruby-3.0.0/include/ruby-3.0.0/x86_64-darwin20 -I/Users/samuel/.rubies/ruby-3.0.0/include/ruby-3.0.0/ruby/backward -I/Users/samuel/.rubies/ruby-3.0.0/include/ruby-3.0.0 -I. -I/opt/local/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT -O3 -ggdb3 -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 -pipe -Wall conftest.c -o conftest.i"
|
41
|
+
conftest.c:3:10: fatal error: 'sys/epoll.h' file not found
|
42
|
+
#include <sys/epoll.h>
|
43
|
+
^~~~~~~~~~~~~
|
44
|
+
1 error generated.
|
45
|
+
checked program was:
|
46
|
+
/* begin */
|
47
|
+
1: #include "ruby.h"
|
48
|
+
2:
|
49
|
+
3: #include <sys/epoll.h>
|
50
|
+
/* end */
|
51
|
+
|
52
|
+
--------------------
|
53
|
+
|
54
|
+
have_header: checking for sys/event.h... -------------------- yes
|
55
|
+
|
56
|
+
"clang -E -I/Users/samuel/.rubies/ruby-3.0.0/include/ruby-3.0.0/x86_64-darwin20 -I/Users/samuel/.rubies/ruby-3.0.0/include/ruby-3.0.0/ruby/backward -I/Users/samuel/.rubies/ruby-3.0.0/include/ruby-3.0.0 -I. -I/opt/local/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT -O3 -ggdb3 -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 -pipe -Wall conftest.c -o conftest.i"
|
57
|
+
checked program was:
|
58
|
+
/* begin */
|
59
|
+
1: #include "ruby.h"
|
60
|
+
2:
|
61
|
+
3: #include <sys/event.h>
|
62
|
+
/* end */
|
63
|
+
|
64
|
+
--------------------
|
65
|
+
|
66
|
+
extconf.h is:
|
67
|
+
/* begin */
|
68
|
+
1: #ifndef EXTCONF_H
|
69
|
+
2: #define EXTCONF_H
|
70
|
+
3: #define HAVE_SYS_EVENT_H 1
|
71
|
+
4: #endif
|
72
|
+
/* end */
|
73
|
+
|
data/lib/event.rb
CHANGED
@@ -1,6 +1,36 @@
|
|
1
|
-
|
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
|
+
require_relative 'event/version'
|
22
|
+
require_relative 'event/backend/select'
|
2
23
|
|
3
24
|
module Event
|
4
|
-
|
5
|
-
|
25
|
+
# These constants are the same as those defined in IO.
|
26
|
+
|
27
|
+
READABLE = 1
|
28
|
+
PRIORITY = 2
|
29
|
+
WRITABLE = 4
|
30
|
+
end
|
31
|
+
|
32
|
+
begin
|
33
|
+
require_relative '../ext/event/event'
|
34
|
+
rescue LoadError
|
35
|
+
# Ignore.
|
6
36
|
end
|
data/lib/event/backend/select.rb
CHANGED
@@ -0,0 +1,72 @@
|
|
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
|
+
module Event
|
22
|
+
module Backend
|
23
|
+
class Select
|
24
|
+
def initialize(loop)
|
25
|
+
@loop = loop
|
26
|
+
|
27
|
+
@readable = {}
|
28
|
+
@writable = {}
|
29
|
+
end
|
30
|
+
|
31
|
+
def io_wait(fiber, io, events)
|
32
|
+
remove_readable = remove_writable = false
|
33
|
+
|
34
|
+
if (events & READABLE) > 0 or (events & PRIORITY) > 0
|
35
|
+
@readable[io] = fiber
|
36
|
+
remove_readable = true
|
37
|
+
end
|
38
|
+
|
39
|
+
if (events & WRITABLE) > 0
|
40
|
+
@writable[io] = fiber
|
41
|
+
remove_writable = true
|
42
|
+
end
|
43
|
+
|
44
|
+
@loop.transfer
|
45
|
+
|
46
|
+
ensure
|
47
|
+
@readable.delete(io) if remove_readable
|
48
|
+
@writable.delete(io) if remove_writable
|
49
|
+
end
|
50
|
+
|
51
|
+
def select(duration = nil)
|
52
|
+
readable, writable, _ = ::IO.select(@readable.keys, @writable.keys, nil, duration)
|
53
|
+
|
54
|
+
ready = Hash.new(0)
|
55
|
+
|
56
|
+
readable&.each do |io|
|
57
|
+
fiber = @readable.delete(io)
|
58
|
+
ready[fiber] |= READABLE
|
59
|
+
end
|
60
|
+
|
61
|
+
writable&.each do |io|
|
62
|
+
fiber = @writable.delete(io)
|
63
|
+
ready[fiber] |= WRITABLE
|
64
|
+
end
|
65
|
+
|
66
|
+
ready.each do |fiber, events|
|
67
|
+
fiber.transfer(events)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,99 @@
|
|
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
|
+
require "event/version"
|
22
|
+
|
23
|
+
module Event
|
24
|
+
module Debug
|
25
|
+
class Selector
|
26
|
+
def initialize(selector)
|
27
|
+
@selector = selector
|
28
|
+
|
29
|
+
@readable = {}
|
30
|
+
@writable = {}
|
31
|
+
@priority = {}
|
32
|
+
end
|
33
|
+
|
34
|
+
def io_wait(fiber, io, events)
|
35
|
+
register_readable(fiber, io, events)
|
36
|
+
end
|
37
|
+
|
38
|
+
def select(duration = nil)
|
39
|
+
@selector.select(duration)
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def register_readable(fiber, io, events)
|
45
|
+
if (events & READABLE) > 0
|
46
|
+
if @readable.key?(io)
|
47
|
+
raise "Cannot wait for #{io} to become readable from multiple fibers."
|
48
|
+
end
|
49
|
+
|
50
|
+
begin
|
51
|
+
@readable[io] = fiber
|
52
|
+
|
53
|
+
register_writable(fiber, io, events)
|
54
|
+
ensure
|
55
|
+
@readable.delete(io)
|
56
|
+
end
|
57
|
+
else
|
58
|
+
register_writable(fiber, io, events)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def register_writable(fiber, io, events)
|
63
|
+
if (events & WRITABLE) > 0
|
64
|
+
if @writable.key?(io)
|
65
|
+
raise "Cannot wait for #{io} to become writable from multiple fibers."
|
66
|
+
end
|
67
|
+
|
68
|
+
begin
|
69
|
+
@writable[io] = fiber
|
70
|
+
|
71
|
+
register_priority(fiber, io, events)
|
72
|
+
ensure
|
73
|
+
@writable.delete(io)
|
74
|
+
end
|
75
|
+
else
|
76
|
+
register_priority(fiber, io, events)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def register_priority(fiber, io, events)
|
81
|
+
if (events & PRIORITY) > 0
|
82
|
+
if @priority.key?(io)
|
83
|
+
raise "Cannot wait for #{io} to become priority from multiple fibers."
|
84
|
+
end
|
85
|
+
|
86
|
+
begin
|
87
|
+
@priority[io] = fiber
|
88
|
+
|
89
|
+
@selector.io_wait(fiber, io, events)
|
90
|
+
ensure
|
91
|
+
@priority.delete(io)
|
92
|
+
end
|
93
|
+
else
|
94
|
+
@selector.io_wait(fiber, io, events)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|