-
Notifications
You must be signed in to change notification settings - Fork 244
Rename Weak to LazyPtr and move it to lazy.rs
#427
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
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a47b8ce
Move `Weak` into a separate module
newpavlov cbd5142
Rename `Weak` to `CachedPtr`
newpavlov 105ce26
Rename to `LazyPtr` and move to the `lazy` module
newpavlov c98c114
Tweak imports
newpavlov 9ef7aaa
Use Acquire load
newpavlov 6e0b5dd
review fixes
newpavlov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| use core::{ | ||
| ffi::c_void, | ||
| ptr::NonNull, | ||
| sync::atomic::{fence, AtomicPtr, Ordering}, | ||
| }; | ||
|
|
||
| // A "weak" binding to a C function that may or may not be present at runtime. | ||
| // Used for supporting newer OS features while still building on older systems. | ||
| // Based off of the DlsymWeak struct in libstd: | ||
| // https://github.com/rust-lang/rust/blob/1.61.0/library/std/src/sys/unix/weak.rs#L84 | ||
| // except that the caller must manually cast self.ptr() to a function pointer. | ||
| pub struct Weak { | ||
| addr: AtomicPtr<c_void>, | ||
| link_fn: fn() -> *mut c_void, | ||
| } | ||
|
|
||
| impl Weak { | ||
| // A non-null pointer value which indicates we are uninitialized. This | ||
| // constant should ideally not be a valid address of a function pointer. | ||
| // However, if by chance libc::dlsym does return UNINIT, there will not | ||
josephlr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // be undefined behavior. libc::dlsym will just be called each time ptr() | ||
| // is called. This would be inefficient, but correct. | ||
| // TODO: Replace with core::ptr::invalid_mut(1) when that is stable. | ||
| const UNINIT: *mut c_void = 1 as *mut c_void; | ||
|
|
||
| // Construct a weak binding a C function. | ||
josephlr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pub const fn new(link_fn: fn() -> *mut c_void) -> Self { | ||
| Self { | ||
| addr: AtomicPtr::new(Self::UNINIT), | ||
| link_fn, | ||
| } | ||
| } | ||
|
|
||
| // Return the address of a function if present at runtime. Otherwise, | ||
| // return None. Multiple callers can call ptr() concurrently. It will | ||
| // always return _some_ value returned by libc::dlsym. However, the | ||
josephlr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // dlsym function may be called multiple times. | ||
| pub fn ptr(&self) -> Option<NonNull<c_void>> { | ||
| // Despite having only a single atomic variable (self.addr), we still | ||
| // cannot always use Ordering::Relaxed, as we need to make sure a | ||
| // successful call to dlsym() is "ordered before" any data read through | ||
josephlr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // the returned pointer (which occurs when the function is called). | ||
| // Our implementation mirrors that of the one in libstd, meaning that | ||
| // the use of non-Relaxed operations is probably unnecessary. | ||
| match self.addr.load(Ordering::Relaxed) { | ||
| Self::UNINIT => { | ||
| let addr = (self.link_fn)(); | ||
| // Synchronizes with the Acquire fence below | ||
| self.addr.store(addr, Ordering::Release); | ||
| NonNull::new(addr) | ||
| } | ||
| addr => { | ||
| let func = NonNull::new(addr)?; | ||
| fence(Ordering::Acquire); | ||
| Some(func) | ||
| } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.