Skip to content

Commit

Permalink
Make the code more coroutiny
Browse files Browse the repository at this point in the history
  • Loading branch information
komarevtsev-d committed Sep 12, 2024
1 parent 442fe31 commit d9f2ce1
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions cloud/blockstore/libs/endpoints_vhost/external_vhost_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,27 +599,25 @@ class TEndpoint final

void Start() override
{
TCondVar var;
auto processStarted = NewPromise();
// To avoid a race, we need to get the shared pointer in the calling
// thread and pass it to the background thread. This guaranteed that the
// background thread will deal with a live this.
auto workFunc = [&var, self = shared_from_this()]()
auto workFunc = [&processStarted, self = shared_from_this()]()
{
// It is important to start the vhost-server on the thread that
// outlives it. vhost-server waits for the parent-death signal via
// PR_SET_PDEATHSIG which tracks the aliveness of the thread that
// spawned the process.
self->Process = self->StartProcess();
var.Signal();
processStarted.SetValue();
self->ThreadProc();
};

with_lock (Mutex) {
std::thread(std::move(workFunc)).detach();
// Infinite time wait is safe here, since we are in the coroutine
// thread.
var.WaitI(Mutex);
}
std::thread(std::move(workFunc)).detach();
// Infinite time wait is safe here, since we are in the coroutine
// thread.
Executor->WaitFor(processStarted);
}

TFuture<NProto::TError> Stop() override
Expand Down

0 comments on commit d9f2ce1

Please sign in to comment.