Skip to content

Move unique_id (configurably) earlier in the static init process #2379

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

Merged
merged 2 commits into from
Mar 29, 2025

Conversation

byteit101
Copy link
Contributor

Fixes #2373

Move unique_id (configurably) earlier in the static initialization process . This enables using the unique_id in C++ static initializers by default, and avoid the static initialization order fiasco.

I added the configuration in the off chance that someone would need to adjust it, but I think 1000 is probably a good-enough default. Values range from 101-65535, or "none", which is after 65535. Normal C++ static initializers are "none". Users can still manually specify to be earlier (101-999) without any configuration changes, via any of the following:

// C  & C++
__attribute__((constructor(101))) some_function() {}
// C++ only
__attribute__((init_priority(101))) some_class instance;
[[gnu::init_priority(101)]] some_class instance;

The following will all be initialized after unique_id is initialized, by default

// C  & C++
__attribute__((constructor(1001))) some_function() {}
__attribute__((constructor)) some_function() {}
// C++ only
__attribute__((init_priority(1001))) some_class instance;
[[gnu::init_priority(1001)]] some_class instance;
some_class instance;

Fixes raspberrypi#2373

This enables using the unique_id in C++ static initializers by default
* \ingroup pico_unique_id
*
* This defines the init_priority of the pico_unique_id. By default, it is 1000. The valid range is
* from 101-65535. Set to -1 to set the priority to none, thus putting it after 65535. Changing
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm afraid that static initializers aren't something I know much about, but would there ever be a reason that you'd want to set this to -1 ?

Copy link
Contributor Author

@byteit101 byteit101 Mar 28, 2025

Choose a reason for hiding this comment

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

The current, existing behavior can be reverted to by setting to -1. I don't think there is a reason you couldn't use 65535 instead, but the default linker script order is that "-1" is after 65535 (I could call it 65536, but I think that may be more confusing, oddly enough)

Linker script: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/pico_crt0/rp2040/memmap_default.ld#L79-L80

Note the .ctor.* are the ones with init_priority, and .ctor is the -1/default/old behavior

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you would prefer, I can remove -1 and we then wait until someone hollers that we broke something? (I think that is unlikely, but not sure, as I haven't explicitly used init_priority before this year)

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a @kilograham decision 😉

@kilograham kilograham added this to the 2.1.2 milestone Mar 28, 2025
@kilograham kilograham merged commit bb5b5f9 into raspberrypi:develop Mar 29, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants