[core] Convert AssetFileSource to ThreadedObject
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
#include <mbgl/storage/asset_file_source.hpp>
|
||||
#include <mbgl/storage/file_source_request.hpp>
|
||||
#include <mbgl/storage/response.hpp>
|
||||
#include <mbgl/util/string.hpp>
|
||||
#include <mbgl/util/thread.hpp>
|
||||
#include <mbgl/util/threaded_object.hpp>
|
||||
#include <mbgl/util/url.hpp>
|
||||
#include <mbgl/util/util.hpp>
|
||||
#include <mbgl/util/io.hpp>
|
||||
@@ -14,11 +15,11 @@ namespace mbgl {
|
||||
|
||||
class AssetFileSource::Impl {
|
||||
public:
|
||||
Impl(std::string root_)
|
||||
Impl(ActorRef<Impl>, std::string root_)
|
||||
: root(std::move(root_)) {
|
||||
}
|
||||
|
||||
void request(const std::string& url, FileSource::Callback callback) {
|
||||
void request(const std::string& url, ActorRef<FileSourceRequest> req) {
|
||||
std::string path;
|
||||
|
||||
if (url.size() <= 8 || url[8] == '/') {
|
||||
@@ -48,7 +49,7 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
callback(response);
|
||||
req.invoke(&FileSourceRequest::setResponse, response);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -56,15 +57,17 @@ private:
|
||||
};
|
||||
|
||||
AssetFileSource::AssetFileSource(const std::string& root)
|
||||
: thread(std::make_unique<util::Thread<Impl>>(
|
||||
util::ThreadContext{"AssetFileSource", util::ThreadPriority::Low},
|
||||
root)) {
|
||||
: impl(std::make_unique<util::ThreadedObject<Impl>>("AssetFileSource", root)) {
|
||||
}
|
||||
|
||||
AssetFileSource::~AssetFileSource() = default;
|
||||
|
||||
std::unique_ptr<AsyncRequest> AssetFileSource::request(const Resource& resource, Callback callback) {
|
||||
return thread->invokeWithCallback(&Impl::request, resource.url, callback);
|
||||
auto req = std::make_unique<FileSourceRequest>(std::move(callback));
|
||||
|
||||
impl->actor().invoke(&Impl::request, resource.url, req->actor());
|
||||
|
||||
return std::move(req);
|
||||
}
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
namespace mbgl {
|
||||
|
||||
namespace util {
|
||||
template <typename T> class Thread;
|
||||
template <typename T> class ThreadedObject;
|
||||
} // namespace util
|
||||
|
||||
class AssetFileSource : public FileSource {
|
||||
@@ -17,7 +17,8 @@ public:
|
||||
|
||||
private:
|
||||
class Impl;
|
||||
std::unique_ptr<util::Thread<Impl>> thread;
|
||||
|
||||
std::unique_ptr<util::ThreadedObject<Impl>> impl;
|
||||
};
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
#include <mbgl/util/platform.hpp>
|
||||
#include <mbgl/util/chrono.hpp>
|
||||
#include <mbgl/util/run_loop.hpp>
|
||||
#include <mbgl/util/thread.hpp>
|
||||
#include <mbgl/util/threaded_object.hpp>
|
||||
#include <mbgl/actor/actor_ref.hpp>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <atomic>
|
||||
|
||||
using namespace mbgl;
|
||||
|
||||
@@ -20,16 +22,11 @@ TEST(AssetFileSource, Load) {
|
||||
#else
|
||||
unsigned numThreads = 50;
|
||||
#endif
|
||||
|
||||
auto callback = [&] {
|
||||
if (!--numThreads) {
|
||||
loop.stop();
|
||||
}
|
||||
};
|
||||
std::atomic_uint completed(numThreads);
|
||||
|
||||
class TestWorker {
|
||||
public:
|
||||
TestWorker(mbgl::AssetFileSource* fs_) : fs(fs_) {}
|
||||
TestWorker(ActorRef<TestWorker>, mbgl::AssetFileSource* fs_) : fs(fs_) {}
|
||||
|
||||
void run(std::function<void()> endCallback) {
|
||||
const std::string asset("asset://nonempty");
|
||||
@@ -59,17 +56,13 @@ TEST(AssetFileSource, Load) {
|
||||
std::function<void(mbgl::Response)> requestCallback;
|
||||
};
|
||||
|
||||
std::vector<std::unique_ptr<util::Thread<TestWorker>>> threads;
|
||||
std::vector<std::unique_ptr<mbgl::AsyncRequest>> requests;
|
||||
util::ThreadContext context = { "Test" };
|
||||
std::vector<std::unique_ptr<util::ThreadedObject<TestWorker>>> threads;
|
||||
|
||||
for (unsigned i = 0; i < numThreads; ++i) {
|
||||
std::unique_ptr<util::Thread<TestWorker>> thread =
|
||||
std::make_unique<util::Thread<TestWorker>>(context, &fs);
|
||||
|
||||
requests.push_back(
|
||||
thread->invokeWithCallback(&TestWorker::run, callback));
|
||||
std::unique_ptr<util::ThreadedObject<TestWorker>> thread =
|
||||
std::make_unique<util::ThreadedObject<TestWorker>>("Test", &fs);
|
||||
|
||||
thread->actor().invoke(&TestWorker::run, [&] { if (!--completed) loop.stop(); });
|
||||
threads.push_back(std::move(thread));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user