[android] Replace util::Thread with util::ThreadedObject
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
#include "asset_manager_file_source.hpp"
|
||||
|
||||
#include <mbgl/storage/file_source_request.hpp>
|
||||
#include <mbgl/storage/response.hpp>
|
||||
#include <mbgl/util/util.hpp>
|
||||
#include <mbgl/util/thread.hpp>
|
||||
#include <mbgl/util/threaded_object.hpp>
|
||||
#include <mbgl/util/url.hpp>
|
||||
|
||||
#include <android/asset_manager.h>
|
||||
@@ -12,10 +13,10 @@ namespace mbgl {
|
||||
|
||||
class AssetManagerFileSource::Impl {
|
||||
public:
|
||||
Impl(AAssetManager* assetManager_) : assetManager(assetManager_) {
|
||||
Impl(ActorRef<Impl>, AAssetManager* assetManager_) : assetManager(assetManager_) {
|
||||
}
|
||||
|
||||
void request(const std::string& url, FileSource::Callback callback) {
|
||||
void request(const std::string& url, ActorRef<FileSourceRequest> req) {
|
||||
// Note: AssetManager already prepends "assets" to the filename.
|
||||
const std::string path = mbgl::util::percentDecode(url.substr(8));
|
||||
|
||||
@@ -30,7 +31,7 @@ public:
|
||||
"Could not read asset");
|
||||
}
|
||||
|
||||
callback(response);
|
||||
req.invoke(&FileSourceRequest::setResponse, response);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -39,15 +40,18 @@ private:
|
||||
|
||||
AssetManagerFileSource::AssetManagerFileSource(jni::JNIEnv& env, jni::Object<android::AssetManager> assetManager_)
|
||||
: assetManager(assetManager_.NewGlobalRef(env)),
|
||||
thread(std::make_unique<util::Thread<Impl>>(
|
||||
util::ThreadContext{"AssetManagerFileSource", util::ThreadPriority::Low},
|
||||
AAssetManager_fromJava(&env, jni::Unwrap(**assetManager)))) {
|
||||
impl(std::make_unique<util::ThreadedObject<Impl>>("AssetManagerFileSource",
|
||||
AAssetManager_fromJava(&env, jni::Unwrap(**assetManager)))) {
|
||||
}
|
||||
|
||||
AssetManagerFileSource::~AssetManagerFileSource() = default;
|
||||
|
||||
std::unique_ptr<AsyncRequest> AssetManagerFileSource::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
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
namespace mbgl {
|
||||
|
||||
namespace util {
|
||||
template <typename T> class Thread;
|
||||
template <typename T> class ThreadedObject;
|
||||
} // namespace util
|
||||
|
||||
class AssetManagerFileSource : public FileSource {
|
||||
@@ -20,9 +20,10 @@ public:
|
||||
std::unique_ptr<AsyncRequest> request(const Resource&, Callback) override;
|
||||
|
||||
private:
|
||||
jni::UniqueObject<android::AssetManager> assetManager;
|
||||
class Impl;
|
||||
std::unique_ptr<util::Thread<Impl>> thread;
|
||||
|
||||
jni::UniqueObject<android::AssetManager> assetManager;
|
||||
std::unique_ptr<util::ThreadedObject<Impl>> impl;
|
||||
};
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
#include "run_loop_impl.hpp"
|
||||
|
||||
#include <mbgl/util/platform.hpp>
|
||||
#include <mbgl/util/thread.hpp>
|
||||
#include <mbgl/util/thread_context.hpp>
|
||||
#include <mbgl/util/thread_local.hpp>
|
||||
#include <mbgl/util/threaded_object.hpp>
|
||||
#include <mbgl/util/timer.hpp>
|
||||
|
||||
#include <android/looper.h>
|
||||
@@ -62,9 +61,13 @@ namespace util {
|
||||
// timeout, but on the main thread `ALooper_pollAll` is called by the activity
|
||||
// automatically, thus we cannot set the timeout. Instead we wake the loop
|
||||
// with an external file descriptor event coming from this thread.
|
||||
//
|
||||
// Usually an actor should not carry pointers to other threads, but in
|
||||
// this case the RunLoop itself owns the Alarm and calling wake() is the most
|
||||
// efficient way of waking up the RunLoop and it is also thread-safe.
|
||||
class Alarm {
|
||||
public:
|
||||
Alarm(RunLoop::Impl* loop_) : loop(loop_) {}
|
||||
Alarm(ActorRef<Alarm>, RunLoop::Impl* loop_) : loop(loop_) {}
|
||||
|
||||
void set(const Milliseconds& timeout) {
|
||||
alarm.start(timeout, mbgl::Duration::zero(), [this]() { loop->wake(); });
|
||||
@@ -102,7 +105,7 @@ RunLoop::Impl::Impl(RunLoop* runLoop_, RunLoop::Type type) : runLoop(runLoop_) {
|
||||
case Type::Default:
|
||||
ret = ALooper_addFd(loop, fds[PIPE_OUT], ALOOPER_POLL_CALLBACK,
|
||||
ALOOPER_EVENT_INPUT, looperCallbackDefault, this);
|
||||
alarm = std::make_unique<Thread<Alarm>>(ThreadContext{"Alarm"}, this);
|
||||
alarm = std::make_unique<ThreadedObject<Alarm>>("Alarm", this);
|
||||
running = true;
|
||||
break;
|
||||
}
|
||||
@@ -190,7 +193,7 @@ Milliseconds RunLoop::Impl::processRunnables() {
|
||||
|
||||
auto timeout = std::chrono::duration_cast<Milliseconds>(nextDue - now);
|
||||
if (alarm) {
|
||||
alarm->invoke(&Alarm::set, timeout);
|
||||
alarm->actor().invoke(&Alarm::set, timeout);
|
||||
}
|
||||
|
||||
return timeout;
|
||||
|
||||
@@ -15,7 +15,7 @@ struct ALooper;
|
||||
namespace mbgl {
|
||||
namespace util {
|
||||
|
||||
template <typename T> class Thread;
|
||||
template <typename T> class ThreadedObject;
|
||||
class Alarm;
|
||||
|
||||
class RunLoop::Impl {
|
||||
@@ -53,7 +53,7 @@ private:
|
||||
JNIEnv *env = nullptr;
|
||||
bool detach = false;
|
||||
|
||||
std::unique_ptr<Thread<Alarm>> alarm;
|
||||
std::unique_ptr<ThreadedObject<Alarm>> alarm;
|
||||
|
||||
std::recursive_mutex mtx;
|
||||
std::list<Runnable*> runnables;
|
||||
|
||||
Reference in New Issue
Block a user