futex 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -0
- data/README.md +5 -1
- data/futex.gemspec +1 -1
- data/lib/futex.rb +5 -2
- data/test/test_futex.rb +4 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53e0b207143da0366545baa93f2801b8e79a5947682954191b9817e8d3c6fc9b
|
4
|
+
data.tar.gz: 5dac5eaab880bf15c6da9e7300fe2b7722d16c8e6c308cabe96372e8db32cce5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec18ba0e63dd2584f9294e1b60d37a1c338029b6d1fba0f88c4e4ba1dd0a920442522b81ba307688340413038c5e01d2bc007a9360999b82d6fe8a1830df45ba
|
7
|
+
data.tar.gz: c1b855e96480add275103776b65e0bad32853292f5c1ee7cb21bb7c5be9b1c220f927b8e7ebb12d499922e296fd5baae87703cb97c37e5c27499bfadb377bcc4
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -34,12 +34,16 @@ constructor of the `Futex` class, including:
|
|
34
34
|
* `log`: an object that implements `debug()` method, which will
|
35
35
|
receive supplementary messages from the locking mechanism;
|
36
36
|
|
37
|
+
* `logging`: set it to `false` if you don't want to see any logs;
|
38
|
+
|
37
39
|
* `timeout`: the number of seconds to wait for the lock availability
|
38
40
|
(an exception is raised when the wait is expired);
|
39
41
|
|
40
42
|
* `sleep`: the number of seconds to wait between attempts to acquire
|
41
43
|
the lock file (the smaller the number, the more reactive is the software,
|
42
|
-
but the higher the load for the file system and the CPU)
|
44
|
+
but the higher the load for the file system and the CPU);
|
45
|
+
|
46
|
+
* `lock`: the absolute path of the lock file;
|
43
47
|
|
44
48
|
That's it.
|
45
49
|
|
data/futex.gemspec
CHANGED
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
|
|
31
31
|
s.rubygems_version = '2.3.3'
|
32
32
|
s.required_ruby_version = '>=2.3'
|
33
33
|
s.name = 'futex'
|
34
|
-
s.version = '0.
|
34
|
+
s.version = '0.2.0'
|
35
35
|
s.license = 'MIT'
|
36
36
|
s.summary = 'File-based mutex'
|
37
37
|
s.description = 'Ruby Gem for file-based locking'
|
data/lib/futex.rb
CHANGED
@@ -31,11 +31,13 @@ require 'time'
|
|
31
31
|
# License:: MIT
|
32
32
|
class Futex
|
33
33
|
def initialize(path, log: STDOUT, timeout: 16, sleep: 0.005,
|
34
|
-
lock: path + '.lock')
|
34
|
+
lock: path + '.lock', logging: true)
|
35
35
|
raise "File path can't be nil" if path.nil?
|
36
36
|
@path = path
|
37
37
|
raise "Log can't be nil" if log.nil?
|
38
38
|
@log = log
|
39
|
+
raise "Logging can't be nil" if logging.nil?
|
40
|
+
@logging = logging
|
39
41
|
raise "Timeout can't be nil" if timeout.nil?
|
40
42
|
raise "Timeout must be positive: #{timeout}" unless timeout.positive?
|
41
43
|
@timeout = timeout
|
@@ -75,7 +77,7 @@ exclusive access to #{@path}, #{age(start)} already: #{IO.read(@lock)}")
|
|
75
77
|
acq = Time.now
|
76
78
|
res = yield(@path)
|
77
79
|
FileUtils.rm(@lock)
|
78
|
-
|
80
|
+
debug("Unlocked by \"#{Thread.current.name}\" in #{age(acq)}: #{@path}")
|
79
81
|
res
|
80
82
|
end
|
81
83
|
|
@@ -86,6 +88,7 @@ exclusive access to #{@path}, #{age(start)} already: #{IO.read(@lock)}")
|
|
86
88
|
end
|
87
89
|
|
88
90
|
def debug(msg)
|
91
|
+
return unless @logging
|
89
92
|
if @log.respond_to?(:debug)
|
90
93
|
@log.debug(msg)
|
91
94
|
elsif @log.respond_to?(:puts)
|
data/test/test_futex.rb
CHANGED
@@ -36,7 +36,7 @@ class FutexTest < Minitest::Test
|
|
36
36
|
Dir.mktmpdir do |dir|
|
37
37
|
path = File.join(dir, 'a/b/c/file.txt')
|
38
38
|
Threads.new(5).assert(100) do |_, r|
|
39
|
-
Futex.new(path).open do |f|
|
39
|
+
Futex.new(path, logging: false).open do |f|
|
40
40
|
text = "op no.#{r}"
|
41
41
|
IO.write(f, text)
|
42
42
|
assert_equal(text, IO.read(f))
|
@@ -48,11 +48,11 @@ class FutexTest < Minitest::Test
|
|
48
48
|
def test_syncs_access_to_file_in_slow_motion
|
49
49
|
Dir.mktmpdir do |dir|
|
50
50
|
path = File.join(dir, 'a/b/c/file.txt')
|
51
|
-
Threads.new(
|
52
|
-
Futex.new(path).open do |f|
|
51
|
+
Threads.new(5).assert(50) do |_, r|
|
52
|
+
Futex.new(path, logging: false).open do |f|
|
53
53
|
text = "op no.#{r}"
|
54
54
|
IO.write(f, text)
|
55
|
-
sleep 0.
|
55
|
+
sleep 0.01
|
56
56
|
assert_equal(text, IO.read(f))
|
57
57
|
end
|
58
58
|
end
|