lightio 0.3.1 → 0.3.2

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
  SHA1:
3
- metadata.gz: 2adcb4321383299d719e6faa66696d14554e0d9c
4
- data.tar.gz: 34ca8bb7b88f2f0753264016c21f7840dd48475d
3
+ metadata.gz: e60501b7bca5cc445161d405a11e4a7d688de116
4
+ data.tar.gz: 6068963377b9805a4cc93090d6d14eaba2df280a
5
5
  SHA512:
6
- metadata.gz: 3ba852354c4797ec86547105b59e8364b6e51cacee2bf72bff882ec0e9a8f8a02c967e3a505766f2c6912dd50c628dbc1d339e1352177f532df86a1969e64187
7
- data.tar.gz: 85b37be690bf476c772ca64082264447a3d07ea9fa2593b313ddca7c37174b618ea118b6e5f3ea99fe68f8edde6827d11ab7e9f84ef01f140e2b873417551711
6
+ metadata.gz: c9e29908ede0af65b6b8a6d86d7ed88f85de30e6275c7a34917ef3ed3e444ac6df2567de2392b9859daabae6158ead27dc2be4b13669adb0070aa895cae3d8ee
7
+ data.tar.gz: 391014cc0762ba84b948e9a18efe76377a61dcc6135926eec7c543a93b0921d9e707b52a1b260173a4286a90822716e264c4973c5238f85c2b4da24b0a875235
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lightio (0.3.1)
4
+ lightio (0.3.2)
5
5
  nio4r (~> 2.2)
6
6
 
7
7
  GEM
@@ -53,6 +53,8 @@ module LightIO::Core
53
53
  extend Forwardable
54
54
  def_delegators :@selector, :closed?, :empty?, :backend, :wakeup
55
55
 
56
+ attr_reader :running
57
+
56
58
  def initialize
57
59
  # @selector = NIO::Selector.new
58
60
  @current_loop_time = nil
