-
Notifications
You must be signed in to change notification settings - Fork 201
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
Support threads in the new crt1-command.c ctor check. #339
Conversation
Use an atomic compare-and-swap for checking whether constructors have been run, when threads are enabled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sense to me.
cc: @haraldh, this might be helpful for you. |
libc-bottom-half/crt/crt1-command.c
Outdated
// ensures that the `_start` function isn't started more than once. | ||
static volatile int started = 0; | ||
#ifdef _REENTRANT | ||
if (__sync_val_compare_and_swap(&started, 0, 1) != 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about using C11 atomics here? (https://www.ibm.com/docs/en/zos/2.1.0?topic=c11-atomic-compare-exchange-strong). Maybe we don't want to depend on C11?
__builtin_trap(); | ||
} | ||
#else | ||
static volatile int started = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for volatile here I think? (or above actually?).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've now added a comment about volatile
. It's admittedly a very theoretical concern. But also, this variable is read and written to exactly once, so there's nothing to optimize about it. So I'm inclined to keep the volatile
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Persisting an in-memory variable past the point of an _exit
call isn't something that's even possible to think about in C, and yet that's what this code is doing, so volatile
helps make sure that the compiler just does what we're telling it to do, because it won't understand what we're actually doing.
Use an atomic compare-and-swap for checking whether constructors have been run, when threads are enabled.
Use an atomic compare-and-swap for checking whether constructors have been run, when threads are enabled.