diff --git a/library/bdwgc/src/lib.rs b/library/bdwgc/src/lib.rs index 19f8b7a775a3e..2ef9f534d1e99 100644 --- a/library/bdwgc/src/lib.rs +++ b/library/bdwgc/src/lib.rs @@ -79,6 +79,8 @@ extern "C" { pub fn GC_init(); + pub fn GC_set_markers_count(count: usize); + pub fn GC_set_warn_proc(level: *mut u8); pub fn GC_tls_rootset() -> *mut u8; diff --git a/library/std/src/gc.rs b/library/std/src/gc.rs index 4907adccadae0..2e8b81da63bdb 100644 --- a/library/std/src/gc.rs +++ b/library/std/src/gc.rs @@ -40,18 +40,15 @@ use core::{ alloc::{AllocError, Allocator, GlobalAlloc, Layout}, any::Any, - cell::RefCell, cmp::{self, Ordering}, fmt, hash::{Hash, Hasher}, marker::{FinalizerSafe, PhantomData, Unsize}, - mem::{transmute, MaybeUninit}, + mem::MaybeUninit, ops::{CoerceUnsized, Deref, DispatchFromDyn, Receiver}, ptr::{self, drop_in_place, null_mut, NonNull}, }; -use crate::{sync::mpsc, sync::mpsc::Sender, thread}; - pub use core::gc::*; #[cfg(profile_gc)] @@ -167,6 +164,7 @@ impl GcAllocator { //////////////////////////////////////////////////////////////////////////////// pub fn init() { + unsafe { bdwgc::GC_set_markers_count(1) } unsafe { bdwgc::GC_init() } } @@ -377,15 +375,11 @@ impl Gc { #[cfg(profile_gc)] FINALIZERS_REGISTERED.fetch_add(1, atomic::Ordering::Relaxed); - // This function gets called by Boehm when an object becomes unreachable. - unsafe extern "C" fn finalizer(object: *mut u8, _meta: *mut u8) { + unsafe extern "C" fn finalizer(obj: *mut u8, _meta: *mut u8) { unsafe { - let cb = finalizer_callback::; - let fo = FinalizableObj { - callback: transmute(cb as unsafe extern "C" fn(*mut T)), - object, - }; - FINALIZER_QUEUE.with(|q| q.borrow().as_ref().unwrap().send(fo).unwrap()); + drop_in_place(obj as *mut T); + #[cfg(profile_gc)] + FINALIZERS_COMPLETED.fetch_add(1, atomic::Ordering::Relaxed); } } @@ -399,6 +393,37 @@ impl Gc { ) } } + + #[unstable(feature = "gc", issue = "none")] + pub fn unregister_finalizer(&mut self) { + let ptr = self.ptr.as_ptr() as *mut GcBox as *mut u8; + unsafe { + bdwgc::GC_register_finalizer( + ptr, + None, + ::core::ptr::null_mut(), + ::core::ptr::null_mut(), + ::core::ptr::null_mut(), + ); + } + } +} + +#[cfg(profile_gc)] +#[derive(Debug)] +pub struct FinalizerInfo { + pub registered: u64, + pub completed: u64, +} + +#[cfg(profile_gc)] +impl FinalizerInfo { + pub fn finalizer_info() -> FinalizerInfo { + FinalizerInfo { + registered: FINALIZERS_REGISTERED.load(atomic::Ordering::Relaxed), + completed: FINALIZERS_COMPLETED.load(atomic::Ordering::Relaxed), + } + } } impl Gc { @@ -755,53 +780,3 @@ impl AsRef for Gc { &**self } } - -thread_local! { - pub static FINALIZER_QUEUE: RefCell> = RefCell::new(None); -} - -type FinalizerQueue = Sender; - -#[derive(Debug)] -pub struct FinalizableObj { - callback: FinalizerCallback, - object: *mut u8, -} - -unsafe impl Send for FinalizableObj {} - -pub(crate) fn init_finalization_thread() { - let (sender, receiver) = mpsc::channel(); - FINALIZER_QUEUE.with(|q| *q.borrow_mut() = Some(sender)); - - thread::spawn(move || { - for finalisable in receiver.iter() { - unsafe { (finalisable.callback)(finalisable.object) }; - } - }); -} - -unsafe extern "C" fn finalizer_callback(obj: *mut T) { - unsafe { drop_in_place(obj as *mut T) }; - #[cfg(profile_gc)] - FINALIZERS_COMPLETED.fetch_add(1, atomic::Ordering::Relaxed); -} - -type FinalizerCallback = unsafe extern "C" fn(data: *mut u8); - -#[cfg(profile_gc)] -#[derive(Debug)] -pub struct FinalizerInfo { - pub registered: u64, - pub completed: u64, -} - -#[cfg(profile_gc)] -impl FinalizerInfo { - pub fn finalizer_info() -> FinalizerInfo { - FinalizerInfo { - registered: FINALIZERS_REGISTERED.load(atomic::Ordering::Relaxed), - completed: FINALIZERS_COMPLETED.load(atomic::Ordering::Relaxed), - } - } -} diff --git a/library/std/src/rt.rs b/library/std/src/rt.rs index 16bbf77717629..ea65829d6b0c1 100644 --- a/library/std/src/rt.rs +++ b/library/std/src/rt.rs @@ -112,8 +112,6 @@ unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) { // info about the stack bounds. let thread = Thread::new(Some(rtunwrap!(Ok, CString::new("main")))); thread_info::set(main_guard, thread); - - crate::gc::init_finalization_thread(); } } diff --git a/library/std/src/thread/mod.rs b/library/std/src/thread/mod.rs index 388f972ccc0fa..59529ceb4d999 100644 --- a/library/std/src/thread/mod.rs +++ b/library/std/src/thread/mod.rs @@ -162,7 +162,6 @@ use crate::any::Any; use crate::cell::UnsafeCell; use crate::ffi::{CStr, CString}; use crate::fmt; -use crate::gc::FINALIZER_QUEUE; use crate::io; use crate::marker::PhantomData; use crate::mem::{self, forget}; @@ -513,16 +512,11 @@ impl Builder { } let f = MaybeDangling::new(f); - - let fin_q_sender = FINALIZER_QUEUE.with(|q| q.borrow().clone()); - let main = move || { if let Some(name) = their_thread.cname() { imp::Thread::set_name(name); } - FINALIZER_QUEUE.with(|q| *q.borrow_mut() = fin_q_sender); - crate::io::set_output_capture(output_capture); // SAFETY: we constructed `f` initialized. diff --git a/src/bdwgc b/src/bdwgc index 10f5f847e9460..a601f2a49e199 160000 --- a/src/bdwgc +++ b/src/bdwgc @@ -1 +1 @@ -Subproject commit 10f5f847e946011c6a689a4318605585855b9c01 +Subproject commit a601f2a49e1991007123a24520f6e6b00a79c7f3