Skip to content

winapi: Run fls_init in startup thread, before regular constructors #497

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 3 commits into from
Jan 2, 2022

Conversation

thrimbor
Copy link
Member

@thrimbor thrimbor commented Aug 24, 2021

This, together with XboxDev/nxdk-pdclib#48, fixes the issue where constructors were run before thread-local storage was available by running them from the main thread instead of the startup thread. However, fls_init is a special constructor that needs to run before any thread creation, so this introduces and uses a new mechanism which allows for constructors to run in the startup thread instead by putting function pointers in a custom subsection of .CRT (the same thing is done automatically by the compiler for regular constructors).

This code can be used to verify that the change works. Before the changes, this would crash during the construction of the global std::stringstream s, but with the changes, it should run correctly.

#include <hal/debug.h>
#include <hal/video.h>
#include <windows.h>
#include <sstream>

std::stringstream s;

int main(void) {
  XVideoSetMode(640, 480, 32, REFRESH_DEFAULT);

  debugPrint("hello\n");

  while (true) {
    Sleep(2000);
  }

  return 0;
}

Fixes #496.

This will be combined with a submodule update and turned into a normal PR when XboxDev/nxdk-pdclib#48 is merged.

@thrimbor thrimbor marked this pull request as ready for review December 14, 2021 09:26
@thrimbor
Copy link
Member Author

thrimbor commented Jan 2, 2022

This has sat for a while, and as it is a crash fix, I'm self-merging this now.

@thrimbor thrimbor merged commit 692597d into XboxDev:master Jan 2, 2022
@thrimbor thrimbor deleted the fiber_pre_init branch January 2, 2022 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

XBE crashes on start, when declaration added in class.
2 participants