thread_local 0.0.1
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 +7 -0
- data/.gitignore +23 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +7 -0
- data/lib/thread_local.rb +119 -0
- data/lib/thread_local/version.rb +3 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/thread_local_spec.rb +58 -0
- data/thread_local.gemspec +24 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e8009c78cf95100df00188a68a828642e94e9288
|
4
|
+
data.tar.gz: 2ad20221fcd7bc40666e0c33f5603b0d09b4ee44
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 66291b1458f1812736f51ca04c7025711934ba8fb360cbf54b72d8e0dbf94b297dda2e94a935d670208753554f24430bb6decd6f33bffdb2f373975f24eba85f
|
7
|
+
data.tar.gz: b419796dca39e403f1a3e5adb4cb846e012e4dd86db86df552f1237f3f6f8ac872e4cbde769b1d6cff04900372f66e1e118353e362465c0f7e588c259a99f51a
|
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
*~
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Yusuke KUOKA
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# ThreadLocal
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'thread_local'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install thread_local
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/[my-github-username]/thread_local/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/thread_local.rb
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
require "thread_local/version"
|
2
|
+
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
class ThreadLocal
|
6
|
+
def initialize(initial_value, *args)
|
7
|
+
opts, = args
|
8
|
+
|
9
|
+
opts ||= {}
|
10
|
+
|
11
|
+
@initial_value = to_callable(initial_value)
|
12
|
+
@prefix = opts[:prefix] || 'thread_local'
|
13
|
+
|
14
|
+
@threads_mutex = Mutex.new
|
15
|
+
@threads = Set.new
|
16
|
+
|
17
|
+
define_initializer!
|
18
|
+
end
|
19
|
+
|
20
|
+
def get
|
21
|
+
t = Thread.current
|
22
|
+
|
23
|
+
if set?
|
24
|
+
t.thread_variable_get value_key
|
25
|
+
else
|
26
|
+
set @initial_value.call
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def set?
|
31
|
+
t = Thread.current
|
32
|
+
t.thread_variable_get value_initialized_key
|
33
|
+
end
|
34
|
+
|
35
|
+
def set(new_value)
|
36
|
+
t = Thread.current
|
37
|
+
|
38
|
+
t.thread_variable_set value_key, new_value
|
39
|
+
t.thread_variable_set value_initialized_key, true
|
40
|
+
|
41
|
+
forget_vanished_threads!
|
42
|
+
|
43
|
+
@threads_mutex.synchronize do
|
44
|
+
@threads.add(t.object_id)
|
45
|
+
end
|
46
|
+
|
47
|
+
new_value
|
48
|
+
end
|
49
|
+
|
50
|
+
def delete
|
51
|
+
t = Thread.current
|
52
|
+
|
53
|
+
t.thread_variable_set value_key, nil
|
54
|
+
|
55
|
+
@threads_mutex.synchronize do
|
56
|
+
@threads.delete(t.object_id)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def __threads_object_ids__
|
61
|
+
@threads
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
# @return String
|
67
|
+
def value_key
|
68
|
+
"#{@prefix}_#{object_id}"
|
69
|
+
end
|
70
|
+
|
71
|
+
# @return String
|
72
|
+
def value_initialized_key
|
73
|
+
"#{@prefix}_#{object_id}_initialized"
|
74
|
+
end
|
75
|
+
|
76
|
+
# @param [Object] possibly_callable_object
|
77
|
+
# @return Proc
|
78
|
+
def to_callable(possibly_callable_object)
|
79
|
+
if possibly_callable_object.respond_to? :call
|
80
|
+
possibly_callable_object
|
81
|
+
else
|
82
|
+
-> { possibly_callable_object }
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def collect_threads_object_ids
|
87
|
+
Thread.list.map(&:object_id)
|
88
|
+
end
|
89
|
+
|
90
|
+
def collect_threads_object_ids_as_set
|
91
|
+
Set.new(collect_threads_object_ids)
|
92
|
+
end
|
93
|
+
|
94
|
+
def forget_vanished_threads!
|
95
|
+
set = collect_threads_object_ids_as_set
|
96
|
+
|
97
|
+
@threads_mutex.synchronize do |t|
|
98
|
+
@threads.select do |threads_object_id|
|
99
|
+
set.include? threads_object_id
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def define_initializer!
|
105
|
+
ObjectSpace.define_finalizer self do
|
106
|
+
# We don't synchronize `@threads` here,
|
107
|
+
# assuming that this finalizer won't be called concurrently with `ThreadLocal#set`
|
108
|
+
@threads.each do |threads_object_id|
|
109
|
+
t = Thread.all.find do |t|
|
110
|
+
t.object_id == threads_object_id
|
111
|
+
end
|
112
|
+
|
113
|
+
unless t.nil?
|
114
|
+
t.thread_variable_set key, nil
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ThreadLocal do
|
4
|
+
it 'has a version number' do
|
5
|
+
expect(ThreadLocal::VERSION).not_to be nil
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:initial_value) {
|
9
|
+
1
|
10
|
+
}
|
11
|
+
|
12
|
+
subject {
|
13
|
+
ThreadLocal.new(initial_value)
|
14
|
+
}
|
15
|
+
|
16
|
+
context 'the associated value are read at least once' do
|
17
|
+
before(:each) do
|
18
|
+
subject.get
|
19
|
+
end
|
20
|
+
|
21
|
+
it "holds the initial value" do
|
22
|
+
expect(subject.get).to eq(initial_value)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "holds object_id's of threads on which the ThreadLocal is created" do
|
26
|
+
expect(subject.__threads_object_ids__).to include(Thread.current.object_id)
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'deleted the value afterward' do
|
30
|
+
before(:each) do
|
31
|
+
subject.delete
|
32
|
+
end
|
33
|
+
|
34
|
+
it "thinks the value is set" do
|
35
|
+
expect(subject.set?).to be_truthy
|
36
|
+
end
|
37
|
+
|
38
|
+
it "holds a `nil` for its value" do
|
39
|
+
expect(subject.get).to be_nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'multi-threaded' do
|
45
|
+
before(:each) do
|
46
|
+
Thread.abort_on_exception = true
|
47
|
+
Thread.start do
|
48
|
+
subject.set 2
|
49
|
+
|
50
|
+
expect(subject.get).to eq(2)
|
51
|
+
end.join
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'holds values separately for the other thread' do
|
55
|
+
expect(subject.get).to eq(initial_value)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'thread_local/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "thread_local"
|
8
|
+
spec.version = ThreadLocal::VERSION
|
9
|
+
spec.authors = ["Yusuke KUOKA"]
|
10
|
+
spec.email = ["yusuke.kuoka@crowdworks.co.jp"]
|
11
|
+
spec.summary = %q{An implementation of the thread-local variable.}
|
12
|
+
spec.description = %q{An implementation of the thread-local variable provided in many programming languages like Java.}
|
13
|
+
spec.homepage = "https://github.com/mumoshu/thread_local"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: thread_local
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yusuke KUOKA
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: An implementation of the thread-local variable provided in many programming
|
56
|
+
languages like Java.
|
57
|
+
email:
|
58
|
+
- yusuke.kuoka@crowdworks.co.jp
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- lib/thread_local.rb
|
71
|
+
- lib/thread_local/version.rb
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
- spec/thread_local_spec.rb
|
74
|
+
- thread_local.gemspec
|
75
|
+
homepage: https://github.com/mumoshu/thread_local
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.2.2
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: An implementation of the thread-local variable.
|
99
|
+
test_files:
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
- spec/thread_local_spec.rb
|