Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc: dont ffi.load if symbol is already loaded #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 38 additions & 38 deletions lib/resty/signal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,49 @@ local new_tab = base.new_tab
local error = error


local load_shared_lib
do
local string_gmatch = string.gmatch
local string_match = string.match
local io_open = io.open
local io_close = io.close

local cpath = package.cpath

function load_shared_lib(so_name)
local tried_paths = new_tab(32, 0)
local i = 1

for k, _ in string_gmatch(cpath, "[^;]+") do
local fpath = string_match(k, "(.*/)")
fpath = fpath .. so_name
-- Don't get me wrong, the only way to know if a file exist is
-- trying to open it.
local f = io_open(fpath)
if f ~= nil then
io_close(f)
return ffi.load(fpath)
end

tried_paths[i] = fpath
i = i + 1
end
ffi.cdef[[
int resty_signal_signum(int num);
]]

return nil, tried_paths
end -- function
end -- do

local resty_signal
if not pcall(function() resty_signal = C.resty_signal_signum and C end) then
local string_gmatch = string.gmatch
local string_match = string.match
local io_open = io.open
local io_close = io.close

local resty_signal, tried_paths = load_shared_lib("librestysignal.so")
if not resty_signal then
error("could not load librestysignal.so from the following paths:\n" ..
table.concat(tried_paths, "\n"), 2)
end
local cpath = package.cpath
local tried_paths = new_tab(32, 0)

local function load_shared_lib(so_name)
local i = 1

ffi.cdef[[
int resty_signal_signum(int num);
]]
for k, _ in string_gmatch(cpath, "[^;]+") do
local fpath = string_match(k, "(.*/)")
fpath = fpath .. so_name
-- Don't get me wrong, the only way to know if a file exist is
-- trying to open it.
local f = io_open(fpath)
if f ~= nil then
io_close(f)
return ffi.load(fpath)
end

tried_paths[i] = fpath
i = i + 1
end

return nil, tried_paths
end -- function

resty_signal, tried_paths = load_shared_lib("librestysignal.so")

if not resty_signal then
error("could not load librestysignal.so from the following paths:\n" ..
table.concat(tried_paths, "\n"), 2)
end
end


if not pcall(function () return C.kill end) then
Expand Down