threadsafety 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: be3944a30b762122e252c6f7a25d5ff76e38f9ea
4
+ data.tar.gz: 8c95681c492195e7dbd2b286d62f8dd7ae14a06a
5
+ SHA512:
6
+ metadata.gz: 2ca7017e08a8a99e613078b96857d68b5abb002328df0f0612e529acbdf8d3177ce04d7910dc8ccabc79c66156b31bfe896d66af1cc98fb4f4741507dd8ced6b
7
+ data.tar.gz: ab64da71284fcba72c27c04dbed1e7509a9356c8cb99079db7273e8e2d3d4932d46cb92261d605828af00bd2f720bdfb2f6ddc2638d8c930c0a0e640c88d6311
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in threadsafety.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Nathaniel Symer
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.
@@ -0,0 +1,54 @@
1
+ # Threadsafety
2
+
3
+ Makes things threadsafe.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'threadsafety'
10
+
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install threadsafety
19
+
20
+ ## Usage
21
+
22
+ Use it via mixin or subclass:
23
+
24
+ require "threadsafety"
25
+
26
+ class MyObject < ThreadsafeObject
27
+ attr_accessor :something
28
+ # ...
29
+ end
30
+
31
+ class MySecondObject
32
+ include Threadsafety
33
+ attr_accessor :something
34
+ # ...
35
+ end
36
+
37
+ It overrides the `attr_writer` and `attr_accessor` methods on `Module` (and therefore `Class` due to inheritance) to create threadsafe setters.
38
+
39
+ If you need to write your own threadsafe code, you can use the method `Threadsafety#threadsafe`:
40
+
41
+ theadsafe do
42
+ # stuff that needs to be threadsafe
43
+ end
44
+
45
+ `Threadsafety#threadsafe` returns whatever the block you pass it returns. You can also nest calls to `Threadsafety#threadsafe` on the same thread, unlike `Mutex#synchronize`. It's defined on global scope, class scope, and object scope, each using their own mutex.
46
+
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it ( https://github.com/[my-github-username]/threadsafety/fork )
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create a new Pull Request
@@ -0,0 +1,82 @@
1
+ require "threadsafety/version"
2
+ require "thread"
3
+
4
+ module Threadsafety
5
+ module AttrOverride
6
+ def attr_accessor *args
7
+ attr_reader *args
8
+ _define_setter_methods *args
9
+ end
10
+
11
+ def attr_writer *args
12
+ _define_setter_methods *args
13
+ end
14
+
15
+ private
16
+
17
+ def _define_setter_methods *args
18
+ args.each do |arg|
19
+ define_method "#{arg.to_s}=" do |v|
20
+ threadsafe { instance_variable_set "@#{arg.to_s}", v }
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ module DSL
27
+ $__mutex ||= Mutex.new
28
+
29
+ def threadsafety_mutex= v
30
+ @__mutex = v
31
+ end
32
+
33
+ def threadsafe?
34
+ Thread.current[:_threadsafe]
35
+ end
36
+
37
+ def threadsafe &block
38
+ if threadsafe?
39
+ block.call
40
+ else
41
+ t = Thread.current
42
+
43
+ begin
44
+ (@__mutex || $__mutex).synchronize do
45
+ t[:_threadsafe] = true
46
+ block.call
47
+ end
48
+ ensure
49
+ t[:_threadsafe] = false
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ def self.included base
56
+ base.class_eval do
57
+ extend InitializeOverrider
58
+ extend AttrOverride
59
+ extend DSL
60
+ include DSL
61
+ @__mutex ||= Mutex.new
62
+ end
63
+ end
64
+
65
+ module InitializeOverrider
66
+ def method_added meth
67
+ if meth == :initialize && !@__initialized
68
+ @__initialized = true
69
+ original_method = instance_method :initialize
70
+ define_method :initialize do |*args, &block|
71
+ original_method.bind(self).call *args, &block
72
+ @__mutex = Mutex.new
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ include Threadsafety::DSL
80
+
81
+ ThreadsafeObject = Class.new Object
82
+ ThreadsafeObject.send :include, Threadsafety
@@ -0,0 +1,3 @@
1
+ module Threadsafety
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'threadsafety/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "threadsafety"
8
+ spec.version = Threadsafety::VERSION
9
+ spec.authors = ["Nathaniel Symer"]
10
+ spec.email = ["nate@natesymer.com"]
11
+ spec.summary = %q{Makes things threadsafe.}
12
+ spec.description = %q{Threadsafety overrides attr_accessor and attr_writer to generate thread-safe setters. It also provides a helper method to add threadsafety anywhere, even outside classes.}
13
+ spec.homepage = "http://github.com/fhsjaagshs/threadsafety"
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.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: threadsafety
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nathaniel Symer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-05 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Threadsafety overrides attr_accessor and attr_writer to generate thread-safe
42
+ setters. It also provides a helper method to add threadsafety anywhere, even outside
43
+ classes.
44
+ email:
45
+ - nate@natesymer.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - lib/threadsafety.rb
55
+ - lib/threadsafety/version.rb
56
+ - threadsafety.gemspec
57
+ homepage: http://github.com/fhsjaagshs/threadsafety
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.2.2
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Makes things threadsafe.
81
+ test_files: []