thread_safe 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e94921d1ca19c1fb6bb564e2db17fd724db5679f
4
+ data.tar.gz: f7305e1979a5366d5859d0e2fab80f111236c413
5
+ SHA512:
6
+ metadata.gz: 1f4dae8815d781c06b68d0d2da595d5fd039b22e1eb8586dbca6ec6a0026e73eaf98b992b3a297580bc6f4b1859834b53bb44b45af043639ee1efac4492f8fb5
7
+ data.tar.gz: 18e37ab075d01e92d0dd46c3553d899bd5d9484c014fdad7785f8c5fe4556b95e88131eb92b474a1398ca1b9f5e07a7b559383baa33931d3a8b094290abc0d3c
data/.gitignore CHANGED
@@ -18,3 +18,4 @@ test/tmp
18
18
  test/version_tmp
19
19
  tmp
20
20
  .DS_Store
21
+ *.swp
data/lib/thread_safe.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'thread_safe/version'
2
+ require 'thread_safe/synchronized_delegator'
2
3
 
3
4
  module ThreadSafe
4
5
  autoload :Cache, 'thread_safe/cache'
@@ -61,4 +62,4 @@ module ThreadSafe
61
62
  end
62
63
  end
63
64
  end
64
- end
65
+ end
@@ -0,0 +1,35 @@
1
+ # This class provides a trivial way to synchronize all calls to a given object
2
+ # by wrapping it with a Delegator that performs Mutex#lock/unlock calls around
3
+ # the delegated #send. Example:
4
+ #
5
+ # array = [] # not thread-safe on many impls
6
+ # array = MutexedDelegator.new(array) # thread-safe
7
+ #
8
+ # A simple Mutex provides a very coarse-grained way to synchronize a given
9
+ # object, in that it will cause synchronization for methods that have no
10
+ # need for it, but this is a trivial way to get thread-safety where none may
11
+ # exist currently on some implementations.
12
+ #
13
+ # This class is currently being considered for inclusion into stdlib, via
14
+ # https://bugs.ruby-lang.org/issues/8556
15
+
16
+ require 'delegate'
17
+
18
+ unless defined?(SynchronizedDelegator)
19
+ class SynchronizedDelegator < SimpleDelegator
20
+ def initialize(*)
21
+ super
22
+ @mutex = Mutex.new
23
+ end
24
+
25
+ def method_missing(m, *args, &block)
26
+ begin
27
+ mutex = @mutex
28
+ mutex.lock
29
+ super
30
+ ensure
31
+ mutex.unlock
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,3 +1,3 @@
1
1
  module Threadsafe
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,42 @@
1
+ require 'test/unit'
2
+ require 'thread_safe/synchronized_delegator.rb'
3
+
4
+ class TestSynchronizedDelegator < Test::Unit::TestCase
5
+ def test_wraps_array
6
+ ary = []
7
+ sync_ary = SynchronizedDelegator.new(ary)
8
+
9
+ ary << 1
10
+ assert_equal 1, sync_ary[0]
11
+ end
12
+
13
+ def test_synchronizes_access
14
+ ary = []
15
+ sync_ary = SynchronizedDelegator.new(ary)
16
+
17
+ t1_continue = false
18
+ t2_continue = false
19
+
20
+ t1 = Thread.new do
21
+ sync_ary << 1
22
+ sync_ary.each do
23
+ t2_continue = true
24
+ Thread.pass until t1_continue
25
+ end
26
+ end
27
+
28
+ t2 = Thread.new do
29
+ Thread.pass until t2_continue
30
+ sync_ary << 2
31
+ end
32
+
33
+ Thread.pass until t2.status == 'sleep'
34
+ assert_equal 1, ary.size
35
+
36
+ t1_continue = true
37
+ t1.join
38
+ t2.join
39
+
40
+ assert_equal 2, sync_ary.size
41
+ end
42
+ end
data/thread_safe.gemspec CHANGED
@@ -2,19 +2,20 @@
2
2
  require File.expand_path('../lib/thread_safe/version', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
- gem.authors = ["Charles Oliver Nutter", "thedarkone"]
6
- gem.email = ["headius@headius.com", "thedarkone2@gmail.com"]
5
+ gem.authors = ["Charles Oliver Nutter"]
6
+ gem.email = ["headius@headius.com"]
7
7
  gem.description = %q{Thread-safe collections and utilities for Ruby}
8
8
  gem.summary = %q{A collection of data structures and utilities to make thread-safe programming in Ruby easier}
9
9
  gem.homepage = "https://github.com/headius/thread_safe"
10
10
 
11
- gem.files = `git ls-files`.split($\) | Dir['{lib,examples,test}/**/*.jar']
11
+ gem.files = `git ls-files`.split($\)
12
12
  gem.platform = 'java' if defined?(JRUBY_VERSION)
13
13
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
14
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
15
  gem.name = "thread_safe"
16
16
  gem.require_paths = ["lib"]
17
17
  gem.version = Threadsafe::VERSION
18
+ gem.license = "Apache-2.0"
18
19
 
19
20
  gem.add_dependency 'atomic'
20
21
  end
metadata CHANGED
@@ -1,49 +1,36 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: thread_safe
3
- version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 0
10
- version: 0.1.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Charles Oliver Nutter
14
- - thedarkone
15
8
  autorequire:
16
9
  bindir: bin
17
10
  cert_chain: []
18
-
19
- date: 2012-12-13 00:00:00 +01:00
20
- default_executable:
21
- dependencies:
22
- - !ruby/object:Gem::Dependency
11
+ date: 2013-07-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
23
14
  name: atomic
24
- prerelease: false
25
- requirement: &id001 !ruby/object:Gem::Requirement
26
- none: false
27
- requirements:
28
- - - ">="
29
- - !ruby/object:Gem::Version
30
- hash: 3
31
- segments:
32
- - 0
33
- version: "0"
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
34
20
  type: :runtime
35
- version_requirements: *id001
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
36
27
  description: Thread-safe collections and utilities for Ruby
37
- email:
28
+ email:
38
29
  - headius@headius.com
39
- - thedarkone2@gmail.com
40
30
  executables: []
41
-
42
31
  extensions: []
43
-
44
32
  extra_rdoc_files: []
45
-
46
- files:
33
+ files:
47
34
  - .gitignore
48
35
  - Gemfile
49
36
  - LICENSE
@@ -62,6 +49,7 @@ files:
62
49
  - lib/thread_safe/mri_cache_backend.rb
63
50
  - lib/thread_safe/non_concurrent_cache_backend.rb
64
51
  - lib/thread_safe/synchronized_cache_backend.rb
52
+ - lib/thread_safe/synchronized_delegator.rb
65
53
  - lib/thread_safe/util.rb
66
54
  - lib/thread_safe/util/adder.rb
67
55
  - lib/thread_safe/util/atomic_reference.rb
@@ -77,45 +65,37 @@ files:
77
65
  - test/test_cache_loops.rb
78
66
  - test/test_hash.rb
79
67
  - test/test_helper.rb
68
+ - test/test_synchronized_delegator.rb
80
69
  - thread_safe.gemspec
81
- - lib/thread_safe/jruby_cache_backend.jar
82
- has_rdoc: true
83
70
  homepage: https://github.com/headius/thread_safe
84
- licenses: []
85
-
71
+ licenses:
72
+ - Apache-2.0
73
+ metadata: {}
86
74
  post_install_message:
87
75
  rdoc_options: []
88
-
89
- require_paths:
76
+ require_paths:
90
77
  - lib
91
- required_ruby_version: !ruby/object:Gem::Requirement
92
- none: false
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- hash: 3
97
- segments:
98
- - 0
99
- version: "0"
100
- required_rubygems_version: !ruby/object:Gem::Requirement
101
- none: false
102
- requirements:
103
- - - ">="
104
- - !ruby/object:Gem::Version
105
- hash: 3
106
- segments:
107
- - 0
108
- version: "0"
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
109
88
  requirements: []
110
-
111
89
  rubyforge_project:
112
- rubygems_version: 1.5.2
90
+ rubygems_version: 2.0.3
113
91
  signing_key:
114
- specification_version: 3
115
- summary: A collection of data structures and utilities to make thread-safe programming in Ruby easier
116
- test_files:
92
+ specification_version: 4
93
+ summary: A collection of data structures and utilities to make thread-safe programming
94
+ in Ruby easier
95
+ test_files:
117
96
  - test/test_array.rb
118
97
  - test/test_cache.rb
119
98
  - test/test_cache_loops.rb
120
99
  - test/test_hash.rb
121
100
  - test/test_helper.rb
101
+ - test/test_synchronized_delegator.rb