http: Avoid detach() in webserver code.

Should make debugger server more viable on Switch.  Also fixes a leak.
This commit is contained in:
Unknown W. Brackets committed 2020-03-03 23:08:34 -08:00
1 parent cb1b7b1e43
commit 11e828053b
4 files changed
+19 -2

No files matched your search

+4
View File
@@ -131,6 +131,10 @@ Server::Server(threading::Executor *executor)
SetFallbackHandler(std::bind(&Server::Handle404, this, std::placeholders::_1));
}
Server::~Server() {
delete executor_;
}
void Server::RegisterHandler(const char *url_path, UrlHandlerFunc handler) {
handlers_[std::string(url_path)] = handler;
}
+2 -1
View File
@@ -62,8 +62,9 @@ private:
// Register handlers on this class to serve stuff.
class Server {
public:
// Takes ownership.
Server(threading::Executor *executor);
virtual ~Server() {}
virtual ~Server();
typedef std::function<void(const Request &)> UrlHandlerFunc;
typedef std::map<std::string, UrlHandlerFunc> UrlHandlerMap;
+7 -1
View File
@@ -10,7 +10,13 @@ void SameThreadExecutor::Run(std::function<void()> func) {
}
void NewThreadExecutor::Run(std::function<void()> func) {
std::thread(func).detach();
thread_ = std::thread(func);
}
NewThreadExecutor::~NewThreadExecutor() {
// If Run was ever called...
if (thread_.joinable())
thread_.join();
}
} // namespace threading
+6
View File
@@ -1,6 +1,7 @@
#pragma once
#include <functional>
#include <thread>
namespace threading {
@@ -8,6 +9,7 @@ namespace threading {
class Executor {
public:
virtual void Run(std::function<void()> func) = 0;
virtual ~Executor() {}
};
class SameThreadExecutor : public Executor {
@@ -17,7 +19,11 @@ public:
class NewThreadExecutor : public Executor {
public:
~NewThreadExecutor() override;
void Run(std::function<void()> func) override;
private:
std::thread thread_;
};
} // namespace threading