Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MutexLock in more places #96166

Merged
merged 1 commit into from
Aug 30, 2024

Conversation

AThousandShips
Copy link
Member

This ensures more safe management of mutexes and makes forgetting to unlock it less likely, most of these cases are trivial though and don't depend on it for safety but I'd prefer to avoid using direct locking unless it's necessary to handle locking between methods etc. or in some cases where using it is just too cluttered

There are some cases which could benefit from a little more of this using the new temp_unlock/relock methods of MutexLock but kept things simple here

@@ -60,32 +60,32 @@ void Resource::set_path(const String &p_path, bool p_take_over) {
p_take_over = false; // Can't take over an empty path
}

ResourceCache::lock.lock();
{
MutexLock lock(ResourceCache::lock);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably rename ResourceCache::lock to ResourceCache::mutex at some point as it's just a recipe for confusion having an incorrect name for it, left it alone in this PR though

baking_navmesh_mutex.lock();
generator_task_mutex.lock();
MutexLock baking_navmesh_lock(baking_navmesh_mutex);
MutexLock generator_task_lock(generator_task_mutex);
Copy link
Member Author

@AThousandShips AThousandShips Aug 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it doesn't seem to cause any problems I feel that locking two mutexes like this is a recipe for deadlock, so would be good to look into that down the line

@akien-mga akien-mga requested a review from RandomShaper August 27, 2024 14:29
@RandomShaper
Copy link
Member

I'm a fan of consistency as much as I'm a fan of expressivity. So, on the one hand, I feel that we lose something by letting explicit lock-unlock go (because in some places, having the unlock line around helps remembering you're reading a critical section, etc.)., while on the other hand, I feel that trying to use the RAII syntax always reduces cognitive load when maintaining code as you don't need to ask yourself which one to use.

I'd say the tradeoff is a bit towards the bad side, but that's a bit subjective, so let's hear more opinions.

@AThousandShips
Copy link
Member Author

AThousandShips commented Aug 27, 2024

Can split up the cases that are trailing etc. and keeping it in trivial cases like a full block especially with newly indicated blocks

@RandomShaper
Copy link
Member

Can split up the cases that are trailing etc. and keeping it in trivial cases like a full block especially with newly indicated blocks

That sounds very reasonable.

@AThousandShips
Copy link
Member Author

Split cases into three categories:

  • Trivial cases where the whole method is covered (with the exception of declarations at the start, and returns at the end)
  • Cases introducing a new block (which are a bit more disruptive, but more readable IMO)
  • Trailing cases which have the least readability IMO

Copy link
Member

@RandomShaper RandomShaper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great now! I added some suggestions, but I'm leaving an approval already.

Aside, if the commit structure is not meant to be squashed, I'd also suggest rewording the messages so they look very similar and it's obvious they part of the same endeavor.

@AThousandShips
Copy link
Member Author

Will squash them up unless we expect to revert some and not others

@akien-mga akien-mga modified the milestones: 4.x, 4.4 Aug 30, 2024
@akien-mga akien-mga merged commit 909629d into godotengine:master Aug 30, 2024
19 checks passed
@akien-mga
Copy link
Member

Thanks!

@AThousandShips AThousandShips deleted the more_lock_raii branch August 30, 2024 08:43
@AThousandShips
Copy link
Member Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants