From 661ffca4a01883bb354e52624bed6d18be977058 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Mon, 7 Dec 2015 18:19:00 +0000 Subject: MirLua: tasks should run in thread git-svn-id: http://svn.miranda-ng.org/main/trunk@15828 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/src/m_schedule.cpp | 46 ++++++++++++++++++++++++++------------- plugins/MirLua/src/mlua.cpp | 1 + 2 files changed, 32 insertions(+), 15 deletions(-) (limited to 'plugins/MirLua/src') diff --git a/plugins/MirLua/src/m_schedule.cpp b/plugins/MirLua/src/m_schedule.cpp index 8d2330f626..ca9a04e271 100644 --- a/plugins/MirLua/src/m_schedule.cpp +++ b/plugins/MirLua/src/m_schedule.cpp @@ -23,12 +23,39 @@ static int TaskCompare(const ScheduleTask *p1, const ScheduleTask *p2) static LIST tasks(1, TaskCompare); -void DestroyScheduleTask(ScheduleTask *task) +void DestroyTask(ScheduleTask *task) { luaL_unref(task->L, LUA_REGISTRYINDEX, task->threadRef); delete task; } +void ExecuteTaskThread(void *arg) +{ + ScheduleTask *task = (ScheduleTask*)arg; + + lua_rawgeti(task->T, LUA_REGISTRYINDEX, task->callbackRef); + if (lua_pcall(task->T, 0, 2, 0)) + CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(task->T, -1)); + + if (task->interval == 0) + { + DestroyTask(task); + return; + } + + { + mir_cslock lock(threadLock); + + time_t timestamp = time(NULL); + if(task->startTime + task->interval >= timestamp) + task->startTime += task->interval; + else + task->startTime = timestamp + task->interval; + tasks.insert(task); + } + SetEvent(hScheduleEvent); +} + void ScheduleThread(void*) { time_t waitTime = INFINITE; @@ -52,22 +79,11 @@ wait: WaitForSingleObject(hScheduleEvent, waitTime); if (task->endTime > 0 && task->endTime < timestamp) { - DestroyScheduleTask(task); + DestroyTask(task); continue; } - lua_rawgeti(task->T, LUA_REGISTRYINDEX, task->callbackRef); - if (lua_pcall(task->T, 0, 2, 0)) - CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(task->T, -1)); - - if (task->interval == 0) - { - DestroyScheduleTask(task); - continue; - } - - task->startTime += task->interval; - tasks.insert(task); + mir_forkthread(ExecuteTaskThread, task); } waitTime = INFINITE; @@ -81,7 +97,7 @@ void KillModuleScheduleTasks() while (ScheduleTask *task = tasks[0]) { tasks.remove(task); - DestroyScheduleTask(task); + DestroyTask(task); } } diff --git a/plugins/MirLua/src/mlua.cpp b/plugins/MirLua/src/mlua.cpp index de7690356e..2b9ad5978e 100644 --- a/plugins/MirLua/src/mlua.cpp +++ b/plugins/MirLua/src/mlua.cpp @@ -67,6 +67,7 @@ void CMLua::Unload() ::KillModuleTTBButton(); ::KillModuleIcons(hScriptsLangpack); + ::KillModuleSounds(hScriptsLangpack); ::KillModuleMenus(hScriptsLangpack); ::KillModuleHotkeys(hScriptsLangpack); ::KillObjectEventHooks(L); -- cgit v1.2.3