Class: Tlopo::Futex

Inherits:
Object
  • Object
show all
Defined in:
lib/tlopo/futex.rb,
lib/tlopo/futex/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

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

#lockObject



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

Returns:

  • (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

#releaseObject



25
26
27
28
29
# File 'lib/tlopo/futex.rb', line 25

def release
  @fh.flock File::LOCK_UN
  @fh.close
  rescue IOError
end

#synchronizeObject



31
32
33
34
35
36
# File 'lib/tlopo/futex.rb', line 31

def synchronize
  lock
  yield
ensure
  release
end