forked from GitHub/bdwgc-rust
Make thread utilities unsafe
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -2,7 +2,7 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bdwgc-alloc"
|
name = "bdwgc-alloc"
|
||||||
version = "0.1.1"
|
version = "0.2.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "bdwgc-alloc"
|
name = "bdwgc-alloc"
|
||||||
description = "impl GlobalAlloc for bdwgc"
|
description = "impl GlobalAlloc for bdwgc"
|
||||||
version = "0.1.1"
|
version = "0.2.0"
|
||||||
authors = ["swgillespie <sean.william.g@gmail.com>", "Yota Toyama <raviqqe@gmail.com>"]
|
authors = ["swgillespie <sean.william.g@gmail.com>", "Yota Toyama <raviqqe@gmail.com>"]
|
||||||
repository = "https://github.com/raviqqe/bdwgc-rs"
|
repository = "https://github.com/raviqqe/bdwgc-rs"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|||||||
4
examples/allocation_loop/Cargo.lock
generated
4
examples/allocation_loop/Cargo.lock
generated
@@ -4,12 +4,12 @@
|
|||||||
name = "allocation_loop"
|
name = "allocation_loop"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bdwgc-alloc 0.1.1",
|
"bdwgc-alloc 0.2.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bdwgc-alloc"
|
name = "bdwgc-alloc"
|
||||||
version = "0.1.1"
|
version = "0.2.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|||||||
4
examples/dynamic_threads/Cargo.lock
generated
4
examples/dynamic_threads/Cargo.lock
generated
@@ -2,7 +2,7 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bdwgc-alloc"
|
name = "bdwgc-alloc"
|
||||||
version = "0.1.1"
|
version = "0.2.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
@@ -11,7 +11,7 @@ dependencies = [
|
|||||||
name = "dynamic_threads"
|
name = "dynamic_threads"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bdwgc-alloc 0.1.1",
|
"bdwgc-alloc 0.2.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ fn main() {
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
let handle = std::thread::spawn(move || {
|
let handle = std::thread::spawn(move || {
|
||||||
Allocator::register_current_thread().unwrap();
|
unsafe { Allocator::register_current_thread().unwrap() }
|
||||||
|
|
||||||
let mut _n =
|
let mut _n =
|
||||||
unsafe { GLOBAL_ALLOCATOR.alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) };
|
unsafe { GLOBAL_ALLOCATOR.alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) };
|
||||||
@@ -20,7 +20,7 @@ fn main() {
|
|||||||
_n = unsafe { GLOBAL_ALLOCATOR.alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) }
|
_n = unsafe { GLOBAL_ALLOCATOR.alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
Allocator::unregister_current_thread();
|
unsafe { Allocator::unregister_current_thread() }
|
||||||
});
|
});
|
||||||
|
|
||||||
handle.join().unwrap();
|
handle.join().unwrap();
|
||||||
|
|||||||
4
examples/static_threads/Cargo.lock
generated
4
examples/static_threads/Cargo.lock
generated
@@ -2,7 +2,7 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bdwgc-alloc"
|
name = "bdwgc-alloc"
|
||||||
version = "0.1.1"
|
version = "0.2.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
@@ -16,7 +16,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
name = "static_threads"
|
name = "static_threads"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bdwgc-alloc 0.1.1",
|
"bdwgc-alloc 0.2.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[metadata]
|
[metadata]
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ fn main() {
|
|||||||
unsafe { Allocator::initialize() }
|
unsafe { Allocator::initialize() }
|
||||||
|
|
||||||
let handle = std::thread::spawn(move || {
|
let handle = std::thread::spawn(move || {
|
||||||
Allocator::register_current_thread().unwrap();
|
unsafe { Allocator::register_current_thread().unwrap() }
|
||||||
|
|
||||||
let mut _n = unsafe { GLOBAL_ALLOCATOR.alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) };
|
let mut _n = unsafe { GLOBAL_ALLOCATOR.alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) };
|
||||||
|
|
||||||
|
|||||||
10
src/lib.rs
10
src/lib.rs
@@ -32,23 +32,23 @@ impl Allocator {
|
|||||||
GC_allow_register_threads();
|
GC_allow_register_threads();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn register_current_thread() -> Result<(), error::Error> {
|
pub unsafe fn register_current_thread() -> Result<(), error::Error> {
|
||||||
let mut base = GcStackBase {
|
let mut base = GcStackBase {
|
||||||
mem_base: std::ptr::null(),
|
mem_base: std::ptr::null(),
|
||||||
reg_base: std::ptr::null(),
|
reg_base: std::ptr::null(),
|
||||||
};
|
};
|
||||||
|
|
||||||
if unsafe { GC_get_stack_base(&mut base) } != GC_SUCCESS {
|
if GC_get_stack_base(&mut base) != GC_SUCCESS {
|
||||||
return Err(error::Error::new("failed to get stack base"));
|
return Err(error::Error::new("failed to get stack base"));
|
||||||
} else if unsafe { GC_register_my_thread(&base) } != GC_SUCCESS {
|
} else if GC_register_my_thread(&base) != GC_SUCCESS {
|
||||||
return Err(error::Error::new("failed to register a thread for GC"));
|
return Err(error::Error::new("failed to register a thread for GC"));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unregister_current_thread() {
|
pub unsafe fn unregister_current_thread() {
|
||||||
unsafe { GC_unregister_my_thread() }
|
GC_unregister_my_thread()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user