Class: Tlopo::Futex
- Inherits:
-
Object
- Object
- Tlopo::Futex
- Defined in:
- lib/tlopo/futex.rb,
lib/tlopo/futex/version.rb
Constant Summary collapse
- VERSION =
"0.1.1"
Instance Method Summary collapse
-
#initialize(path) ⇒ Futex
constructor
A new instance of Futex.
- #lock ⇒ Object
- #locked? ⇒ Boolean
- #release ⇒ Object
- #synchronize ⇒ Object
Constructor Details
#initialize(path) ⇒ Futex
Returns a new instance of Futex.
7 8 9 10 |
# File 'lib/tlopo/futex.rb', line 7 def initialize(path) @path = path @fh = nil end |
Instance Method Details
#lock ⇒ Object
19 20 21 22 23 |
# File 'lib/tlopo/futex.rb', line 19 def lock fh = File.open(@path, File::RDWR | File::CREAT, 0o644) fh.flock File::LOCK_EX @fh = fh end |
#locked? ⇒ Boolean
12 13 14 15 16 17 |
# File 'lib/tlopo/futex.rb', line 12 def locked? fh = File.open(@path, File::RDWR | File::CREAT, 0o644) r = fh.flock(File::LOCK_EX | File::LOCK_NB) fh.close r != 0 end |
#release ⇒ Object
25 26 27 28 29 |
# File 'lib/tlopo/futex.rb', line 25 def release @fh.flock File::LOCK_UN @fh.close rescue IOError end |
#synchronize ⇒ Object
31 32 33 34 35 36 |
# File 'lib/tlopo/futex.rb', line 31 def synchronize lock yield ensure release end |