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.

102 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. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. /** Manages a set of ChildProcesses and periodically checks their return value. Upon completion
  20. it calls listeners added with addChildProcessExitedListener().
  21. This class is mostly aimed for usage on Linux, where terminated child processes are only
  22. cleaned up if their return code is read after termination. In order to ensure this one needs
  23. to call ChildProcess::isFinished() until it returns false or
  24. ChildProcess::waitForProcessToFinish() until it returns true.
  25. This class will keep querying the return code on a Timer thread until the process
  26. terminates. This can be handy if one wants to start and stop multiple ChildProcesses on
  27. Linux that could take a long time to complete.
  28. Since this class uses a Timer to check subprocess status, it's generally only safe to
  29. access the returned ChildProcesses from the message thread.
  30. @see ChildProcessManagerSingleton
  31. @tags{Events}
  32. */
  33. class JUCE_API ChildProcessManager final : private DeletedAtShutdown
  34. {
  35. public:
  36. #ifndef DOXYGEN
  37. JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL (ChildProcessManager)
  38. #endif
  39. /** Creates a new ChildProcess and starts it with the provided arguments.
  40. The arguments are the same as the overloads to ChildProcess::start().
  41. The manager will keep the returned ChildProcess object alive until it terminates and its
  42. return value has been queried. Calling ChildProcess::kill() on the returned object will
  43. eventually cause its removal from the ChildProcessManager after it terminates.
  44. */
  45. template <typename... Args>
  46. std::shared_ptr<ChildProcess> createAndStartManagedChildProcess (Args&&... args)
  47. {
  48. auto p = std::make_shared<ChildProcess>();
  49. if (! p->start (std::forward<Args> (args)...))
  50. return nullptr;
  51. processes.insert (p);
  52. timer.startTimer (1000);
  53. return p;
  54. }
  55. /** Registers a callback function that is called for every ChildProcess that terminated.
  56. This registration is deleted when the returned ErasedScopedGuard is deleted.
  57. */
  58. auto addChildProcessExitedListener (std::function<void (ChildProcess*)> listener)
  59. {
  60. return listeners.addListener (std::move (listener));
  61. }
  62. /** Returns true if the ChildProcessManager contains any running ChildProcesses that it's
  63. monitoring.
  64. */
  65. auto hasRunningProcess() const
  66. {
  67. return timer.isTimerRunning();
  68. }
  69. private:
  70. ChildProcessManager() = default;
  71. ~ChildProcessManager() override { clearSingletonInstance(); }
  72. void checkProcesses();
  73. std::set<std::shared_ptr<ChildProcess>> processes;
  74. detail::CallbackListenerList<ChildProcess*> listeners;
  75. TimedCallback timer { [this] { checkProcesses(); } };
  76. };
  77. } // namespace juce