Add Map::pause/resume
Fixed bug after switching apps
This commit is contained in:
@@ -45,6 +45,12 @@ public:
|
||||
// function.
|
||||
void stop(stop_callback cb = nullptr, void *data = nullptr);
|
||||
|
||||
// Pauses the render thread. The render thread will stop running but will not be terminated and will not lose state until resumed.
|
||||
void pause();
|
||||
|
||||
// Resumes a paused render thread
|
||||
void resume();
|
||||
|
||||
// Runs the map event loop. ONLY run this function when you want to get render a single frame
|
||||
// with this map object. It will *not* spawn a separate thread and instead block until the
|
||||
// frame is completely rendered.
|
||||
@@ -145,6 +151,9 @@ private:
|
||||
static void terminate(uv_async_t *async);
|
||||
static void cleanup(uv_async_t *async);
|
||||
|
||||
// Checks if render thread needs to pause
|
||||
void check_for_pause();
|
||||
|
||||
// Setup
|
||||
void setup();
|
||||
|
||||
@@ -167,6 +176,12 @@ private:
|
||||
std::unique_ptr<uv_async_t> async_render;
|
||||
std::unique_ptr<uv_async_t> async_cleanup;
|
||||
|
||||
private:
|
||||
bool terminating = false;
|
||||
bool is_paused = false;
|
||||
std::unique_ptr<uv::mutex> mutex_pause;
|
||||
std::unique_ptr<uv::cond> cond_resume;
|
||||
|
||||
private:
|
||||
// If cleared, the next time the render thread attempts to render the map, it will *actually*
|
||||
// render the map.
|
||||
|
||||
@@ -21,6 +21,8 @@ class thread;
|
||||
class rwlock;
|
||||
class loop;
|
||||
class worker;
|
||||
class mutex;
|
||||
class cond;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -52,6 +52,7 @@ private:
|
||||
};
|
||||
|
||||
class mutex {
|
||||
friend class cond;
|
||||
public:
|
||||
inline mutex() {
|
||||
if (uv_mutex_init(&mtx) != 0) {
|
||||
@@ -66,6 +67,23 @@ private:
|
||||
uv_mutex_t mtx;
|
||||
};
|
||||
|
||||
class cond {
|
||||
public:
|
||||
inline cond() {
|
||||
if (uv_cond_init(&cnd) != 0) {
|
||||
throw std::runtime_error("failed to initialize condition variable");
|
||||
}
|
||||
}
|
||||
inline ~cond() { uv_cond_destroy(&cnd); }
|
||||
inline void signal() { uv_cond_signal(&cnd); }
|
||||
inline void broadcast() { uv_cond_broadcast(&cnd); }
|
||||
inline void wait(mutex &mtx) { uv_cond_wait(&cnd, &mtx.mtx); }
|
||||
inline void timedwait(mutex &mtx, uint64_t timeout) { uv_cond_timedwait(&cnd, &mtx.mtx, timeout); }
|
||||
|
||||
private:
|
||||
uv_cond_t cnd;
|
||||
};
|
||||
|
||||
class lock {
|
||||
public:
|
||||
lock(mutex &mtx_) : mtx(mtx_) { mtx.lock(); }
|
||||
|
||||
Reference in New Issue
Block a user