flock_mutex 1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 888a6f198752055fc6bc7c46e885ebfcc21ac349
4
+ data.tar.gz: 333e3670b48a3017c70e5930d1a15c32addd6fc6
5
+ SHA512:
6
+ metadata.gz: 27a764e5620944c9caae5c6ca482c923553c52fbeff737758f5ea84c3438ace2712ea11e91aed21da76d09550349ccc712a938b185a3829528caa88e99fcdb79
7
+ data.tar.gz: a17a64fd1772525f96f64ef497e7e999b7d5695b3f5138dfec103026fd513d7588de730c5f225bd3e9de6b102b91f7de5f5301f399e684db6ffba16a5eede121
@@ -0,0 +1,2 @@
1
+ .DS_Store
2
+ pkg/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format=nested
3
+ --backtrace
@@ -0,0 +1,35 @@
1
+ # FlockMutex
2
+
3
+ A very simple ruby mutex class using file lock.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ require "flock_mutex"
9
+
10
+ mutex = FlockMutex.new("/tmp/test.lock")
11
+ mutex.lock
12
+ mutex.unlock
13
+ mutex.locked?
14
+ mutex.synchronize {
15
+ #...
16
+ }
17
+ ```
18
+
19
+ ## Examples
20
+
21
+ ```ruby
22
+ # my_app.rb
23
+
24
+ mutex = FlockMutex.new("/tmp/my_app.lock")
25
+
26
+ if mutex.locked?
27
+ puts "Already running"
28
+ exit false
29
+ end
30
+ ```
31
+
32
+ ## Install
33
+
34
+ gem install flock_mutex
35
+
@@ -0,0 +1,28 @@
1
+ class FlockMutex
2
+ def initialize(path)
3
+ @file = File.open(path, 'a')
4
+ end
5
+
6
+ def lock
7
+ @file.flock(File::LOCK_EX)
8
+ self
9
+ end
10
+
11
+ def unlock
12
+ @file.flock(File::LOCK_UN)
13
+ self
14
+ end
15
+
16
+ def synchronize
17
+ lock
18
+ yield
19
+ ensure
20
+ unlock
21
+ end
22
+
23
+ def locked?
24
+ File.open(@file.path, 'a') do |f|
25
+ ! f.flock(File::LOCK_EX | File::LOCK_NB)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,46 @@
1
+ require "flock_mutex"
2
+
3
+ describe FlockMutex do
4
+
5
+ it "test locking" do
6
+ m1 = FlockMutex.new("test.lock")
7
+ m2 = FlockMutex.new("test.lock")
8
+
9
+ m1.locked?.should be false
10
+ m2.locked?.should be false
11
+
12
+ m1.lock
13
+
14
+ m1.locked?.should be true
15
+ m2.locked?.should be true
16
+
17
+ m1.unlock
18
+
19
+ m1.locked?.should be false
20
+ m2.locked?.should be false
21
+
22
+ m1.lock
23
+
24
+ my_var = 1
25
+
26
+ thread = Thread.new do
27
+ my_var = m2.synchronize { 2 }
28
+ end
29
+
30
+ my_var.should be 1
31
+
32
+ m1.unlock
33
+
34
+ thread.join
35
+
36
+ my_var.should be 2
37
+
38
+ m1.locked?.should be false
39
+ m2.locked?.should be false
40
+ end
41
+
42
+ after do
43
+ File.delete("test.lock") rescue nil
44
+ end
45
+
46
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flock_mutex
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Benoit MARTIN-CHAVE
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - benoitmartinchave@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - README.md
23
+ - lib/flock_mutex.rb
24
+ - spec/flock_mutex_spec.rb
25
+ homepage: http://github.com/BenoitMC/flock_mutex
26
+ licenses:
27
+ - WTFPL
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.2.2
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: A very simple ruby mutex class using file lock.
49
+ test_files: []