As described in the comment on `BLI_task_isolate`, deadlocks can happen when isolation is used with threading primitives that separate spawning tasks from executing them. All threads are waiting the tasks to complete but no thread is able to continue working due to task isolation. The fix is to not pass lazy-threading hints through task isolations. This way isolated regions can't create new tasks in a scheduler further up the call stack. This may lead to minor slowdowns because less threading may be used. It's generally possible to get rid of the slowdown again by sending the lazy-threading hint before entering the isolated region.
51 lines
1.1 KiB
C++
51 lines
1.1 KiB
C++
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
#include "BLI_lazy_threading.hh"
|
|
#include "BLI_stack.hh"
|
|
#include "BLI_vector.hh"
|
|
|
|
namespace blender::lazy_threading {
|
|
|
|
/**
|
|
* This uses a "raw" stack and vector so that it can be destructed after Blender checks for memory
|
|
* leaks. A new list of receivers is created whenever an isolated region is entered to avoid
|
|
* deadlocks.
|
|
*/
|
|
using HintReceivers = RawStack<RawVector<FunctionRef<void()>, 0>, 0>;
|
|
thread_local HintReceivers hint_receivers = []() {
|
|
HintReceivers receivers;
|
|
/* Make sure there is always at least one vector. */
|
|
receivers.push_as();
|
|
return receivers;
|
|
}();
|
|
|
|
void send_hint()
|
|
{
|
|
for (const FunctionRef<void()> &fn : hint_receivers.peek()) {
|
|
fn();
|
|
}
|
|
}
|
|
|
|
HintReceiver::HintReceiver(const FunctionRef<void()> fn)
|
|
{
|
|
hint_receivers.peek().append(fn);
|
|
}
|
|
|
|
HintReceiver::~HintReceiver()
|
|
{
|
|
hint_receivers.peek().pop_last();
|
|
}
|
|
|
|
ReceiverIsolation::ReceiverIsolation()
|
|
{
|
|
hint_receivers.push_as();
|
|
}
|
|
|
|
ReceiverIsolation::~ReceiverIsolation()
|
|
{
|
|
BLI_assert(hint_receivers.peek().is_empty());
|
|
hint_receivers.pop();
|
|
}
|
|
|
|
} // namespace blender::lazy_threading
|