@@ -49,7 +49,7 @@ module LightIO::Core
49
49
  super() {
50
50
  begin
51
51
  @value = yield(*args)
52
- rescue StandardError => e
52
+ rescue Exception => e
53
53
  @error = e
54
54
  end
55
55
  # mark as dead
@@ -136,8 +136,11 @@ module LightIO::Core
136
136
  #
137
137
  # @return [nil]
138
138
  def pass
139
+ running = IOloop.current.running
139
140
  schedule = LightIO::Watchers::Schedule.new
140
141
  IOloop.current.wait(schedule)
142
+ # make sure ioloop run once
143
+ pass unless running
141
144
  end
142
145
  end
143
146
 
@@ -15,7 +15,8 @@ module LightIO::Core
15
15
  end
16
16
 
17
17
  extend Forwardable
18
- def_delegators :@backend, :run, :add_timer, :add_callback, :add_io_wait, :cancel_io_wait, :backend, :close, :closed?, :stop
18
+ def_delegators :@backend, :run, :add_timer, :add_callback, :add_io_wait, :cancel_io_wait, :backend,
19
+ :close, :closed?, :stop, :running
19
20
 
20
21
  # Wait a watcher, watcher can be a timer or socket.
21
22
  # see LightIO::Watchers module for detail
@@ -118,16 +118,15 @@ module LightIO::Library
118
118
  @io_watcher.wait_readable
119
119
  end
120
120
 
121
- def io_watcher
122
- @io_watcher
123
- end
124
-
125
121
  class << self
126
122
  def open(*args)
127
123
  io = self.new(*args)
128
- yield io
129
- ensure
130
- io.close if io.respond_to? :close
124
+ return io unless block_given?
125
+ begin
126
+ yield io
127
+ ensure
128
+ io.close if io.respond_to? :close
129
+ end
131
130
  end
132
131
 
133
132
  def pipe(*args)
@@ -148,13 +147,19 @@ module LightIO::Library
148
147
  read_fds ||= []
149
148
  write_fds ||= []
150
149
  loop do
151
- # clear io watcher status
152
- read_fds.each {|fd| fd.send(:io_watcher).clear_status}
153
- write_fds.each {|fd| fd.send(:io_watcher).clear_status}
150
+ # make sure io registered, then clear io watcher status
151
+ read_fds.each {|fd| get_io_watcher(fd).tap {|io| io.readable?; io.clear_status}}
152
+ write_fds.each {|fd| get_io_watcher(fd).tap {|io| io.writable?; io.clear_status}}
154
153
  # run ioloop once
155
154
  LightIO.sleep 0
156
- r_fds = read_fds.select {|fd| fd.closed? ? raise(IOError, 'closed stream') : fd.send(:io_watcher).readable?}
157
- w_fds = write_fds.select {|fd| fd.closed? ? raise(IOError, 'closed stream') : fd.send(:io_watcher).writable?}
155
+ r_fds = read_fds.select {|fd|
156
+ io = convert_to_io(fd)
157
+ io.closed? ? raise(IOError, 'closed stream') : get_io_watcher(io).readable?
158
+ }
159
+ w_fds = write_fds.select {|fd|
160
+ io = convert_to_io(fd)
161
+ io.closed? ? raise(IOError, 'closed stream') : get_io_watcher(io).writable?
162
+ }
158
163
  e_fds = []
159
164
  if r_fds.empty? && w_fds.empty?
160
165
  if timeout && Time.now - timer > timeout
@@ -165,6 +170,25 @@ module LightIO::Library
165
170
  end
166
171
  end
167
172
  end
173
+
174
+ private
175
+ def convert_to_io(io)
176
+ unless io.respond_to?(:to_io)
177
+ raise TypeError, "no implicit conversion of #{io.class} into IO"
178
+ end
179
+ to_io = io.to_io
180
+ unless to_io.is_a?(IO)
181
+ raise TypeError, "can't convert #{io.class} to IO (#{io.class}#to_io gives #{to_io.class})"
182
+ end
183
+ to_io
184
+ end
185
+
186
+ def get_io_watcher(io)
187
+ unless io.is_a?(IO)
188
+ io = convert_to_io(io)
189
+ end
190
+ io.instance_variable_get(:@io_watcher)
191
+ end
168
192
  end
169
193
  end
170
194
  end
@@ -4,7 +4,7 @@ module LightIO::Library
4
4
  class Thread
5
5
  class Mutex
6
6
  def initialize
7
- @queue = Queue.new
7
+ @queue = LightIO::Library::Queue.new
8
8
  @queue << true
9
9
  @locked_thread = nil
10
10
  end
@@ -56,7 +56,38 @@ module LightIO::Library
56
56
  end
57
57
  end
58
58
  end
59
+
60
+ class ConditionVariable
61
+ def initialize
62
+ @queue = LightIO::Library::Queue.new
63
+ end
64
+
65
+
66
+ def broadcast
67
+ signal until @queue.num_waiting == 0
68
+ self
69
+ end
70
+
71
+ def signal
72
+ @queue << true unless @queue.num_waiting == 0
73
+ self
74
+ end
75
+
76
+ def wait(mutex, timeout=nil)
77
+ mutex.unlock
78
+ begin
79
+ LightIO::Library::Timeout.timeout(timeout) do
80
+ @queue.pop
81
+ end
82
+ rescue Timeout::Error
83
+ nil
84
+ end
85
+ mutex.lock
86
+ self
87
+ end
88
+ end
59
89
  end
60
90
 
61
91
  Mutex = Thread::Mutex
92
+ ConditionVariable = Thread::ConditionVariable
62
93
  end
@@ -96,6 +96,11 @@ module LightIO::Library
96
96
  [self.class._wrap(socket), Addrinfo._wrap(addrinfo)]
97
97
  end
98
98
 
99
+ def accept_nonblock(*args)
100
+ socket, addrinfo = @io.accept_nonblock(*args)
101
+ [self.class._wrap(socket), Addrinfo._wrap(addrinfo)]
102
+ end
103
+
99
104
  def sys_accept
100
105
  @io_watcher.wait_readable
101
106
  @io.sys_accept
@@ -181,6 +186,11 @@ module LightIO::Library
181
186
  TCPSocket._wrap(socket)
182
187
  end
183
188
 
189
+ def accept_nonblock(*args)
190
+ socket = @io.accept_nonblock(*args)
191
+ TCPSocket._wrap(socket)
192
+ end
193
+
184
194
  def sys_accept
185
195
  @io_watcher.wait_readable
186
196
  @io.sys_accept
@@ -51,13 +51,6 @@ module LightIO::Library
51
51
  obj.public_send method, *args
52
52
  end
53
53
  end
54
-
55
- def fallback_thread_class_methods(*methods)
56
- methods.each {|m| fallback_method(RAW_THREAD.main, m, "This method is fallback to native Thread class,"\
57
- " it may cause unexpected behaviour,"\
58
- " open issues on https://github.com/socketry/lightio/issues"\
59
- " if this behaviour not approach you purpose")}
60
- end
61
54
  end
62
55
 
63
56
  include ClassMethods
@@ -76,10 +69,15 @@ module LightIO::Library
76
69
 
77
70
  class << self
78
71
  extend Forwardable
79
- def_delegators :'LightIO::Library::Thread::RAW_THREAD', :DEBUG, :DEBUG=
72
+ def_delegators :'LightIO::Library::Thread::RAW_THREAD',
73
+ :DEBUG,
74
+ :DEBUG=,
75
+ :handle_interrupt,
76
+ :abort_on_exception,
77
+ :abort_on_exception=,
78
+ :pending_interrupt?
80
79
 
81
80
  include FallbackHelper
82
- fallback_thread_class_methods :abort_on_exception, :abort_on_exception=
83
81
 
84
82
  def fork(*args, &blk)
85
83
  obj = allocate
@@ -155,8 +153,7 @@ module LightIO::Library
155
153
 
156
154
  fallback_main_thread_methods :abort_on_exception,
157
155
  :abort_on_exception=,
158
- :handle_interrupt,
159
- :pending_interrupt,
156
+ :pending_interrupt?,
160
157
  :add_trace_func,
161
158
  :backtrace,
162
159
  :backtrace_locations,
@@ -1,3 +1,3 @@
1
1
  module LightIO
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lightio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jiang Jinyang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-06 00:00:00.000000000 Z
11
+ date: 2018-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nio4r