evt 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5cba38d1879267d4e9ffa38598b11e4062a9c93f4c539c451ed480397109fea2
4
- data.tar.gz: 33cd316d1ca95d4e28a884a24ded65e87d1d63eb64c098620ab9133e03d2c455
3
+ metadata.gz: 4c45ef9b7d8ed527d6280ca836777fb2c54822e23c9414c0cdeb234561115f47
4
+ data.tar.gz: 9543f98474500b00ae1a68009a261db1538d986e71bb7d9437bc9f190aff3c38
5
5
  SHA512:
6
- metadata.gz: cb6126c17c2087bf8fb906d9f79d00adf844f83e53af6943d70693b97ee46cfadffffccbd97427cf13b4d11bfb3802107b16f35e12ac70d465b274ca7ddb4e77
7
- data.tar.gz: ea632137e88bd34eac453a2a3af24a025ea635025d8758657d9704410e37fd01d6286fcb0c625e63423ce30e8a7eff221730955e13e4681731fa7def2eaf4d67
6
+ metadata.gz: d9fe5b68df1d9e72d10314744ac173bc4f99a3d420034d861b4d1685c1f36bfbe24c993c9686c5862d45926dd8ddb901759d913dfb0ab39d730c380bfa27f3d4
7
+ data.tar.gz: cfbe9a5c112c71d96a737f29d9b4c5e1fd078b060331d999b7007a846b8e2a71f4de2eeebfc53e22dae8c33bf89afb15df5b36205b17f43f6f7e00096d3950f7
data/.gitignore CHANGED
@@ -6,3 +6,6 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /*.gem
10
+ /lib/*.bundle
11
+ /lib/*.so
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - ruby-head
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at dsh0416@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in evt.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ evt (0.1.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.14.1)
10
+ rake (12.3.3)
11
+ rake-compiler (1.1.1)
12
+ rake
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ evt!
19
+ minitest (~> 5.0)
20
+ rake (~> 12.0)
21
+ rake-compiler (~> 1.0)
22
+
23
+ BUNDLED WITH
24
+ 2.2.0.dev
data/README.md CHANGED
@@ -1,40 +1,2 @@
1
- # Evt
2
-
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/evt`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'evt'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle install
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install evt
22
-
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/evt. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/evt/blob/master/CODE_OF_CONDUCT.md).
36
-
37
-
38
- ## Code of Conduct
39
-
40
- Everyone interacting in the Evt project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/evt/blob/master/CODE_OF_CONDUCT.md).
1
+ # evt
2
+ A low-level Event Handler designed for Ruby 3 Scheduler
@@ -0,0 +1,16 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ require 'rake/extensiontask'
4
+
5
+ spec = Gem::Specification.load('evt.gemspec')
6
+ Rake::ExtensionTask.new('evt_ext', spec) do |ext|
7
+ ext.ext_dir = "ext/evt"
8
+ end
9
+
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << "test"
12
+ t.libs << "lib"
13
+ t.test_files = FileList["test/**/*_test.rb"]
14
+ end
15
+
16
+ task :default => :test
@@ -0,0 +1,27 @@
1
+ require_relative 'lib/evt/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "evt"
5
+ spec.version = Evt::VERSION
6
+ spec.authors = ["Delton Ding"]
7
+ spec.email = ["dsh0416@gmail.com"]
8
+
9
+ spec.summary = "A low-level Event Handler designed for Ruby 3 Scheduler"
10
+ spec.description = "A low-level Event Handler designed for Ruby 3 Scheduler for better performance"
11
+ spec.homepage = "https://github.com/dsh0416/evt"
12
+ spec.license = 'BSD-3-Clause'
13
+ spec.required_ruby_version = '>= 2.7.1'
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/dsh0416/evt"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.require_paths = ["lib"]
24
+ spec.extensions = ['ext/evt/extconf.rb']
25
+
26
+ spec.add_development_dependency 'rake-compiler', '~> 1.0'
27
+ end
@@ -0,0 +1,122 @@
1
+ #include <ruby.h>
2
+
3
+ VALUE Scheduler = Qnil;
4
+
5
+ void Init_evt_ext();
6
+ VALUE method_scheduler_init(VALUE self);
7
+ VALUE method_scheduler_register(VALUE self, VALUE io, VALUE interest);
8
+ VALUE method_scheduler_deregister(VALUE self, VALUE io);
9
+ VALUE method_scheduler_wait(VALUE self);
10
+
11
+ void Init_evt_ext()
12
+ {
13
+ Scheduler = rb_define_class("Scheduler", rb_cObject);
14
+ rb_define_method(Scheduler, "init_selector", method_scheduler_init, 0);
15
+ rb_define_method(Scheduler, "register", method_scheduler_register, 2);
16
+ rb_define_method(Scheduler, "deregister", method_scheduler_deregister, 1);
17
+ rb_define_method(Scheduler, "wait", method_scheduler_wait, 0);
18
+ }
19
+
20
+
21
+ #if defined(__linux__) // TODO: Do more checks for using epoll
22
+ #include <sys/epoll.h>
23
+ #define EPOLL_MAX_EVENTS 65535
24
+
25
+ VALUE method_scheduler_init(VALUE self) {
26
+ rb_iv_set(self, "@epfd", INT2NUM(epoll_create(1))); // Size of epoll is ignored after Linux 2.6.8.
27
+ return Qnil;
28
+ }
29
+
30
+ VALUE method_scheduler_register(VALUE self, VALUE io, VALUE interest) {
31
+ struct epoll_event event;
32
+ ID id_fileno = rb_intern("fileno");
33
+ int epfd = NUM2INT(rb_iv_get(self, "@epfd"));
34
+ int fd = NUM2INT(rb_funcall(io, id_fileno, 0));
35
+ int ruby_interest = NUM2INT(interest);
36
+ int readable = NUM2INT(rb_const_get(rb_cIO, rb_intern("WAIT_READABLE")));
37
+ int writable = NUM2INT(rb_const_get(rb_cIO, rb_intern("WAIT_WRITABLE")));
38
+
39
+ if (ruby_interest & readable) {
40
+ event.events |= EPOLLIN;
41
+ } else if (ruby_interest & writable) {
42
+ event.events |= EPOLLOUT;
43
+ }
44
+ event.data.ptr = (void*) io;
45
+
46
+ epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event);
47
+ return Qnil;
48
+ }
49
+
50
+ VALUE method_scheduler_deregister(VALUE self, VALUE io) {
51
+ ID id_fileno = rb_intern("fileno");
52
+ int epfd = NUM2INT(rb_iv_get(self, "@epfd"));
53
+ int fd = NUM2INT(rb_funcall(io, id_fileno, 0));
54
+ epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL); // Require Linux 2.6.9 for NULL event.
55
+ return Qnil;
56
+ }
57
+
58
+ VALUE method_scheduler_wait(VALUE self) {
59
+ int n, epfd, i, event_flag;
60
+ VALUE next_timeout, obj_io, readables, writables, result;
61
+ ID id_next_timeout = rb_intern("next_timeout");
62
+ ID id_push = rb_intern("push");
63
+
64
+ epfd = NUM2INT(rb_iv_get(self, "@epfd"));
65
+ next_timeout = rb_funcall(self, id_next_timeout, 0);
66
+ readables = rb_ary_new();
67
+ writables = rb_ary_new();
68
+
69
+ struct epoll_event* events = (struct epoll_event*) xmalloc(sizeof(struct epoll_event) * EPOLL_MAX_EVENTS);
70
+
71
+ n = epoll_wait(epfd, events, EPOLL_MAX_EVENTS, next_timeout);
72
+ // Check if n > 0
73
+
74
+ for (i = 0; i < n; i++) {
75
+ event_flag = events[i].events;
76
+ if (event_flag & EPOLLIN) {
77
+ obj_io = (VALUE) events[i].data.ptr;
78
+ rb_funcall(readables, id_push, 1, obj_io);
79
+ } else if (event_flag & EPOLLOUT) {
80
+ obj_io = (VALUE) events[i].data.ptr;
81
+ rb_funcall(writables, id_push, 1, obj_io);
82
+ }
83
+ }
84
+
85
+ result = rb_ary_new2(2);
86
+ rb_ary_store(result, 0, readables);
87
+ rb_ary_store(result, 1, writables);
88
+
89
+ xfree(events);
90
+ return result;
91
+ }
92
+ #else
93
+ // Fallback to IO.select
94
+ VALUE method_scheduler_init(VALUE self) {
95
+ return Qnil;
96
+ }
97
+
98
+ VALUE method_scheduler_register(VALUE self, VALUE io, VALUE interest) {
99
+ return Qnil;
100
+ }
101
+
102
+ VALUE method_scheduler_deregister(VALUE self, VALUE io) {
103
+ return Qnil;
104
+ }
105
+
106
+ VALUE method_scheduler_wait(VALUE self) {
107
+ // return IO.select(@readable.keys, @writable.keys, [], next_timeout)
108
+ VALUE readable, writable, readable_keys, writable_keys, next_timeout;
109
+ ID id_select = rb_intern("select");
110
+ ID id_keys = rb_intern("keys");
111
+ ID id_next_timeout = rb_intern("next_timeout");
112
+
113
+ readable = rb_iv_get(self, "@readable");
114
+ writable = rb_iv_get(self, "@writable");
115
+
116
+ readable_keys = rb_funcall(readable, id_keys, 0);
117
+ writable_keys = rb_funcall(writable, id_keys, 0);
118
+ next_timeout = rb_funcall(self, id_next_timeout, 0);
119
+
120
+ return rb_funcall(rb_cIO, id_select, 4, readable_keys, writable_keys, rb_ary_new(), next_timeout);
121
+ }
122
+ #endif
@@ -0,0 +1,5 @@
1
+ require 'mkmf'
2
+ extension_name = 'evt_ext'
3
+ create_header
4
+ dir_config(extension_name)
5
+ create_makefile(extension_name)
@@ -0,0 +1,8 @@
1
+ require_relative 'evt/version'
2
+ require 'evt_ext'
3
+ require_relative 'evt/scheduler'
4
+
5
+ module Evt
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,170 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fiber'
4
+ require 'socket'
5
+
6
+ begin
7
+ require 'io/nonblock'
8
+ rescue LoadError
9
+ # Ignore.
10
+ end
11
+
12
+ class IO
13
+ WAIT_READABLE = 1
14
+ WAIT_WRITABLE = 3
15
+ end
16
+
17
+ class Scheduler
18
+ def initialize
19
+ @readable = {}
20
+ @writable = {}
21
+ @waiting = {}
22
+ @blocking = []
23
+
24
+ @ios = ObjectSpace::WeakMap.new
25
+ init_selector
26
+ end
27
+
28
+ attr :readable
29
+ attr :writable
30
+ attr :waiting
31
+ attr :blocking
32
+
33
+ def next_timeout
34
+ _fiber, timeout = @waiting.min_by{|key, value| value}
35
+
36
+ if timeout
37
+ offset = timeout - current_time
38
+
39
+ if offset < 0
40
+ return 0
41
+ else
42
+ return offset
43
+ end
44
+ end
45
+ end
46
+
47
+ def run
48
+ while @readable.any? or @writable.any? or @waiting.any?
49
+ # Can only handle file descriptors up to 1024...
50
+ readable, writable = self.wait
51
+
52
+ # puts "readable: #{readable}" if readable&.any?
53
+ # puts "writable: #{writable}" if writable&.any?
54
+
55
+ readable&.each do |io|
56
+ @readable[io]&.resume
57
+ end
58
+
59
+ writable&.each do |io|
60
+ @writable[io]&.resume
61
+ end
62
+
63
+ if @waiting.any?
64
+ time = current_time
65
+ waiting = @waiting
66
+ @waiting = {}
67
+
68
+ waiting.each do |fiber, timeout|
69
+ if timeout <= time
70
+ fiber.resume
71
+ else
72
+ @waiting[fiber] = timeout
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ def for_fd(fd)
80
+ @ios[fd] ||= ::IO.for_fd(fd, autoclose: false)
81
+ end
82
+
83
+ def wait_readable(io)
84
+ @readable[io] = Fiber.current
85
+ self.register(io, IO::WAIT_READABLE)
86
+ Fiber.yield
87
+ @readable.delete(io)
88
+ self.deregister(io)
89
+ return true
90
+ end
91
+
92
+ def wait_readable_fd(fd)
93
+ wait_readable(
94
+ for_fd(fd)
95
+ )
96
+ end
97
+
98
+ def wait_writable(io)
99
+ @writable[io] = Fiber.current
100
+ self.register(io, IO::WAIT_READABLE)
101
+ Fiber.yield
102
+ @writable.delete(io)
103
+ self.deregister(io)
104
+ return true
105
+ end
106
+
107
+ def wait_writable_fd(fd)
108
+ wait_writable(
109
+ for_fd(fd)
110
+ )
111
+ end
112
+
113
+ def current_time
114
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
115
+ end
116
+
117
+ def wait_sleep(duration = nil)
118
+ @waiting[Fiber.current] = current_time + duration
119
+
120
+ Fiber.yield
121
+
122
+ return true
123
+ end
124
+
125
+ def wait_any(io, events, duration)
126
+ unless (events & IO::WAIT_READABLE).zero?
127
+ @readable[io] = Fiber.current
128
+ end
129
+
130
+ unless (events & IO::WAIT_WRITABLE).zero?
131
+ @writable[io] = Fiber.current
132
+ end
133
+
134
+ self.register(io, events)
135
+
136
+ Fiber.yield
137
+
138
+ @readable.delete(io)
139
+ @writable.delete(io)
140
+ self.deregister(io)
141
+
142
+ return true
143
+ end
144
+
145
+
146
+ def wait_for_single_fd(fd, events, duration)
147
+ wait_any(
148
+ for_fd(fd),
149
+ events,
150
+ duration
151
+ )
152
+ end
153
+
154
+ def enter_blocking_region
155
+ # puts "Enter blocking region: #{caller.first}"
156
+ end
157
+
158
+ def exit_blocking_region
159
+ # puts "Exit blocking region: #{caller.first}"
160
+ @blocking << caller.first
161
+ end
162
+
163
+ def fiber(&block)
164
+ fiber = Fiber.new(blocking: false, &block)
165
+
166
+ fiber.resume
167
+
168
+ return fiber
169
+ end
170
+ end
@@ -0,0 +1,3 @@
1
+ module Evt
2
+ VERSION = "0.1.1"
3
+ end
metadata CHANGED
@@ -1,27 +1,54 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Delton Ding
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2020-08-17 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake-compiler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
13
27
  description: A low-level Event Handler designed for Ruby 3 Scheduler for better performance
