Skip to content

Commit 67f74af

Browse files
committed
锁的粒度改为从进入invoke session到退出
1 parent 6a5ba40 commit 67f74af

File tree

2 files changed

+58
-68
lines changed

2 files changed

+58
-68
lines changed

Assets/XLua/Src/LuaEnv.cs

+17-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public class LuaEnv : IDisposable
3232

3333
internal int errorFuncRef = -1;
3434

35+
#if THREAD_SAFT || HOTFIX_ENABLE
3536
internal object luaEnvLock = new object();
37+
#endif
3638

3739
public LuaEnv()
3840
{
@@ -342,16 +344,23 @@ public virtual void Dispose(bool dispose)
342344

343345
public void ThrowExceptionFromError(int oldTop)
344346
{
345-
object err = translator.GetObject(L, -1);
346-
LuaAPI.lua_settop(L, oldTop);
347+
#if THREAD_SAFT || HOTFIX_ENABLE
348+
lock (luaEnvLock)
349+
{
350+
#endif
351+
object err = translator.GetObject(L, -1);
352+
LuaAPI.lua_settop(L, oldTop);
347353

348-
// A pre-wrapped exception - just rethrow it (stack trace of InnerException will be preserved)
349-
Exception ex = err as Exception;
350-
if (ex != null) throw ex;
354+
// A pre-wrapped exception - just rethrow it (stack trace of InnerException will be preserved)
355+
Exception ex = err as Exception;
356+
if (ex != null) throw ex;
351357

352-
// A non-wrapped Lua error (best interpreted as a string) - wrap it and throw it
353-
if (err == null) err = "Unknown Lua Error";
354-
throw new LuaException(err.ToString());
358+
// A non-wrapped Lua error (best interpreted as a string) - wrap it and throw it
359+
if (err == null) err = "Unknown Lua Error";
360+
throw new LuaException(err.ToString());
361+
#if THREAD_SAFT || HOTFIX_ENABLE
362+
}
363+
#endif
355364
}
356365

357366
internal struct GCAction

Assets/XLua/Src/LuaFunction.cs

+41-60
Original file line numberDiff line numberDiff line change
@@ -238,70 +238,57 @@ public override string ToString()
238238

239239
public void InvokeSessionStart()
240240
{
241-
lock (luaEnv.luaEnvLock)
242-
{
243-
var L = luaEnv.L;
244-
_stack.Push(_oldTop);
245-
_oldTop = LuaAPI.lua_gettop(L);
246-
LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
247-
LuaAPI.lua_getref(L, luaReference);
248-
}
241+
System.Threading.Monitor.Enter(luaEnv.luaEnvLock);
242+
var L = luaEnv.L;
243+
_stack.Push(_oldTop);
244+
_oldTop = LuaAPI.lua_gettop(L);
245+
LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
246+
LuaAPI.lua_getref(L, luaReference);
249247
}
250248

251249
public void Invoke(int nRet)
252250
{
253-
lock (luaEnv.luaEnvLock)
251+
int error = LuaAPI.lua_pcall(luaEnv.L, LuaAPI.lua_gettop(luaEnv.L) - _oldTop - 2, nRet, _oldTop + 1);
252+
if (error != 0)
254253
{
255-
int error = LuaAPI.lua_pcall(luaEnv.L, LuaAPI.lua_gettop(luaEnv.L) - _oldTop - 2, nRet, _oldTop + 1);
256-
if (error != 0)
257-
{
258-
var lastOldTop = _oldTop;
259-
InvokeSessionEnd();
260-
luaEnv.ThrowExceptionFromError(lastOldTop);
261-
}
254+
var lastOldTop = _oldTop;
255+
InvokeSessionEnd();
256+
luaEnv.ThrowExceptionFromError(lastOldTop);
262257
}
263258
}
264259

265260
public void InvokeSessionEnd()
266261
{
267-
lock (luaEnv.luaEnvLock)
268-
{
269-
LuaAPI.lua_settop(luaEnv.L, _oldTop);
270-
}
262+
LuaAPI.lua_settop(luaEnv.L, _oldTop);
271263
_oldTop = _stack.Pop();
264+
System.Threading.Monitor.Exit(luaEnv.luaEnvLock);
272265
}
273266

274267
public TResult InvokeSessionEndWithResult<TResult>()
275268
{
276-
lock (luaEnv.luaEnvLock)
269+
if (LuaAPI.lua_gettop(luaEnv.L) < _oldTop + 2)
277270
{
278-
if (LuaAPI.lua_gettop(luaEnv.L) < _oldTop + 2)
279-
{
280-
InvokeSessionEnd();
281-
throw new InvalidOperationException("no result!");
282-
}
271+
InvokeSessionEnd();
272+
throw new InvalidOperationException("no result!");
273+
}
283274

284-
try
285-
{
286-
TResult ret;
287-
luaEnv.translator.Get(luaEnv.L, _oldTop + 2, out ret);
288-
return ret;
289-
}
290-
finally
291-
{
292-
InvokeSessionEnd();
293-
}
275+
try
276+
{
277+
TResult ret;
278+
luaEnv.translator.Get(luaEnv.L, _oldTop + 2, out ret);
279+
return ret;
280+
}
281+
finally
282+
{
283+
InvokeSessionEnd();
294284
}
295285
}
296286

297287
public void InParam<T>(T p)
298288
{
299289
try
300290
{
301-
lock (luaEnv.luaEnvLock)
302-
{
303-
luaEnv.translator.PushByType(luaEnv.L, p);
304-
}
291+
luaEnv.translator.PushByType(luaEnv.L, p);
305292
}
306293
catch (Exception e)
307294
{
@@ -314,12 +301,9 @@ public void InParams<T>(T[] ps)
314301
{
315302
try
316303
{
317-
lock (luaEnv.luaEnvLock)
304+
for (int i = 0; i < ps.Length; i++)
318305
{
319-
for (int i = 0; i < ps.Length; i++)
320-
{
321-
luaEnv.translator.PushByType<T>(luaEnv.L, ps[i]);
322-
}
306+
luaEnv.translator.PushByType<T>(luaEnv.L, ps[i]);
323307
}
324308
}
325309
catch (Exception e)
@@ -332,23 +316,20 @@ public void InParams<T>(T[] ps)
332316
//pos start from 0
333317
public void OutParam<TResult>(int pos, out TResult ret)
334318
{
335-
lock (luaEnv.luaEnvLock)
319+
if (LuaAPI.lua_gettop(luaEnv.L) < _oldTop + 2 + pos)
336320
{
337-
if (LuaAPI.lua_gettop(luaEnv.L) < _oldTop + 2 + pos)
338-
{
339-
InvokeSessionEnd();
340-
throw new InvalidOperationException("no result in " + pos);
341-
}
321+
InvokeSessionEnd();
322+
throw new InvalidOperationException("no result in " + pos);
323+
}
342324

343-
try
344-
{
345-
luaEnv.translator.Get(luaEnv.L, _oldTop + 2 + pos, out ret);
346-
}
347-
catch (Exception e)
348-
{
349-
InvokeSessionEnd();
350-
throw e;
351-
}
325+
try
326+
{
327+
luaEnv.translator.Get(luaEnv.L, _oldTop + 2 + pos, out ret);
328+
}
329+
catch (Exception e)
330+
{
331+
InvokeSessionEnd();
332+
throw e;
352333
}
353334
}
354335
#endif

0 commit comments

Comments
 (0)