| 423 | } |
| 424 | |
| 425 | Maybe<vm::ThreadCtx*> VirtualMachine::CreateThreadCtx(Symbol<Device> device, StreamType stream_type, |
| 426 | size_t thread_uid) { |
| 427 | std::unique_lock<std::recursive_mutex> lock(stream_and_thread_ctx_mutex_); |
| 428 | // thread_ctx_ptr may be used after timout. |
| 429 | auto thread_ctx_ptr = std::make_shared<vm::ThreadCtx*>(nullptr); |
| 430 | { |
| 431 | auto bc = std::make_shared<BlockingCounter>(1); |
| 432 | engine_->InsertProbe([thread_ctx_ptr, bc](vm::VirtualMachineEngine* engine) { |
| 433 | auto thread_ctx = intrusive::make_shared<vm::ThreadCtx>(); |
| 434 | engine->mut_thread_ctx_list()->PushBack(thread_ctx.Mutable()); |
| 435 | *thread_ctx_ptr = thread_ctx.Mutable(); |
| 436 | bc->Decrease(); |
| 437 | return true; |
| 438 | }); |
| 439 | JUST(NotifyOrRunScheduler()); |
| 440 | JUST(bc->WaitUntilCntEqualZero(VirtualMachine::GetPredicatorNoMoreInstructionsFinished())); |
| 441 | } |
| 442 | auto* thread_ctx = *thread_ctx_ptr; |
| 443 | { |
| 444 | const std::string thread_tag = [&] { |
| 445 | std::string device_tag = *CHECK_JUST(DeviceTag4DeviceType(device->enum_type())); |
| 446 | if (StreamOnIndependentThread::Visit(stream_type)) { |
| 447 | return device_tag + GetStreamTypeName::Visit(stream_type); |
| 448 | } else { |
| 449 | return std::to_string(thread_uid); |
| 450 | } |
| 451 | }(); |
| 452 | const auto& WorkerInitializer = [thread_tag](vm::ThreadCtx* thread_ctx) { |
| 453 | OF_PROFILER_NAME_THIS_HOST_THREAD("_VM::Worker_" + thread_tag); |
| 454 | }; |
| 455 | auto thread = std::make_unique<std::thread>(&WorkerLoop, thread_ctx, WorkerInitializer); |
| 456 | { |
| 457 | std::unique_lock<std::mutex> lock(worker_threads_mutex_); |
| 458 | worker_threads_.push_back(std::move(thread)); |
| 459 | } |
| 460 | } |
| 461 | return thread_ctx; |
| 462 | } |
| 463 | |
| 464 | Maybe<vm::Stream*> VirtualMachine::CreateStream(vm::ThreadCtx* thread_ctx, Symbol<Stream> stream) { |
| 465 | std::unique_lock<std::recursive_mutex> lock(stream_and_thread_ctx_mutex_); |
nothing calls this directly
no test coverage detected