Skip to content

Commit babc209

Browse files
authored
Merge pull request #79 from take-cheeze/fix_leaks
Updates!
2 parents c4f6e76 + 2e39460 commit babc209

File tree

19 files changed

+1738
-1459
lines changed

19 files changed

+1738
-1459
lines changed

.travis.yml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
sudo: false
2-
addons:
3-
apt:
4-
packages:
5-
- gperf
1+
sudo: required
62
compiler:
73
- gcc
84
- clang
@@ -11,6 +7,13 @@ env:
117
- MRUBY_VERSION=master
128
- MRUBY_VERSION=1.2.0
139
- MRUBY_VERSION=1.3.0
10+
- MRUBY_VERSION=1.4.0
11+
before_script:
12+
# Add an IPv6 config - see the corresponding Travis issue
13+
# https://github.com/travis-ci/travis-ci/issues/8361
14+
- if [ "${TRAVIS_OS_NAME}" == "linux" ]; then
15+
sudo sh -c 'echo 0 > /proc/sys/net/ipv6/conf/all/disable_ipv6';
16+
fi
1417
script:
1518
- export MRUBY_CONFIG="$TRAVIS_BUILD_DIR/.travis_config.rb"
1619
- 'if [ "$MRUBY_VERSION" = "master" ] ; then
@@ -20,4 +23,4 @@ script:
2023
tar xf mruby-$MRUBY_VERSION.tar.gz;
2124
fi'
2225
- cd mruby-$MRUBY_VERSION
23-
- ./minirake all test
26+
- ./minirake -v all test

example/pipe-client-win.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!mruby
22
begin; require 'mruby-uv'; rescue Exception; end
33

