reentrant_mutex 1.0.0 → 1.1.0
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 +4 -4
- data/lib/reentrant_mutex.rb +10 -0
- data/reentrant_mutex.gemspec +1 -1
- data/spec/reentrant_mutex_spec.rb +19 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b626086142c3356ccc40d96a806e18f256778da
|
4
|
+
data.tar.gz: 1b3a1dab63094ecca1ec176f0fbf42fcd060a484
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8fe2383a5392af05e16a85d14df729e323da501c64dd85789f63e56ca2798272f66ea9bbf61eee179abd16911b628dea3d3dd8a0d35c07ac6bb3eb98a742e212
|
7
|
+
data.tar.gz: d049c9ed3f9eaa4286d79b8a41de3c5fd4e6de075d4fe835e38e010bcbee141df586e7c611bf4467789957bcdfb6cc9c4f48c8cd89013dfe968dd807bb97ab61
|
data/lib/reentrant_mutex.rb
CHANGED
@@ -6,6 +6,16 @@ class ReentrantMutex < Mutex
|
|
6
6
|
super
|
7
7
|
end
|
8
8
|
|
9
|
+
def synchronize
|
10
|
+
raise ThreadError, 'Must be called with a block' unless block_given?
|
11
|
+
|
12
|
+
lock
|
13
|
+
ret = yield
|
14
|
+
unlock
|
15
|
+
|
16
|
+
ret
|
17
|
+
end
|
18
|
+
|
9
19
|
def lock
|
10
20
|
c = increase_count Thread.current
|
11
21
|
super if c <= 1
|
data/reentrant_mutex.gemspec
CHANGED
@@ -3,6 +3,25 @@ require 'spec_helper'
|
|
3
3
|
describe ReentrantMutex do
|
4
4
|
let(:mutex) { ReentrantMutex.new }
|
5
5
|
|
6
|
+
describe '#synchronize' do
|
7
|
+
it 'should raise an error when no block is given' do
|
8
|
+
expect{mutex.synchronize}.to raise_error
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should not raise an error when called twice' do
|
12
|
+
expect{mutex.synchronize{mutex.synchronize{}}}.not_to raise_error
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should return whatever the block returns' do
|
16
|
+
res = mutex.synchronize { 42 }
|
17
|
+
expect(res).to eql(42)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should yield the block' do
|
21
|
+
expect{|b| mutex.synchronize &b}.to yield_with_no_args
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
6
25
|
describe '#lock' do
|
7
26
|
it 'should not deadlock when called twice' do
|
8
27
|
mutex.lock
|