From 06505aa49ce48d13ca839a57b6dec1afe6be2a01 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sun, 14 Apr 2019 06:17:54 +0000 Subject: [PATCH] Add dynamic threads example --- examples/dynamic_threads/Cargo.lock | 23 +++++++++++++++++++++++ examples/dynamic_threads/Cargo.toml | 9 +++++++++ examples/dynamic_threads/src/main.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 examples/dynamic_threads/Cargo.lock create mode 100644 examples/dynamic_threads/Cargo.toml create mode 100644 examples/dynamic_threads/src/main.rs diff --git a/examples/dynamic_threads/Cargo.lock b/examples/dynamic_threads/Cargo.lock new file mode 100644 index 0000000..1736337 --- /dev/null +++ b/examples/dynamic_threads/Cargo.lock @@ -0,0 +1,23 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "bdwgc-allocator" +version = "0.1.0" +dependencies = [ + "libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "dynamic_threads" +version = "0.1.0" +dependencies = [ + "bdwgc-allocator 0.1.0", +] + +[[package]] +name = "libc" +version = "0.2.51" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)" = "bedcc7a809076656486ffe045abeeac163da1b558e963a31e29fbfbeba916917" diff --git a/examples/dynamic_threads/Cargo.toml b/examples/dynamic_threads/Cargo.toml new file mode 100644 index 0000000..90d5da0 --- /dev/null +++ b/examples/dynamic_threads/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "dynamic_threads" +version = "0.1.0" +authors = ["Yota Toyama "] +edition = "2018" +publish = false + +[dependencies] +bdwgc-allocator = { path = "../.." } diff --git a/examples/dynamic_threads/src/main.rs b/examples/dynamic_threads/src/main.rs new file mode 100644 index 0000000..284eda9 --- /dev/null +++ b/examples/dynamic_threads/src/main.rs @@ -0,0 +1,28 @@ +extern crate bdwgc_allocator; + +use bdwgc_allocator::Allocator; +use std::alloc::{GlobalAlloc, Layout}; + +#[global_allocator] +static GLOBAL_ALLOCATOR: Allocator = Allocator; + +fn main() { + unsafe { Allocator::initialize() } + + loop { + let handle = std::thread::spawn(move || { + Allocator::register_current_thread().unwrap(); + + let mut _n = + unsafe { GLOBAL_ALLOCATOR.alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) }; + + for _ in 0..100 { + _n = unsafe { GLOBAL_ALLOCATOR.alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) } + } + + Allocator::unregister_current_thread(); + }); + + handle.join().unwrap(); + } +}