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

Add missing thread safety to PagedAllocator #76755

Merged
merged 1 commit into from
May 9, 2023

Conversation

RandomShaper
Copy link
Member

@RandomShaper RandomShaper commented May 5, 2023

This has involved in the end making SpinLock's functions const, which is also consistent with Mutex.

@RandomShaper RandomShaper requested a review from a team as a code owner May 5, 2023 17:49
@RandomShaper RandomShaper added this to the 4.1 milestone May 5, 2023
@RandomShaper RandomShaper force-pushed the fix_allocator_thsafe branch 3 times, most recently from b3b3d29 to 73ec6b0 Compare May 5, 2023 18:16
@RandomShaper RandomShaper force-pushed the fix_allocator_thsafe branch from 73ec6b0 to 53c8a4f Compare May 8, 2023 10:41
@RandomShaper RandomShaper force-pushed the fix_allocator_thsafe branch from 53c8a4f to 341b958 Compare May 8, 2023 16:39
@RandomShaper RandomShaper requested a review from lawnjelly May 8, 2023 17:17
if (thread_safe) {
spin_lock.unlock();
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Wonder if it is possible to have a spinlock guard like MutexLock?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not a bad idea, but that would be out of the scope of this PR. And, in any case, some of the Godot code is not even using the MutexLock, preferring explicit lock-unlock even when the RAII lock would be usable.

@lawnjelly
Copy link
Member

Also, I hate to be that guy 😃 , (and as I know very little about the low level details I feel qualified to make newbie question), should we be using spinlocks now that modern mutexes are very good?

https://matklad.github.io/2020/01/04/mutexes-are-faster-than-spinlocks.html

@RandomShaper
Copy link
Member Author

RandomShaper commented May 9, 2023

Also, I hate to be that guy 😃 , (and as I know very little about the low level details I feel qualified to make newbie question), should we be using spinlocks now that modern mutexes are very good?

https://matklad.github.io/2020/01/04/mutexes-are-faster-than-spinlocks.html

This is something I had in my mental (and, I think, in my written) TODO list. I attended a talk in the GDC from AMD people on how to optimize games for Ryzen processors. I think most of what they said is universal. And one of the guidelines (after comparing trivial user spinlock, clever user spinlock, and mutex) was, "Use std::mutex."

So I'd really like that this topic was researched and benchmarked in Godot. If modern mutexes have all the good of both classic OS mutexes and spinlocks, then why not just use them.

@RandomShaper
Copy link
Member Author

In any case, the modification to SpinLock here is incidental. The basic target of this PR is PagedAllocator.

@AThousandShips
Copy link
Member

AThousandShips commented May 9, 2023

Without getting more off topic than this I'd suggest for researching the spinlock Vs mutex for Godot would be to replace spinlock in cases where they don't necessarily need to be spinlocks (I believe there are some cases where they are still suggested) with a SpinlockOrMutex that can be selected based on platform and be initially conservatively decided (for example I'm not sure how the comparison goes for mobile platforms etc.)

But maybe it's a good idea to open a proposal or discussion to track this

@akien-mga akien-mga merged commit 58ea42e into godotengine:master May 9, 2023
@akien-mga
Copy link
Member

Thanks!

@RandomShaper RandomShaper deleted the fix_allocator_thsafe branch May 9, 2023 15:59
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.

5 participants