summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.cc2
-rw-r--r--src/resolver.cc48
-rw-r--r--src/resolver.hh4
3 files changed, 38 insertions, 16 deletions
diff --git a/src/main.cc b/src/main.cc
index d03dd45..cd73833 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -172,7 +172,7 @@ int main(int argc, char** argv) {
auto foreground = config->get("foreground", false);
auto cwd = get_cwd();
std::unique_ptr<Looper> looper(Looper::create());
- std::unique_ptr<Resolver> resolver(Resolver::create(looper.get()));
+ std::unique_ptr<Resolver> resolver(Resolver::create(looper.get(), true));
std::unique_ptr<Proxy> proxy(
Proxy::create(config.get(), cwd, configfile,
foreground ? "bogus" :
diff --git a/src/resolver.cc b/src/resolver.cc
index 2623089..68b4533 100644
--- a/src/resolver.cc
+++ b/src/resolver.cc
@@ -22,9 +22,32 @@ size_t const WORKERS = 4;
class ResolverImpl : public Resolver {
public:
- ResolverImpl(Looper* looper)
+ ResolverImpl(Looper* looper, bool delay)
: looper_(looper), request_(nullptr), buf_(new char[sizeof(Request*)]),
- fill_(0), quit_(false) {
+ fill_(0), quit_(false), setup_(false) {
+ if (!delay) {
+ if (!setup()) {
+ assert(false);
+ }
+ }
+ }
+
+ ~ResolverImpl() override {
+ quit_ = true;
+ if (setup_) {
+ cond_.notify_all();
+ for (auto& thread : threads_) {
+ thread.join();
+ }
+ }
+ }
+
+ bool setup() override {
+ if (setup_) {
+ assert(false);
+ return true;
+ }
+ setup_ = true;
while (threads_.size() < WORKERS) {
threads_.emplace_back(std::bind(&ResolverImpl::worker, this));
}
@@ -33,21 +56,16 @@ public:
Looper::EVENT_READ,
std::bind(&ResolverImpl::event, this,
std::placeholders::_1, std::placeholders::_2));
- } else {
- assert(false);
- }
- }
-
- ~ResolverImpl() override {
- quit_ = true;
- cond_.notify_all();
- for (auto& thread : threads_) {
- thread.join();
+ return true;
}
+ return false;
}
void* request(std::string const& host, uint16_t port,
Callback const& callback) override {
+ if (!setup_) {
+ setup();
+ }
auto req = new Request();
req->host = host;
req->port = port;
@@ -61,6 +79,7 @@ public:
}
void cancel(void* ptr) override {
+ assert(setup_);
auto req = reinterpret_cast<Request*>(ptr);
req->canceled = true;
std::unique_lock<std::mutex> lock(mutex_);
@@ -196,12 +215,13 @@ protected:
std::unique_ptr<char[]> buf_;
size_t fill_;
bool quit_;
+ bool setup_;
std::vector<std::thread> threads_;
};
} // namespace
// static
-Resolver* Resolver::create(Looper* looper) {
- return new ResolverImpl(looper);
+Resolver* Resolver::create(Looper* looper, bool delay) {
+ return new ResolverImpl(looper, delay);
}
diff --git a/src/resolver.hh b/src/resolver.hh
index 856f561..319f8b1 100644
--- a/src/resolver.hh
+++ b/src/resolver.hh
@@ -15,7 +15,9 @@ public:
virtual ~Resolver() {}
- static Resolver* create(Looper* looper);
+ static Resolver* create(Looper* looper, bool delay = false);
+
+ virtual bool setup() = 0;
virtual void* request(std::string const& host, uint16_t port,
Callback const& callback) = 0;