event 0.9.1 → 0.10.0

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: df986c171ee38a8b081284b28cb7972f3b7ba96b60d47d370ccac6808fe01ff9
4
- data.tar.gz: fd87f66165976f4704e9244f044aedda6a949c87f2879ede07cfc728a24f778e
3
+ metadata.gz: a4afaae3a40f7c5f5531db11369710205aa5e8f7410edbc8d7311114e733ec91
4
+ data.tar.gz: c8bee5169e94359781b5b3b1eb79f1d535ca992843791709b70794415d144e99
5
5
  SHA512:
6
- metadata.gz: e522241d001f0d42b364ab51ff0e555c4cbde87daf91f3bedcd93bb8c14ee8a351b413b6156d7f9e94606c3a7536de39fcc71ef2525d85c420c6def51a092cc5
7
- data.tar.gz: 297fe75b5789cb10dc32807a2a78bf36514773c1d41646cf1163c6fd7be7ecbedd1239bed631ff825836934dab9fc08eccfb27a2d15d39abb08159b05d01b95c
6
+ metadata.gz: cbfe1d0c91aa4ac371f61af680837db84e788dd8ecb8a604f8a76b3ede3a634afa7206f8855f04fa80f2ad93085976bc37b751fd0d48b57a49ffa843b8694be3
7
+ data.tar.gz: 3a37df8c300c32d5c8032d9249a5f9dc303267cf894973fdd177eb6f113a84ad87cf7b65806d06574ba757a3100effd82ab1386fece11219e1e854eba5f11f58
@@ -117,9 +117,7 @@ VALUE Event_Selector_EPoll_transfer(VALUE self)
117
117
  struct Event_Selector_EPoll *data = NULL;
118
118
  TypedData_Get_Struct(self, struct Event_Selector_EPoll, &Event_Selector_EPoll_Type, data);
119
119
 
120
- Event_Selector_fiber_transfer(data->backend.loop, 0, NULL);
121
-
122
- return Qnil;
120
+ return Event_Selector_fiber_transfer(data->backend.loop, 0, NULL);
123
121
  }
124
122
 
125
123
  VALUE Event_Selector_EPoll_resume(int argc, VALUE *argv, VALUE self)
@@ -127,9 +125,7 @@ VALUE Event_Selector_EPoll_resume(int argc, VALUE *argv, VALUE self)
127
125
  struct Event_Selector_EPoll *data = NULL;
128
126
  TypedData_Get_Struct(self, struct Event_Selector_EPoll, &Event_Selector_EPoll_Type, data);
129
127
 
130
- Event_Selector_resume(&data->backend, argc, argv);
131
-
132
- return Qnil;
128
+ return Event_Selector_resume(&data->backend, argc, argv);
133
129
  }
134
130
 
135
131
  VALUE Event_Selector_EPoll_yield(VALUE self)
@@ -137,9 +133,7 @@ VALUE Event_Selector_EPoll_yield(VALUE self)
137
133
  struct Event_Selector_EPoll *data = NULL;
138
134
  TypedData_Get_Struct(self, struct Event_Selector_EPoll, &Event_Selector_EPoll_Type, data);
139
135
 
140
- Event_Selector_yield(&data->backend);
141
-
142
- return Qnil;
136
+ return Event_Selector_yield(&data->backend);
143
137
  }
144
138
 
145
139
  VALUE Event_Selector_EPoll_push(VALUE self, VALUE fiber)
@@ -157,7 +151,7 @@ VALUE Event_Selector_EPoll_raise(int argc, VALUE *argv, VALUE self)
157
151
  struct Event_Selector_EPoll *data = NULL;
158
152
  TypedData_Get_Struct(self, struct Event_Selector_EPoll, &Event_Selector_EPoll_Type, data);
159
153
 
160
- return Event_Selector_wait_and_raise(&data->backend, argc, argv);
154
+ return Event_Selector_raise(&data->backend, argc, argv);
161
155
  }
162
156
 
163
157
  VALUE Event_Selector_EPoll_ready_p(VALUE self) {
@@ -117,9 +117,7 @@ VALUE Event_Selector_KQueue_transfer(VALUE self)
117
117
  struct Event_Selector_KQueue *data = NULL;
118
118
  TypedData_Get_Struct(self, struct Event_Selector_KQueue, &Event_Selector_KQueue_Type, data);
119
119
 
120
- Event_Selector_fiber_transfer(data->backend.loop, 0, NULL);
121
-
122
- return Qnil;
120
+ return Event_Selector_fiber_transfer(data->backend.loop, 0, NULL);
123
121
  }
