1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
//! # generator
//!
//! Rust generator implementation
//!
use crate::detail::gen_init;
use std::any::Any;
use std::fmt;
use std::marker::PhantomData;
use std::panic;
use std::thread;
use crate::reg_context::RegContext;
use crate::rt::{Context, ContextStack, Error};
use crate::scope::Scope;
use crate::stack::{Func, Stack, StackBox};
/// The default stack size for generators, in bytes.
// windows has a minimal size as 0x4a8!!!!
pub const DEFAULT_STACK_SIZE: usize = 0x1000;
/// the generator obj type, the functor passed to it must be Send
pub struct GeneratorObj<'a, A, T, const LOCAL: bool> {
gen: StackBox<GeneratorImpl<'a, A, T>>,
}
/// the generator type, the functor passed to it must be Send
pub type Generator<'a, A, T> = GeneratorObj<'a, A, T, false>;
// only when A, T and Functor are all sendable, the generator could be send
unsafe impl<A: Send, T: Send> Send for Generator<'static, A, T> {}
impl<'a, A, T> Generator<'a, A, T> {
/// init a heap based generator with scoped closure
pub fn scoped_init<F>(&mut self, f: F)
where
for<'scope> F: FnOnce(Scope<'scope, 'a, A, T>) -> T + Send + 'a,
T: Send + 'a,
A: Send + 'a,
{
self.gen.scoped_init(f);
}
/// init a heap based generator
// it's can be used to re-init a 'done' generator before it's get dropped
pub fn init_code<F: FnOnce() -> T + Send + 'a>(&mut self, f: F)
where
T: Send + 'a,
{
self.gen.init_code(f);
}
}
/// the local generator type, can't Send
pub type LocalGenerator<'a, A, T> = GeneratorObj<'a, A, T, true>;
impl<'a, A, T> LocalGenerator<'a, A, T> {
/// init a heap based generator with scoped closure
pub fn scoped_init<F>(&mut self, f: F)
where
for<'scope> F: FnOnce(Scope<'scope, 'a, A, T>) -> T + 'a,
T: 'a,
A: 'a,
{
self.gen.scoped_init(f);
}
}
impl<'a, A, T, const LOCAL: bool> GeneratorObj<'a, A, T, LOCAL> {
/// Constructs a Generator from a raw pointer.
///
/// # Safety
///
/// This function is unsafe because improper use may lead to
/// memory problems. For example, a double-free may occur if the
/// function is called twice on the same raw pointer.
#[inline]
pub unsafe fn from_raw(raw: *mut usize) -> Self {
GeneratorObj {
gen: StackBox::from_raw(raw as *mut GeneratorImpl<'a, A, T>),
}
}
/// Consumes the `Generator`, returning a wrapped raw pointer.
#[inline]
pub fn into_raw(self) -> *mut usize {
let ret = self.gen.as_ptr() as *mut usize;
std::mem::forget(self);
ret
}
/// prefetch the generator into cache
#[inline]
pub fn prefetch(&self) {
self.gen.prefetch();
}
/// prepare the para that passed into generator before send
#[inline]
pub fn set_para(&mut self, para: A) {
self.gen.set_para(para);
}
/// set the generator local data
#[inline]
pub fn set_local_data(&mut self, data: *mut u8) {
self.gen.set_local_data(data);
}
/// get the generator local data
#[inline]
pub fn get_local_data(&self) -> *mut u8 {
self.gen.get_local_data()
}
/// get the generator panic data
#[inline]
pub fn get_panic_data(&mut self) -> Option<Box<dyn Any + Send>> {
self.gen.get_panic_data()
}
/// resume the generator without touch the para
/// you should call `set_para` before this method
#[inline]
pub fn resume(&mut self) -> Option<T> {
self.gen.resume()
}
/// `raw_send`
#[inline]
pub fn raw_send(&mut self, para: Option<A>) -> Option<T> {
self.gen.raw_send(para)
}
/// send interface
pub fn send(&mut self, para: A) -> T {
self.gen.send(para)
}
/// cancel the generator
/// this will trigger a Cancel panic to unwind the stack and finish the generator
pub fn cancel(&mut self) {
self.gen.cancel()
}
/// is finished
#[inline]
pub fn is_done(&self) -> bool {
self.gen.is_done()
}
/// get stack total size and used size in word
pub fn stack_usage(&self) -> (usize, usize) {
self.gen.stack_usage()
}
}
impl<'a, T, const LOCAL: bool> Iterator for GeneratorObj<'a, (), T, LOCAL> {
type Item = T;
fn next(&mut self) -> Option<T> {
self.resume()
}
}
impl<'a, A, T, const LOCAL: bool> fmt::Debug for GeneratorObj<'a, A, T, LOCAL> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Generator<{}, Output={}, Local={}> {{ ... }}",
std::any::type_name::<A>(),
std::any::type_name::<T>(),
LOCAL
)
}
}
/// Generator helper
pub struct Gn<A = ()> {
dummy: PhantomData<A>,
}
impl<A> Gn<A> {
/// create a scoped generator with default stack size
pub fn new_scoped<'a, T, F>(f: F) -> Generator<'a, A, T>
where
for<'scope> F: FnOnce(Scope<'scope, 'a, A, T>) -> T + Send + 'a,
T: Send + 'a,
A: Send + 'a,
{
Self::new_scoped_opt(DEFAULT_STACK_SIZE, f)
}
/// create a scoped local generator with default stack size
pub fn new_scoped_local<'a, T, F>(f: F) -> LocalGenerator<'a, A, T>
where
F: FnOnce(Scope<A, T>) -> T + 'a,
T: 'a,
A: 'a,
{
Self::new_scoped_opt_local(DEFAULT_STACK_SIZE, f)
}
/// create a scoped generator with specified stack size
pub fn new_scoped_opt<'a, T, F>(size: usize, f: F) -> Generator<'a, A, T>
where
for<'scope> F: FnOnce(Scope<'scope, 'a, A, T>) -> T + Send + 'a,
T: Send + 'a,
A: Send + 'a,
{
let mut gen = GeneratorImpl::<A, T>::new(Stack::new(size));
gen.scoped_init(f);
Generator { gen }
}
/// create a scoped local generator with specified stack size
pub fn new_scoped_opt_local<'a, T, F>(size: usize, f: F) -> LocalGenerator<'a, A, T>
where
F: FnOnce(Scope<A, T>) -> T + 'a,
T: 'a,
A: 'a,
{
let mut gen = GeneratorImpl::<A, T>::new(Stack::new(size));
gen.scoped_init(f);
LocalGenerator { gen }
}
}
impl<A: Any> Gn<A> {
/// create a new generator with default stack size
#[allow(clippy::new_ret_no_self)]
#[deprecated(since = "0.6.18", note = "please use `scope` version instead")]
pub fn new<'a, T: Any, F>(f: F) -> Generator<'a, A, T>
where
F: FnOnce() -> T + Send + 'a,
{
Self::new_opt(DEFAULT_STACK_SIZE, f)
}
/// create a new generator with specified stack size
// the `may` library use this API so we can't deprecated it yet.
pub fn new_opt<'a, T: Any, F>(size: usize, f: F) -> Generator<'a, A, T>
where
F: FnOnce() -> T + Send + 'a,
{
let mut gen = GeneratorImpl::<A, T>::new(Stack::new(size));
gen.init_context();
gen.init_code(f);
Generator { gen }
}
}
/// `GeneratorImpl`
#[repr(C)]
struct GeneratorImpl<'a, A, T> {
// run time context
context: Context,
// stack
stack: Stack,
// save the input
para: Option<A>,
// save the output
ret: Option<T>,
// boxed functor
f: Option<Func>,
// phantom lifetime
phantom: PhantomData<&'a T>,
}
impl<'a, A: Any, T: Any> GeneratorImpl<'a, A, T> {
/// create a new generator with default stack size
fn init_context(&mut self) {
unsafe {
std::ptr::write(
self.context.para.as_mut_ptr(),
&mut self.para as &mut dyn Any,
);
std::ptr::write(self.context.ret.as_mut_ptr(), &mut self.ret as &mut dyn Any);
}
}
}
impl<'a, A, T> GeneratorImpl<'a, A, T> {
/// create a new generator with specified stack size
fn new(mut stack: Stack) -> StackBox<Self> {
// the stack box would finally dealloc the stack!
unsafe {
let mut stack_box = stack.alloc_uninit_box::<GeneratorImpl<'a, A, T>>();
(*stack_box.as_mut_ptr()).init(GeneratorImpl {
para: None,
stack,
ret: None,
f: None,
context: Context::new(),
phantom: PhantomData,
});
stack_box.assume_init()
}
}
/// prefetch the generator into cache
#[inline]
pub fn prefetch(&self) {
self.context.regs.prefetch();
}
/// init a heap based generator with scoped closure
fn scoped_init<F>(&mut self, f: F)
where
for<'scope> F: FnOnce(Scope<'scope, 'a, A, T>) -> T + 'a,
T: 'a,
A: 'a,
{
use std::mem::transmute;
let scope = unsafe { transmute(Scope::new(&mut self.para, &mut self.ret)) };
self.init_code(move || f(scope));
}
/// init a heap based generator
// it's can be used to re-init a 'done' generator before it's get dropped
fn init_code<F: FnOnce() -> T + 'a>(&mut self, f: F)
where
T: 'a,
{
// make sure the last one is finished
if self.f.is_none() && self.context._ref == 0 {
self.cancel();
} else {
let _ = self.f.take();
}
// init ctx parent to itself, this would be the new top
self.context.parent = &mut self.context;
// init the ref to 0 means that it's ready to start
self.context._ref = 0;
let ret = &mut self.ret as *mut _;
// alloc the function on stack
let func = StackBox::new_fn_once(&mut self.stack, move || {
let r = f();
unsafe { *ret = Some(r) };
});
self.f = Some(func);
let guard = (self.stack.begin() as usize, self.stack.end() as usize);
self.context.stack_guard = guard;
self.context.regs.init_with(
gen_init,
0,
&mut self.f as *mut _ as *mut usize,
&self.stack,
);
}
/// resume the generator
#[inline]
fn resume_gen(&mut self) {
let env = ContextStack::current();
// get the current regs
let cur = &mut env.top().regs;
// switch to new context, always use the top context's reg
// for normal generator self.context.parent == self.context
// for coroutine self.context.parent == top generator context
debug_assert!(!self.context.parent.is_null());
let top = unsafe { &mut *self.context.parent };
// save current generator context on stack
env.push_context(&mut self.context);
// swap to the generator
RegContext::swap(cur, &top.regs);
// comes back, check the panic status
// this would propagate the panic until root context
// if it's a coroutine just stop propagate
if !self.context.local_data.is_null() {
return;
}
if let Some(err) = self.context.err.take() {
// pass the error to the parent until root
panic::resume_unwind(err);
}
}
#[inline]
fn is_started(&self) -> bool {
// when the f is consumed we think it's running
self.f.is_none()
}
/// prepare the para that passed into generator before send
#[inline]
fn set_para(&mut self, para: A) {
self.para = Some(para);
}
/// set the generator local data
#[inline]
fn set_local_data(&mut self, data: *mut u8) {
self.context.local_data = data;
}
/// get the generator local data
#[inline]
fn get_local_data(&self) -> *mut u8 {
self.context.local_data
}
/// get the generator panic data
#[inline]
fn get_panic_data(&mut self) -> Option<Box<dyn Any + Send>> {
self.context.err.take()
}
/// resume the generator without touch the para
/// you should call `set_para` before this method
#[inline]
fn resume(&mut self) -> Option<T> {
if self.is_done() {
return None;
}
// every time we call the function, increase the ref count
// yield will decrease it and return will not
self.context._ref += 1;
self.resume_gen();
self.ret.take()
}
/// `raw_send`
#[inline]
fn raw_send(&mut self, para: Option<A>) -> Option<T> {
if self.is_done() {
return None;
}
// this is the passed in value of the send primitive
// the yield part would read out this value in the next round
self.para = para;
// every time we call the function, increase the ref count
// yield will decrease it and return will not
self.context._ref += 1;
self.resume_gen();
self.ret.take()
}
/// send interface
fn send(&mut self, para: A) -> T {
let ret = self.raw_send(Some(para));
ret.expect("send got None return")
}
/// cancel the generator without any check
#[inline]
fn raw_cancel(&mut self) {
// tell the func to panic
// so that we can stop the inner func
self.context._ref = 2;
// save the old panic hook, we don't want to print anything for the Cancel
let old = panic::take_hook();
panic::set_hook(Box::new(|_| {}));
self.resume_gen();
panic::set_hook(old);
}
/// cancel the generator
/// this will trigger a Cancel panic to unwind the stack
fn cancel(&mut self) {
if self.is_done() {
return;
}
// consume the fun if it's not started
if !self.is_started() {
self.f.take();
self.context._ref = 1;
} else {
self.raw_cancel();
}
}
/// is finished
#[inline]
fn is_done(&self) -> bool {
self.is_started() && (self.context._ref & 0x3) != 0
}
/// get stack total size and used size in word
fn stack_usage(&self) -> (usize, usize) {
(self.stack.size(), self.stack.get_used_size())
}
}
impl<'a, A, T> Drop for GeneratorImpl<'a, A, T> {
fn drop(&mut self) {
// when the thread is already panic, do nothing
if thread::panicking() {
return;
}
if !self.is_started() {
// not started yet, just drop the gen
return;
}
if !self.is_done() {
trace!("generator is not done while drop");
self.raw_cancel()
}
assert!(self.is_done());
let (total_stack, used_stack) = self.stack_usage();
if used_stack < total_stack {
// here we should record the stack in the class
// next time will just use
// set_stack_size::<F>(used_stack);
} else {
error!("stack overflow detected!");
panic::panic_any(Error::StackErr);
}
}
}