move requests to global threadpool
This commit is contained in:
@@ -4,30 +4,33 @@
|
||||
#include <pthread.h>
|
||||
#include <forward_list>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include "std.hpp"
|
||||
|
||||
namespace llmr {
|
||||
namespace util {
|
||||
|
||||
class Threadpool {
|
||||
private:
|
||||
class Worker {
|
||||
public:
|
||||
Worker(Threadpool& pool);
|
||||
Worker(Threadpool& pool, const std::string name);
|
||||
~Worker();
|
||||
static void *loop(void *ptr);
|
||||
|
||||
private:
|
||||
Threadpool& pool;
|
||||
const std::string name;
|
||||
pthread_t thread;
|
||||
};
|
||||
|
||||
public:
|
||||
typedef void (*Callback)(void *);
|
||||
typedef std::pair<Callback, void *> Task;
|
||||
Threadpool(int max_workers = 4);
|
||||
|
||||
typedef void (*Callback)(void *);
|
||||
void add(Callback callback, void *data);
|
||||
|
||||
private:
|
||||
typedef std::pair<Callback, void *> Task;
|
||||
const int max_workers;
|
||||
pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
|
||||
pthread_cond_t condition = PTHREAD_COND_INITIALIZER;
|
||||
@@ -36,6 +39,8 @@ private:
|
||||
std::queue<Task> tasks;
|
||||
};
|
||||
|
||||
extern std::unique_ptr<Threadpool> threadpool;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,8 +39,13 @@ public:
|
||||
glfwSetWindowUserPointer(window, this);
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
int width, height;
|
||||
glfwGetWindowSize(window, &width, &height);
|
||||
int fb_width, fb_height;
|
||||
glfwGetFramebufferSize(window, &fb_width, &fb_height);
|
||||
|
||||
settings.load();
|
||||
map.setup();
|
||||
map.setup((double)fb_width / width);
|
||||
|
||||
resize(window, 0, 0);
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
using namespace llmr::platform;
|
||||
|
||||
|
||||
llmr::util::Threadpool *Request::pool = nullptr;
|
||||
CURLSH *Request::curl_share = nullptr;
|
||||
pthread_mutex_t Request::curl_share_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
@@ -28,16 +27,10 @@ size_t Request::curl_write_callback(void *contents, size_t size, size_t nmemb, v
|
||||
|
||||
int Request::curl_progress_callback(void *ptr, double dltotal, double dlnow, double ultotal, double ulnow) {
|
||||
Request *req = static_cast<Request *>(ptr);
|
||||
bool cancel = req->cancelled;
|
||||
if (cancel) {
|
||||
fprintf(stderr, "cancel download %s\n", req->url.c_str());
|
||||
}
|
||||
return cancel;
|
||||
return req->cancelled;
|
||||
}
|
||||
|
||||
void Request::initialize() {
|
||||
pool = new llmr::util::Threadpool();
|
||||
|
||||
// curl init
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
curl_share = curl_share_init();
|
||||
@@ -46,7 +39,6 @@ void Request::initialize() {
|
||||
}
|
||||
|
||||
void Request::finish() {
|
||||
delete pool;
|
||||
curl_share_cleanup(curl_share);
|
||||
}
|
||||
|
||||
@@ -56,8 +48,7 @@ Request::Request(std::string url, std::function<void(platform::Response&)> bg, s
|
||||
url(url),
|
||||
background_function(bg),
|
||||
foreground_callback(fg) {
|
||||
assert(pool);
|
||||
pool->add(request, this);
|
||||
llmr::util::threadpool->add(request, this);
|
||||
}
|
||||
|
||||
void Request::request(void *ptr) {
|
||||
|
||||
@@ -7,11 +7,6 @@
|
||||
|
||||
|
||||
namespace llmr {
|
||||
|
||||
namespace util {
|
||||
class Threadpool;
|
||||
}
|
||||
|
||||
namespace platform {
|
||||
|
||||
struct Response;
|
||||
@@ -40,7 +35,6 @@ public:
|
||||
const std::function<void()> foreground_callback;
|
||||
|
||||
private:
|
||||
static util::Threadpool* pool;
|
||||
static CURLSH *curl_share;
|
||||
static pthread_mutex_t curl_share_mutex;
|
||||
};
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
#include <llmr/util/threadpool.hpp>
|
||||
#include <thread>
|
||||
|
||||
using namespace llmr::util;
|
||||
|
||||
std::unique_ptr<Threadpool> llmr::util::threadpool = std::make_unique<Threadpool>(std::thread::hardware_concurrency());
|
||||
|
||||
Threadpool::Threadpool(int max_workers)
|
||||
: max_workers(max_workers) {
|
||||
}
|
||||
|
||||
void Threadpool::add(Callback callback, void *data) {
|
||||
if (worker_count < max_workers) {
|
||||
workers.emplace_front(*this);
|
||||
worker_count++;
|
||||
workers.emplace_front(*this, std::string("worker ") + std::to_string(worker_count));
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
@@ -18,8 +21,9 @@ void Threadpool::add(Callback callback, void *data) {
|
||||
pthread_cond_signal(&condition);
|
||||
}
|
||||
|
||||
Threadpool::Worker::Worker(Threadpool& pool)
|
||||
: pool(pool) {
|
||||
Threadpool::Worker::Worker(Threadpool& pool, const std::string name)
|
||||
: pool(pool),
|
||||
name(name) {
|
||||
pthread_create(&thread, nullptr, loop, (void *)this);
|
||||
}
|
||||
|
||||
@@ -32,7 +36,7 @@ void *Threadpool::Worker::loop(void *ptr) {
|
||||
Worker *worker = static_cast<Worker *>(ptr);
|
||||
Threadpool& pool = worker->pool;
|
||||
|
||||
pthread_setname_np("worker");
|
||||
pthread_setname_np(worker->name.c_str());
|
||||
pthread_mutex_lock(&pool.mutex);
|
||||
while (true) {
|
||||
if (pool.tasks.size()) {
|
||||
|
||||
Reference in New Issue
Block a user