1 /*
2  *  Copyright (C) 2013-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "interfaces/generic/ILanguageInvoker.h"
12 #include "threads/Thread.h"
13 
14 #include <string>
15 #include <vector>
16 
17 class CScriptInvocationManager;
18 
19 class CLanguageInvokerThread : public ILanguageInvoker, protected CThread
20 {
21 public:
22   CLanguageInvokerThread(LanguageInvokerPtr invoker,
23                          CScriptInvocationManager* invocationManager,
24                          bool reuseable);
25   ~CLanguageInvokerThread() override;
26 
27   virtual InvokerState GetState() const;
28 
GetScript()29   const std::string& GetScript() const { return m_script; };
GetInvoker()30   LanguageInvokerPtr GetInvoker() const { return m_invoker; };
Reuseable(const std::string & script)31   bool Reuseable(const std::string& script) const
32   {
33     return !m_bStop && m_reusable && GetState() == InvokerStateScriptDone && m_script == script;
34   };
35   virtual void Release();
36 
37 protected:
38   bool execute(const std::string &script, const std::vector<std::string> &arguments) override;
39   bool stop(bool wait) override;
40 
41   void OnStartup() override;
42   void Process() override;
43   void OnExit() override;
44   void OnException() override;
45 
46 private:
47   LanguageInvokerPtr m_invoker;
48   CScriptInvocationManager *m_invocationManager;
49   std::string m_script;
50   std::vector<std::string> m_args;
51 
52   std::mutex m_mutex;
53   std::condition_variable m_condition;
54   bool m_restart = false;
55   bool m_reusable = false;
56 };
57