reentrant_flock 0.1.0 → 0.1.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/LICENSE +22 -0
- data/README.md +5 -5
- data/example/process.rb +18 -0
- data/example/reentrant.rb +13 -0
- data/example/thread.rb +19 -0
- data/lib/reentrant_flock.rb +8 -9
- data/lib/reentrant_flock/version.rb +1 -1
- data/reentrant_flock.gemspec +1 -0
- metadata +8 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0578b8a600fc577d9c97dc5e1a97b287d164142a'
|
4
|
+
data.tar.gz: fabd3a6bfd101c6388456d81eeac11d69c12fdbd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46bf52c3fb8ef838c54f910cbb16bf1a34f0fb3bd61af2eb7b4e8c775e5864f1dc4ea1531eca950eb65bb1401cf1e21b16dec2d2cd1afd8c4f71e729d2e4fea1
|
7
|
+
data.tar.gz: caec8d9f8c038cefdc523284ce670b945461e7d7f18b656fc875affc44a23228083044906f5e88b3f0727ae49bb7ac09008e11134bccb193312cb471bb05131e
|
data/CHANGELOG.md
CHANGED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2017 Naotoshi Seo
|
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
CHANGED
@@ -26,7 +26,7 @@ require 'reentrant_flock'
|
|
26
26
|
File.open('/tmp/lock', File::RDWR | File::CREAT) do |fp|
|
27
27
|
ReentrantFlock.synchronize(fp, File::LOCK_EX) do
|
28
28
|
ReentrantFlock.synchronize(fp, File::LOCK_EX) do
|
29
|
-
#
|
29
|
+
# Not blocked by myself
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
@@ -35,15 +35,15 @@ end
|
|
35
35
|
```ruby
|
36
36
|
require 'reentrant_flock'
|
37
37
|
|
38
|
-
def
|
38
|
+
def with_rlock(&block)
|
39
39
|
File.open('/tmp/lock', File::RDWR | File::CREAT) do |fp|
|
40
40
|
ReentrantFlock.synchronize(fp, File::LOCK_EX, &block)
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
|
45
|
-
|
46
|
-
#
|
44
|
+
with_rlock do
|
45
|
+
with_rlock do
|
46
|
+
# Not blocked by myself
|
47
47
|
end
|
48
48
|
end
|
49
49
|
```
|
data/example/process.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'reentrant_flock'
|
2
|
+
|
3
|
+
def with_rlock(&block)
|
4
|
+
File.open('/tmp/lock', File::RDWR | File::CREAT) do |fp|
|
5
|
+
ReentrantFlock.synchronize(fp, File::LOCK_EX | File::LOCK_NB, &block)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
# Should raise AlreadyLocked
|
10
|
+
fork do
|
11
|
+
with_rlock do
|
12
|
+
sleep 0.5
|
13
|
+
end
|
14
|
+
end
|
15
|
+
sleep 0.1
|
16
|
+
with_rlock do
|
17
|
+
sleep 0.5
|
18
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'reentrant_flock'
|
2
|
+
|
3
|
+
def with_rlock(&block)
|
4
|
+
File.open('/tmp/lock', File::RDWR | File::CREAT) do |fp|
|
5
|
+
ReentrantFlock.synchronize(fp, File::LOCK_EX, &block)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
with_rlock do
|
10
|
+
with_rlock do
|
11
|
+
puts 'Not blocked by myself'
|
12
|
+
end
|
13
|
+
end
|
data/example/thread.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'reentrant_flock'
|
2
|
+
|
3
|
+
def with_rlock(&block)
|
4
|
+
File.open('/tmp/lock', File::RDWR | File::CREAT) do |fp|
|
5
|
+
ReentrantFlock.synchronize(fp, File::LOCK_EX | File::LOCK_NB, &block)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
# Should raise AlreadyLocked
|
10
|
+
th = Thread.new do
|
11
|
+
with_rlock do
|
12
|
+
sleep 0.5
|
13
|
+
end
|
14
|
+
end
|
15
|
+
sleep 0.1
|
16
|
+
with_rlock do
|
17
|
+
sleep 0.5
|
18
|
+
end
|
19
|
+
th.join
|
data/lib/reentrant_flock.rb
CHANGED
@@ -11,9 +11,7 @@ class ReentrantFlock
|
|
11
11
|
# when a file is closed. Use ReentrantFlock.synchronize to assure
|
12
12
|
# decrementing internal counts.
|
13
13
|
#
|
14
|
-
# @param
|
15
|
-
# @param operation [PARAM]
|
16
|
-
# @see File#flock for more details
|
14
|
+
# @param [File] fp
|
17
15
|
# @param [PARAM] operation See File#flock
|
18
16
|
# @return see File#flock
|
19
17
|
def flock(fp, operation)
|
@@ -50,7 +48,7 @@ class ReentrantFlock
|
|
50
48
|
private
|
51
49
|
|
52
50
|
# @return [0] if current thread holds the lock
|
53
|
-
# @return [false] if
|
51
|
+
# @return [false] if another already locked (LOCK_NB)
|
54
52
|
def lock(fp, operation)
|
55
53
|
c = incr(key(fp))
|
56
54
|
if c <= 1
|
@@ -91,7 +89,7 @@ class ReentrantFlock
|
|
91
89
|
end
|
92
90
|
end
|
93
91
|
|
94
|
-
#
|
92
|
+
# Please note that:
|
95
93
|
#
|
96
94
|
# ```
|
97
95
|
# fp = File.open('a', 'w')
|
@@ -101,10 +99,11 @@ end
|
|
101
99
|
# fp.flock(File::LOCK_EX) # block
|
102
100
|
# ```
|
103
101
|
#
|
104
|
-
# That is, File#flock is orginally reentrant for the same
|
105
|
-
#
|
106
|
-
#
|
107
|
-
#
|
102
|
+
# That is, File#flock is orginally reentrant for the same file
|
103
|
+
# object. On linux, file lock is associated with file descriptor,
|
104
|
+
# so another file descriptor is required to get blocked.
|
105
|
+
# This version holds the same file object, so might be useless.
|
106
|
+
# I may delete this. Using flock directly should be enough.
|
108
107
|
class ReentrantFlock
|
109
108
|
attr_reader :fp
|
110
109
|
|
data/reentrant_flock.gemspec
CHANGED
@@ -12,6 +12,7 @@ Gem::Specification.new do |spec|
|
|
12
12
|
spec.summary = %q{A reentrant/recursive flock.}
|
13
13
|
spec.description = %q{A reentrant/recursive flock.}
|
14
14
|
spec.homepage = "https://github.com/sonots/reentrant_flock"
|
15
|
+
spec.licenses = ["MIT"]
|
15
16
|
|
16
17
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
18
|
f.match(%r{^(test|spec|features)/})
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reentrant_flock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Naotoshi Seo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-11-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -77,15 +77,20 @@ files:
|
|
77
77
|
- ".travis.yml"
|
78
78
|
- CHANGELOG.md
|
79
79
|
- Gemfile
|
80
|
+
- LICENSE
|
80
81
|
- README.md
|
81
82
|
- Rakefile
|
82
83
|
- bin/console
|
83
84
|
- bin/setup
|
85
|
+
- example/process.rb
|
86
|
+
- example/reentrant.rb
|
87
|
+
- example/thread.rb
|
84
88
|
- lib/reentrant_flock.rb
|
85
89
|
- lib/reentrant_flock/version.rb
|
86
90
|
- reentrant_flock.gemspec
|
87
91
|
homepage: https://github.com/sonots/reentrant_flock
|
88
|
-
licenses:
|
92
|
+
licenses:
|
93
|
+
- MIT
|
89
94
|
metadata: {}
|
90
95
|
post_install_message:
|
91
96
|
rdoc_options: []
|