| 308 | } |
| 309 | |
| 310 | void TContExecutor::RunScheduler() noexcept { |
| 311 | try { |
| 312 | TContExecutor* const prev = ThisThreadExecutor(); |
| 313 | ThisThreadExecutor() = this; |
| 314 | TCont* caller = Current_; |
| 315 | TExceptionSafeContext* context = caller ? caller->Trampoline_.Context() : &SchedContext_; |
| 316 | Y_DEFER { |
| 317 | ThisThreadExecutor() = prev; |
| 318 | }; |
| 319 | |
| 320 | while (true) { |
| 321 | if (ScheduleCallback_ && Current_) { |
| 322 | ScheduleCallback_->OnUnschedule(*this); |
| 323 | } |
| 324 | |
| 325 | WaitForIO(); |
| 326 | DeleteScheduled(); |
| 327 | Ready_.Append(ReadyNext_); |
| 328 | |
| 329 | if (Ready_.Empty()) { |
| 330 | Current_ = nullptr; |
| 331 | if (caller) { |
| 332 | context->SwitchTo(&SchedContext_); |
| 333 | } |
| 334 | break; |
| 335 | } |
| 336 | |
| 337 | TCont* cont = Ready_.PopFront(); |
| 338 | |
| 339 | if (ScheduleCallback_) { |
| 340 | ScheduleCallback_->OnSchedule(*this, *cont); |
| 341 | } |
| 342 | |
| 343 | Current_ = cont; |
| 344 | cont->Scheduled_ = false; |
| 345 | if (cont == caller) { |
| 346 | break; |
| 347 | } |
| 348 | context->SwitchTo(cont->Trampoline_.Context()); |
| 349 | if (Paused_) { |
| 350 | Paused_ = false; |
| 351 | Current_ = nullptr; |
| 352 | break; |
| 353 | } |
| 354 | if (caller) { |
| 355 | break; |
| 356 | } |
| 357 | } |
| 358 | } catch (...) { |
| 359 | TBackTrace::FromCurrentException().PrintTo(Cerr); |
| 360 | Y_ABORT("Uncaught exception in the scheduler: %s", CurrentExceptionMessage().c_str()); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | void TContExecutor::Pause() { |
| 365 | if (auto cont = Running()) { |