schoefmax-tlattr_accessors 0.0.2 → 0.0.3
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.
- data/lib/tlattr_accessors.rb +4 -4
- data/spec/thread_local_accessors_spec.rb +10 -1
- data/tlattr_accessors.gemspec +1 -1
- metadata +1 -1
data/lib/tlattr_accessors.rb
CHANGED
|
@@ -33,11 +33,11 @@ module ThreadLocalAccessors
|
|
|
33
33
|
|
|
34
34
|
def #{name}=(val)
|
|
35
35
|
#{ivar} = Hash.new #{'{|h, k| h[k] = val}' if first_is_default} unless #{ivar}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
ObjectSpace.define_finalizer(Thread.current, lambda { #{ivar}.delete(
|
|
36
|
+
thread_id = Thread.current.object_id
|
|
37
|
+
unless #{ivar}.has_key?(thread_id)
|
|
38
|
+
ObjectSpace.define_finalizer(Thread.current, lambda { #{ivar}.delete(thread_id) })
|
|
39
39
|
end
|
|
40
|
-
#{ivar}[
|
|
40
|
+
#{ivar}[thread_id] = val
|
|
41
41
|
end
|
|
42
42
|
}, __FILE__, __LINE__
|
|
43
43
|
end
|
|
@@ -60,7 +60,6 @@ describe ThreadLocalAccessors do
|
|
|
60
60
|
x.bar.should == 2
|
|
61
61
|
end
|
|
62
62
|
|
|
63
|
-
# This will epically FAIL under JRuby, as JRuby doesn't support finalizers
|
|
64
63
|
it 'should not leak memory' do
|
|
65
64
|
x = Foo.new
|
|
66
65
|
n = 6000
|
|
@@ -74,5 +73,15 @@ describe ThreadLocalAccessors do
|
|
|
74
73
|
hash.size.should < (n / 2) # it should be a lot lower than n!
|
|
75
74
|
end
|
|
76
75
|
|
|
76
|
+
it 'should define only one finalizer per thread' do
|
|
77
|
+
ObjectSpace.should_receive(:define_finalizer).exactly(:twice)
|
|
78
|
+
x = Foo.new
|
|
79
|
+
2.times do
|
|
80
|
+
Thread.new do
|
|
81
|
+
10.times { x.foo = "bar" }
|
|
82
|
+
end.join
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
77
86
|
end
|
|
78
87
|
|
data/tlattr_accessors.gemspec
CHANGED