blob: 59f18bb39db1608f4f4c05cec3242d927546caf6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
/*
* Copyright (C) 2017-2021 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.
*/
#pragma once
#include "interfaces/generic/ScriptInvocationManager.h"
#include "interfaces/generic/ScriptRunner.h"
#include "threads/CriticalSection.h"
#include "threads/Event.h"
#include <cstdint>
#include <map>
#include <mutex>
template<class TScript>
class CRunningScriptsHandler : protected CScriptRunner
{
protected:
using HandleType = int;
CRunningScriptsHandler() = default;
virtual ~CRunningScriptsHandler() = default;
using CScriptRunner::ExecuteScript;
using CScriptRunner::GetAddon;
using CScriptRunner::SetDone;
using CScriptRunner::StartScript;
bool RunScript(TScript* script,
const ADDON::AddonPtr& addon,
const std::string& path,
bool resume)
{
if (script == nullptr || addon == nullptr || path.empty())
return false;
// reuse an existing script handle or get a new one if necessary
int handle = CScriptInvocationManager::GetInstance().GetReusablePluginHandle(addon->LibPath());
if (handle < 0)
handle = GetNewScriptHandle(script);
else
ReuseScriptHandle(handle, script);
// run the script
auto result = CScriptRunner::RunScript(addon, path, handle, resume);
// remove the script handle if necessary
RemoveScriptHandle(handle);
return result;
}
static HandleType GetNewScriptHandle(TScript* script)
{
std::unique_lock<CCriticalSection> lock(s_critical);
uint32_t handle = ++s_scriptHandleCounter;
s_scriptHandles[handle] = script;
return handle;
}
static void ReuseScriptHandle(HandleType handle, TScript* script)
{
std::unique_lock<CCriticalSection> lock(s_critical);
s_scriptHandles[handle] = script;
}
static void RemoveScriptHandle(HandleType handle)
{
std::unique_lock<CCriticalSection> lock(s_critical);
s_scriptHandles.erase(handle);
}
static TScript* GetScriptFromHandle(HandleType handle)
{
std::unique_lock<CCriticalSection> lock(s_critical);
auto scriptHandle = s_scriptHandles.find(handle);
if (scriptHandle == s_scriptHandles.end())
return nullptr;
return scriptHandle->second;
}
static inline CCriticalSection& GetScriptsLock() { return s_critical; }
private:
static std::map<HandleType, TScript*> s_scriptHandles;
static CCriticalSection s_critical;
static HandleType s_scriptHandleCounter;
};
template<class TScript>
std::map<typename CRunningScriptsHandler<TScript>::HandleType, TScript*>
CRunningScriptsHandler<TScript>::s_scriptHandles;
template<class TScript>
CCriticalSection CRunningScriptsHandler<TScript>::s_critical;
template<class TScript>
typename CRunningScriptsHandler<TScript>::HandleType
CRunningScriptsHandler<TScript>::s_scriptHandleCounter = 0;
|