event 0.1.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -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/lib/event.rb CHANGED
@@ -18,4 +18,19 @@
18
18
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
19
  # THE SOFTWARE.
20
20
 
21
- require "event/version"
21
+ require_relative 'event/version'
22
+ require_relative 'event/backend/select'
23
+
24
+ module Event
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.
36
+ end
@@ -18,47 +18,58 @@
18
18
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
19
  # THE SOFTWARE.
20
20
 
21
- require "event/version"
22
-
23
21
  module Event
24
22
  module Backend
25
23
  class Select
26
- def initialize(loop = Fiber.current)
24
+ def initialize(loop)
27
25
  @loop = loop
28
26
 
29
27
  @readable = {}
30
28
  @writable = {}
31
29
  end
32
30
 
31
+ def close
32
+ @loop = nil
33
+ @readable = nil
34
+ @writable = nil
35
+ end
36
+
33
37
  def io_wait(fiber, io, events)
34
- if (events & IO::READABLE) > 0 or (events & IO::PRIORITY) > 0
38
+ remove_readable = remove_writable = false
39
+
40
+ if (events & READABLE) > 0 or (events & PRIORITY) > 0
35
41
  @readable[io] = fiber
42
+ remove_readable = true
36
43
  end
37
44
 
38
- if (events & IO::WRITABLE) > 0
45
+ if (events & WRITABLE) > 0
39
46
  @writable[io] = fiber
47
+ remove_writable = true
40
48
  end
41
49
 
42
50
  @loop.transfer
51
+ ensure
52
+ @readable.delete(io) if remove_readable
53
+ @writable.delete(io) if remove_writable
43
54
  end
44
55
 
45
56
  def select(duration = nil)
46
- readable, writable, _ = IO.select(@readable.keys, @writable.keys, nil, duration)
57
+ readable, writable, _ = ::IO.select(@readable.keys, @writable.keys, nil, duration)
47
58
 
48
- ready = {}
59
+ ready = Hash.new(0)
49
60
 
50
- readable.each do |io|
61
+ readable&.each do |io|
51
62
  fiber = @readable.delete(io)
52
- ready[fiber] = true
63
+ ready[fiber] |= READABLE
53
64
  end
54
65
 
55
- writable.each do |io|
66
+ writable&.each do |io|
56
67
  fiber = @writable.delete(io)
57
- ready[fiber] = true
68
+ ready[fiber] |= WRITABLE
58
69
  end
59
70
 
60
- ready.each_key do |fiber|
61
- fiber.transfer
71
+ ready.each do |fiber, events|
72
+ fiber.transfer(events)
62
73
  end
63
74
  end
64
75
  end
@@ -0,0 +1,108 @@
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 close
35
+ if @selector.nil?
36
+ raise "Selector already closed!"
37
+ end
38
+
39
+ @selector.close
40
+ @selector = nil
41
+ end
42
+
43
+ def io_wait(fiber, io, events)
44
+ register_readable(fiber, io, events)
45
+ end
46
+
47
+ def select(duration = nil)
48
+ @selector.select(duration)
49
+ end
50
+
51
+ private
52
+
53
+ def register_readable(fiber, io, events)
54
+ if (events & READABLE) > 0
55
+ if @readable.key?(io)
56
+ raise "Cannot wait for #{io} to become readable from multiple fibers."
57
+ end
58
+
59
+ begin
60
+ @readable[io] = fiber
61
+
62
+ register_writable(fiber, io, events)
63
+ ensure
64
+ @readable.delete(io)
65
+ end
66
+ else
67
+ register_writable(fiber, io, events)
68
+ end
69
+ end
70
+
71
+ def register_writable(fiber, io, events)
72
+ if (events & WRITABLE) > 0
73
+ if @writable.key?(io)
74
+ raise "Cannot wait for #{io} to become writable from multiple fibers."
75
+ end
76
+
77
+ begin
78
+ @writable[io] = fiber
79
+
80
+ register_priority(fiber, io, events)
81
+ ensure
82
+ @writable.delete(io)
83
+ end
84
+ else
85
+ register_priority(fiber, io, events)
86
+ end
87
+ end
88
+
89
+ def register_priority(fiber, io, events)
90
+ if (events & PRIORITY) > 0
91
+ if @priority.key?(io)
92
+ raise "Cannot wait for #{io} to become priority from multiple fibers."
93
+ end
94
+
95
+ begin
96
+ @priority[io] = fiber
97
+
98
+ @selector.io_wait(fiber, io, events)
99
+ ensure
100
+ @priority.delete(io)
101
+ end
102
+ else
103
+ @selector.io_wait(fiber, io, events)
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
data/lib/event/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Event
2
- VERSION = "0.1.1"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-26 00:00:00.000000000 Z
11
+ date: 2021-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -55,11 +69,23 @@ dependencies:
55
69
  description:
56
70
  email:
57
71
  executables: []
58
- extensions: []
72
+ extensions:
73
+ - ext/event/extconf.rb
59
74
  extra_rdoc_files: []
60
75
  files:
76
+ - ext/event/backend/backend.h
77
+ - ext/event/backend/epoll.c
78
+ - ext/event/backend/epoll.h
79
+ - ext/event/backend/kqueue.c
80
+ - ext/event/backend/kqueue.h
81
+ - ext/event/backend/uring.c
82
+ - ext/event/backend/uring.h
83
+ - ext/event/event.c
84
+ - ext/event/event.h
85
+ - ext/event/extconf.rb
61
86
  - lib/event.rb
62
87
  - lib/event/backend/select.rb
88
+ - lib/event/debug/selector.rb
63
89
  - lib/event/selector.rb
64
90
  - lib/event/version.rb
65
91
  homepage: https://github.com/socketry/event