124
122
 
125
123
  VALUE Event_Selector_KQueue_resume(int argc, VALUE *argv, VALUE self)
@@ -135,9 +133,7 @@ VALUE Event_Selector_KQueue_yield(VALUE self)
135
133
  struct Event_Selector_KQueue *data = NULL;
136
134
  TypedData_Get_Struct(self, struct Event_Selector_KQueue, &Event_Selector_KQueue_Type, data);
137
135
 
138
- Event_Selector_yield(&data->backend);
139
-
140
- return Qnil;
136
+ return Event_Selector_yield(&data->backend);
141
137
  }
142
138
 
143
139
  VALUE Event_Selector_KQueue_push(VALUE self, VALUE fiber)
@@ -155,7 +151,7 @@ VALUE Event_Selector_KQueue_raise(int argc, VALUE *argv, VALUE self)
155
151
  struct Event_Selector_KQueue *data = NULL;
156
152
  TypedData_Get_Struct(self, struct Event_Selector_KQueue, &Event_Selector_KQueue_Type, data);
157
153
 
158
- return Event_Selector_wait_and_raise(&data->backend, argc, argv);
154
+ return Event_Selector_raise(&data->backend, argc, argv);
159
155
  }
160
156
 
161
157
  VALUE Event_Selector_KQueue_ready_p(VALUE self) {
@@ -26,14 +26,17 @@ static ID id_transfer, id_alive_p;
26
26
  VALUE Event_Selector_fiber_transfer(VALUE fiber, int argc, VALUE *argv) {
27
27
  // TODO Consider introducing something like `rb_fiber_scheduler_transfer(...)`.
28
28
  #ifdef HAVE__RB_FIBER_TRANSFER
29
- if (RTEST(rb_fiber_alive_p(fiber))) {
30
- return rb_fiber_transfer(fiber, argc, argv);
29
+ if (RTEST(rb_obj_is_fiber(fiber))) {
30
+ if (RTEST(rb_fiber_alive_p(fiber))) {
31
+ return rb_fiber_transfer(fiber, argc, argv);
32
+ }
33
+
34
+ return Qnil;
31
35
  }
32
- #else
36
+ #endif
33
37
  if (RTEST(rb_funcall(fiber, id_alive_p, 0))) {
34
38
  return rb_funcallv(fiber, id_transfer, argc, argv);
35
39
  }
36
- #endif
37
40
 
38
41
  return Qnil;
39
42
  }
@@ -197,7 +200,7 @@ static VALUE wait_and_raise(VALUE _arguments) {
197
200
  return Event_Selector_fiber_raise(fiber, argc, argv);
198
201
  }
199
202
 
200
- VALUE Event_Selector_wait_and_raise(struct Event_Selector *backend, int argc, VALUE *argv)
203
+ VALUE Event_Selector_raise(struct Event_Selector *backend, int argc, VALUE *argv)
201
204
  {
202
205
  rb_check_arity(argc, 2, UNLIMITED_ARGUMENTS);
203
206
 
@@ -105,7 +105,7 @@ void Event_Selector_mark(struct Event_Selector *backend) {
105
105
  }
106
106
 
107
107
  VALUE Event_Selector_resume(struct Event_Selector *backend, int argc, VALUE *argv);
108
- VALUE Event_Selector_wait_and_raise(struct Event_Selector *backend, int argc, VALUE *argv);
108
+ VALUE Event_Selector_raise(struct Event_Selector *backend, int argc, VALUE *argv);
109
109
 
110
110
  static inline
111
111
  VALUE Event_Selector_yield(struct Event_Selector *backend)
@@ -120,9 +120,7 @@ VALUE Event_Selector_URing_transfer(VALUE self)
120
120
  struct Event_Selector_URing *data = NULL;
121
121
  TypedData_Get_Struct(self, struct Event_Selector_URing, &Event_Selector_URing_Type, data);
122
122
 
123
- Event_Selector_fiber_transfer(data->backend.loop, 0, NULL);
124
-
125
- return Qnil;
123
+ return Event_Selector_fiber_transfer(data->backend.loop, 0, NULL);
126
124
  }
127
125
 
128
126
  VALUE Event_Selector_URing_resume(int argc, VALUE *argv, VALUE self)
@@ -130,9 +128,7 @@ VALUE Event_Selector_URing_resume(int argc, VALUE *argv, VALUE self)
130
128
  struct Event_Selector_URing *data = NULL;
131
129
  TypedData_Get_Struct(self, struct Event_Selector_URing, &Event_Selector_URing_Type, data);
132
130
 
133
- Event_Selector_resume(&data->backend, argc, argv);
134
-
135
- return Qnil;
131
+ return Event_Selector_resume(&data->backend, argc, argv);
136
132
  }
137
133
 
138
134
  VALUE Event_Selector_URing_yield(VALUE self)
@@ -140,9 +136,7 @@ VALUE Event_Selector_URing_yield(VALUE self)
140
136
  struct Event_Selector_URing *data = NULL;
141
137
  TypedData_Get_Struct(self, struct Event_Selector_URing, &Event_Selector_URing_Type, data);
142
138
 
143
- Event_Selector_yield(&data->backend);
144
-
145
- return Qnil;
139
+ return Event_Selector_yield(&data->backend);
146
140
  }
