futex 0.6.3 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/futex.gemspec +1 -1
- data/lib/futex.rb +18 -3
- data/test/test_futex.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6b80bd02f4bb99bf83264c534c6c9f13265e11acd484cb389f75fbfbc809650
|
4
|
+
data.tar.gz: a599ea5bd0024323fbaed93313985e424b16c8d2468a44809e2626281c1b86ff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d2fd4b4c45ac7d182e76c3f32bc727554fb426198c18d6483d80f8eb954d6a2e233f26032a1b3cbddc1c412ec2b5cb927bcbd727f40b7c1134b7f12ab961485
|
7
|
+
data.tar.gz: cacd448755258045d862c633bab834440f80f21c5a37f9d341241b464f8a8f10f7643ac82d8a68969251c647ce12af78bd799e7a251407095b058bea300f09d3
|
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.7.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
@@ -34,7 +34,8 @@ require 'time'
|
|
34
34
|
# end
|
35
35
|
#
|
36
36
|
# The file <tt>/tmp/my-file.txt.lock<tt> will be created and
|
37
|
-
# used as an entrance lock.
|
37
|
+
# used as an entrance lock. If the file is already locked by another thread
|
38
|
+
# or another process, exception <tt>Futex::CantLock</tt> will be raised.
|
38
39
|
#
|
39
40
|
# If you are not planning to write to the file, to speed things up, you may
|
40
41
|
# want to get a non-exclusive access to it, by providing <tt>false</tt> to
|
@@ -52,6 +53,10 @@ require 'time'
|
|
52
53
|
# Copyright:: Copyright (c) 2018 Yegor Bugayenko
|
53
54
|
# License:: MIT
|
54
55
|
class Futex
|
56
|
+
# Exception that is raised when we can't lock because of some other
|
57
|
+
# process that is holding the lock now.
|
58
|
+
class CantLock < StandardError; end
|
59
|
+
|
55
60
|
# Creates a new instance of the class.
|
56
61
|
def initialize(path, log: STDOUT, timeout: 16, sleep: 0.005,
|
57
62
|
lock: path + '.lock', logging: false)
|
@@ -63,7 +68,17 @@ class Futex
|
|
63
68
|
@lock = lock
|
64
69
|
end
|
65
70
|
|
66
|
-
# Open the file.
|
71
|
+
# Open the file. By default the file will be locked for exclusive access,
|
72
|
+
# which means that absolutely no other process will be able to do the same.
|
73
|
+
# This type of access (exclusive) is supposed to be used when you are
|
74
|
+
# making changes to the file. However, very often you may need just to
|
75
|
+
# read it and it's OK to let many processes do the reading at the same time,
|
76
|
+
# provided none of them do the writing. In that case you should call this
|
77
|
+
# method <tt>open()</tt> with <tt>false</tt> first argument, which will mean
|
78
|
+
# "shared" access. Many threads and processes may have shared access to the
|
79
|
+
# same lock file, but they all will stop and wait if one of them will require
|
80
|
+
# an "exclusive" access. This mechanism is inherited from POSIX, read about
|
81
|
+
# it <a href="http://man7.org/linux/man-pages/man2/flock.2.html">here</a>.
|
67
82
|
def open(exclusive = true)
|
68
83
|
FileUtils.mkdir_p(File.dirname(@lock))
|
69
84
|
step = (1 / @sleep).to_i
|
@@ -85,7 +100,7 @@ class Futex
|
|
85
100
|
Thread.current.thread_variable_set(:futex_cycle, cycle)
|
86
101
|
Thread.current.thread_variable_set(:futex_time, Time.now - start)
|
87
102
|
if Time.now - start > @timeout
|
88
|
-
raise "#{b} can't get #{prefix}exclusive access \
|
103
|
+
raise CantLock, "#{b} can't get #{prefix}exclusive access \
|
89
104
|
to the file #{@path} because of the lock at #{@lock}, after #{age(start)} \
|
90
105
|
of waiting: #{IO.read(@lock)} (modified #{age(File.mtime(@lock))} ago)"
|
91
106
|
end
|
data/test/test_futex.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: futex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yegor Bugayenko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-12-
|
11
|
+
date: 2018-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|