Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1015 from mapbox/log_scope
Browse files Browse the repository at this point in the history
Scope threads to an Environment
  • Loading branch information
kkaefer committed Mar 18, 2015
2 parents 7b99601 + 3254cdc commit 1c276cb
Show file tree
Hide file tree
Showing 20 changed files with 229 additions and 115 deletions.
4 changes: 2 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ Standard: Cpp11
IndentWidth: 4
AccessModifierOffset: -4
UseTab: Never
BinPackParameters: true
BinPackParameters: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
AlwaysBreakTemplateDeclarations: true
NamespaceIndentation: None
PointerBindsToType: false
PointerBindsToType: true
SpacesInParentheses: false
BreakBeforeBraces: Attach
ColumnLimit: 100
Expand Down
3 changes: 0 additions & 3 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,6 @@ class Map : private util::noncopyable {
// Stores whether the map thread has been stopped already.
std::atomic_bool isStopped;

const std::thread::id mainThread;
std::thread::id mapThread;

Transform transform;
TransformState state;

Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/platform/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Log {
// This method is the data sink that must be implemented by each platform we
// support. It should ideally output the error message in a human readable
// format to the developer.
static void platformRecord(EventSeverity severity, Event event, int64_t code, const std::string &msg);
static void platformRecord(EventSeverity severity, const std::string &msg);
};

}
Expand Down
7 changes: 2 additions & 5 deletions platform/android/log_android.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#include <mbgl/platform/log.hpp>

#define __STDC_FORMAT_MACROS // NDK bug workaround: https://code.google.com/p/android/issues/detail?id=72349
#include <cinttypes>

#include <android/log.h>