147
141
 
148
142
  VALUE Event_Selector_URing_push(VALUE self, VALUE fiber)
@@ -160,7 +154,7 @@ VALUE Event_Selector_URing_raise(int argc, VALUE *argv, VALUE self)
160
154
  struct Event_Selector_URing *data = NULL;
161
155
  TypedData_Get_Struct(self, struct Event_Selector_URing, &Event_Selector_URing_Type, data);
162
156
 
163
- return Event_Selector_wait_and_raise(&data->backend, argc, argv);
157
+ return Event_Selector_raise(&data->backend, argc, argv);
164
158
  }
165
159
 
166
160
  VALUE Event_Selector_URing_ready_p(VALUE self) {
data/lib/event.rb CHANGED
@@ -21,14 +21,6 @@
21
21
  require_relative 'event/version'
22
22
  require_relative 'event/selector'
23
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
24
  begin
33
25
  require_relative '../ext/event/event'
34
26
  rescue LoadError
@@ -0,0 +1,140 @@
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 transfer(fiber, *arguments)
44
+ @selector.transfer(fiber, *arguments)
45
+ end
46
+
47
+ def transfer(*arguments)
48
+ @selector.transfer(*arguments)
49
+ end
50
+
51
+ def resume(*arguments)
52
+ @selector.resume(*arguments)
53
+ end
54
+
55
+ def yield
56
+ @selector.yield
57
+ end
58
+
59
+ def push(fiber)
60
+ @selector.push(fiber)
61
+ end
62
+
63
+ def raise(fiber, *arguments)
64
+ @selector.raise(fiber, *arguments)
65
+ end
66
+
67
+ def ready?
68
+ @selector.ready?
69
+ end
70
+
71
+ def process_wait(*arguments)
72
+ @selector.process_wait(*arguments)
73
+ end
74
+
75
+ def io_wait(fiber, io, events)
76
+ register_readable(fiber, io, events)
77
+ end
78
+
79
+ def select(duration = nil)
80
+ @selector.select(duration)
81
+ end
82
+
83
+ private
84
+
85
+ def register_readable(fiber, io, events)
86
+ if (events & READABLE) > 0
87
+ if @readable.key?(io)
88
+ raise "Cannot wait for #{io} to become readable from multiple fibers."
89
+ end
90
+
91
+ begin
92
+ @readable[io] = fiber
93
+
94
+ register_writable(fiber, io, events)
95
+ ensure
96
+ @readable.delete(io)
97
+ end
98
+ else
99
+ register_writable(fiber, io, events)
100
+ end
101
+ end
102
+
103
+ def register_writable(fiber, io, events)
104
+ if (events & WRITABLE) > 0
105
+ if @writable.key?(io)
106
+ raise "Cannot wait for #{io} to become writable from multiple fibers."
107
+ end
108
+
109
+ begin
110
+ @writable[io] = fiber
111
+
112
+ register_priority(fiber, io, events)
113
+ ensure
114
+ @writable.delete(io)
115
+ end
116
+ else
117
+ register_priority(fiber, io, events)
118
+ end
119
+ end
120
+
121
+ def register_priority(fiber, io, events)
122
+ if (events & PRIORITY) > 0
123
+ if @priority.key?(io)
124
+ raise "Cannot wait for #{io} to become priority from multiple fibers."
125
+ end
126
+
127
+ begin
128
+ @priority[io] = fiber
129
+
130
+ @selector.io_wait(fiber, io, events)
131
+ ensure
132
+ @priority.delete(io)
133
+ end
134
+ else
135
+ @selector.io_wait(fiber, io, events)
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,55 @@
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 'selector'
22
+
23
+ module Event
24
+ # A thread safe synchronisation primative.
25
+ class Interrupt
26
+ def self.call(selector, &block)
27
+ self.new(selector, block)
28
+ end
29
+
30
+ def initialize(selector, block)
31
+ @selector = selector
32
+ @input, @output = ::IO.pipe
33
+
34
+ @fiber = Fiber.new do
35
+ while true
36
+ @selector.io_wait(@fiber, @input, READABLE)
37
+ block.call(@input.read_nonblock(1)&.ord)
38
+ end
39
+ end
40
+
41
+ @fiber.transfer
42
+ end
43
+
44
+ # Send a sigle byte interrupt.
45
+ def signal(event = 0)
46
+ @output.write(event.chr)
47
+ @output.flush
48
+ end
49
+
50
+ def close
51
+ @input.close
52
+ @output.close
53
+ end
54
+ end
55
+ end
@@ -21,6 +21,11 @@
21
21
  require_relative 'selector/select'
22
22
 
23
23
  module Event
24
+ # These constants are the same as those defined in IO.
25
+ READABLE = 1
26
+ PRIORITY = 2
27
+ WRITABLE = 4
28
+
24
29
  module Selector
25
30
  def self.default(env = ENV)
26
31
  if name = env['EVENT_SELECTOR']&.to_sym
data/lib/event/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Event
2
- VERSION = "0.9.1"
2
+ VERSION = "0.10.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.10.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-07-15 00:00:00.000000000 Z
11
+ date: 2021-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bake
@@ -86,7 +86,8 @@ files:
86
86
  - ext/event/selector/uring.c
87
87
  - ext/event/selector/uring.h
88
88
  - lib/event.rb
89
- - lib/event/debug.rb
89
+ - lib/event/debug/selector.rb
90
+ - lib/event/interrupt.rb
90
91
  - lib/event/selector.rb
91
92
  - lib/event/selector/select.rb
92
93
  - lib/event/version.rb
data/lib/event/debug.rb DELETED
@@ -1,126 +0,0 @@
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
- class Debug
25
- def initialize(selector)
26
- @selector = selector
27
-
28
- @readable = {}
29
- @writable = {}
30
- @priority = {}
31
- end
32
-
33
- def close
34
- if @selector.nil?
35
- raise "Selector already closed!"
36
- end
37
-
38
- @selector.close
39
- @selector = nil
40
- end
41
-
42
- def transfer(fiber, *arguments)
43
- @selector.transfer(fiber, *arguments)
44
- end
45
-
46
- def yield
47
- @selector.yield
48
- end
49
-
50
- def push(fiber)
51
- @selector.push(fiber)
52
- end
53
-
54
- def raise(fiber, *arguments)
55
- @selector.raise(fiber, *arguments)
56
- end
57
-
58
- def ready?
59
- @selector.ready?
60
- end
61
-
62
- def io_wait(fiber, io, events)
63
- register_readable(fiber, io, events)
64
- end
65
-
66
- def select(duration = nil)
67
- @selector.select(duration)
68
- end
69
-
70
- private
71
-
72
- def register_readable(fiber, io, events)
73
- if (events & READABLE) > 0
74
- if @readable.key?(io)
75
- raise "Cannot wait for #{io} to become readable from multiple fibers."
76
- end
77
-
78
- begin
79
- @readable[io] = fiber
80
-
81
- register_writable(fiber, io, events)
82
- ensure
83
- @readable.delete(io)
84
- end
85
- else
86
- register_writable(fiber, io, events)
87
- end
88
- end
89
-
90
- def register_writable(fiber, io, events)
91
- if (events & WRITABLE) > 0
92
- if @writable.key?(io)
93
- raise "Cannot wait for #{io} to become writable from multiple fibers."
94
- end
95
-
96
- begin
97
- @writable[io] = fiber
98
-
99
- register_priority(fiber, io, events)
100
- ensure
101
- @writable.delete(io)
102
- end
103
- else
104
- register_priority(fiber, io, events)
105
- end
106
- end
107
-
108
- def register_priority(fiber, io, events)
109
- if (events & PRIORITY) > 0
110
- if @priority.key?(io)
111
- raise "Cannot wait for #{io} to become priority from multiple fibers."
112
- end
113
-
114
- begin
115
- @priority[io] = fiber
116
-
117
- @selector.io_wait(fiber, io, events)
118
- ensure
119
- @priority.delete(io)
120
- end
121
- else
122
- @selector.io_wait(fiber, io, events)
123
- end
124
- end
125
- end
126
- end