forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#102655 - joboet:windows_tls_opt, r=ChrisDenton
Optimize TLS on Windows This implements the suggestion in the current TLS code to embed the linked list of destructors in the `StaticKey` structure to save allocations. Additionally, locking is avoided when no destructor needs to be run. By using one Windows-provided `Once` per key instead of a global lock, locking is more finely-grained (this unblocks rust-lang#100579).
- Loading branch information
Showing
9 changed files
with
202 additions
and
117 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,53 @@ | ||
use super::StaticKey; | ||
use crate::ptr; | ||
|
||
#[test] | ||
fn smoke() { | ||
static K1: StaticKey = StaticKey::new(None); | ||
static K2: StaticKey = StaticKey::new(None); | ||
|
||
unsafe { | ||
assert!(K1.get().is_null()); | ||
assert!(K2.get().is_null()); | ||
K1.set(ptr::invalid_mut(1)); | ||
K2.set(ptr::invalid_mut(2)); | ||
assert_eq!(K1.get() as usize, 1); | ||
assert_eq!(K2.get() as usize, 2); | ||
} | ||
} | ||
|
||
#[test] | ||
fn destructors() { | ||
use crate::mem::ManuallyDrop; | ||
use crate::sync::Arc; | ||
use crate::thread; | ||
|
||
unsafe extern "C" fn destruct(ptr: *mut u8) { | ||
drop(Arc::from_raw(ptr as *const ())); | ||
} | ||
|
||
static KEY: StaticKey = StaticKey::new(Some(destruct)); | ||
|
||
let shared1 = Arc::new(()); | ||
let shared2 = Arc::clone(&shared1); | ||
|
||
unsafe { | ||
assert!(KEY.get().is_null()); | ||
KEY.set(Arc::into_raw(shared1) as *mut u8); | ||
} | ||
|
||
thread::spawn(move || unsafe { | ||
assert!(KEY.get().is_null()); | ||
KEY.set(Arc::into_raw(shared2) as *mut u8); | ||
}) | ||
.join() | ||
.unwrap(); | ||
|
||
// Leak the Arc, let the TLS destructor clean it up. | ||
let shared1 = unsafe { ManuallyDrop::new(Arc::from_raw(KEY.get() as *const ())) }; | ||
assert_eq!( | ||
Arc::strong_count(&shared1), | ||
1, | ||
"destructor should have dropped the other reference on thread exit" | ||
); | ||
} |
Oops, something went wrong.