mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
Add ChildProcessManager
The new class hold a list of ChildProcesses and periodically checks their return value until they report termination. On Linux this check is necessary to avoid leaving zombie processes behind.
This commit is contained in:
parent
76b9b28658
commit
0611baf1be
43 changed files with 647 additions and 0 deletions
69
modules/juce_core/detail/juce_CallbackListenerList.h
Normal file
69
modules/juce_core/detail/juce_CallbackListenerList.h
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce::detail
|
||||
{
|
||||
|
||||
template <typename... Ts>
|
||||
constexpr bool isValueOrLvalueReferenceToConst()
|
||||
{
|
||||
return (( (! std::is_reference_v<Ts>)
|
||||
|| (std::is_lvalue_reference_v<Ts> && std::is_const_v<std::remove_reference_t<Ts>>)) && ...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
class CallbackListenerList
|
||||
{
|
||||
public:
|
||||
static_assert (isValueOrLvalueReferenceToConst<Args...>(),
|
||||
"CallbackListenerList can only forward values or const lvalue references");
|
||||
|
||||
using Callback = std::function<void (Args...)>;
|
||||
|
||||
ErasedScopeGuard addListener (Callback callback)
|
||||
{
|
||||
jassert (callback != nullptr);
|
||||
|
||||
const auto it = callbacks.insert (callbacks.end(), std::move (callback));
|
||||
listeners.add (&*it);
|
||||
|
||||
return ErasedScopeGuard { [this, it]
|
||||
{
|
||||
listeners.remove (&*it);
|
||||
callbacks.erase (it);
|
||||
} };
|
||||
}
|
||||
|
||||
void call (Args... args)
|
||||
{
|
||||
listeners.call ([&] (auto& l) { l (std::forward<Args> (args)...); });
|
||||
}
|
||||
|
||||
private:
|
||||
std::list<Callback> callbacks;
|
||||
ListenerList<Callback> listeners;
|
||||
};
|
||||
|
||||
} // namespace juce::detail
|
||||
|
|
@ -354,6 +354,8 @@ JUCE_END_IGNORE_WARNINGS_MSVC
|
|||
#include "files/juce_AndroidDocument.h"
|
||||
#include "streams/juce_AndroidDocumentInputSource.h"
|
||||
|
||||
#include "detail/juce_CallbackListenerList.h"
|
||||
|
||||
#if JUCE_CORE_INCLUDE_OBJC_HELPERS && (JUCE_MAC || JUCE_IOS)
|
||||
#include "native/juce_CFHelpers_mac.h"
|
||||
#include "native/juce_ObjCHelpers_mac.h"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
void ChildProcessManager::checkProcesses()
|
||||
{
|
||||
for (auto it = processes.begin(); it != processes.end();)
|
||||
{
|
||||
auto processPtr = *it;
|
||||
|
||||
if (! processPtr->isRunning())
|
||||
{
|
||||
listeners.call (processPtr.get());
|
||||
it = processes.erase (it);
|
||||
}
|
||||
else
|
||||
{
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
if (processes.empty())
|
||||
timer.stopTimer();
|
||||
}
|
||||
|
||||
JUCE_IMPLEMENT_SINGLETON (ChildProcessManager)
|
||||
|
||||
} // namespace juce
|
||||
104
modules/juce_events/interprocess/juce_ChildProcessManager.h
Normal file
104
modules/juce_events/interprocess/juce_ChildProcessManager.h
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
/** Manages a set of ChildProcesses and periodically checks their return value. Upon completion
|
||||
it calls listeners added with addChildProcessExitedListener().
|
||||
|
||||
This class is mostly aimed for usage on Linux, where terminated child processes are only
|
||||
cleaned up if their return code is read after termination. In order to ensure this one needs
|
||||
to call ChildProcess::isFinished() until it returns false or
|
||||
ChildProcess::waitForProcessToFinish() until it returns true.
|
||||
|
||||
This class will keep querying the return code on a Timer thread until the process
|
||||
terminates. This can be handy if one wants to start and stop multiple ChildProcesses on
|
||||
Linux that could take a long time to complete.
|
||||
|
||||
Since this class uses a Timer to check subprocess status, it's generally only safe to
|
||||
access the returned ChildProcesses from the message thread.
|
||||
|
||||
@see ChildProcessManagerSingleton
|
||||
|
||||
@tags{Events}
|
||||
*/
|
||||
class JUCE_API ChildProcessManager final : private DeletedAtShutdown
|
||||
{
|
||||
public:
|
||||
#ifndef DOXYGEN
|
||||
JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL (ChildProcessManager)
|
||||
#endif
|
||||
|
||||
/** Creates a new ChildProcess and starts it with the provided arguments.
|
||||
|
||||
The arguments are the same as the overloads to ChildProcess::start().
|
||||
|
||||
The manager will keep the returned ChildProcess object alive until it terminates and its
|
||||
return value has been queried. Calling ChildProcess::kill() on the returned object will
|
||||
eventually cause its removal from the ChildProcessManager after it terminates.
|
||||
*/
|
||||
template <typename... Args>
|
||||
std::shared_ptr<ChildProcess> createAndStartManagedChildProcess (Args&&... args)
|
||||
{
|
||||
auto p = std::make_shared<ChildProcess>();
|
||||
|
||||
if (! p->start (std::forward<Args> (args)...))
|
||||
return nullptr;
|
||||
|
||||
processes.insert (p);
|
||||
timer.startTimer (1000);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
/** Registers a callback function that is called for every ChildProcess that terminated.
|
||||
|
||||
This registration is deleted when the returned ErasedScopedGuard is deleted.
|
||||
*/
|
||||
auto addChildProcessExitedListener (std::function<void (ChildProcess*)> listener)
|
||||
{
|
||||
return listeners.addListener (std::move (listener));
|
||||
}
|
||||
|
||||
/** Returns true if the ChildProcessManager contains any running ChildProcesses that it's
|
||||
monitoring.
|
||||
*/
|
||||
auto hasRunningProcess() const
|
||||
{
|
||||
return timer.isTimerRunning();
|
||||
}
|
||||
|
||||
private:
|
||||
ChildProcessManager() = default;
|
||||
~ChildProcessManager() override { clearSingletonInstance(); }
|
||||
|
||||
void checkProcesses();
|
||||
|
||||
std::set<std::shared_ptr<ChildProcess>> processes;
|
||||
detail::CallbackListenerList<ChildProcess*> listeners;
|
||||
TimedCallback timer { [this] { checkProcesses(); } };
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
|
|
@ -64,6 +64,7 @@
|
|||
#include "broadcasters/juce_ChangeBroadcaster.cpp"
|
||||
#include "timers/juce_MultiTimer.cpp"
|
||||
#include "timers/juce_Timer.cpp"
|
||||
#include "interprocess/juce_ChildProcessManager.cpp"
|
||||
#include "interprocess/juce_InterprocessConnection.cpp"
|
||||
#include "interprocess/juce_InterprocessConnectionServer.cpp"
|
||||
#include "interprocess/juce_ConnectedChildProcess.cpp"
|
||||
|
|
|
|||
|
|
@ -86,7 +86,9 @@
|
|||
#include "broadcasters/juce_ChangeListener.h"
|
||||
#include "broadcasters/juce_ChangeBroadcaster.h"
|
||||
#include "timers/juce_Timer.h"
|
||||
#include "timers/juce_TimedCallback.h"
|
||||
#include "timers/juce_MultiTimer.h"
|
||||
#include "interprocess/juce_ChildProcessManager.h"
|
||||
#include "interprocess/juce_InterprocessConnection.h"
|
||||
#include "interprocess/juce_InterprocessConnectionServer.h"
|
||||
#include "interprocess/juce_ConnectedChildProcess.h"
|
||||
|
|
|
|||
63
modules/juce_events/timers/juce_TimedCallback.h
Normal file
63
modules/juce_events/timers/juce_TimedCallback.h
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
/** Utility class wrapping a single non-null callback called by a Timer.
|
||||
|
||||
You can use the usual Timer functions to start and stop the TimedCallback. Deleting the
|
||||
TimedCallback will automatically stop the underlying Timer.
|
||||
|
||||
With this class you can use the Timer facility without inheritance.
|
||||
|
||||
@see Timer
|
||||
@tags{Events}
|
||||
*/
|
||||
class TimedCallback final : private Timer
|
||||
{
|
||||
public:
|
||||
/** Constructor. The passed in callback must be non-null. */
|
||||
explicit TimedCallback (std::function<void()> callbackIn)
|
||||
: callback (std::move (callbackIn))
|
||||
{
|
||||
jassert (callback);
|
||||
}
|
||||
|
||||
/** Destructor. */
|
||||
~TimedCallback() noexcept override { stopTimer(); }
|
||||
|
||||
using Timer::startTimer;
|
||||
using Timer::startTimerHz;
|
||||
using Timer::stopTimer;
|
||||
using Timer::isTimerRunning;
|
||||
|
||||
private:
|
||||
void timerCallback() override { callback(); }
|
||||
|
||||
std::function<void()> callback;
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
Loading…
Add table
Add a link
Reference in a new issue