thread_variables 0.1.0 → 0.2.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.
- data/.gitignore +1 -0
- data/README.md +19 -3
- data/lib/thread_variables/access.rb +31 -0
- data/lib/thread_variables/version.rb +1 -1
- data/test/thread_variables_test.rb +45 -2
- metadata +27 -38
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# ThreadVariables
|
2
2
|
|
3
3
|
This gem provides methods to access thread local variables with a ruby trunk compatible
|
4
|
-
API. It also provides an implementation for ruby 1.9, since ruby
|
5
|
-
fiber local.
|
4
|
+
API. It also provides an implementation of thread local variables for ruby 1.9, since ruby
|
5
|
+
1.9 made thread locals fiber local. This change in semantics can lead to surprising
|
6
|
+
results when accessing thread locals from enumerators.
|
6
7
|
|
7
8
|
See http://bugs.ruby-lang.org/issues/7097
|
8
9
|
|
9
|
-
|
10
10
|
## Installation
|
11
11
|
|
12
12
|
Add this line to your application's Gemfile:
|
@@ -23,12 +23,28 @@ Or install it yourself as:
|
|
23
23
|
|
24
24
|
## Usage
|
25
25
|
|
26
|
+
Basic interface: (this is the native code interface for ruby trunk)
|
27
|
+
|
26
28
|
require "thread_variables"
|
27
29
|
Thread.current.thread_variable_set :foo, 5
|
28
30
|
Thread.current.thread_variable_get :foo
|
29
31
|
Thread.current.thread_variable? :foo
|
30
32
|
Thread.current.thread_variables
|
31
33
|
|
34
|
+
Convenience interface: (proxied access to native interface)
|
35
|
+
|
36
|
+
require "thread_variables/access"
|
37
|
+
Thread.current.locals[:foo] = 5
|
38
|
+
Thread.current.locals[:foo]
|
39
|
+
Thread.current.locals.key?(:foo)
|
40
|
+
Thread.current.locals.keys
|
41
|
+
|
42
|
+
Especially useful for the ||= pattern:
|
43
|
+
|
44
|
+
Thread.current.locals[:counter] ||= 0
|
45
|
+
Thread.current.locals[:counter] += 1
|
46
|
+
|
47
|
+
|
32
48
|
## Contributing
|
33
49
|
|
34
50
|
1. Fork it
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "thread_variables"
|
2
|
+
|
3
|
+
module ThreadVariables
|
4
|
+
class AccessProxy < (RUBY_VERSION >= "1.9" ? BasicObject : Object)
|
5
|
+
def initialize(thread)
|
6
|
+
@thread = thread
|
7
|
+
end
|
8
|
+
|
9
|
+
def [](k)
|
10
|
+
@thread.thread_variable_get(k)
|
11
|
+
end
|
12
|
+
|
13
|
+
def []=(k,v)
|
14
|
+
@thread.thread_variable_set(k,v)
|
15
|
+
end
|
16
|
+
|
17
|
+
def key?(k)
|
18
|
+
@thread.thread_variable?(k)
|
19
|
+
end
|
20
|
+
|
21
|
+
def keys
|
22
|
+
@thread.thread_variables
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Thread
|
28
|
+
def locals
|
29
|
+
@thread_variables_access_proxy ||= ThreadVariables::AccessProxy.new(self)
|
30
|
+
end
|
31
|
+
end
|
@@ -1,7 +1,12 @@
|
|
1
|
-
require 'thread_variables'
|
2
1
|
require 'test/unit'
|
3
2
|
require File.expand_path('../colorized_test_output', __FILE__)
|
4
3
|
|
4
|
+
require 'thread'
|
5
|
+
if $NATIVE_THREAD_VARIABLES = Thread.instance_methods.include?(:thread_variables)
|
6
|
+
puts ANSI.ansi("\nYou have native thread variables. Hurray!\n", :green)
|
7
|
+
end
|
8
|
+
require 'thread_variables/access'
|
9
|
+
|
5
10
|
class ThreadVariablesTest < Test::Unit::TestCase
|
6
11
|
|
7
12
|
def test_symbol_setter_and_getter
|
@@ -125,6 +130,44 @@ class ThreadVariablesTest < Test::Unit::TestCase
|
|
125
130
|
assert_raises(SecurityError) do
|
126
131
|
Thread.new { $SAFE = 4; t.thread_variable_set(:foo, :baz) }.join
|
127
132
|
end
|
128
|
-
end if
|
133
|
+
end if $NATIVE_THREAD_VARIABLES
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
class ProxyTest < Test::Unit::TestCase
|
138
|
+
|
139
|
+
def test_proxy_get
|
140
|
+
t = Thread.new { Thread.current.thread_variable_set :foo, :lol }
|
141
|
+
t.join
|
142
|
+
assert_equal :lol, t.locals[:foo]
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_proxy_set
|
146
|
+
t = Thread.new { Thread.current.locals[:foo] = :lol }
|
147
|
+
t.join
|
148
|
+
assert_equal :lol, t.thread_variable_get(:foo)
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_proxy_key
|
152
|
+
t = Thread.new { Thread.current.thread_variable_set :foo, :lol }
|
153
|
+
t.join
|
154
|
+
assert t.locals.key?(:foo)
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_proxy_keys
|
158
|
+
t = Thread.new { Thread.current.thread_variable_set :foo, :lol }
|
159
|
+
t.join
|
160
|
+
assert_equal [:foo], t.locals.keys
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_or_equals_pattern
|
164
|
+
t = Thread.new do
|
165
|
+
locals = Thread.current.locals
|
166
|
+
locals[:counter] ||= 1
|
167
|
+
locals[:counter] += 1
|
168
|
+
end
|
169
|
+
t.join
|
170
|
+
assert_equal 2, t.thread_variable_get(:counter)
|
171
|
+
end
|
129
172
|
|
130
173
|
end
|
metadata
CHANGED
@@ -1,77 +1,66 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: thread_variables
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 0
|
10
|
-
version: 0.1.0
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Stefan Kaes
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
date: 2012-11-02 00:00:00 Z
|
12
|
+
date: 2012-11-03 00:00:00.000000000 Z
|
19
13
|
dependencies: []
|
20
|
-
|
21
14
|
description: Provide thread local variables for ruby 1.9 and API for all ruby versions
|
22
|
-
email:
|
15
|
+
email:
|
23
16
|
- stefan.kaes@xing.com
|
24
17
|
executables: []
|
25
|
-
|
26
18
|
extensions: []
|
27
|
-
|
28
19
|
extra_rdoc_files: []
|
29
|
-
|
30
|
-
files:
|
20
|
+
files:
|
31
21
|
- .gitignore
|
32
22
|
- Gemfile
|
33
23
|
- LICENSE.txt
|
34
24
|
- README.md
|
35
25
|
- Rakefile
|
36
26
|
- lib/thread_variables.rb
|
27
|
+
- lib/thread_variables/access.rb
|
37
28
|
- lib/thread_variables/version.rb
|
38
29
|
- test/colorized_test_output.rb
|
39
30
|
- test/thread_variables_test.rb
|
40
31
|
- thread_variables.gemspec
|
41
32
|
- thread_variables.patch
|
42
|
-
homepage:
|
33
|
+
homepage: ''
|
43
34
|
licenses: []
|
44
|
-
|
45
35
|
post_install_message:
|
46
36
|
rdoc_options: []
|
47
|
-
|
48
|
-
require_paths:
|
37
|
+
require_paths:
|
49
38
|
- lib
|
50
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
40
|
none: false
|
52
|
-
requirements:
|
53
|
-
- -
|
54
|
-
- !ruby/object:Gem::Version
|
55
|
-
|
56
|
-
segments:
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
segments:
|
57
46
|
- 0
|
58
|
-
|
59
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
hash: -1903362225607811994
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
49
|
none: false
|
61
|
-
requirements:
|
62
|
-
- -
|
63
|
-
- !ruby/object:Gem::Version
|
64
|
-
|
65
|
-
segments:
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
segments:
|
66
55
|
- 0
|
67
|
-
|
56
|
+
hash: -1903362225607811994
|
68
57
|
requirements: []
|
69
|
-
|
70
58
|
rubyforge_project:
|
71
59
|
rubygems_version: 1.8.24
|
72
60
|
signing_key:
|
73
61
|
specification_version: 3
|
74
|
-
summary: Provide functionality compatible to latest trunk commit for all ruby version
|
75
|
-
|
62
|
+
summary: Provide functionality compatible to latest trunk commit for all ruby version
|
63
|
+
above 1.8
|
64
|
+
test_files:
|
76
65
|
- test/colorized_test_output.rb
|
77
66
|
- test/thread_variables_test.rb
|