diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 18:07:22 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 18:07:22 +0000 |
commit | c04dcc2e7d834218ef2d4194331e383402495ae1 (patch) | |
tree | 7333e38d10d75386e60f336b80c2443c1166031d /xbmc/interfaces/generic/LanguageInvokerThread.cpp | |
parent | Initial commit. (diff) | |
download | kodi-c04dcc2e7d834218ef2d4194331e383402495ae1.tar.xz kodi-c04dcc2e7d834218ef2d4194331e383402495ae1.zip |
Adding upstream version 2:20.4+dfsg.upstream/2%20.4+dfsg
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'xbmc/interfaces/generic/LanguageInvokerThread.cpp')
-rw-r--r-- | xbmc/interfaces/generic/LanguageInvokerThread.cpp | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/xbmc/interfaces/generic/LanguageInvokerThread.cpp b/xbmc/interfaces/generic/LanguageInvokerThread.cpp new file mode 100644 index 0000000..67f3d14 --- /dev/null +++ b/xbmc/interfaces/generic/LanguageInvokerThread.cpp @@ -0,0 +1,133 @@ +/* + * Copyright (C) 2013-2018 Team Kodi + * This file is part of Kodi - https://kodi.tv + * + * SPDX-License-Identifier: GPL-2.0-or-later + * See LICENSES/README.md for more information. + */ + +#include "LanguageInvokerThread.h" + +#include "ScriptInvocationManager.h" + +#include <utility> + +CLanguageInvokerThread::CLanguageInvokerThread(LanguageInvokerPtr invoker, + CScriptInvocationManager* invocationManager, + bool reuseable) + : ILanguageInvoker(NULL), + CThread("LanguageInvoker"), + m_invoker(std::move(invoker)), + m_invocationManager(invocationManager), + m_reusable(reuseable) +{ } + +CLanguageInvokerThread::~CLanguageInvokerThread() +{ + Stop(true); +} + +InvokerState CLanguageInvokerThread::GetState() const +{ + if (m_invoker == NULL) + return InvokerStateFailed; + + return m_invoker->GetState(); +} + +void CLanguageInvokerThread::Release() +{ + m_bStop = true; + m_condition.notify_one(); +} + +bool CLanguageInvokerThread::execute(const std::string &script, const std::vector<std::string> &arguments) +{ + if (m_invoker == NULL || script.empty()) + return false; + + m_script = script; + m_args = arguments; + + if (CThread::IsRunning()) + { + std::unique_lock<std::mutex> lck(m_mutex); + m_restart = true; + m_condition.notify_one(); + } + else + Create(); + + //Todo wait until running + + return true; +} + +bool CLanguageInvokerThread::stop(bool wait) +{ + if (m_invoker == NULL) + return false; + + if (!CThread::IsRunning()) + return false; + + Release(); + + bool result = true; + if (m_invoker->GetState() < InvokerStateExecutionDone) + { + // stop the language-specific invoker + result = m_invoker->Stop(wait); + } + // stop the thread + CThread::StopThread(wait); + + return result; +} + +void CLanguageInvokerThread::OnStartup() +{ + if (m_invoker == NULL) + return; + + m_invoker->SetId(GetId()); + if (m_addon != NULL) + m_invoker->SetAddon(m_addon); +} + +void CLanguageInvokerThread::Process() +{ + if (m_invoker == NULL) + return; + + std::unique_lock<std::mutex> lckdl(m_mutex); + do + { + m_restart = false; + m_invoker->Execute(m_script, m_args); + + if (m_invoker->GetState() != InvokerStateScriptDone) + m_reusable = false; + + m_condition.wait(lckdl, [this] { return m_bStop || m_restart || !m_reusable; }); + + } while (m_reusable && !m_bStop); +} + +void CLanguageInvokerThread::OnExit() +{ + if (m_invoker == NULL) + return; + + m_invoker->onExecutionDone(); + m_invocationManager->OnExecutionDone(GetId()); +} + +void CLanguageInvokerThread::OnException() +{ + if (m_invoker == NULL) + return; + + m_invoker->onExecutionFailed(); + m_invocationManager->OnExecutionDone(GetId()); +}
\ No newline at end of file |