luajit / physicsfs互斥锁死锁

时间:2015-02-12 19:43:29

标签: lua pthreads mutex deadlock luajit

我已获得以下代码:

local M=ffi.load "physfs"
ffi.cdef [[ //basically the preprocessed content of physfs.h, see http://icculus.org/physfs/docs/html/physfs_8h.html ]]
M.PHYSFS_init(arg[0])
M.PHYSFS_setSaneConfig("a","b","zip",0,0)

function file2str(path)
  local cpath=ffi.cast("const char *",path)
  print(1) --debug
  if M.PHYSFS_exists(cpath)==0 then return nil,"file not found" end
  print(2) --debug
  -- some more magic
end
assert(file2str("someFile.txt"))

调用时,我希望调试输出1和2,或者至少是断言触发,但我只得到:

1
["endless" (i pressed ^C after about a minute) freeze]

当我终于让luajit在gdb中运行时,这是冻结时的回溯:

(gdb) bt
#0  0x00007ffff37a5c40 in __pause_nocancel ()
    at ../sysdeps/unix/syscall-template.S:81
#1  0x00007ffff379bce6 in __pthread_mutex_lock_full (mutex=0x68cbf0)
    at ../nptl/pthread_mutex_lock.c:354
#2  0x00007ffff606951f in __PHYSFS_platformGrabMutex (mutex=0x68cbf0)
    at /home/kyra/YDist/src/physfs-2.0.3/platform/unix.c:403
#3  0x00007ffff606410d in PHYSFS_getWriteDir ()
    at /home/kyra/YDist/src/physfs-2.0.3/physfs.c:913
#4  0x000000000045482b in ?? ()
#5  0x000000000043a829 in ?? ()
#6  0x000000000043af17 in ?? ()
#7  0x00000000004526a6 in ?? ()
#8  0x0000000000446fb0 in lua_pcall ()
#9  0x00000000004047dc in _start ()

所以在我看来有些东西阻塞了互斥锁,这有点奇怪,因为,虽然有两个线程正在运行,但只有一个线程触及了physfs(第二个线程甚至没有ffi.load "physfs"

我能做什么/应该做什么?

1 个答案:

答案 0 :(得分:1)

我仍然不知道到底发生了什么,但在尝试进一步调试gdb中的互斥锁时LD_PRELOAD编辑libpthread.so到gdb进程,突然它起作用了。< / p>

然后我尝试将它预加载到没有gdb的luajit,也可以。

然后我进一步挖掘了physfs和lualanes(这是我用于线程的pthread ffi包装器),发现他们都尝试加载libpthread(如果尚未加载),但来自C和lualanes的物理使用ffi,以某种方式看不到由physfs加载的那个,并且该过程以加载的库的2个副本结束。

因此修复是在ffi.load"pthread"之前明确地执行ffi.load"physfs",因为虽然通道看不到physfs加载的版本,但是physfs对我们加载的版本感到满意,并且没有尝试再次加载它,而luajit ffi忽略了由车道进行的进一步加载尝试。