Skip to content

Commit a55a87c

Browse files
committed
fix: single-thread pool
1 parent 68491d0 commit a55a87c

File tree

2 files changed

+65
-63
lines changed

2 files changed

+65
-63
lines changed

lib/Support/ExecutorGroup.cpp

+30-29
Original file line numberDiff line numberDiff line change
@@ -97,42 +97,43 @@ run(std::unique_lock<std::mutex> lock)
9797
std::unique_ptr<AnyAgent> agent(std::move(agents_.back()));
9898
agents_.pop_back();
9999
++impl_->busy;
100+
lock.unlock();
100101

101102
impl_->threadPool.async(
102-
[this, agent = std::move(agent)]() mutable
103+
[this, agent = std::move(agent)]() mutable
104+
{
105+
std::unique_lock<std::mutex> lock(impl_->mutex);
106+
scoped_agent scope(*this, std::move(agent));
107+
for(;;)
103108
{
104-
std::unique_lock<std::mutex> lock(impl_->mutex);
105-
scoped_agent scope(*this, std::move(agent));
106-
for(;;)
109+
if(work_.empty())
110+
break;
111+
any_callable<void(void*)> work(
112+
std::move(work_.front()));
113+
work_.pop_front();
107114
{
108-
if(work_.empty())
109-
break;
110-
any_callable<void(void*)> work(
111-
std::move(work_.front()));
112-
work_.pop_front();
115+
lock.unlock();
116+
try
117+
{
118+
work(scope.get());
119+
lock.lock();
120+
}
121+
catch(Exception const& ex)
113122
{
114-
lock.unlock();
115-
try
116-
{
117-
work(scope.get());
118-
lock.lock();
119-
}
120-
catch(Exception const& ex)
121-
{
122-
lock.lock();
123-
impl_->errors.emplace(ex.error());
124-
}
125-
catch(std::exception const& ex)
126-
{
127-
// Any exception which is not
128-
// derived from Exception should
129-
// be reported and terminate
130-
// the process immediately.
131-
reportUnhandledException(ex);
132-
}
123+
lock.lock();
124+
impl_->errors.emplace(ex.error());
125+
}
126+
catch(std::exception const& ex)
127+
{
128+
// Any exception which is not
129+
// derived from Exception should
130+
// be reported and terminate
131+
// the process immediately.
132+
reportUnhandledException(ex);
133133
}
134134
}
135-
});
135+
}
136+
});
136137
}
137138

138139
std::vector<Error>

lib/Support/ThreadPool.cpp

+35-34
Original file line numberDiff line numberDiff line change
@@ -163,45 +163,46 @@ TaskGroup::
163163
post(
164164
any_callable<void(void)> f)
165165
{
166-
if(! impl_->taskGroup)
166+
if(impl_->taskGroup)
167167
{
168-
try
169-
{
170-
f();
171-
}
172-
catch(Exception const& ex)
173-
{
174-
std::lock_guard<std::mutex> lock(impl_->mutex);
175-
impl_->errors.emplace(ex.error());
176-
}
177-
catch(std::exception const& ex)
168+
impl_->taskGroup->async(
169+
[&, sp = std::make_shared<
170+
any_callable<void(void)>>(std::move(f))]
178171
{
179-
reportUnhandledException(ex);
180-
}
172+
try
173+
{
174+
(*sp)();
175+
}
176+
catch(Exception const& ex)
177+
{
178+
std::lock_guard<std::mutex> lock(impl_->mutex);
179+
impl_->errors.emplace(ex.error());
180+
}
181+
catch(std::exception const& ex)
182+
{
183+
// Any exception which is not
184+
// derived from Error should
185+
// be reported and terminate
186+
// the process immediately.
187+
reportUnhandledException(ex);
188+
}
189+
});
181190
return;
182191
}
183-
impl_->taskGroup->async(
184-
[&, sp = std::make_shared<
185-
any_callable<void(void)>>(std::move(f))]
192+
193+
try
186194
{
187-
try
188-
{
189-
(*sp)();
190-
}
191-
catch(Exception const& ex)
192-
{
193-
std::lock_guard<std::mutex> lock(impl_->mutex);
194-
impl_->errors.emplace(ex.error());
195-
}
196-
catch(std::exception const& ex)
197-
{
198-
// Any exception which is not
199-
// derived from Error should
200-
// be reported and terminate
201-
// the process immediately.
202-
reportUnhandledException(ex);
203-
}
204-
});
195+
f();
196+
}
197+
catch(Exception const& ex)
198+
{
199+
impl_->errors.emplace(ex.error());
200+
}
201+
catch(std::exception const& ex)
202+
{
203+
reportUnhandledException(ex);
204+
}
205+
return;
205206
}
206207

207208
} // mrdox

0 commit comments

Comments
 (0)