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

Fix ThreadLocalRandom #53

Merged
merged 1 commit into from
Sep 13, 2024
Merged

Conversation

isaacl
Copy link
Member

@isaacl isaacl commented Sep 12, 2024

Current impl was not creating thread local randoms!

@isaacl
Copy link
Member Author

isaacl commented Sep 12, 2024

Note: there's other ways to fix this -- you could have a singleton RandomApi wrapper that gets it's underlying ThreadLocalRandom implicitly... But it definitely needs to be fixed!

@isaacl
Copy link
Member Author

isaacl commented Sep 12, 2024

@ornicar ^

Current impl was not creating thread local randoms!
@isaacl isaacl force-pushed the fixThreadLocalRandom branch 3 times, most recently from 362c732 to d526674 Compare September 12, 2024 19:13
@niklasf
Copy link
Member

niklasf commented Sep 12, 2024

Good find! This could potentially be pretty bad. Looks like ThreadLocalRandom.current() is already using a thread-local internally, so we could just call that instead of wrapping it in another one?

@ornicar ornicar merged commit aa2235c into lichess-org:master Sep 13, 2024
2 checks passed
@isaacl
Copy link
Member Author

isaacl commented Sep 13, 2024

Sorry this isn't actually good enough, because there are multithreaded classes which import a ThreadLocalRandom top level object and then might use it in multiple threads. PR follow up coming

@isaacl
Copy link
Member Author

isaacl commented Sep 13, 2024

See #54

@isaacl
Copy link
Member Author

isaacl commented Sep 14, 2024

@niklasf my read of the code is that the old impl causes a lot of uninitialized random sources which will all have the same sequence. ThreadLocalRandom.current always returns the same singleton object (which internally uses storage on fields of the Thread class to manage state). But without the call to current on each thread, the random source spits out values without initialization. And once it spits out a value, it no longer thinks it needs to be initialized.

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