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

Experimental use of constructor functions to call init #334

Merged
merged 4 commits into from
Jun 1, 2020

Conversation

sagebind
Copy link
Collaborator

As mentioned in #333, this is a potentially helpful addition that ensures that curl is initialized on the main thread by using constructor functions that get called by the OS before the current program's main is called.

This has the advantage that, assuming you are on one of the supported platforms, init() will be called safely, correctly, and automatically without concerning the user about the gotchas.

This does have some disadvantages:

  • Constructor functions are always invoked, which means that simply including curl can slow down the startup of a user's program even if curl is only conditionally used, or used later in program execution.
  • On platforms without a constructor implementation, the user still needs to initialize curl on the main thread. All the common platforms are covered though, so maybe this is a niche scenario.

There's no additional runtime cost to this implementation except on platforms without a constructor, where we do an extra atomic swap that isn't necessary. This is probably fixable with additional conditional compilation.

sagebind added 2 commits May 23, 2020 19:21
As mentioned in #333, this is a potentially helpful addition that ensures that curl is initialized on the main thread by using constructor functions that get called by the OS before the current program's `main` is called.

This has the advantage that, assuming you are on one of the supported platforms, `init()` will be called safely, correctly, and automatically without concerning the user about the gotchas.

This does have some disadvantages:

- Constructor functions are always invoked, which means that simply including curl can slow down the startup of a user's program even if curl is only conditionally used, or used later in program execution.
- On platforms without a constructor implementation, the user still needs to initialize curl on the main thread. All the common platforms are covered though, so maybe this is a niche scenario.

There's no additional runtime cost to this implementation except on platforms without a constructor, where we do an extra atomic swap that isn't necessary. This is probably fixable with additional conditional compilation.
Ensure subsequent init calls are compiled down to a single atomic compare-and-swap.
src/lib.rs Outdated Show resolved Hide resolved
Since it is not possible for the `Once` to have contention in a constructor, it is safe to use it. (Under contention, `Once` requires `std::thread` machinery to work, which is not yet available before `main`.)

Also update documentation on initialization.
@sagebind
Copy link
Collaborator Author

I removed the second atomic so it should be just as efficient as it was before now. I also went ahead and updated the documentation.

@alexcrichton alexcrichton merged commit ee68a61 into master Jun 1, 2020
@alexcrichton alexcrichton deleted the automatic-init branch June 1, 2020 13:40
@alexcrichton
Copy link
Owner

👍

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.

2 participants