-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add a Once
primitive
#11187
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
Add a Once
primitive
#11187
Conversation
Of the 8 static mutexes that are currently in-use by the compiler and its libraries, 4 of them are currently used for one-time initialization. The unforunate side effect of using a static mutex is that the mutex is leaked. This primitive should provide the basis for efficiently keeping track of one-time initialization as well as ensuring that it does not leak the internal mutex that is used. I have chosen to put this in libstd because libstd is currently making use of a static initialization mutex (rt::local_ptr), but I can also see a more refined version of this type being suitable to initialize FFI bindings (such as initializing LLVM and initializing winsock networking on windows). I also intend on adding "helper threads" to libnative, and those will greatly benefit from a simple "once" primitive rather than always reinventing the wheel by using mutexes and bools. I would much rather see this primitive built on a mutex that blocks green threads appropriately, but that does not exist at this time, so it does not belong outside of `std::unstable`.
A |
You are correct. This is not a perf-sensitive piece of code, if you're invoking a Regardless, a |
This might be better called |
Maybe |
Oh, I didn't realize this was mutex-specific. |
I should have read the code before commenting. Now I understand what this is about. I'm not sure that this is appropriate in |
@alexcrichton: It seems to me that Rust should stop being overly opinionated and let you initialize statics as you can in C++. It's a significant performance hit to need branches like this in every function using some globally initialized state. The work has to happen at some point, so why not run it before |
Let's discuss life before main in a different issue, that is not the topic of this pull request. @brson, no particular reason to use a |
Updated to remove the usage of |
Rationale can be found in the first commit, but this is basically the same thing as `pthread_once`
Rationale can be found in the first commit, but this is basically the same thing as
pthread_once