Created
September 18, 2019 08:55
-
-
Save kares/d2d27d3c1961639bebe6eb12cf8b5c9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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