Skip to content

Commit

Permalink
修复线程并发问题
Browse files Browse the repository at this point in the history
  • Loading branch information
vimfung committed Apr 9, 2019
1 parent a18ab65 commit e14e2ae
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@ public void addSearchPath (String path)
String regExp = "/([^/]+)[.]([^/]+)$";
if (!Pattern.matches(regExp, path))
{
regExp = "/$";
if (Pattern.matches(regExp, path))
if (!path.endsWith("/"))
{
path = path + "/";
}
Expand Down
5 changes: 3 additions & 2 deletions Source/iOS_OSX/Code/LSCContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ - (instancetype)initWithConfig:(LSCConfig *)config

//设置搜索路径
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
[self addSearchPath:resourcePath];
[self addSearchPath:[resourcePath stringByAppendingString:@"/"]];

//初始化数据交换器
self.dataExchanger = [[LSCDataExchanger alloc] initWithContext:self];
Expand Down Expand Up @@ -444,10 +444,11 @@ - (void)runThreadWithFunction:(LSCFunction *)function
{
//创建一个协程状态
__weak typeof(self) theContext = self;
LSCCoroutine *coroutine = [[LSCCoroutine alloc] initWithContext:theContext];

dispatch_queue_t queue = dispatch_queue_create("ThreadQueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{

LSCCoroutine *coroutine = [[LSCCoroutine alloc] initWithContext:theContext];
lua_State *state = coroutine.state;

coroutine.scriptController = scriptController;
Expand Down
45 changes: 8 additions & 37 deletions Source/lua-common/LuaContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ static void _raiseLuaException(lua_State *state, void *ud)
* @param arguments 参数列表
* @param scriptController 脚本控制器
*/
static void threadHandler(LuaContext *context, LuaFunction *handler, LuaArgumentList *arguments, LuaScriptController *scriptController)
static void threadHandler(LuaCoroutine *coroutine, LuaFunction *handler, LuaArgumentList *arguments, LuaScriptController *scriptController)
{
LuaCoroutine *coroutine = new LuaCoroutine(context);
lua_State *state = coroutine -> getState();

//设置脚本执行配置
Expand All @@ -85,7 +84,7 @@ static void threadHandler(LuaContext *context, LuaFunction *handler, LuaArgument
}

int top = LuaEngineAdapter::getTop(state);
context -> getDataExchanger() -> getLuaObject(handler, state, NULL);
coroutine -> getContext() -> getDataExchanger() -> getLuaObject(handler, state, NULL);

if (LuaEngineAdapter::isFunction(state, -1))
{
Expand All @@ -95,7 +94,7 @@ static void threadHandler(LuaContext *context, LuaFunction *handler, LuaArgument
for (LuaArgumentList::iterator i = arguments -> begin(); i != arguments -> end() ; ++i)
{
LuaValue *item = *i;
context -> getDataExchanger() -> pushStack(item);
coroutine -> getContext() -> getDataExchanger() -> pushStack(item);
}

if (LuaEngineAdapter::pCall(state, (int)arguments -> size(), LUA_MULTRET, errFuncIndex) == LUA_OK)
Expand Down Expand Up @@ -129,7 +128,7 @@ static void threadHandler(LuaContext *context, LuaFunction *handler, LuaArgument
delete arguments;

//释放内存
context -> gc();
coroutine -> getContext() -> gc();

//取消设置脚本执行配置
coroutine -> setScriptController(NULL);
Expand Down Expand Up @@ -327,37 +326,6 @@ LuaContext::LuaContext(std::string const& platform)
registerMethod(CatchLuaExceptionHandlerName, catchLuaExceptionHandler);
}

//LuaContext::LuaContext(std::string const& platform, std::function<lua_State*(void)> const& createStateHandler)
// : LuaObject()
//{
// _operationQueue = new LuaOperationQueue();
//
// _isActive = true;
// _exceptionHandler = NULL;
// _dataExchanger = new LuaDataExchanger(this);
//
// _operationQueue -> performAction([this, createStateHandler]() {
//
// lua_State *state = createStateHandler();
//
// LuaEngineAdapter::GC(state, LUA_GCSTOP, 0);
// //加载标准库
// LuaEngineAdapter::openLibs(state);
// LuaEngineAdapter::GC(state, LUA_GCRESTART, 0);
//
// _mainSession = new LuaSession(state, this, false);
//
// });
//
// _currentSession = NULL;
//
// //初始化类型导出管理器
// _exportsTypeManager = new LuaExportsTypeManager(this, platform);
//
// //注册错误捕获方法
// registerMethod(CatchLuaExceptionHandlerName, catchLuaExceptionHandler);
//}

LuaContext::~LuaContext()
{
_isActive = false; //标记Context已经销毁
Expand Down Expand Up @@ -790,6 +758,9 @@ void LuaContext::runThread(LuaFunction *handler, LuaArgumentList *arguments)

void LuaContext::runThread(LuaFunction *handler, LuaArgumentList *arguments, LuaScriptController *scriptController)
{
//创建协程
LuaCoroutine *coroutine = new LuaCoroutine(this);

//赋值参数列表
LuaArgumentList *args = new LuaArgumentList();
for (LuaArgumentList::iterator i = arguments -> begin(); i != arguments -> end() ; ++i)
Expand All @@ -799,7 +770,7 @@ void LuaContext::runThread(LuaFunction *handler, LuaArgumentList *arguments, Lua
args -> push_back(item);
}

std::thread t(&threadHandler, this, handler, args, scriptController);
std::thread t(&threadHandler, coroutine, handler, args, scriptController);
t.detach();
}

Expand Down

0 comments on commit e14e2ae

Please sign in to comment.