Move run() to MapContext

This commit is contained in:
John Firebaugh
2015-04-20 17:30:20 -07:00
parent 7bd735220d
commit 424dae0a2c
6 changed files with 136 additions and 146 deletions

View File

@@ -12,7 +12,6 @@
#include <string>
#include <functional>
#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <memory>
@@ -150,33 +149,12 @@ private:
void resize(uint16_t width, uint16_t height, float ratio = 1);
void resize(uint16_t width, uint16_t height, float ratio, uint16_t fbWidth, uint16_t fbHeight);
// Checks if render thread needs to pause
void checkForPause();
// Setup
void setup();
size_t sourceCacheSize;
const std::unique_ptr<Environment> env;
std::unique_ptr<EnvironmentScope> scope;
View &view;
const std::unique_ptr<MapData> data;
std::unique_ptr<MapContext> context;
private:
std::thread thread;
bool pausing = false;
bool isPaused = false;
std::mutex mutexRun;
std::condition_variable condRun;
std::mutex mutexPause;
std::condition_variable condPause;
// Stores whether the map thread has been stopped already.
std::atomic_bool isStopped;
StillImageCallback callback;
};
}

View File

@@ -17,6 +17,7 @@ typedef XID GLXPbuffer;
#include <mbgl/platform/gl.hpp>
#include <memory>
#include <thread>
namespace mbgl {

View File

@@ -3,7 +3,6 @@
#include <mbgl/map/map_context.hpp>
#include <mbgl/map/view.hpp>
#include <mbgl/map/map_data.hpp>
#include <mbgl/map/still_image.hpp>
#include <mbgl/map/sprite.hpp>
#include <mbgl/map/source.hpp>
@@ -73,27 +72,14 @@ void Map::start(bool startPaused, MapMode renderMode) {
data->mode = renderMode;
// Reset the flag.
isStopped = false;
context->start();
data->isStopped = false;
// Do we need to pause first?
if (startPaused) {
pause();
}
thread = std::thread([this]() {
#ifdef __APPLE__
pthread_setname_np("Map");
#endif
run();
// Make sure that the stop() function knows when to stop invoking the callback function.
isStopped = true;
view.notify();
});
context->start();
context->triggerUpdate();
}
@@ -112,14 +98,14 @@ void Map::stop(std::function<void ()> cb) {
// the case with Cocoa's NSURLRequest. Otherwise, we will eventually deadlock because this
// thread (== main thread) is blocked. The callback function should use an efficient waiting
// function to avoid a busy waiting loop.
while (!isStopped) {
while (!data->isStopped) {
cb();
}
}
// If a callback function was provided, this should return immediately because the thread has
// already finished executing.
thread.join();
data->thread.join();
data->mode = MapMode::None;
}
@@ -127,17 +113,17 @@ void Map::stop(std::function<void ()> cb) {
void Map::pause(bool waitForPause) {
assert(Environment::currentlyOn(ThreadType::Main));
assert(data->mode == MapMode::Continuous);
mutexRun.lock();
pausing = true;
mutexRun.unlock();
data->mutexRun.lock();
data->pausing = true;
data->mutexRun.unlock();
uv_stop(env->loop);
context->triggerUpdate(); // Needed to ensure uv_stop is seen and uv_run exits, otherwise we deadlock on wait_for_pause
if (waitForPause) {
std::unique_lock<std::mutex> lockPause (mutexPause);
while (!isPaused) {
condPause.wait(lockPause);
std::unique_lock<std::mutex> lockPause (data->mutexPause);
while (!data->isPaused) {
data->condPause.wait(lockPause);
}
}
}
@@ -146,10 +132,10 @@ void Map::resume() {
assert(Environment::currentlyOn(ThreadType::Main));
assert(data->mode != MapMode::None);
mutexRun.lock();
pausing = false;
condRun.notify_all();
mutexRun.unlock();
data->mutexRun.lock();
data->pausing = false;
data->condRun.notify_all();
data->mutexRun.unlock();
}
void Map::renderStill(StillImageCallback fn) {
@@ -159,80 +145,17 @@ void Map::renderStill(StillImageCallback fn) {
throw util::Exception("Map is not in still image render mode");
}
if (callback) {
if (data->callback) {
throw util::Exception("Map is currently rendering an image");
}
assert(data->mode == MapMode::Still);
callback = std::move(fn);
data->callback = std::move(fn);
context->triggerUpdate(Update::RenderStill);
}
void Map::run() {
EnvironmentScope mapScope(*env, ThreadType::Map, "Map");
assert(Environment::currentlyOn(ThreadType::Map));
assert(data->mode != MapMode::None);
if (data->mode == MapMode::Continuous) {
checkForPause();
}
view.activate();
view.discard();
context->workers = util::make_unique<Worker>(env->loop, 4);
setup();
context->prepare();
if (data->mode == MapMode::Continuous) {
context->terminating = false;
while (!context->terminating) {
uv_run(env->loop, UV_RUN_DEFAULT);
checkForPause();
}
} else if (data->mode == MapMode::Still) {
context->terminating = false;
while (!context->terminating) {
uv_run(env->loop, UV_RUN_DEFAULT);
// After the loop terminated, these async handles may have been deleted if the terminate()
// callback was fired. In this case, we are exiting the loop.
if (context->asyncTerminate && context->asyncUpdate) {
// Otherwise, loop termination means that we have acquired and parsed all resources
// required for this map image and we can now proceed to rendering.
context->render();
auto image = view.readStillImage();
// We are moving the callback out of the way and empty it in case the callback function
// starts the next map image render.
assert(callback);
StillImageCallback cb;
std::swap(cb, callback);
// Now we can finally invoke the callback function with the map image we rendered.
cb(std::move(image));
// To prepare for the next event loop run, we have to make sure the async handles keep
// the loop alive.
context->asyncTerminate->ref();
context->asyncUpdate->ref();
context->asyncInvoke->ref();
context->asyncRender->ref();
}
}
} else {
abort();
}
// Run the event loop once more to make sure our async delete handlers are called.
uv_run(env->loop, UV_RUN_ONCE);
view.deactivate();
}
void Map::renderSync() {
// Must be called in UI thread.
assert(Environment::currentlyOn(ThreadType::Main));
@@ -242,38 +165,12 @@ void Map::renderSync() {
context->rendered.wait();
}
void Map::checkForPause() {
std::unique_lock<std::mutex> lockRun (mutexRun);
while (pausing) {
view.deactivate();
mutexPause.lock();
isPaused = true;
condPause.notify_all();
mutexPause.unlock();
condRun.wait(lockRun);
view.activate();
}
mutexPause.lock();
isPaused = false;
mutexPause.unlock();
}
void Map::update() {
context->triggerUpdate();
}
#pragma mark - Setup
void Map::setup() {
assert(Environment::currentlyOn(ThreadType::Map));
assert(context->painter);
context->painter->setup();
}
std::string Map::getStyleURL() const {
return data->getStyleInfo().url;
}

View File

@@ -4,6 +4,7 @@
#include <mbgl/map/environment.hpp>
#include <mbgl/map/source.hpp>
#include <mbgl/map/sprite.hpp>
#include <mbgl/map/still_image.hpp>
#include <mbgl/platform/log.hpp>
@@ -54,7 +55,7 @@ void MapContext::start() {
// could dispatch to the worker pool.
workers.reset();
terminating = true;
data.terminating = true;
// Closes all open handles on the loop. This means that the loop will automatically terminate.
asyncRender.reset();
@@ -86,8 +87,100 @@ void MapContext::start() {
// Finally, notify all listeners that we have finished rendering this frame.
rendered.notify();
});
data.thread = std::thread([this]() {
#ifdef __APPLE__
pthread_setname_np("Map");
#endif
EnvironmentScope mapScope(env, ThreadType::Map, "Map");
assert(Environment::currentlyOn(ThreadType::Map));
assert(data.mode != MapMode::None);
if (data.mode == MapMode::Continuous) {
checkForPause();
}
view.activate();
view.discard();
workers = util::make_unique<Worker>(env.loop, 4);
assert(painter);
painter->setup();
prepare();
if (data.mode == MapMode::Continuous) {
data.terminating = false;
while (!data.terminating) {
uv_run(env.loop, UV_RUN_DEFAULT);
checkForPause();
}
} else if (data.mode == MapMode::Still) {
data.terminating = false;
while (!data.terminating) {
uv_run(env.loop, UV_RUN_DEFAULT);
// After the loop terminated, these async handles may have been deleted if the terminate()
// callback was fired. In this case, we are exiting the loop.
if (asyncTerminate && asyncUpdate) {
// Otherwise, loop termination means that we have acquired and parsed all resources
// required for this map image and we can now proceed to rendering.
render();
auto image = view.readStillImage();
// We are moving the callback out of the way and empty it in case the callback function
// starts the next map image render.
assert(data.callback);
MapData::StillImageCallback cb;
std::swap(cb, data.callback);
// Now we can finally invoke the callback function with the map image we rendered.
cb(std::move(image));
// To prepare for the next event loop run, we have to make sure the async handles keep
// the loop alive.
asyncTerminate->ref();
asyncUpdate->ref();
asyncInvoke->ref();
asyncRender->ref();
}
}
} else {
abort();
}
// Run the event loop once more to make sure our async delete handlers are called.
uv_run(env.loop, UV_RUN_ONCE);
view.deactivate();
// Make sure that the stop() function knows when to stop invoking the callback function.
data.isStopped = true;
view.notify();
});
}
void MapContext::checkForPause() {
std::unique_lock<std::mutex> lockRun (data.mutexRun);
while (data.pausing) {
view.deactivate();
data.mutexPause.lock();
data.isPaused = true;
data.condPause.notify_all();
data.mutexPause.unlock();
data.condRun.wait(lockRun);
view.activate();
}
data.mutexPause.lock();
data.isPaused = false;
data.mutexPause.unlock();
}
void MapContext::terminate() {
assert(asyncTerminate);

View File

@@ -9,6 +9,7 @@
#include <vector>
#include <queue>
#include <future>
#include <thread>
namespace uv {
class async;
@@ -33,7 +34,7 @@ class MapContext {
public:
MapContext(Environment&, View&, MapData&);
// Starts the async handles.
// Starts the map thread.
void start();
// Terminates the map thread
@@ -58,6 +59,9 @@ public:
// These can only be called from the Map thread.
private:
// Checks if render thread needs to pause
void checkForPause();
Worker& getWorker();
util::ptr<Sprite> getSprite();
void updateTiles();
@@ -114,9 +118,6 @@ public:
// Used to signal that rendering completed.
public: util::Signal rendered;
// TODO: document this
public: bool terminating = false;
};
}

View File

@@ -17,6 +17,8 @@
namespace mbgl {
class StillImage;
struct StyleInfo {
std::string url;
std::string base;
@@ -119,6 +121,24 @@ private:
std::atomic<uint8_t> debug { false };
std::atomic<Duration> animationTime;
std::atomic<Duration> defaultTransitionDuration;
public:
// TODO: make private
bool terminating = false;
std::thread thread;
bool pausing = false;
bool isPaused = false;
std::mutex mutexRun;
std::condition_variable condRun;
std::mutex mutexPause;
std::condition_variable condPause;
// Stores whether the map thread has been stopped already.
std::atomic_bool isStopped;
using StillImageCallback = std::function<void(std::unique_ptr<const StillImage>)>;
StillImageCallback callback;
};
}