futex 0.4.3 → 0.5.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: 047a5eec58f0138706f3e1ae934cccde006f2106a42859f6f0fce0371b7459fc
4
- data.tar.gz: 9bd6d01c2f45511ede96555fa20b0cf0d7426873cfef1a0a4ac645d78f6d38a8
3
+ metadata.gz: 92dabfba9a9b06fbe28ef35bc6add8eab32cb2720c4369b40bc2e491c6c3a6a0
4
+ data.tar.gz: 2a52f78833399ba74f218fcd9ab370703d781f455fd44500f8f3639ff6c2294a
5
5
  SHA512:
6
- metadata.gz: a959ddda978d3dc620b3dbcc5c2e780b6d47d1a9803a1c8aa4220037c7dca3319a703c5341b0f79407611ef775094f46f4fe9a5a6686a91ecb5867d1309c3929
7
- data.tar.gz: b3e53ad7e41c7c04554930515f49d1acd912f07f8103554fd399e299cbb31164e960f3e5531be8408ce93d9eecf8facfa92ebb1d5cc8895bae8120e6a4b94937
6
+ metadata.gz: ce76e8fc437c55952863c2509fd1c448577419542d9cec5d4c540545d8148ad3504d084b5488599633e8653d3060e84f64dab371ee8bd3e320fb97be6ae77062
7
+ data.tar.gz: 61896f299d5ab8b93bf066f7868a6ac67a68357d1e0fc5589b9aaffb8634b5fe46b114e9a0ab97d290c6aaec85b4250534fd632d10a3d770e3e51ad6e302be7f
data/.rubocop.yml CHANGED
@@ -9,7 +9,7 @@ Layout/MultilineMethodCallIndentation:
9
9
  Metrics/AbcSize:
10
10
  Max: 50
11
11
  Metrics/MethodLength:
12
- Max: 30
12
+ Max: 40
13
13
  Metrics/CyclomaticComplexity:
14
14
  Max: 10
15
15
  Metrics/PerceivedComplexity:
data/appveyor.yml CHANGED
@@ -15,18 +15,7 @@ environment:
15
15
  install:
16
16
  - ps: |
17
17
  $Env:PATH = "C:\Ruby${Env:ruby_version}\bin;${Env:PATH}"
18
- if ($Env:ruby_version -match "^23" ) {
19
- # RubyInstaller; download OpenSSL headers from OpenKnapsack Project
20
- $Env:openssl_dir = "C:\Ruby${Env:ruby_version}"
21
- appveyor DownloadFile http://dl.bintray.com/oneclick/OpenKnapsack/x64/openssl-1.0.2j-x64-windows.tar.lzma
22
- 7z e openssl-1.0.2j-x64-windows.tar.lzma
23
- 7z x -y -oC:\Ruby${Env:ruby_version} openssl-1.0.2j-x64-windows.tar
24
- } else {
25
- # RubyInstaller2; openssl package seems to be installed already
26
- $Env:openssl_dir = "C:\msys64\mingw64"
27
- }
28
18
  - bundle config --local path vendor/bundle
29
- - bundle config build.openssl --with-openssl-dir=%openssl_dir%
30
19
  - ruby -v
31
20
  - bundle -v
32
21
  build_script:
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.4.3'
34
+ s.version = '0.5.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
@@ -70,6 +70,8 @@ class Futex
70
70
  start = Time.now
71
71
  prefix = exclusive ? '' : 'non-'
72
72
  b = badge(exclusive)
73
+ Thread.current.thread_variable_set(:futex_lock, @lock)
74
+ Thread.current.thread_variable_set(:futex_badge, b)
73
75
  File.open(@lock, File::CREAT | File::RDWR) do |f|
74
76
  cycle = 0
75
77
  loop do
@@ -78,6 +80,8 @@ class Futex
78
80
  end
79
81
  sleep(@sleep)
80
82
  cycle += 1
83
+ Thread.current.thread_variable_set(:futex_cycle, cycle)
84
+ Thread.current.thread_variable_set(:futex_time, Time.now - start)
81
85
  if Time.now - start > @timeout
82
86
  raise "#{b} can't get #{prefix}exclusive access \
83
87
  to the file #{@path} because of the lock at #{@lock}, after #{age(start)} \
@@ -107,7 +111,12 @@ access to #{@path}, #{age(start)} already: #{IO.read(@lock)}")
107
111
  end
108
112
 
109
113
  def age(time)
110
- "#{((Time.now - time) * 1000).round}ms"
114
+ sec = Time.now - time
115
+ return "#{(sec * 1_000_000).round}μs" if sec < 0.001
116
+ return "#{(sec * 1000).round}ms" if sec < 1
117
+ return "#{sec.round(2)}s" if sec < 60
118
+ return "#{(sec / 60).round}m" if sec < 60 * 60
119
+ "#{(sec / 3600).round}h"
111
120
  end
112
121
 
113
122
  def debug(msg)
data/test/test_futex.rb CHANGED
@@ -47,6 +47,19 @@ class FutexTest < Minitest::Test
47
47
  end
48
48
  end
49
49
 
50
+ def test_syncs_read_only_access_to_file
51
+ Dir.mktmpdir do |dir|
52
+ path = File.join(dir, 'file.txt')
53
+ text = 'Hello, world!'
54
+ IO.write(path, text)
55
+ Threads.new(2).assert do
56
+ Futex.new(path).open(false) do |f|
57
+ assert_equal(text, IO.read(f))
58
+ end
59
+ end
60
+ end
61
+ end
62
+
50
63
  def test_syncs_access_to_file_in_slow_motion
51
64
  Dir.mktmpdir do |dir|
52
65
  path = File.join(dir, 'a/b/c/file.txt')
@@ -113,4 +126,18 @@ class FutexTest < Minitest::Test
113
126
  assert_equal(2, Dir.new(dir).count)
114
127
  end
115
128
  end
129
+
130
+ def test_sets_thread_vars
131
+ Dir.mktmpdir do |dir|
132
+ Futex.new(File.join(dir, 'hey.txt')).open do |f|
133
+ assert_equal(
134
+ "#{f}.lock",
135
+ Thread.current.thread_variable_get(:futex_lock)
136
+ )
137
+ assert(
138
+ Thread.current.thread_variable_get(:futex_badge).end_with?('-ex/nil')
139
+ )
140
+ end
141
+ end
142
+ end
116
143
  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.4.3
4
+ version: 0.5.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-11-22 00:00:00.000000000 Z
11
+ date: 2018-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest