Skip to content

Commit

Permalink
fix: single-thread pool
Browse files Browse the repository at this point in the history
  • Loading branch information
vinniefalco committed Jul 11, 2023
1 parent 68491d0 commit a55a87c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 63 deletions.
59 changes: 30 additions & 29 deletions lib/Support/ExecutorGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,42 +97,43 @@ run(std::unique_lock<std::mutex> lock)
std::unique_ptr<AnyAgent> agent(std::move(agents_.back()));
agents_.pop_back();
++impl_->busy;
lock.unlock();

impl_->threadPool.async(
[this, agent = std::move(agent)]() mutable
[this, agent = std::move(agent)]() mutable
{
std::unique_lock<std::mutex> lock(impl_->mutex);
scoped_agent scope(*this, std::move(agent));
for(;;)
{
std::unique_lock<std::mutex> lock(impl_->mutex);
scoped_agent scope(*this, std::move(agent));
for(;;)
if(work_.empty())
break;
any_callable<void(void*)> work(
std::move(work_.front()));
work_.pop_front();
{
if(work_.empty())
break;
any_callable<void(void*)> work(
std::move(work_.front()));
work_.pop_front();
lock.unlock();
try
{
work(scope.get());
lock.lock();
}
catch(Exception const& ex)
{
lock.unlock();
try
{
work(scope.get());
lock.lock();
}
catch(Exception const& ex)
{
lock.lock();
impl_->errors.emplace(ex.error());
}
catch(std::exception const& ex)
{
// Any exception which is not
// derived from Exception should
// be reported and terminate
// the process immediately.
reportUnhandledException(ex);
}
lock.lock();
impl_->errors.emplace(ex.error());
}
catch(std::exception const& ex)
{
// Any exception which is not
// derived from Exception should
// be reported and terminate
// the process immediately.
reportUnhandledException(ex);
}
}
});
}
});
}

std::vector<Error>
Expand Down
69 changes: 35 additions & 34 deletions lib/Support/ThreadPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,45 +163,46 @@ TaskGroup::
post(
any_callable<void(void)> f)
{
if(! impl_->taskGroup)
if(impl_->taskGroup)
{
try
{
f();
}
catch(Exception const& ex)
{
std::lock_guard<std::mutex> lock(impl_->mutex);
impl_->errors.emplace(ex.error());
}
catch(std::exception const& ex)
impl_->taskGroup->async(
[&, sp = std::make_shared<
any_callable<void(void)>>(std::move(f))]
{
reportUnhandledException(ex);
}
try
{
(*sp)();
}
catch(Exception const& ex)
{
std::lock_guard<std::mutex> lock(impl_->mutex);
impl_->errors.emplace(ex.error());
}
catch(std::exception const& ex)
{
// Any exception which is not
// derived from Error should
// be reported and terminate
// the process immediately.
reportUnhandledException(ex);
}
});
return;
}
impl_->taskGroup->async(
[&, sp = std::make_shared<
any_callable<void(void)>>(std::move(f))]

try
{
try
{
(*sp)();
}
catch(Exception const& ex)
{
std::lock_guard<std::mutex> lock(impl_->mutex);
impl_->errors.emplace(ex.error());
}
catch(std::exception const& ex)
{
// Any exception which is not
// derived from Error should
// be reported and terminate
// the process immediately.
reportUnhandledException(ex);
}
});
f();
}
catch(Exception const& ex)
{
impl_->errors.emplace(ex.error());
}
catch(std::exception const& ex)
{
reportUnhandledException(ex);
}
return;
}

} // mrdox
Expand Down

0 comments on commit a55a87c

Please sign in to comment.