reentrant_mutex 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +25 -0
- data/Rakefile +1 -0
- data/lib/reentrant_mutex.rb +35 -0
- data/reentrant_mutex.gemspec +19 -0
- data/spec/reentrant_mutex_spec.rb +26 -0
- data/spec/spec_helper.rb +6 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b0824ae06f5a109d6437d7330f58bcf78d7394a7
|
4
|
+
data.tar.gz: cd3d9f14e60c5be608a4ea82ef39e39fe8cd154d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 734e428dbe83f084e6b6112d46b15a194207b5bfc56544c9e9210f48862ead33f456c8635b51a82273b5ec34edec269cabd68e84dbcbdd6bd4499ece0216b4ed
|
7
|
+
data.tar.gz: d83450b1e06cf49faccf752be0036e87176bc46947819c0672dcd5dec10cd5ee313690da31f67638a9e26c3178144ff2586f2198fe68f93f3d43bd4f2634e331
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Boris Bera
|
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,25 @@
|
|
1
|
+
ReentrantMutex
|
2
|
+
==============
|
3
|
+
|
4
|
+
A simple gem that implements a [reentrant mutex](https://en.wikipedia.org/wiki/Reentrant_mutex).
|
5
|
+
|
6
|
+
Installation
|
7
|
+
------------
|
8
|
+
|
9
|
+
1. Install it `gem install reentrant_lock`
|
10
|
+
2. Require it `require 'reentrant_lock'`
|
11
|
+
3. Use it
|
12
|
+
|
13
|
+
Usage
|
14
|
+
-----
|
15
|
+
|
16
|
+
This works exactly like a normal mutex except that you can lock a mutex you already have locked.
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
require 'reentrant_mutex'
|
20
|
+
|
21
|
+
mutex = ReentrantMutex.new
|
22
|
+
mutex.synchronize do
|
23
|
+
# . . .
|
24
|
+
end
|
25
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class ReentrantMutex < Mutex
|
2
|
+
def initialize
|
3
|
+
@count_mutex = Mutex.new
|
4
|
+
@counts = Hash.new(0)
|
5
|
+
|
6
|
+
super
|
7
|
+
end
|
8
|
+
|
9
|
+
def lock
|
10
|
+
c = increase_count Thread.current
|
11
|
+
super if c <= 1
|
12
|
+
end
|
13
|
+
|
14
|
+
def unlock
|
15
|
+
c = decrease_count Thread.current
|
16
|
+
if c <= 0
|
17
|
+
super
|
18
|
+
delete_count Thread.current
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def increase_count(thread)
|
25
|
+
@count_mutex.synchronize { @counts[thread] += 1 }
|
26
|
+
end
|
27
|
+
|
28
|
+
def decrease_count(thread)
|
29
|
+
@count_mutex.synchronize { @counts[thread] -= 1 }
|
30
|
+
end
|
31
|
+
|
32
|
+
def delete_count(thread)
|
33
|
+
@count_mutex.synchronize { @counts.delete(thread) }
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
Gem::Specification.new do |spec|
|
3
|
+
spec.name = 'reentrant_mutex'
|
4
|
+
spec.version = '1.0.0'
|
5
|
+
spec.authors = ['Boris Bera']
|
6
|
+
spec.email = ['bboris@rsoft.ca']
|
7
|
+
spec.summary = %q{A simple reentrant/recursive mutex}
|
8
|
+
spec.homepage = 'https://github.com/beraboris/reentrant_mutex'
|
9
|
+
spec.license = 'MIT'
|
10
|
+
|
11
|
+
spec.files = `git ls-files -z`.split("\x0")
|
12
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
13
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
14
|
+
spec.require_paths = ['lib']
|
15
|
+
|
16
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
17
|
+
spec.add_development_dependency 'rake'
|
18
|
+
spec.add_development_dependency 'rspec', '~> 2.14.1'
|
19
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ReentrantMutex do
|
4
|
+
let(:mutex) { ReentrantMutex.new }
|
5
|
+
|
6
|
+
describe '#lock' do
|
7
|
+
it 'should not deadlock when called twice' do
|
8
|
+
mutex.lock
|
9
|
+
expect{mutex.lock}.not_to raise_error
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#unlock' do
|
14
|
+
it 'should raise an error when called without a lock' do
|
15
|
+
expect{mutex.unlock}.to raise_error
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should not raise an error when locked multiple times' do
|
19
|
+
mutex.lock
|
20
|
+
mutex.lock
|
21
|
+
|
22
|
+
expect{mutex.unlock}.not_to raise_error
|
23
|
+
expect{mutex.unlock}.not_to raise_error
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: reentrant_mutex
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Boris Bera
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-02 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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
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: 2.14.1
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.14.1
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- bboris@rsoft.ca
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/reentrant_mutex.rb
|
69
|
+
- reentrant_mutex.gemspec
|
70
|
+
- spec/reentrant_mutex_spec.rb
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
homepage: https://github.com/beraboris/reentrant_mutex
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.2.2
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: A simple reentrant/recursive mutex
|
96
|
+
test_files:
|
97
|
+
- spec/reentrant_mutex_spec.rb
|
98
|
+
- spec/spec_helper.rb
|
99
|
+
has_rdoc:
|