The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. /** Manages a set of ChildProcesses and periodically checks their return value. Upon completion
  21. it calls listeners added with addChildProcessExitedListener().
  22. This class is mostly aimed for usage on Linux, where terminated child processes are only
  23. cleaned up if their return code is read after termination. In order to ensure this one needs
  24. to call ChildProcess::isFinished() until it returns false or
  25. ChildProcess::waitForProcessToFinish() until it returns true.
  26. This class will keep querying the return code on a Timer thread until the process
  27. terminates. This can be handy if one wants to start and stop multiple ChildProcesses on
  28. Linux that could take a long time to complete.
  29. Since this class uses a Timer to check subprocess status, it's generally only safe to
  30. access the returned ChildProcesses from the message thread.
  31. @see ChildProcessManagerSingleton
  32. @tags{Events}
  33. */
  34. class JUCE_API ChildProcessManager final : private DeletedAtShutdown
  35. {
  36. public:
  37. #ifndef DOXYGEN
  38. JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL (ChildProcessManager)
  39. #endif
  40. /** Creates a new ChildProcess and starts it with the provided arguments.
  41. The arguments are the same as the overloads to ChildProcess::start().
  42. The manager will keep the returned ChildProcess object alive until it terminates and its
  43. return value has been queried. Calling ChildProcess::kill() on the returned object will
  44. eventually cause its removal from the ChildProcessManager after it terminates.
  45. */
  46. template <typename... Args>
  47. std::shared_ptr<ChildProcess> createAndStartManagedChildProcess (Args&&... args)
  48. {
  49. auto p = std::make_shared<ChildProcess>();
  50. if (! p->start (std::forward<Args> (args)...))
  51. return nullptr;
  52. processes.insert (p);
  53. timer.startTimer (1000);
  54. return p;
  55. }
  56. /** Registers a callback function that is called for every ChildProcess that terminated.
  57. This registration is deleted when the returned ErasedScopedGuard is deleted.
  58. */
  59. auto addChildProcessExitedListener (std::function<void (ChildProcess*)> listener)
  60. {
  61. return listeners.addListener (std::move (listener));
  62. }
  63. /** Returns true if the ChildProcessManager contains any running ChildProcesses that it's
  64. monitoring.
  65. */
  66. auto hasRunningProcess() const
  67. {
  68. return timer.isTimerRunning();
  69. }
  70. private:
  71. ChildProcessManager() = default;
  72. ~ChildProcessManager() override { clearSingletonInstance(); }
  73. void checkProcesses();
  74. std::set<std::shared_ptr<ChildProcess>> processes;
  75. detail::CallbackListenerList<ChildProcess*> listeners;
  76. TimedCallback timer { [this] { checkProcesses(); } };
  77. };
  78. } // namespace juce