Skip to content

Instantly share code, notes, and snippets.

@kares
Created September 18, 2019 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kares/d2d27d3c1961639bebe6eb12cf8b5c9b to your computer and use it in GitHub Desktop.
Save kares/d2d27d3c1961639bebe6eb12cf8b5c9b to your computer and use it in GitHub Desktop.
require 'ffi'
module MemoryLock
extend FFI::Library
ffi_lib FFI::Library::LIBC
# int mlockall(int flags);
attach_function :mlockall, [:int], :int
# int munlockall(void);
attach_function :munlockall, [], :int
MCL_CURRENT = 1
#MCL_FUTURE = 2
def self.lock!
ret = MemoryLock.mlockall MemoryLock::MCL_CURRENT
if ret.zero?
warn "mlockall locked current process #{$$}"
else
err = SystemCallError.new('mlockall', FFI.errno)
warn "mlockall failed with #{err.inspect} (#{FFI.errno})"
end
ret
end
def self.unlock!
ret = MemoryLock.munlockall
if ret.zero?
warn "munlockall un-locked current process #{$$}"
else
err = SystemCallError.new('munlockall', FFI.errno)
warn "munlockall failed with #{err.inspect} (#{FFI.errno})"
end
ret
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment