thread_safe 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +144 -0
- data/README.md +32 -0
- data/Rakefile +10 -0
- data/lib/.DS_Store +0 -0
- data/lib/thread_safe.rb +20 -0
- data/lib/thread_safe/.DS_Store +0 -0
- data/lib/thread_safe/version.rb +3 -0
- data/test/test_array.rb +20 -0
- data/test/test_hash.rb +20 -0
- data/thread_safe.gemspec +17 -0
- metadata +76 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
Apache License
|
2
|
+
|
3
|
+
Version 2.0, January 2004
|
4
|
+
|
5
|
+
http://www.apache.org/licenses/
|
6
|
+
|
7
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
8
|
+
|
9
|
+
1. Definitions.
|
10
|
+
|
11
|
+
"License" shall mean the terms and conditions for use, reproduction, and distribution as
|
12
|
+
defined by Sections 1 through 9 of this document.
|
13
|
+
|
14
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that
|
15
|
+
is granting the License.
|
16
|
+
|
17
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities that
|
18
|
+
control, are controlled by, or are under common control with that entity. For the purposes
|
19
|
+
of this definition, "control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or otherwise, or (ii) ownership
|
21
|
+
of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of
|
22
|
+
such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by
|
25
|
+
this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications, including but not
|
28
|
+
limited to software source code, documentation source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical transformation or translation of
|
31
|
+
a Source form, including but not limited to compiled object code, generated documentation,
|
32
|
+
and conversions to other media types.
|
33
|
+
|
34
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made available
|
35
|
+
under the License, as indicated by a copyright notice that is included in or attached to the
|
36
|
+
work (an example is provided in the Appendix below).
|
37
|
+
|
38
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on
|
39
|
+
(or derived from) the Work and for which the editorial revisions, annotations, elaborations,
|
40
|
+
or other modifications represent, as a whole, an original work of authorship. For the
|
41
|
+
purposes of this License, Derivative Works shall not include works that remain separable
|
42
|
+
from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works
|
43
|
+
thereof.
|
44
|
+
|
45
|
+
"Contribution" shall mean any work of authorship, including the original version of the Work
|
46
|
+
and any modifications or additions to that Work or Derivative Works thereof, that is
|
47
|
+
intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by
|
48
|
+
an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the
|
49
|
+
purposes of this definition, "submitted" means any form of electronic, verbal, or written
|
50
|
+
communication sent to the Licensor or its representatives, including but not limited to
|
51
|
+
communication on electronic mailing lists, source code control systems, and issue tracking
|
52
|
+
systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and
|
53
|
+
improving the Work, but excluding communication that is conspicuously marked or otherwise
|
54
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
55
|
+
|
56
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a
|
57
|
+
Contribution has been received by Licensor and subsequently incorporated within the Work.
|
58
|
+
|
59
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this License, each
|
60
|
+
Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge,
|
61
|
+
royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of,
|
62
|
+
publicly display, publicly perform, sublicense, and distribute the Work and such Derivative
|
63
|
+
Works in Source or Object form.
|
64
|
+
|
65
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License, each
|
66
|
+
Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge,
|
67
|
+
royalty-free, irrevocable (except as stated in this section) patent license to make, have
|
68
|
+
made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license
|
69
|
+
applies only to those patent claims licensable by such Contributor that are necessarily
|
70
|
+
infringed by their Contribution(s) alone or by combination of their Contribution(s) with the
|
71
|
+
Work to which such Contribution(s) was submitted. If You institute patent litigation against
|
72
|
+
any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or
|
73
|
+
a Contribution incorporated within the Work constitutes direct or contributory patent
|
74
|
+
infringement, then any patent licenses granted to You under this License for that Work shall
|
75
|
+
terminate as of the date such litigation is filed.
|
76
|
+
|
77
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works
|
78
|
+
thereof in any medium, with or without modifications, and in Source or Object form, provided
|
79
|
+
that You meet the following conditions:
|
80
|
+
|
81
|
+
You must give any other recipients of the Work or Derivative Works a copy of this License;
|
82
|
+
and
|
83
|
+
|
84
|
+
You must cause any modified files to carry prominent notices stating that You changed the
|
85
|
+
files; and
|
86
|
+
|
87
|
+
You must retain, in the Source form of any Derivative Works that You distribute, all
|
88
|
+
copyright, patent, trademark, and attribution notices from the Source form of the Work,
|
89
|
+
excluding those notices that do not pertain to any part of the Derivative Works; and
|
90
|
+
|
91
|
+
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative
|
92
|
+
Works that You distribute must include a readable copy of the attribution notices contained
|
93
|
+
within such NOTICE file, excluding those notices that do not pertain to any part of the
|
94
|
+
Derivative Works, in at least one of the following places: within a NOTICE text file
|
95
|
+
distributed as part of the Derivative Works; within the Source form or documentation, if
|
96
|
+
provided along with the Derivative Works; or, within a display generated by the Derivative
|
97
|
+
Works, if and wherever such third-party notices normally appear. The contents of the NOTICE
|
98
|
+
file are for informational purposes only and do not modify the License. You may add Your own
|
99
|
+
attribution notices within Derivative Works that You distribute, alongside or as an addendum
|
100
|
+
to the NOTICE text from the Work, provided that such additional attribution notices cannot
|
101
|
+
be construed as modifying the License. You may add Your own copyright statement to Your
|
102
|
+
modifications and may provide additional or different license terms and conditions for use,
|
103
|
+
reproduction, or distribution of Your modifications, or for any such Derivative Works as a
|
104
|
+
whole, provided Your use, reproduction, and distribution of the Work otherwise complies with
|
105
|
+
the conditions stated in this License.
|
106
|
+
|
107
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution
|
108
|
+
intentionally submitted for inclusion in the Work by You to the Licensor shall be under the
|
109
|
+
terms and conditions of this License, without any additional terms or conditions.
|
110
|
+
Notwithstanding the above, nothing herein shall supersede or modify the terms of any
|
111
|
+
separate license agreement you may have executed with Licensor regarding such Contributions.
|
112
|
+
|
113
|
+
6. Trademarks. This License does not grant permission to use the trade names, trademarks,
|
114
|
+
service marks, or product names of the Licensor, except as required for reasonable and
|
115
|
+
customary use in describing the origin of the Work and reproducing the content of the NOTICE
|
116
|
+
file.
|
117
|
+
|
118
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing,
|
119
|
+
Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS"
|
120
|
+
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including,
|
121
|
+
without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT,
|
122
|
+
MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for
|
123
|
+
determining the appropriateness of using or redistributing the Work and assume any risks
|
124
|
+
associated with Your exercise of permissions under this License.
|
125
|
+
|
126
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in tort
|
127
|
+
(including negligence), contract, or otherwise, unless required by applicable law (such as
|
128
|
+
deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be
|
129
|
+
liable to You for damages, including any direct, indirect, special, incidental, or
|
130
|
+
consequential damages of any character arising as a result of this License or out of the use
|
131
|
+
or inability to use the Work (including but not limited to damages for loss of goodwill,
|
132
|
+
work stoppage, computer failure or malfunction, or any and all other commercial damages or
|
133
|
+
losses), even if such Contributor has been advised of the possibility of such damages.
|
134
|
+
|
135
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative
|
136
|
+
Works thereof, You may choose to offer, and charge a fee for, acceptance of support,
|
137
|
+
warranty, indemnity, or other liability obligations and/or rights consistent with this
|
138
|
+
License. However, in accepting such obligations, You may act only on Your own behalf and on
|
139
|
+
Your sole responsibility, not on behalf of any other Contributor, and only if You agree to
|
140
|
+
indemnify, defend, and hold each Contributor harmless for any liability incurred by, or
|
141
|
+
claims asserted against, such Contributor by reason of your accepting any such warranty or
|
142
|
+
additional liability.
|
143
|
+
|
144
|
+
END OF TERMS AND CONDITIONS
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Threadsafe
|
2
|
+
|
3
|
+
A collection of thread-safe versions of common core Ruby classes.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'thread_safe'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install thread_safe
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
require 'thread_safe'
|
22
|
+
|
23
|
+
sa = ThreadSafe::Array.new # supports standard Array.new forms
|
24
|
+
sh = ThreadSafe::Hash.new # supports standard Hash.new forms
|
25
|
+
|
26
|
+
## Contributing
|
27
|
+
|
28
|
+
1. Fork it
|
29
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
30
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
31
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
32
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/.DS_Store
ADDED
Binary file
|
data/lib/thread_safe.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'thread_safe/version'
|
2
|
+
|
3
|
+
if defined?(JRUBY_VERSION)
|
4
|
+
require 'jruby/synchronized'
|
5
|
+
|
6
|
+
module ThreadSafe
|
7
|
+
class Array < ::Array
|
8
|
+
include JRuby::Synchronized
|
9
|
+
end
|
10
|
+
|
11
|
+
class Hash < ::Hash
|
12
|
+
include JRuby::Synchronized
|
13
|
+
end
|
14
|
+
end
|
15
|
+
else
|
16
|
+
module ThreadSafe
|
17
|
+
Array = ::Array
|
18
|
+
Hash = ::Hash
|
19
|
+
end
|
20
|
+
end
|
Binary file
|
data/test/test_array.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'thread_safe'
|
3
|
+
|
4
|
+
class TestArray < Test::Unit::TestCase
|
5
|
+
def test_concurrency
|
6
|
+
ary = ThreadSafe::Array.new
|
7
|
+
assert_nothing_raised do
|
8
|
+
(1..100).map do |i|
|
9
|
+
Thread.new do
|
10
|
+
1000.times do
|
11
|
+
ary << i
|
12
|
+
ary.each {|x| x * 2}
|
13
|
+
ary.shift
|
14
|
+
ary.last
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end.map(&:join)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/test/test_hash.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'thread_safe'
|
3
|
+
|
4
|
+
class TestHash < Test::Unit::TestCase
|
5
|
+
def test_concurrency
|
6
|
+
hsh = ThreadSafe::Hash.new
|
7
|
+
assert_nothing_raised do
|
8
|
+
(1..100).map do |i|
|
9
|
+
Thread.new do
|
10
|
+
1000.times do |j|
|
11
|
+
hsh[i*1000+j] = i
|
12
|
+
hsh.each {|k,v| k + v}
|
13
|
+
hsh[i*1000+j]
|
14
|
+
hsh.delete(i*1000+j)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end.map(&:join)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/thread_safe.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/thread_safe/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Charles Oliver Nutter"]
|
6
|
+
gem.email = ["headius@headius.com"]
|
7
|
+
gem.description = %q{Thread-safe collections and utilities for Ruby}
|
8
|
+
gem.summary = %q{A collection of data structures and utilities to make thread-safe programming in Ruby easier}
|
9
|
+
gem.homepage = "https://github.com/headius/thread_safe"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "thread_safe"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Threadsafe::VERSION
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: thread_safe
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Charles Oliver Nutter
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-26 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Thread-safe collections and utilities for Ruby
|
15
|
+
email:
|
16
|
+
- headius@headius.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- !binary |-
|
22
|
+
LmdpdGlnbm9yZQ==
|
23
|
+
- !binary |-
|
24
|
+
R2VtZmlsZQ==
|
25
|
+
- !binary |-
|
26
|
+
TElDRU5TRQ==
|
27
|
+
- !binary |-
|
28
|
+
UkVBRE1FLm1k
|
29
|
+
- !binary |-
|
30
|
+
UmFrZWZpbGU=
|
31
|
+
- !binary |-
|
32
|
+
bGliLy5EU19TdG9yZQ==
|
33
|
+
- !binary |-
|
34
|
+
bGliL3RocmVhZF9zYWZlLnJi
|
35
|
+
- !binary |-
|
36
|
+
bGliL3RocmVhZF9zYWZlLy5EU19TdG9yZQ==
|
37
|
+
- !binary |-
|
38
|
+
bGliL3RocmVhZF9zYWZlL3ZlcnNpb24ucmI=
|
39
|
+
- !binary |-
|
40
|
+
dGVzdC90ZXN0X2FycmF5LnJi
|
41
|
+
- !binary |-
|
42
|
+
dGVzdC90ZXN0X2hhc2gucmI=
|
43
|
+
- !binary |-
|
44
|
+
dGhyZWFkX3NhZmUuZ2Vtc3BlYw==
|
45
|
+
homepage: https://github.com/headius/thread_safe
|
46
|
+
licenses: []
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: !binary |-
|
56
|
+
MA==
|
57
|
+
none: false
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: !binary |-
|
63
|
+
MA==
|
64
|
+
none: false
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.8.21
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: A collection of data structures and utilities to make thread-safe programming in Ruby easier
|
71
|
+
test_files:
|
72
|
+
- !binary |-
|
73
|
+
dGVzdC90ZXN0X2FycmF5LnJi
|
74
|
+
- !binary |-
|
75
|
+
dGVzdC90ZXN0X2hhc2gucmI=
|
76
|
+
...
|