2015-04-16 20:06:57 +08:00
|
|
|
// This file is part of libfringe, a low-level green threading library.
|
2016-03-22 15:25:23 +08:00
|
|
|
// Copyright (c) edef <edef@edef.eu>
|
2015-04-16 18:08:44 +08:00
|
|
|
// See the LICENSE file included in this distribution.
|
2015-04-16 03:54:07 +08:00
|
|
|
#![feature(test)]
|
|
|
|
extern crate test;
|
2015-04-16 20:06:57 +08:00
|
|
|
extern crate fringe;
|
2016-03-22 14:35:25 +08:00
|
|
|
use fringe::Context;
|
2015-04-16 03:54:07 +08:00
|
|
|
|
2015-04-16 15:53:49 +08:00
|
|
|
static mut ctx_slot: *mut Context<'static, SliceStack<'static>> = 0 as *mut Context<_>;
|
2015-04-16 10:01:08 +08:00
|
|
|
static mut stack_buf: [u8; 1024] = [0; 1024];
|
2015-04-16 03:54:07 +08:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn context_new(b: &mut test::Bencher) {
|
|
|
|
b.iter(|| unsafe {
|
2015-04-16 10:01:08 +08:00
|
|
|
let stack = SliceStack(&mut stack_buf);
|
2015-04-16 04:25:09 +08:00
|
|
|
|
|
|
|
let mut ctx = Context::new(stack, move || {
|
2015-04-16 03:54:07 +08:00
|
|
|
let ctx_ptr = ctx_slot;
|
|
|
|
loop {
|
2016-04-04 01:54:40 +08:00
|
|
|
Context::swap(ctx_ptr, ctx_ptr);
|
2015-04-16 03:54:07 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx_slot = &mut ctx;
|
|
|
|
|
2016-04-04 01:54:40 +08:00
|
|
|
Context::swap(ctx_slot, ctx_slot);
|
2015-04-16 03:54:07 +08:00
|
|
|
})
|
|
|
|
}
|
2015-04-16 10:01:08 +08:00
|
|
|
|
|
|
|
struct SliceStack<'a>(&'a mut [u8]);
|
2015-04-16 20:06:57 +08:00
|
|
|
impl<'a> fringe::Stack for SliceStack<'a> {
|
2015-04-16 10:01:08 +08:00
|
|
|
fn top(&mut self) -> *mut u8 {
|
|
|
|
unsafe {
|
|
|
|
self.0.as_mut_ptr().offset(self.0.len() as isize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn limit(&self) -> *const u8 {
|
|
|
|
self.0.as_ptr()
|
|
|
|
}
|
|
|
|
}
|