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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e89a9a954b7117d46792c48cbd5b5c123a33050321266028f70bd2fd3df0702a
4
- data.tar.gz: 4bc7abbb6b455e073bc9fb0c5f69612ce2bcc5fc9c09e2fdf651d57af5b551a5
3
+ metadata.gz: b6b80bd02f4bb99bf83264c534c6c9f13265e11acd484cb389f75fbfbc809650
4
+ data.tar.gz: a599ea5bd0024323fbaed93313985e424b16c8d2468a44809e2626281c1b86ff
5
5
  SHA512:
6
- metadata.gz: f96770a733ecc3661551ddb1d99b9098610176236a06c8dcb7001f8dce2476f28cabc58f2ac1b2ff7eb4bbb061e6fb0885b3b63711f7165c14273cb283601b5c
7
- data.tar.gz: 6a9700fef33eebaceae3caa7d07d0f7fa494a2b0cfc02187ffd5362b79bf0dd4c74c3a70d2bac8cc90e44350f73dc0c0b69992f62510d7b349e5011ce790e8a4
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.6.3'
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
@@ -83,7 +83,7 @@ class FutexTest < Minitest::Test
83
83
  end
84
84
  end
85
85
  sleep 0.1
86
- ex = assert_raises do
86
+ ex = assert_raises(Futex::CantLock) do
87
87
  Futex.new(path, timeout: 0.1).open do |f|
88
88
  # Will never reach this point
89
89
  end
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.6.3
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-04 00:00:00.000000000 Z
11
+ date: 2018-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest