| 384 | } |
| 385 | |
| 386 | void resume(const fiber_t &fi_p) |
| 387 | { |
| 388 | static cs_fiber_ucontext_t global_ctx; |
| 389 | unix_fiber *fi = static_cast<unix_fiber *>(fi_p.get()); |
| 390 | if (fi == nullptr) |
| 391 | throw internal_error("Resuming a corrupted fiber."); |
| 392 | if (fi->state == fiber_state::running || fi->state == fiber_state::finished) |
| 393 | throw lang_error("Fiber is not reentrant."); |
| 394 | if (fi->state == fiber_state::ready) { |
| 395 | memset(&fi->ctx, 0, sizeof(fi->ctx)); |
| 396 | cs_fiber_getcontext(&fi->ctx); |
| 397 | fi->ctx.uc_stack.ss_sp = fi->stack.get_sp(); |
| 398 | fi->ctx.uc_stack.ss_size = fi->stack.get_ss(); |
| 399 | fi->ctx.uc_link = nullptr; |
| 400 | cs_fiber_makecontext(&fi->ctx, reinterpret_cast<void (*)()>(unix_fiber::entry), 0); |
| 401 | if (!current_process->fiber_stack.empty()) |
| 402 | fi->prev_ctx = &static_cast<unix_fiber *>(current_process->fiber_stack.top().get())->ctx; |
| 403 | else |
| 404 | fi->prev_ctx = &global_ctx; |
| 405 | } |
| 406 | else if (fi->state == fiber_state::suspended) { |
| 407 | if (fi->prev_ctx == nullptr) |
| 408 | throw internal_error("Fiber context corrupted."); |
| 409 | fi->state = fiber_state::running; |
| 410 | } |
| 411 | fi->state = fiber_state::running; |
| 412 | current_process->fiber_stack.push(fi_p); |
| 413 | fi->cs_swap_in(); |
| 414 | cs_fiber_swapcontext(fi->prev_ctx, &fi->ctx); |
| 415 | if (!current_process->fiber_stack.empty()) |
| 416 | current_process->fiber_stack.pop(); |
| 417 | else |
| 418 | throw internal_error("Fiber stack corrupted."); |
| 419 | if (!current_process->fiber_stack.empty()) |
| 420 | static_cast<unix_fiber *>(current_process->fiber_stack.top().get())->cs_swap_in(); |
| 421 | else |
| 422 | fi->cs_swap_out(); |
| 423 | if (fi->eptr != nullptr) { |
| 424 | std::exception_ptr e = nullptr; |
| 425 | std::swap(fi->eptr, e); |
| 426 | std::rethrow_exception(e); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | void yield() |
| 431 | { |
nothing calls this directly
no test coverage detected