[core] Clean up ThreadContext vestiges

This commit is contained in:
John Firebaugh
2016-02-16 13:48:33 -08:00
parent 253a007d99
commit 6e78eabfff
26 changed files with 32 additions and 151 deletions

View File

@@ -100,7 +100,7 @@ private:
AssetFileSource::AssetFileSource(const std::string& root)
: thread(std::make_unique<util::Thread<Impl>>(
util::ThreadContext{"AssetFileSource", util::ThreadType::Worker, util::ThreadPriority::Regular},
util::ThreadContext{"AssetFileSource"},
root)) {
}

View File

@@ -49,7 +49,7 @@ namespace util {
RunLoop::Impl::Impl(RunLoop* runLoop, RunLoop::Type type) {
using namespace mbgl::android;
detach = attach_jni_thread(theJVM, &env, ThreadContext::getName());
detach = attach_jni_thread(theJVM, &env, "");
loop = ALooper_prepare(0);
assert(loop);

View File

@@ -57,7 +57,7 @@ private:
AssetFileSource::AssetFileSource(const std::string& root)
: thread(std::make_unique<util::Thread<Impl>>(
util::ThreadContext{"AssetFileSource", util::ThreadType::Worker, util::ThreadPriority::Regular},
util::ThreadContext{"AssetFileSource"},
root)) {
}

View File

@@ -139,7 +139,7 @@ private:
DefaultFileSource::DefaultFileSource(const std::string& cachePath,
const std::string& assetRoot,
uint64_t maximumCacheSize)
: thread(std::make_unique<util::Thread<Impl>>(util::ThreadContext{"DefaultFileSource", util::ThreadType::Unknown, util::ThreadPriority::Low},
: thread(std::make_unique<util::Thread<Impl>>(util::ThreadContext{"DefaultFileSource", util::ThreadPriority::Low},
cachePath, maximumCacheSize)),
assetFileSource(std::make_unique<AssetFileSource>(assetRoot)) {
}

View File

@@ -5,7 +5,6 @@
#include <mbgl/gl/gl_object_store.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/thread_context.hpp>
#include <memory>
#include <cstdlib>

View File

@@ -6,7 +6,6 @@
#include <mbgl/gl/gl_object_store.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/platform/platform.hpp>
#include <mbgl/util/thread_context.hpp>
#include <cassert>
#include <algorithm>
@@ -23,7 +22,6 @@ GlyphAtlas::GlyphAtlas(uint16_t width_, uint16_t height_)
}
GlyphAtlas::~GlyphAtlas() {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
}
void GlyphAtlas::addGlyphs(uintptr_t tileUID,

View File

@@ -3,7 +3,6 @@
#include <mbgl/gl/gl_object_store.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/platform/platform.hpp>
#include <mbgl/util/thread_context.hpp>
#include <boost/functional/hash.hpp>
@@ -20,12 +19,9 @@ LineAtlas::LineAtlas(GLsizei w, GLsizei h)
}
LineAtlas::~LineAtlas() {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
}
LinePatternPos LineAtlas::getDashPosition(const std::vector<float> &dasharray, bool round, gl::GLObjectStore& glObjectStore) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
size_t key = round ? std::numeric_limits<size_t>::min() : std::numeric_limits<size_t>::max();
for (const float part : dasharray) {
boost::hash_combine<float>(key, part);
@@ -132,8 +128,6 @@ void LineAtlas::upload(gl::GLObjectStore& glObjectStore) {
}
void LineAtlas::bind(gl::GLObjectStore& glObjectStore) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
bool first = false;
if (!texture) {
texture.create(glObjectStore);

View File

@@ -117,8 +117,6 @@ void MapContext::setStyleJSON(const std::string& json, const std::string& base)
}
void MapContext::loadStyleJSON(const std::string& json, const std::string& base) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
style->setJSON(json, base);
style->setObserver(this);
styleJSON = json;
@@ -130,8 +128,6 @@ void MapContext::loadStyleJSON(const std::string& json, const std::string& base)
}
void MapContext::update() {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
if (!style) {
updateFlags = Update::Nothing;
}
@@ -205,8 +201,6 @@ void MapContext::renderStill(const TransformState& state, const FrameData& frame
}
bool MapContext::renderSync(const TransformState& state, const FrameData& frame) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
// Style was not loaded yet.
if (!style) {
return false;
@@ -240,17 +234,14 @@ bool MapContext::isLoaded() const {
}
void MapContext::addAnnotationIcon(const std::string& name, std::shared_ptr<const SpriteImage> sprite) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
data.getAnnotationManager()->addIcon(name, sprite);
}
void MapContext::removeAnnotationIcon(const std::string& name) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
data.getAnnotationManager()->removeIcon(name);
}
double MapContext::getTopOffsetPixelsForAnnotationIcon(const std::string& name) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
return data.getAnnotationManager()->getTopOffsetPixelsForIcon(name);
}
@@ -286,7 +277,6 @@ void MapContext::setClasses(const std::vector<std::string>& classNames, const Pr
}
void MapContext::setSourceTileCacheSize(size_t size) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
if (size != sourceCacheSize) {
sourceCacheSize = size;
if (!style) return;
@@ -296,7 +286,6 @@ void MapContext::setSourceTileCacheSize(size_t size) {
}
void MapContext::onLowMemory() {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
if (!style) return;
style->onLowMemory();
view.invalidate();

View File

@@ -1,7 +1,5 @@
#include <mbgl/platform/log.hpp>
#include <mbgl/util/thread_context.hpp>
#include <cstdio>
#include <cstdarg>
#include <sstream>
@@ -49,7 +47,13 @@ void Log::record(EventSeverity severity, Event event, int64_t code, const std::s
}
std::stringstream logStream;
logStream << "{" << util::ThreadContext::getName() << "}";
#if !defined(__ANDROID__) && (defined( __APPLE__) || defined(__linux__))
char name[32];
pthread_getname_np(pthread_self(), name, sizeof(name));
logStream << "{" << name << "}";
#endif
logStream << "[" << event << "]";
if (code >= 0) {

View File

@@ -11,7 +11,7 @@ namespace mbgl {
class PlainShader;
namespace util {
namespace gl {
class GLObjectStore;
}

View File

@@ -6,7 +6,6 @@
#include <mbgl/util/math.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/thread_context.hpp>
#include <cassert>
#include <cmath>

View File

@@ -341,7 +341,6 @@ void Style::onLowMemory() {
}
void Style::setObserver(Observer* observer_) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
observer = observer_;
}

View File

@@ -1,7 +1,5 @@
#include <mbgl/text/glyph_store.hpp>
#include <mbgl/text/glyph_pbf.hpp>
#include <mbgl/util/thread_context.hpp>
#include <cassert>
@@ -14,8 +12,6 @@ GlyphStore::GlyphStore(FileSource& fileSource_)
GlyphStore::~GlyphStore() = default;
void GlyphStore::requestGlyphRange(const std::string& fontStackName, const GlyphRange& range) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
std::lock_guard<std::mutex> lock(rangesMutex);
auto& rangeSets = ranges[fontStackName];

View File

@@ -76,7 +76,7 @@ private:
}
template <typename P, std::size_t... I>
void run(ThreadContext, P&& params, std::index_sequence<I...>);
void run(P&& params, std::index_sequence<I...>);
std::promise<void> running;
std::promise<void> joinable;
@@ -107,7 +107,7 @@ Thread<Object>::Thread(const ThreadContext& context, Args&&... args) {
platform::makeThreadLowPriority();
}
run(context, std::move(params), std::index_sequence_for<Args...>{});
run(std::move(params), std::index_sequence_for<Args...>{});
});
running.get_future().get();
@@ -115,9 +115,7 @@ Thread<Object>::Thread(const ThreadContext& context, Args&&... args) {
template <class Object>
template <typename P, std::size_t... I>
void Thread<Object>::run(ThreadContext context, P&& params, std::index_sequence<I...>) {
ThreadContext::Set(&context);
void Thread<Object>::run(P&& params, std::index_sequence<I...>) {
RunLoop loop_(RunLoop::Type::New);
loop = &loop_;
@@ -130,8 +128,6 @@ void Thread<Object>::run(ThreadContext context, P&& params, std::index_sequence<
loop = nullptr;
object = nullptr;
ThreadContext::Set(nullptr);
joinable.get_future().get();
}

View File

@@ -1,67 +1,12 @@
#include <mbgl/util/thread_context.hpp>
#include <mbgl/util/thread_local.hpp>
#include <cassert>
namespace {
using namespace mbgl::util;
static ThreadLocal<ThreadContext>& current = *new ThreadLocal<ThreadContext>;
} // namespace
namespace mbgl {
namespace util {
ThreadContext::ThreadContext(const std::string& name_, ThreadType type_, ThreadPriority priority_)
ThreadContext::ThreadContext(const std::string& name_, ThreadPriority priority_)
: name(name_),
type(type_),
priority(priority_) {
}
void ThreadContext::Set(ThreadContext* context) {
current.set(context);
}
bool ThreadContext::currentlyOn(ThreadType type) {
return current.get()->type == type;
}
std::string ThreadContext::getName() {
if (current.get() != nullptr) {
return current.get()->name;
} else {
return "Unknown";
}
}
ThreadPriority ThreadContext::getPriority() {
if (current.get() != nullptr) {
return current.get()->priority;
} else {
return ThreadPriority::Regular;
}
}
class MainThreadContextRegistrar {
public:
MainThreadContextRegistrar() : context("Main", ThreadType::Main, ThreadPriority::Regular) {
ThreadContext::Set(&context);
}
~MainThreadContextRegistrar() {
ThreadContext::Set(nullptr);
}
private:
ThreadContext context;
};
// Will auto register the main thread context
// at startup. Must be instantiated after the
// ThreadContext::current object.
MainThreadContextRegistrar registrar;
} // namespace util
} // namespace mbgl

View File

@@ -1,9 +1,7 @@
#ifndef MBGL_UTIL_THREAD_CONTEXT
#define MBGL_UTIL_THREAD_CONTEXT
#include <cstdint>
#include <string>
#include <thread>
namespace mbgl {
namespace util {
@@ -13,25 +11,11 @@ enum class ThreadPriority : bool {
Low,
};
enum class ThreadType : uint8_t {
Main,
Map = Main,
Worker,
Unknown,
};
struct ThreadContext {
public:
ThreadContext(const std::string& name, ThreadType type, ThreadPriority priority);
static void Set(ThreadContext* context);
static bool currentlyOn(ThreadType type);
static std::string getName();
static ThreadPriority getPriority();
ThreadContext(const std::string& name, ThreadPriority priority = ThreadPriority::Regular);
std::string name;
ThreadType type;
ThreadPriority priority;
};

View File

@@ -60,7 +60,7 @@ public:
};
Worker::Worker(std::size_t count) {
util::ThreadContext context = { "Worker", util::ThreadType::Worker, util::ThreadPriority::Low };
util::ThreadContext context = { "Worker", util::ThreadPriority::Low };
for (std::size_t i = 0; i < count; i++) {
threads.emplace_back(std::make_unique<util::Thread<Impl>>(context));
}

View File

@@ -17,7 +17,7 @@ TEST(MapContext, DoubleStyleLoad) {
HeadlessView view(display, 1, 512, 512);
StubFileSource fileSource;
util::Thread<MapContext> context({"Map", util::ThreadType::Map, util::ThreadPriority::Regular},
util::Thread<MapContext> context({"Map"},
view, fileSource, MapMode::Continuous, GLContextMode::Unique, view.getPixelRatio());
context.invokeSync(&MapContext::setStyleJSON, "", "");

View File

@@ -73,7 +73,7 @@ TEST(AssetFileSource, Stress) {
std::vector<std::unique_ptr<util::Thread<TestWorker>>> threads;
std::vector<std::unique_ptr<mbgl::AsyncRequest>> requests;
util::ThreadContext context = { "Test", util::ThreadType::Map, util::ThreadPriority::Regular };
util::ThreadContext context = { "Test" };
for (unsigned i = 0; i < numThreads; ++i) {
std::unique_ptr<util::Thread<TestWorker>> thread =

View File

@@ -13,7 +13,6 @@ using namespace mbgl;
class GlyphStoreTest {
public:
util::ThreadContext context { "Map", util::ThreadType::Map, util::ThreadPriority::Regular };
util::RunLoop loop;
StubFileSource fileSource;
StubStyleObserver observer;
@@ -23,8 +22,6 @@ public:
// Squelch logging.
Log::setObserver(std::make_unique<Log::NullObserver>());
util::ThreadContext::Set(&context);
glyphStore.setObserver(&observer);
glyphStore.setURL(url);
glyphStore.hasGlyphRanges(fontStack, glyphRanges);

View File

@@ -21,7 +21,6 @@ using namespace mbgl;
class SourceTest {
public:
util::ThreadContext context { "Map", util::ThreadType::Map, util::ThreadPriority::Regular };
util::RunLoop loop;
StubFileSource fileSource;
StubStyleObserver observer;
@@ -51,8 +50,6 @@ public:
// Squelch logging.
Log::setObserver(std::make_unique<Log::NullObserver>());
util::ThreadContext::Set(&context);
transform.resize({{ 512, 512 }});
transform.setLatLngZoom({0, 0}, 0);

View File

@@ -9,8 +9,6 @@ using namespace mbgl;
TEST(Style, UnusedSource) {
util::RunLoop loop;
util::ThreadContext context { "Map", util::ThreadType::Map, util::ThreadPriority::Regular };
util::ThreadContext::Set(&context);
MapData data { MapMode::Still, GLContextMode::Unique, 1.0 };
StubFileSource fileSource;
@@ -33,8 +31,6 @@ TEST(Style, UnusedSource) {
TEST(Style, UnusedSourceActiveViaClassUpdate) {
util::RunLoop loop;
util::ThreadContext context { "Map", util::ThreadType::Map, util::ThreadPriority::Regular };
util::ThreadContext::Set(&context);
MapData data { MapMode::Still, GLContextMode::Unique, 1.0 };
StubFileSource fileSource;

View File

@@ -98,7 +98,7 @@ TEST(AsyncTask, RequestCoalescingMultithreaded) {
AsyncTask async([&count] { ++count; });
std::vector<std::unique_ptr<Thread<TestWorker>>> threads;
ThreadContext context = {"Test", ThreadType::Map, ThreadPriority::Regular};
ThreadContext context = {"Test"};
unsigned numThreads = 25;
for (unsigned i = 0; i < numThreads; ++i) {
@@ -133,7 +133,7 @@ TEST(AsyncTask, ThreadSafety) {
std::vector<std::unique_ptr<Thread<TestWorker>>> threads;
std::vector<std::unique_ptr<mbgl::AsyncRequest>> requests;
ThreadContext context = {"Test", ThreadType::Map, ThreadPriority::Regular};
ThreadContext context = {"Test"};
for (unsigned i = 0; i < numThreads; ++i) {
std::unique_ptr<Thread<TestWorker>> thread =

View File

@@ -55,9 +55,7 @@ public:
}
void checkContext(std::function<void (bool)> cb) const {
cb(ThreadContext::currentlyOn(ThreadType::Worker)
&& ThreadContext::getName() == "Test"
&& ThreadContext::getPriority() == ThreadPriority::Low);
cb(tid == std::this_thread::get_id());
}
const std::thread::id tid;
@@ -71,7 +69,7 @@ TEST(Thread, invoke) {
loop.invoke([&] {
EXPECT_EQ(tid, std::this_thread::get_id());
Thread<TestObject> thread({"Test", ThreadType::Map, ThreadPriority::Regular}, tid);
Thread<TestObject> thread({"Test"}, tid);
thread.invoke(&TestObject::fn1, 1);
requests.push_back(thread.invokeWithCallback(&TestObject::fn2, [&] (int result) {
@@ -114,19 +112,13 @@ TEST(Thread, invoke) {
}
TEST(Thread, context) {
bool isMainThreadContext = ThreadContext::currentlyOn(ThreadType::Main)
&& ThreadContext::getName() == "Main"
&& ThreadContext::getPriority() == ThreadPriority::Regular;
EXPECT_EQ(isMainThreadContext, true);
const std::thread::id tid = std::this_thread::get_id();
RunLoop loop;
std::vector<std::unique_ptr<mbgl::AsyncRequest>> requests;
loop.invoke([&] {
Thread<TestObject> thread({"Test", ThreadType::Worker, ThreadPriority::Low}, tid);
Thread<TestObject> thread({"Test"}, tid);
requests.push_back(thread.invokeWithCallback(&TestObject::checkContext, [&] (bool inTestThreadContext) {
EXPECT_EQ(inTestThreadContext, true);
@@ -149,7 +141,7 @@ public:
TEST(Thread, ExecutesAfter) {
RunLoop loop;
Thread<TestWorker> thread({"Test", ThreadType::Map, ThreadPriority::Regular});
Thread<TestWorker> thread({"Test"});
bool didWork = false;
bool didAfter = false;
@@ -170,7 +162,7 @@ TEST(Thread, ExecutesAfter) {
TEST(Thread, WorkRequestDeletionWaitsForWorkToComplete) {
RunLoop loop;
Thread<TestWorker> thread({"Test", ThreadType::Map, ThreadPriority::Regular});
Thread<TestWorker> thread({"Test"});
std::promise<void> started;
bool didWork = false;
@@ -188,7 +180,7 @@ TEST(Thread, WorkRequestDeletionWaitsForWorkToComplete) {
TEST(Thread, WorkRequestDeletionCancelsAfter) {
RunLoop loop;
Thread<TestWorker> thread({"Test", ThreadType::Map, ThreadPriority::Regular});
Thread<TestWorker> thread({"Test"});
std::promise<void> started;
bool didAfter = false;
@@ -207,7 +199,7 @@ TEST(Thread, WorkRequestDeletionCancelsAfter) {
TEST(Thread, WorkRequestDeletionCancelsImmediately) {
RunLoop loop;
Thread<TestWorker> thread({"Test", ThreadType::Map, ThreadPriority::Regular});
Thread<TestWorker> thread({"Test"});
std::promise<void> started;

View File

@@ -37,7 +37,7 @@ TEST(ThreadLocalStorage, Basic) {
int number2 = 2;
int number3 = 3;
ThreadContext context = {"Test", ThreadType::Map, ThreadPriority::Regular};
ThreadContext context = {"Test"};
Thread<TestThread> thread1(context, &number1);
Thread<TestThread> thread2(context, &number2);
@@ -83,7 +83,7 @@ TEST(ThreadLocalStorage, AutoReclaim) {
DtorCounter* dtorCounter1 = new DtorCounter{ &counter };
DtorCounter* dtorCounter2 = new DtorCounter{ &counter };
ThreadContext context = {"Test", ThreadType::Map, ThreadPriority::Regular};
ThreadContext context = {"Test"};
auto thread1 = std::make_unique<Thread<TestThreadReclaim>>(context, dtorCounter1);
auto thread2 = std::make_unique<Thread<TestThreadReclaim>>(context, dtorCounter2);

View File

@@ -13,8 +13,6 @@ public:
TestThread(WorkQueue* queue_) : queue(queue_) {}
void send(std::function<void()>&& fn) {
EXPECT_TRUE(ThreadContext::currentlyOn(ThreadType::Map));
queue->push(std::move(fn));
}
@@ -26,13 +24,11 @@ TEST(WorkQueue, push) {
RunLoop loop;
WorkQueue queue;
Thread<TestThread> thread({"Test", ThreadType::Map, ThreadPriority::Regular}, &queue);
Thread<TestThread> thread({"Test"}, &queue);
uint8_t count = 0;
auto endTest = [&]() {
EXPECT_TRUE(ThreadContext::currentlyOn(ThreadType::Main));
if (++count == 4) {
loop.stop();
}