14
28
  email:
15
29
  - dsh0416@gmail.com
16
30
  executables: []
17
- extensions: []
31
+ extensions:
32
+ - ext/evt/extconf.rb
18
33
  extra_rdoc_files: []
19
34
  files:
20
35
  - ".gitignore"
36
+ - ".travis.yml"
37
+ - CODE_OF_CONDUCT.md
38
+ - Gemfile
39
+ - Gemfile.lock
21
40
  - LICENSE
22
41
  - README.md
42
+ - Rakefile
43
+ - evt.gemspec
44
+ - ext/evt/evt.c
45
+ - ext/evt/extconf.rb
46
+ - lib/evt.rb
47
+ - lib/evt/scheduler.rb
48
+ - lib/evt/version.rb
23
49
  homepage: https://github.com/dsh0416/evt
24
- licenses: []
50
+ licenses:
51
+ - BSD-3-Clause
25
52
  metadata:
26
53
  homepage_uri: https://github.com/dsh0416/evt
27
54
  source_code_uri: https://github.com/dsh0416/evt
@@ -33,7 +60,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
33
60
  requirements:
34
61
  - - ">="
35
62
  - !ruby/object:Gem::Version
36
- version: 2.3.0
63
+ version: 2.7.1
37
64
  required_rubygems_version: !ruby/object:Gem::Requirement
38
65
  requirements:
39
66
  - - ">="