-
Notifications
You must be signed in to change notification settings - Fork 199
Description
In my project I have multiple resources (meaning POD) which should not be modified concurrently. So each task should wait until a specific set of resources it needs is available, lock them all at once, perform some work and unlock them again.
With the use case of only one resource it is easy to do, since I can just wait for an marl::Event and defer the events signal once the task has finished modifying the resource.
Now with multiple resources there is the danger of running into a deadlock if two tasks require the same resources and already acquired the locks for a subset of the required resources without releasing them in a way one tasks gets all resources at once.
Example: Task A and Task B both need Resource A and Resource B. Task A gets Resource A -> Task B gets Resource B -> Task A wants Resource B and Task B wants Resource A.
At first I was looking for something like marl::Event::all similar to marl::Event::any but there doesn't seem to such a thing.
Then I used marl::Event::test to free all previously acquired resources (just calling marl::Event::signal) if the test returned false.
At first this seemed to work but strangely after a while the frame time gets worse and worse. This happens only when the code below is included.
In this case two such tasks are scheduled every frame and as work they just count through a for loop (not even accessing the resources). The only difference which results in ever increasing frame time is if they try to acquire all resources like below. Using the Tracy profiler it looks like the time between the tasks being executed by the scheduler increases.
I wonder if there is a better way to do this with marl or if there are still some errors with this code.
Here is the code I currently use (just rewritten for a fixed subset of two resources):
auto task = [=] {
defer(velocityResourceAvailable.signal());
defer(positionResourceAvailable.signal());
{
bool allResourcesAcquired = false;
std::vector<marl::Event> requiredResources = { positionResourceAvailable , velocityResourceAvailable };
while (allResourcesAcquired == false)
{
marl::Event::any(requiredResources.begin(), requiredResources.end()).wait();
if (positionResourceAvailable.test())
{
if (velocityResourceAvailable.test())
{
allResourcesAcquired = true;
}
else
{
positionResourceAvailable.signal();
}
}
}
// do work ...