namespace mbgl {
Expand Down Expand Up @@ -30,8 +27,8 @@ int severityToPriority(EventSeverity severity) {

} // namespace

void Log::platformRecord(EventSeverity severity, Event event, int64_t code, const std::string &msg) {
__android_log_print(severityToPriority(severity), EventClass(event).c_str(), "(%" PRId64 ") %s", code, msg.c_str());
void Log::platformRecord(EventSeverity severity, const std::string &msg) {
__android_log_print(severityToPriority(severity), "mbgl", "%s", msg.c_str());
}

}
6 changes: 2 additions & 4 deletions platform/darwin/log_nslog.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

namespace mbgl {

void Log::platformRecord(EventSeverity severity, Event event, int64_t code,
const std::string &msg) {
void Log::platformRecord(EventSeverity severity, const std::string &msg) {
NSString *message =
[[NSString alloc] initWithBytes:msg.data() length:msg.size() encoding:NSUTF8StringEncoding];
NSLog(@"[%s] %s: (%lld) %@", EventSeverityClass(severity).c_str(), EventClass(event).c_str(),
code, message);
NSLog(@"[%s] %@", EventSeverityClass(severity).c_str(), message);
}

}
4 changes: 2 additions & 2 deletions platform/default/log_stderr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace mbgl {

void Log::platformRecord(EventSeverity severity, Event event, int64_t code, const std::string &msg) {
std::cerr << "[" << severity << "] " << event << ": (" << code << ") " << msg << std::endl;
void Log::platformRecord(EventSeverity severity, const std::string &msg) {
std::cerr << "[" << severity << "] " << msg << std::endl;
}

}
117 changes: 107 additions & 10 deletions src/mbgl/map/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,129 @@

#include <uv.h>

#include <atomic>
#include <cassert>
#include <mutex>
#include <unordered_map>

namespace mbgl {

Environment::Environment(FileSource &fs) : fileSource(fs), loop(uv_loop_new()) {
namespace {

class ThreadInfoStore {
private:
struct ThreadInfo {
Environment* env;
ThreadType type;
std::string name;
};

public:
ThreadInfoStore() {
registerThread(nullptr, ThreadType::Main, "Main");
}

~ThreadInfoStore() {
unregisterThread();
assert(threadSet.size() == 0);
}

void registerThread(Environment* env, ThreadType type, const std::string& name) {
std::lock_guard<std::mutex> lock(mtx);

// FIXME: We should never need to overwrite a thread here and we only allow
// this today because on the Static mode, the Map thread and the Main thread
// are same. Replace this with emplace() when this gets fixed.
threadSet[std::this_thread::get_id()] = ThreadInfo{ env, type, name };
}

void unregisterThread() {
std::lock_guard<std::mutex> lock(mtx);

ThreadSet::iterator it = threadSet.find(std::this_thread::get_id());
if (it != threadSet.end()) {
threadSet.erase(it);
}
}

const ThreadInfo& getThreadInfo() const {
static ThreadInfo emptyInfo;
std::lock_guard<std::mutex> lock(mtx);

ThreadSet::const_iterator it = threadSet.find(std::this_thread::get_id());
if (it != threadSet.end()) {
return it->second;
} else {
return emptyInfo;
}
}

private:
typedef std::unordered_map<std::thread::id, ThreadInfo> ThreadSet;
ThreadSet threadSet;

mutable std::mutex mtx;
};

unsigned makeEnvironmentID() {
static std::atomic<unsigned> id(0);
return id++;
}

ThreadInfoStore threadInfoStore;

} // namespace

Environment::Scope::Scope(Environment& env, ThreadType type, const std::string& name)
: id(std::this_thread::get_id()) {
threadInfoStore.registerThread(&env, type, name);
}

Environment::Scope::~Scope() {
assert(id == std::this_thread::get_id());
threadInfoStore.unregisterThread();
}

Environment::Environment(FileSource& fs)
: id(makeEnvironmentID()), fileSource(fs), loop(uv_loop_new()) {
}

Environment& Environment::Get() {
Environment* env = threadInfoStore.getThreadInfo().env;
assert(env);

return *env;
}

bool Environment::inScope() {
return threadInfoStore.getThreadInfo().env;
}

bool Environment::currentlyOn(ThreadType type) {
return static_cast<uint8_t>(threadInfoStore.getThreadInfo().type) & static_cast<uint8_t>(type);
}

void Environment::setup() {
mapThread = std::this_thread::get_id();
std::string Environment::threadName() {
return threadInfoStore.getThreadInfo().name;
}

bool Environment::inMapThread() const {
return std::this_thread::get_id() == mapThread;
unsigned Environment::getID() const {
return id;
}

void Environment::requestAsync(const Resource &resource, std::function<void(const Response &)> callback) {
void Environment::requestAsync(const Resource& resource,
std::function<void(const Response&)> callback) {
fileSource.request(resource, *this, std::move(callback));
}

Request *Environment::request(const Resource &resource, std::function<void(const Response &)> callback) {
assert(inMapThread());
Request* Environment::request(const Resource& resource,
std::function<void(const Response&)> callback) {
assert(currentlyOn(ThreadType::Map));
return fileSource.request(resource, loop, *this, std::move(callback));
}

void Environment::cancelRequest(Request *req) {
assert(inMapThread());
void Environment::cancelRequest(Request* req) {
assert(currentlyOn(ThreadType::Map));
fileSource.cancel(req);
}

Expand Down
38 changes: 28 additions & 10 deletions src/mbgl/map/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,45 @@ class Request;
class Response;
struct Resource;

class Environment : private util::noncopyable {
enum class ThreadType : uint8_t {
Unknown = 0,
Main = 1 << 0,
Map = 1 << 1,
TileWorker = 1 << 2,
};

class Environment final : private util::noncopyable {
public:
Environment(FileSource &);
class Scope final {
public:
Scope(Environment&, ThreadType, const std::string& name);
~Scope();

private:
std::thread::id id;
};

void setup();
Environment(FileSource&);

bool inMapThread() const;
static Environment& Get();
static bool inScope();
static bool currentlyOn(ThreadType);
static std::string threadName();

void requestAsync(const Resource &, std::function<void(const Response &)>);
Request *request(const Resource &, std::function<void(const Response &)>);
void cancelRequest(Request *);
unsigned getID() const;
void requestAsync(const Resource&, std::function<void(const Response&)>);
Request* request(const Resource&, std::function<void(const Response&)>);
void cancelRequest(Request*);

// Request to terminate the environment.
void terminate();

private:
FileSource &fileSource;
std::thread::id mapThread;
unsigned id;
FileSource& fileSource;

public:
uv_loop_t *const loop;
uv_loop_t* const loop;
};

}
Expand Down
5 changes: 2 additions & 3 deletions src/mbgl/map/live_tile_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ LiveTileData::LiveTileData(Tile::ID const& id_,
GlyphStore& glyphStore_,
SpriteAtlas& spriteAtlas_,
util::ptr<Sprite> sprite_,
const SourceInfo& source_,
Environment& env_)
const SourceInfo& source_)
: VectorTileData::VectorTileData(id_, mapMaxZoom, style_, glyphAtlas_, glyphStore_,
spriteAtlas_, sprite_, source_, env_),
spriteAtlas_, sprite_, source_),
annotationManager(annotationManager_) {
// live features are always ready
state = State::loaded;
Expand Down
3 changes: 1 addition & 2 deletions src/mbgl/map/live_tile_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class LiveTileData : public VectorTileData {
GlyphStore&,
SpriteAtlas&,
util::ptr<Sprite>,
const SourceInfo&,
Environment&);
const SourceInfo&);
~LiveTileData();

void parse() override;
Expand Down
Loading

0 comments on commit 1c276cb

Please sign in to comment.