Skip to content

Instantly share code, notes, and snippets.

@redradist
Last active May 24, 2020 12:59
Show Gist options
  • Save redradist/d0fd2b49717a2ac72df9f2f302234c22 to your computer and use it in GitHub Desktop.
Save redradist/d0fd2b49717a2ac72df9f2f302234c22 to your computer and use it in GitHub Desktop.
use ferris_gc::{Trace, Finalize, ferris_gc_main, ApplicationCleanup, BASIC_STRATEGY_GLOBAL_GC};
use ferris_gc::sync::{Gc, GcRefCell, GLOBAL_GC_STRATEGY};
use std::{time, thread};
use std::fs::File;
use std::io::Write;
use std::sync::atomic::Ordering;
use std::sync::RwLock;
#[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() });
}
{
let gc0 = Gc::new(MyStruct { id: 17 });
let gc1 = Gc::new(MyStruct { id: 3 });
thread::spawn(move || {
println!("gc1.id is {}", gc1.id);
});
let global_strategy = &(*GLOBAL_GC_STRATEGY);
global_strategy.change_strategy(|global_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 { global_gc.collect(); }
}
});
None
},
|global_gc| {
let mut basic_strategy_global_gc = BASIC_STRATEGY_GLOBAL_GC.write().unwrap();
*basic_strategy_global_gc = Some(global_gc);
});
global_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