Rewrite Worker in terms of Thread<>
This commit is contained in:
@@ -4,70 +4,26 @@
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
Worker::Worker(uv_loop_t* loop, std::size_t count)
|
||||
: queue(new Queue(loop, [this](Fn after) { afterWork(after); }))
|
||||
{
|
||||
queue->unref();
|
||||
class Worker::Impl {
|
||||
public:
|
||||
Impl(uv_loop_t*) {}
|
||||
|
||||
void doWork(Fn work) {
|
||||
work();
|
||||
}
|
||||
};
|
||||
|
||||
Worker::Worker(std::size_t count) {
|
||||
for (std::size_t i = 0; i < count; i++) {
|
||||
threads.emplace_back(&Worker::workLoop, this);
|
||||
threads.emplace_back(util::make_unique<util::Thread<Impl>>("Worker"));
|
||||
}
|
||||
}
|
||||
|
||||
Worker::~Worker() {
|
||||
MBGL_VERIFY_THREAD(tid);
|
||||
|
||||
if (active++ == 0) {
|
||||
queue->ref();
|
||||
}
|
||||
|
||||
channel.send(Work());
|
||||
|
||||
for (auto& thread : threads) {
|
||||
thread.join();
|
||||
}
|
||||
|
||||
queue->stop();
|
||||
}
|
||||
Worker::~Worker() = default;
|
||||
|
||||
void Worker::send(Fn work, Fn after) {
|
||||
MBGL_VERIFY_THREAD(tid);
|
||||
assert(work);
|
||||
|
||||
if (active++ == 0) {
|
||||
queue->ref();
|
||||
}
|
||||
|
||||
channel.send({work, after});
|
||||
}
|
||||
|
||||
void Worker::workLoop() {
|
||||
#ifdef __APPLE__
|
||||
pthread_setname_np("Worker");
|
||||
#endif
|
||||
|
||||
while (true) {
|
||||
Work item = channel.receive();
|
||||
|
||||
if (!item.work)
|
||||
break;
|
||||
|
||||
item.work();
|
||||
queue->send(std::move(item.after));
|
||||
}
|
||||
|
||||
// Make sure to close all other workers too.
|
||||
channel.send(Work());
|
||||
}
|
||||
|
||||
void Worker::afterWork(Fn after) {
|
||||
if (after) {
|
||||
after();
|
||||
}
|
||||
|
||||
if (--active == 0) {
|
||||
queue->unref();
|
||||
}
|
||||
threads[current]->invokeWithResult(&Worker::Impl::doWork, after, work);
|
||||
current = (current + 1) % threads.size();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user