Skip to content

Instantly share code, notes, and snippets.

@redradist
Last active May 24, 2020 12:59
Show Gist options
  • Save redradist/a8b5c7c6ced77a02f38f425eb68e410d to your computer and use it in GitHub Desktop.
Save redradist/a8b5c7c6ced77a02f38f425eb68e410d to your computer and use it in GitHub Desktop.
use ferris_gc::{Gc, GcRefCell, Trace, Finalize, ferris_gc_main, ApplicationCleanup, LOCAL_GC_STRATEGY, BASIC_STRATEGY_LOCAL_GCS};
use std::{time, thread};
use std::fs::File;
use std::io::Write;
use std::sync::atomic::Ordering;
#[derive(Trace)]
struct MyStruct {
id: u32,
}
impl Finalize for MyStruct {
fn finalize(&self) {
println!("MyStruct in finalize !!");
let mut file = File::create("foo.txt");
match file {
Ok(mut f) => { f.write_all(b"Hello, world!") },
Err(e) => { Err(e) },
};
}
}
#[derive(Trace, Finalize)]
struct Counter {
count: Gc<u32>,
}
#[ferris_gc_main]
fn main() {
{
let counter = Gc::new(2);
let gc0 = Gc::new(MyStruct { id: 3 });
let gc1 = GcRefCell::new(MyStruct { id: 4 });
gc1.borrow_mut().id = 5;
let gc3 = Gc::new(Counter { count: counter.clone() });
LOCAL_GC_STRATEGY.with(|local_gc_strategy| {
local_gc_strategy.borrow_mut().change_strategy(|local_gc, app_active| {
thread::spawn(move || {
while app_active.load(Ordering::Acquire) {
let five_secs = time::Duration::from_millis(5000);
thread::sleep(five_secs);
unsafe { local_gc.collect(); }
}
});
None
},
|local_gc| {
let mut basic_strategy_local_gcs = BASIC_STRATEGY_LOCAL_GCS.write().unwrap();
basic_strategy_local_gcs.push(local_gc);
});
let strategy = unsafe { &mut *local_gc_strategy.as_ptr() };
strategy.start();
});
}
let ten_secs = time::Duration::from_secs(10);
thread::sleep(ten_secs);
println!("Hello, GC World !!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment