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

Avoid using global Mutex variables #1252

Merged
merged 3 commits into from
Nov 27, 2018

Conversation

nguyenhuy
Copy link
Member

@nguyenhuy nguyenhuy commented Nov 26, 2018

After 5c9815f, some Mutexes are used as global C++ variables which are loaded before main(). Since the Mutex constructor checks for unfair lock experiment, it triggers an experiment configuration load, and our app isn't ready to respond that early in the process.

@CLAassistant
Copy link

CLAassistant commented Nov 26, 2018

CLA assistant check
All committers have signed the CLA.

@ghost
Copy link

ghost commented Nov 26, 2018

🚫 CI failed with log

@Adlai-Holler
Copy link
Member

Huh, I was under the impression that these new's shouldn't be executed until the variable is accessed ("magic statics" in C11). Could you post the stack trace where they're running early?

@nguyenhuy
Copy link
Member Author

Sure, here is the stack trace

screen shot 2018-11-26 at 10 43 44 am

@ghost
Copy link

ghost commented Nov 26, 2018

🚫 CI failed with log

@nguyenhuy
Copy link
Member Author

These are run by dlyd before main().

This reverts part of 5c9815f. Using Mutex for global C++ variables causes Texture experiments to be triggered before main(). Our (internal) experiment framework isn't ready that early in the process.

We can get rid of StaticMutex once the unfair lock experiment is shipped.
@ghost
Copy link

ghost commented Nov 26, 2018

🚫 CI failed with log

@Adlai-Holler
Copy link
Member

Oh, right, magic statics only work for function-local statics not global variables. How about instead, we move the static variables into the functions in which they're used? That'll make them lazily generated. Currently each one is only used in one function.

Performance will be better if we use dispatch_once than if we rely on lazy statics, so:

- (void)myMethod {
static ASDN::Mutex *cacheMutex;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
  cacheMutex = new ASDN::Mutex();
});

// is faster than

static auto *cacheMutex = new ASDN::Mutex();

// because the latter actually uses a global mutex around all accesses to all function-local statics.
}

@nguyenhuy
Copy link
Member Author

@Adlai-Holler Good call. I updated the call sites to use dispatch_once. Mind taking another look?

because the latter actually uses a global mutex around all accesses to all function-local statics.

Ohh, good to know!

@nguyenhuy nguyenhuy changed the title Get StaticMutex back Avoid using global Mutex variables Nov 26, 2018
Copy link
Member

@Adlai-Holler Adlai-Holler left a comment

Choose a reason for hiding this comment

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

lgtm!

@nguyenhuy
Copy link
Member Author

Thanks for reviewing!

@nguyenhuy nguyenhuy merged commit a255953 into TextureGroup:master Nov 27, 2018
@nguyenhuy nguyenhuy deleted the HN-Get-StaticMutex-Back branch November 27, 2018 01:25
@appleguy
Copy link
Member

Cool. We're pretty confident this will have the desired lifecycle ever during app teardown, right? As some of the comments mention, the original solution was put in after some tricky-to-debug crashes on suspend.

@nguyenhuy
Copy link
Member Author

@appleguy The lifecycle was a problem because ASDN::Mutex used to be a struct. It should be fine now, and we haven't seen any crashes related to this change.

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.

None yet

5 participants