4-
c = UV::Pipe.new(0)
4+
c = UV::Pipe.new(false)
55
c.connect('\\\\.\\pipe\\mruby-uv') {|x|
66
if x == 0
77
c.read_start {|b|

example/pipe-client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!mruby
22
begin; require 'mruby-uv'; rescue Exception; end
33

4-
c = UV::Pipe.new(1)
4+
c = UV::Pipe.new(true)
55
c.connect('/tmp/mrub-yuv') {|x|
66
if x == 0
77
c.read_start {|b|

example/pipe-server-win.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!mruby
22
begin; require 'mruby-uv'; rescue Exception; end
33

4-
s = UV::Pipe.new(0)
4+
s = UV::Pipe.new(false)
55
s.bind('\\\\.\\pipe\\mruby-uv')
66
s.listen(1) {|x|
77
return if x != 0

example/pipe-server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!mruby
22
begin; require 'mruby-uv'; rescue Exception; end
33

4-
s = UV::Pipe.new(1)
4+
s = UV::Pipe.new(true)
55
s.bind('/tmp/mruby-uv')
66
s.listen(5) {|x|
77
return if x != 0

example/process.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
'args' => ['../..']
1313
})
1414
end
15-
ps.stdout_pipe = UV::Pipe.new(0)
15+
ps.stdout_pipe = UV::Pipe.new false
1616

1717
ps.spawn do |sig|
1818
puts "exit #{sig}"

include/mruby/uv.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ mrb_value mrb_uv_dlopen(mrb_state *mrb, char const *name);
1111
void* mrb_uv_dlsym(mrb_state *mrb, mrb_value dl, char const *name);
1212
void mrb_uv_dlclose(mrb_state *mrb, mrb_value dl);
1313

14+
typedef struct mrb_uv_req_t mrb_uv_req_t;
15+
1416
#define E_UV_ERROR mrb_class_get(mrb, "UVError")
1517
void mrb_uv_check_error(mrb_state*, int);
18+
void mrb_uv_req_check_error(mrb_state*, mrb_uv_req_t*, int);
1619

1720
char** mrb_uv_setup_args(mrb_state *mrb, int *argc, char **argv, mrb_bool set_global);
1821

mrbgem.rake

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ MRuby::Gem::Specification.new('mruby-uv') do |spec|
22
spec.license = 'MIT'
33
spec.authors = 'mattn'
44
spec.summary = 'libuv mruby binding'
5-
spec.add_dependency 'mruby-time', core: 'mruby-time'
6-
spec.add_dependency 'mruby-sprintf', core: 'mruby-sprintf'
5+
spec.add_dependency 'mruby-time', core: 'mruby-time'
6+
spec.add_dependency 'mruby-sprintf', core: 'mruby-sprintf'
7+
spec.add_dependency 'mruby-fiber', core: 'mruby-fiber'
8+
spec.add_dependency 'mruby-string-ext', core: 'mruby-string-ext'
79

810
def self.run_command(env, command)
911
fail "#{command} failed" unless system(env, command)
@@ -14,7 +16,7 @@ MRuby::Gem::Specification.new('mruby-uv') do |spec|
1416
def self.bundle_uv
1517
visualcpp = ENV['VisualStudioVersion'] || ENV['VSINSTALLDIR']
1618

17-
version = '1.14.1'
19+
version = '1.19.1'
1820
libuv_dir = "#{build_dir}/libuv-#{version}"
1921
libuv_lib = libfile "#{libuv_dir}/.libs/libuv"
2022
header = "#{libuv_dir}/include/uv.h"
@@ -46,7 +48,7 @@ MRuby::Gem::Specification.new('mruby-uv') do |spec|
4648
file libuv_lib => header do |t|
4749
Dir.chdir(libuv_dir) do
4850
e = {
49-
'CC' => "#{build.cc.command} #{build.cc.flags.join(' ')}",
51+
'CC' => "#{build.cc.command} #{build.cc.flags.join(' ')} -DNDEBUG",
5052
'CXX' => "#{build.cxx.command} #{build.cxx.flags.join(' ')}",
5153
'LD' => "#{build.linker.command} #{build.linker.flags.join(' ')}",
5254
'AR' => build.archiver.command

mrblib/thread.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ module UV
22
class Thread
33
def != other; !(self == other) end
44
end
5+
6+
class Loop
7+
def != other; !(self == other) end
8+
end
59
end

mrblib/yarn.rb

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
module UV
2+
def self.sleep sec
3+
current_yarn.sleep sec
4+
end
5+
6+
def self.quote cmd
7+
current_yarn.quote(cmd)
8+
end
9+
10+
def self.current_yarn
11+
ret = UV.current_loop.current_yarn
12+
raise "yarn not running" if ret.nil?
13+
ret
14+
end
15+
16+
class Loop
17+
attr_reader :current_yarn
18+
19+
def current_yarn=(y)
20+
raise "cannot double run yarn" if current_yarn
21+
raise "not yarn" unless y.kind_of? UV::Yarn
22+
23+
@current_yarn = y
24+
end
25+
26+
def clear_current_yarn(y)
27+
raise 'cannot clear yarn' if y != @current_yarn
28+
@current_yarn = nil
29+
end
30+
end
31+
32+
class Yarn
33+
def initialize(loop = UV.current_loop, &blk)
34+
@fiber = Fiber.new(&blk)
35+
@loop = loop
36+
end
37+
38+
attr_reader :loop, :error, :req
39+
40+
def ended?; @fiber.nil? end
41+
def result
42+
raise "not ended" unless ended?
43+
@result
44+
end
45+
46+
def check_error
47+
raise @error if @error
48+
end
49+
50+
def timer
51+
@timer ||= UV::Timer.new
52+
# raise 'timer already used' if @timer.active?
53+
@timer
54+
end
55+
56+
def to_proc
57+
@proc ||= Proc.new{|*args, &blk| self.resume(*args, &blk) }
58+
end
59+
60+
def start(*args)
61+
raise "already started" if @started
62+
@started = true
63+
64+
resume(*args)
65+
end
66+
67+
def sleep sec
68+
timer.start(sec * 1000.0, 0, &self.to_proc)
69+
Fiber.yield(timer, self)
70+
end
71+
72+
def quote cmd
73+
out = UV::Pipe.new false
74+
ps = Process.new file: :sh, args: ['-c', cmd], stdio: [nil, out, nil]
75+
y = self
76+
ps.spawn do |x, sig|
77+
str = ''
78+
out.read_start do |b|
79+
if b.kind_of? String
80+
str.concat b
81+
next
82+
end
83+
y.resume(str)
84+
out.close
85+
end
86+
end
87+
Fiber.yield ps, self
88+
end
89+
90+
def resume(*args)
91+
raise "cannot resume unstarted yarn" unless @started
92+
raise "cannot run ended yarn" if ended?
93+
94+
@loop.current_yarn = self
95+
96+
prev_loop = UV.current_loop
97+
@loop.make_current
98+
99+
*ret = @fiber.resume(*args)
100+
101+
if @fiber.alive?
102+
raise "invalid yield #{ret.inspect}" unless ALIVE_CLASSES.any?{|v| ret.first.kind_of? v }
103+
else
104+
@fiber = nil
105+
@result = ret
106+
end
107+
108+
rescue => e
109+
@error = e
110+
111+
ensure
112+
@loop.clear_current_yarn self
113+
prev_loop.make_current if prev_loop
114+
end
115+
116+
ALIVE_CLASSES = [UV::Req, UV::Timer, UV::Process]
117+
end
118+
end

0 commit comments

Comments
 (0)