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

[c11] define mono atomics in terms of standard atomics #91489

Merged
merged 17 commits into from
Sep 7, 2023

Conversation

lambdageek
Copy link
Member

Contributes to #90404

src/mono/mono/utils/atomic.h Outdated Show resolved Hide resolved
src/mono/mono/utils/atomic.h Outdated Show resolved Hide resolved
@lambdageek

This comment was marked as outdated.

#define MONO_IGNORE_STDATOMIC 1
#endif

#if defined(HOST_ANDROID) && (defined(HOST_X86) || defined(HOST_AMD64) || defined(HOST_ARM))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the opt-in setup would make more sense here

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not so sure. I would expect the number of platforms that don't support stdatomic to decrease over time.

I can try it both ways. (And I want to move this decision to cmake to make it more visible)

Copy link
Member Author

@lambdageek lambdageek Sep 5, 2023

Choose a reason for hiding this comment

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

Opt-in seems ok

@lambdageek
Copy link
Member Author

Oh right, moving the logic for ENABLE_STDATOMIC to cmake isn't great because we include the atomic.h header is consumed by offset-tool.py

@lambdageek
Copy link
Member Author

/azp run runtime-community

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@lambdageek lambdageek marked this pull request as ready for review September 5, 2023 19:15
@lambdageek
Copy link
Member Author

/azp run runtime-community

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

mono_atomic_cas_i32 (volatile gint32 *dest, gint32 exch, gint32 comp)
{
g_static_assert (sizeof(atomic_int) == sizeof(*dest) && ATOMIC_INT_LOCK_FREE == 2);
(void)atomic_compare_exchange_strong ((atomic_int*)dest, &comp, exch);
Copy link
Member

@lateralusX lateralusX Sep 6, 2023

Choose a reason for hiding this comment

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

Do we have strong guarantees for mono_atomic_cas_* functions? I believe we normally call these in loops to handle potential spuriously fails on platforms that could trigger them. If we would like to make the default implementation using strong guarantees, then maybe we should also offer the option to use weak when we already do cas in loops to potentially be more performant on platforms that takes advantage of spuriously fails?

Copy link
Member Author

Choose a reason for hiding this comment

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

yea good point. I need to look at the callers and check if they're all in loops. My feeling is that we should make weak the default and add strong as a second option. I didn't want to change the mono functions as part of this PR (and strong seemed like a safer default). But I can audit and report back and then we can add the non-default versions

Copy link
Member Author

Choose a reason for hiding this comment

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

Found 173 occurrences of mono_atomic_cas in src/mono/mono. Looked through about 2/3rds of them, chasing down callers if it's in a static helper method or a macro. About half are weak uses and about half are strong. So strong is definitely the right default. In a few places we could switch over to weak (particularly inside sgen there are some loops that could use weak). But it should be follow-up work, IMO.

Copy link
Member

Choose a reason for hiding this comment

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

That is what I expected, we have made different assumption around the strong/weak guarantees around the runtime over years. Moving over to strong will potential correct some false assumptions previously made in runtime (at least it won't break anything), so it should be safe, and I agree that we could identify and use weak versions where it might benefit.

Copy link
Member Author

Choose a reason for hiding this comment

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

Created #91747 for follow-up work

@lambdageek lambdageek merged commit bb2cfd5 into dotnet:main Sep 7, 2023
104 checks passed
lewing added a commit that referenced this pull request Sep 8, 2023
lewing added a commit that referenced this pull request Sep 8, 2023
lambdageek added a commit to lambdageek/runtime that referenced this pull request Oct 6, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Oct 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants