[core] Rename ThreadedObject to Thread

Now that the old Thread class is gone, we can give ThreadedObject
a better name.
This commit is contained in:
Thiago Marcos P. Santos
2017-06-21 13:38:49 +03:00
parent 1427628c56
commit 7e8bdadb91
16 changed files with 67 additions and 67 deletions

View File

@@ -604,7 +604,7 @@ set(MBGL_CORE_FILES
src/mbgl/util/stopwatch.hpp
src/mbgl/util/string.cpp
src/mbgl/util/thread_local.hpp
src/mbgl/util/threaded_object.hpp
src/mbgl/util/thread.hpp
src/mbgl/util/throttler.cpp
src/mbgl/util/throttler.hpp
src/mbgl/util/tile_coordinate.hpp

View File

@@ -131,7 +131,7 @@ set(MBGL_TEST_FILES
test/util/projection.test.cpp
test/util/run_loop.test.cpp
test/util/text_conversions.test.cpp
test/util/threaded_object.test.cpp
test/util/thread.test.cpp
test/util/thread_local.test.cpp
test/util/tile_cover.test.cpp
test/util/timer.test.cpp

View File

@@ -10,7 +10,7 @@
namespace mbgl {
namespace util {
template <typename T> class ThreadedObject;
template <typename T> class Thread;
} // namespace util
class DefaultFileSource : public FileSource {
@@ -143,7 +143,7 @@ public:
private:
// Shared so destruction is done on this thread
const std::shared_ptr<FileSource> assetFileSource;
const std::unique_ptr<util::ThreadedObject<Impl>> impl;
const std::unique_ptr<util::Thread<Impl>> impl;
std::mutex cachedBaseURLMutex;
std::string cachedBaseURL = mbgl::util::API_BASE_URL;

View File

@@ -3,7 +3,7 @@
#include <mbgl/storage/file_source_request.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/util/util.hpp>
#include <mbgl/util/threaded_object.hpp>
#include <mbgl/util/thread.hpp>
#include <mbgl/util/url.hpp>
#include <android/asset_manager.h>
@@ -40,7 +40,7 @@ private:
AssetManagerFileSource::AssetManagerFileSource(jni::JNIEnv& env, jni::Object<android::AssetManager> assetManager_)
: assetManager(assetManager_.NewGlobalRef(env)),
impl(std::make_unique<util::ThreadedObject<Impl>>("AssetManagerFileSource",
impl(std::make_unique<util::Thread<Impl>>("AssetManagerFileSource",
AAssetManager_fromJava(&env, jni::Unwrap(**assetManager)))) {
}

View File

@@ -9,7 +9,7 @@
namespace mbgl {
namespace util {
template <typename T> class ThreadedObject;
template <typename T> class Thread;
} // namespace util
class AssetManagerFileSource : public FileSource {
@@ -23,7 +23,7 @@ private:
class Impl;
jni::UniqueObject<android::AssetManager> assetManager;
std::unique_ptr<util::ThreadedObject<Impl>> impl;
std::unique_ptr<util::Thread<Impl>> impl;
};
} // namespace mbgl

View File

@@ -2,7 +2,7 @@
#include <mbgl/util/platform.hpp>
#include <mbgl/util/thread_local.hpp>
#include <mbgl/util/threaded_object.hpp>
#include <mbgl/util/thread.hpp>
#include <mbgl/util/timer.hpp>
#include <android/looper.h>
@@ -105,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<ThreadedObject<Alarm>>("Alarm", this);
alarm = std::make_unique<Thread<Alarm>>("Alarm", this);
running = true;
break;
}

View File

@@ -15,7 +15,7 @@ struct ALooper;
namespace mbgl {
namespace util {
template <typename T> class ThreadedObject;
template <typename T> class Thread;
class Alarm;
class RunLoop::Impl {
@@ -53,7 +53,7 @@ private:
JNIEnv *env = nullptr;
bool detach = false;
std::unique_ptr<ThreadedObject<Alarm>> alarm;
std::unique_ptr<Thread<Alarm>> alarm;
std::recursive_mutex mtx;
std::list<Runnable*> runnables;

View File

@@ -2,7 +2,7 @@
#include <mbgl/storage/file_source_request.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/threaded_object.hpp>
#include <mbgl/util/thread.hpp>
#include <mbgl/util/url.hpp>
#include <mbgl/util/util.hpp>
#include <mbgl/util/io.hpp>
@@ -57,7 +57,7 @@ private:
};
AssetFileSource::AssetFileSource(const std::string& root)
: impl(std::make_unique<util::ThreadedObject<Impl>>("AssetFileSource", root)) {
: impl(std::make_unique<util::Thread<Impl>>("AssetFileSource", root)) {
}
AssetFileSource::~AssetFileSource() = default;

View File

@@ -8,7 +8,7 @@
#include <mbgl/util/platform.hpp>
#include <mbgl/util/url.hpp>
#include <mbgl/util/threaded_object.hpp>
#include <mbgl/util/thread.hpp>
#include <mbgl/util/work_request.hpp>
#include <cassert>
@@ -194,7 +194,7 @@ DefaultFileSource::DefaultFileSource(const std::string& cachePath,
std::unique_ptr<FileSource>&& assetFileSource_,
uint64_t maximumCacheSize)
: assetFileSource(std::move(assetFileSource_))
, impl(std::make_unique<util::ThreadedObject<Impl>>("DefaultFileSource", assetFileSource, cachePath, maximumCacheSize)) {
, impl(std::make_unique<util::Thread<Impl>>("DefaultFileSource", assetFileSource, cachePath, maximumCacheSize)) {
}
DefaultFileSource::~DefaultFileSource() = default;

View File

@@ -2,7 +2,7 @@
#include <mbgl/storage/file_source_request.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/threaded_object.hpp>
#include <mbgl/util/thread.hpp>
#include <mbgl/util/url.hpp>
#include <mbgl/util/util.hpp>
#include <mbgl/util/io.hpp>
@@ -53,7 +53,7 @@ public:
};
LocalFileSource::LocalFileSource()
: impl(std::make_unique<util::ThreadedObject<Impl>>("LocalFileSource")) {
: impl(std::make_unique<util::Thread<Impl>>("LocalFileSource")) {
}
LocalFileSource::~LocalFileSource() = default;

View File

@@ -5,7 +5,7 @@
namespace mbgl {
namespace util {
template <typename T> class ThreadedObject;
template <typename T> class Thread;
} // namespace util
class AssetFileSource : public FileSource {
@@ -18,7 +18,7 @@ public:
private:
class Impl;
std::unique_ptr<util::ThreadedObject<Impl>> impl;
std::unique_ptr<util::Thread<Impl>> impl;
};
} // namespace mbgl

View File

@@ -5,7 +5,7 @@
namespace mbgl {
namespace util {
template <typename T> class ThreadedObject;
template <typename T> class Thread;
} // namespace util
class LocalFileSource : public FileSource {
@@ -20,7 +20,7 @@ public:
private:
class Impl;
std::unique_ptr<util::ThreadedObject<Impl>> impl;
std::unique_ptr<util::Thread<Impl>> impl;
};
} // namespace mbgl

View File

@@ -22,8 +22,8 @@ namespace util {
// Manages a thread with `Object`.
// Upon creation of this object, it launches a thread and creates an object of type `Object`
// in that thread. When the `ThreadedObject<>` object is destructed, the destructor waits
// for thread termination. The `ThreadedObject<>` constructor blocks until the thread and
// in that thread. When the `Thread<>` object is destructed, the destructor waits
// for thread termination. The `Thread<>` constructor blocks until the thread and
// the `Object` are fully created, so after the object creation, it's safe to obtain the
// `Object` stored in this thread. The thread created will always have low priority on
// the platforms that support setting thread priority.
@@ -32,15 +32,15 @@ namespace util {
//
// - Only one thread is created.
// - `Object` will live in a single thread, providing thread affinity.
// - It is safe to use `ThreadLocal` in an `Object` managed by `ThreadedObject<>`
// - It is safe to use `ThreadLocal` in an `Object` managed by `Thread<>`
// - A `RunLoop` is created for the `Object` thread.
// - `Object` can use `Timer` and do asynchronous I/O, like wait for sockets events.
//
template<class Object>
class ThreadedObject : public Scheduler {
class Thread : public Scheduler {
public:
template <class... Args>
ThreadedObject(const std::string& name, Args&&... args) {
Thread(const std::string& name, Args&&... args) {
std::promise<void> running;
thread = std::thread([&] {
@@ -60,7 +60,7 @@ public:
running.get_future().get();
}
~ThreadedObject() override {
~Thread() override {
MBGL_VERIFY_THREAD(tid);
if (paused) {
@@ -86,7 +86,7 @@ public:
// Returns a non-owning reference to `Object` that
// can be used to send messages to `Object`. It is safe
// to the non-owning reference to outlive this object
// and be used after the `ThreadedObject<>` gets destroyed.
// and be used after the `Thread<>` gets destroyed.
ActorRef<std::decay_t<Object>> actor() const {
return object->self();
}

View File

@@ -2,7 +2,7 @@
#include <mbgl/util/platform.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/threaded_object.hpp>
#include <mbgl/util/thread.hpp>
#include <mbgl/actor/actor_ref.hpp>
#include <gtest/gtest.h>
@@ -56,11 +56,11 @@ TEST(AssetFileSource, Load) {
std::function<void(mbgl::Response)> requestCallback;
};
std::vector<std::unique_ptr<util::ThreadedObject<TestWorker>>> threads;
std::vector<std::unique_ptr<util::Thread<TestWorker>>> threads;
for (unsigned i = 0; i < numThreads; ++i) {
std::unique_ptr<util::ThreadedObject<TestWorker>> thread =
std::make_unique<util::ThreadedObject<TestWorker>>("Test", &fs);
std::unique_ptr<util::Thread<TestWorker>> thread =
std::make_unique<util::Thread<TestWorker>>("Test", &fs);
thread->actor().invoke(&TestWorker::run, [&] { if (!--completed) loop.stop(); });
threads.push_back(std::move(thread));

View File

@@ -2,7 +2,7 @@
#include <mbgl/test/util.hpp>
#include <mbgl/util/default_thread_pool.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/threaded_object.hpp>
#include <mbgl/util/thread.hpp>
#include <mbgl/util/timer.hpp>
#include <atomic>
@@ -58,9 +58,9 @@ public:
const std::thread::id tid;
};
TEST(ThreadedObject, invoke) {
TEST(Thread, invoke) {
const std::thread::id tid = std::this_thread::get_id();
ThreadedObject<TestObject> thread("Test", tid);
Thread<TestObject> thread("Test", tid);
thread.actor().invoke(&TestObject::fn1, 1);
thread.actor().invoke(&TestObject::fn2, [] (int result) { EXPECT_EQ(result, 1); } );
@@ -77,9 +77,9 @@ TEST(ThreadedObject, invoke) {
resultFuture.get();
}
TEST(ThreadedObject, Context) {
TEST(Thread, Context) {
const std::thread::id tid = std::this_thread::get_id();
ThreadedObject<TestObject> thread("Test", tid);
Thread<TestObject> thread("Test", tid);
std::promise<bool> result;
auto resultFuture = result.get_future();
@@ -106,9 +106,9 @@ private:
Timer timer;
};
TEST(ThreadedObject, ExecutesAfter) {
TEST(Thread, ExecutesAfter) {
RunLoop loop;
ThreadedObject<TestWorker> thread("Test");
Thread<TestWorker> thread("Test");
bool didWork = false;
bool didAfter = false;
@@ -122,9 +122,9 @@ TEST(ThreadedObject, ExecutesAfter) {
EXPECT_TRUE(didAfter);
}
TEST(ThreadedObject, CanSelfWakeUp) {
TEST(Thread, CanSelfWakeUp) {
RunLoop loop;
ThreadedObject<TestWorker> thread("Test");
Thread<TestWorker> thread("Test");
thread.actor().invoke(&TestWorker::sendDelayed, [&] {
loop.stop();
@@ -133,7 +133,7 @@ TEST(ThreadedObject, CanSelfWakeUp) {
loop.run();
}
TEST(ThreadedObject, Concurrency) {
TEST(Thread, Concurrency) {
auto loop = std::make_shared<RunLoop>();
unsigned numMessages = 100000;
@@ -143,10 +143,10 @@ TEST(ThreadedObject, Concurrency) {
Actor<TestWorker> poolWorker(threadPool);
auto poolWorkerRef = poolWorker.self();
ThreadedObject<TestWorker> threadedObject("Test");
Thread<TestWorker> threadedObject("Test");
auto threadedObjectRef = threadedObject.actor();
// 10 threads sending 100k messages to the ThreadedObject. The
// 10 threads sending 100k messages to the Thread. The
// idea here is to test if the scheduler is handling concurrency
// correctly, otherwise this test should crash.
for (unsigned i = 0; i < numMessages; ++i) {
@@ -162,18 +162,18 @@ TEST(ThreadedObject, Concurrency) {
loop->run();
}
TEST(ThreadedObject, ThreadPoolMessaging) {
TEST(Thread, ThreadPoolMessaging) {
auto loop = std::make_shared<RunLoop>();
ThreadPool threadPool(1);
Actor<TestWorker> poolWorker(threadPool);
auto poolWorkerRef = poolWorker.self();
ThreadedObject<TestWorker> threadedObject("Test");
Thread<TestWorker> threadedObject("Test");
auto threadedObjectRef = threadedObject.actor();
// This is sending a message to the ThreadedObject from the main
// thread. Then the ThreadedObject will send another message to
// This is sending a message to the Thread from the main
// thread. Then the Thread will send another message to
// a worker on the ThreadPool.
threadedObjectRef.invoke(&TestWorker::send, [poolWorkerRef, loop] () mutable {
poolWorkerRef.invoke(&TestWorker::send, [loop] () { loop->stop(); });
@@ -189,8 +189,8 @@ TEST(ThreadedObject, ThreadPoolMessaging) {
loop->run();
}
TEST(ThreadedObject, ReferenceCanOutliveThreadedObject) {
auto thread = std::make_unique<ThreadedObject<TestWorker>>("Test");
TEST(Thread, ReferenceCanOutliveThread) {
auto thread = std::make_unique<Thread<TestWorker>>("Test");
auto worker = thread->actor();
thread.reset();
@@ -202,10 +202,10 @@ TEST(ThreadedObject, ReferenceCanOutliveThreadedObject) {
usleep(10000);
}
TEST(ThreadedObject, DeletePausedThreadedObject) {
TEST(Thread, DeletePausedThread) {
std::atomic_bool flag(false);
auto thread = std::make_unique<ThreadedObject<TestWorker>>("Test");
auto thread = std::make_unique<Thread<TestWorker>>("Test");
thread->pause();
thread->actor().invoke(&TestWorker::send, [&] { flag = true; });
@@ -216,15 +216,15 @@ TEST(ThreadedObject, DeletePausedThreadedObject) {
ASSERT_TRUE(flag);
}
TEST(ThreadedObject, Pause) {
TEST(Thread, Pause) {
RunLoop loop;
std::atomic_bool flag(false);
ThreadedObject<TestWorker> thread1("Test1");
Thread<TestWorker> thread1("Test1");
thread1.pause();
ThreadedObject<TestWorker> thread2("Test2");
Thread<TestWorker> thread2("Test2");
for (unsigned i = 0; i < 100; ++i) {
thread1.actor().invoke(&TestWorker::send, [&] { flag = true; });
@@ -236,19 +236,19 @@ TEST(ThreadedObject, Pause) {
loop.run();
}
TEST(ThreadedObject, Resume) {
TEST(Thread, Resume) {
RunLoop loop;
std::atomic_bool flag(false);
ThreadedObject<TestWorker> thread("Test");
Thread<TestWorker> thread("Test");
thread.pause();
for (unsigned i = 0; i < 100; ++i) {
thread.actor().invoke(&TestWorker::send, [&] { flag = true; });
}
// ThreadedObject messages are ondered, when we resume, this is going
// Thread messages are ondered, when we resume, this is going
// to me the last thing to run on the message queue.
thread.actor().invoke(&TestWorker::send, [&] { loop.stop(); });
@@ -261,10 +261,10 @@ TEST(ThreadedObject, Resume) {
ASSERT_TRUE(flag);
}
TEST(ThreadedObject, PauseResume) {
TEST(Thread, PauseResume) {
RunLoop loop;
ThreadedObject<TestWorker> thread("Test");
Thread<TestWorker> thread("Test");
// Test if multiple pause/resume work.
for (unsigned i = 0; i < 100; ++i) {

View File

@@ -1,5 +1,5 @@
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/threaded_object.hpp>
#include <mbgl/util/thread.hpp>
#include <mbgl/util/thread_local.hpp>
#include <mbgl/test/util.hpp>
@@ -39,9 +39,9 @@ TEST(ThreadLocalStorage, Basic) {
int number2 = 2;
int number3 = 3;
ThreadedObject<TestThread> thread1("Test", &number1);
ThreadedObject<TestThread> thread2("Test", &number2);
ThreadedObject<TestThread> thread3("Test", &number3);
Thread<TestThread> thread1("Test", &number1);
Thread<TestThread> thread2("Test", &number2);
Thread<TestThread> thread3("Test", &number3);
auto thread1Ref = thread1.actor();
auto thread2Ref = thread2.actor();
@@ -98,8 +98,8 @@ TEST(ThreadLocalStorage, AutoReclaim) {
auto dtorCounter1 = new DtorCounter{ &counter };
auto dtorCounter2 = new DtorCounter{ &counter };
auto thread1 = std::make_unique<ThreadedObject<TestThreadReclaim>>("Test", dtorCounter1);
auto thread2 = std::make_unique<ThreadedObject<TestThreadReclaim>>("Test", dtorCounter2);
auto thread1 = std::make_unique<Thread<TestThreadReclaim>>("Test", dtorCounter1);
auto thread2 = std::make_unique<Thread<TestThreadReclaim>>("Test", dtorCounter2);
thread1.reset();
thread2.reset();