1
0

Added cPluginLua::cResettable interface, used for scheduled tasks.

This allows plugins to register objects that can "survive" the plugin unloading - they will simply bail out if the plugin is already unloaded, instead of referencing bad plugin data.
Fixes #1556.
This commit is contained in:
Mattes D
2015-03-20 15:13:33 +01:00
parent 13ccd1adb0
commit 781c8683f7
6 changed files with 151 additions and 80 deletions

View File

@@ -6,10 +6,11 @@
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#ifdef __APPLE__
#define LUA_USE_MACOSX
#define LUA_USE_MACOSX
#else
#define LUA_USE_POSIX
#define LUA_USE_POSIX
#endif
#include "PluginLua.h"
#include "../CommandOutput.h"
#include "PluginManager.h"
@@ -52,24 +53,35 @@ cPluginLua::~cPluginLua()
void cPluginLua::Close(void)
{
if (m_LuaState.IsValid())
{
// Release all the references in the hook map:
for (cHookMap::iterator itrH = m_HookMap.begin(), endH = m_HookMap.end(); itrH != endH; ++itrH)
{
for (cLuaRefs::iterator itrR = itrH->second.begin(), endR = itrH->second.end(); itrR != endR; ++itrR)
{
delete *itrR;
} // for itrR - itrH->second[]
} // for itrH - m_HookMap[]
m_HookMap.clear();
m_LuaState.Close();
}
else
cCSLock Lock(m_CriticalSection);
// If already closed, bail out:
if (!m_LuaState.IsValid())
{
ASSERT(m_Resettables.empty());
ASSERT(m_HookMap.empty());
return;
}
// Notify and remove all m_Resettables:
for (auto resettable: m_Resettables)
{
resettable->Reset();
}
m_Resettables.clear();
// Release all the references in the hook map:
for (cHookMap::iterator itrH = m_HookMap.begin(), endH = m_HookMap.end(); itrH != endH; ++itrH)
{
for (cLuaRefs::iterator itrR = itrH->second.begin(), endR = itrH->second.end(); itrR != endR; ++itrR)
{
delete *itrR;
} // for itrR - itrH->second[]
} // for itrH - m_HookMap[]
m_HookMap.clear();
// Close the Lua engine:
m_LuaState.Close();
}
@@ -1709,6 +1721,16 @@ int cPluginLua::CallFunctionFromForeignState(
void cPluginLua::AddResettable(cPluginLua::cResettablePtr a_Resettable)
{
cCSLock Lock(m_CriticalSection);
m_Resettables.push_back(a_Resettable);
}
AString cPluginLua::HandleWebRequest(const HTTPRequest * a_Request)
{
cCSLock Lock(m_CriticalSection);
@@ -1826,3 +1848,26 @@ void cPluginLua::CallbackWindowSlotChanged(int a_FnRef, cWindow & a_Window, int
////////////////////////////////////////////////////////////////////////////////
// cPluginLua::cResettable:
cPluginLua::cResettable::cResettable(cPluginLua & a_Plugin):
m_Plugin(&a_Plugin),
m_CSPlugin(a_Plugin.m_CriticalSection)
{
}
void cPluginLua::cResettable::Reset(void)
{
cCSLock Lock(m_CSPlugin);
m_Plugin = nullptr;
}