-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/CartoDB/mobile-sdk
- Loading branch information
Showing
17 changed files
with
293 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include "RedrawWorker.h" | ||
#include "renderers/MapRenderer.h" | ||
#include "renderers/RedrawRequestListener.h" | ||
#include "utils/Log.h" | ||
#include "utils/ThreadUtils.h" | ||
|
||
namespace carto { | ||
|
||
RedrawWorker::RedrawWorker() : | ||
_wakeupMap(), | ||
_mapRenderer(), | ||
_stop(false), | ||
_idle(false), | ||
_condition(), | ||
_mutex() | ||
{ | ||
} | ||
|
||
RedrawWorker::~RedrawWorker() { | ||
} | ||
|
||
void RedrawWorker::setComponents(const std::weak_ptr<MapRenderer>& mapRenderer, const std::shared_ptr<RedrawWorker>& worker) { | ||
_mapRenderer = mapRenderer; | ||
// When the map component gets destroyed all threads get detatched. Detatched threads need their worker objects to be alive, | ||
// so worker objects need to keep references to themselves, until the loop finishes. | ||
_worker = worker; | ||
} | ||
|
||
void RedrawWorker::init(const DirectorPtr<RedrawRequestListener>& listener) { | ||
std::lock_guard<std::mutex> lock(_mutex); | ||
|
||
std::chrono::steady_clock::time_point wakeupTime = std::chrono::steady_clock::now(); | ||
if (_wakeupMap.find(listener) != _wakeupMap.end()) { | ||
return; | ||
} | ||
_wakeupMap[listener] = wakeupTime; | ||
_idle = false; | ||
_condition.notify_one(); | ||
} | ||
|
||
void RedrawWorker::stop() { | ||
std::lock_guard<std::mutex> lock(_mutex); | ||
_stop = true; | ||
_condition.notify_all(); | ||
} | ||
|
||
bool RedrawWorker::isIdle() const { | ||
std::lock_guard<std::mutex> lock(_mutex); | ||
return _idle; | ||
} | ||
|
||
void RedrawWorker::operator ()() { | ||
run(); | ||
_worker.reset(); | ||
} | ||
|
||
void RedrawWorker::run() { | ||
ThreadUtils::SetThreadPriority(ThreadPriority::HIGH); | ||
while (true) { | ||
std::vector<DirectorPtr<RedrawRequestListener> > listeners; | ||
{ | ||
std::unique_lock<std::mutex> lock(_mutex); | ||
|
||
if (_stop) { | ||
return; | ||
} | ||
|
||
std::chrono::steady_clock::time_point currentTime = std::chrono::steady_clock::now(); | ||
std::chrono::steady_clock::time_point wakeupTime = std::chrono::steady_clock::now() + std::chrono::hours(24); | ||
for (auto it = _wakeupMap.begin(); it != _wakeupMap.end(); ) { | ||
if (it->second - currentTime < std::chrono::milliseconds(1)) { | ||
listeners.push_back(it->first); | ||
it = _wakeupMap.erase(it); | ||
} else { | ||
wakeupTime = std::min(wakeupTime, it->second); | ||
it++; | ||
} | ||
} | ||
|
||
if (listeners.empty()) { | ||
_idle = _wakeupMap.empty(); | ||
_condition.wait_for(lock, wakeupTime - std::chrono::steady_clock::now()); | ||
_idle = false; | ||
} | ||
} | ||
|
||
for (const DirectorPtr<RedrawRequestListener>& listener : listeners) { | ||
listener->onRedrawRequested(); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2016 CartoDB. All rights reserved. | ||
* Copying and using this code is allowed only according | ||
* to license terms, as given in https://cartodb.com/terms/ | ||
*/ | ||
|
||
#ifndef _CARTO_REDRAWWORKER_H_ | ||
#define _CARTO_REDRAWWORKER_H_ | ||
|
||
#include "components/DirectorPtr.h" | ||
#include "components/ThreadWorker.h" | ||
|
||
#include <chrono> | ||
#include <condition_variable> | ||
#include <map> | ||
#include <memory> | ||
#include <thread> | ||
#include <vector> | ||
|
||
namespace carto { | ||
class MapRenderer; | ||
class Options; | ||
class RedrawRequestListener; | ||
|
||
class RedrawWorker : public ThreadWorker { | ||
public: | ||
RedrawWorker(); | ||
virtual ~RedrawWorker(); | ||
|
||
void setComponents(const std::weak_ptr<MapRenderer>& mapRenderer, const std::shared_ptr<RedrawWorker>& worker); | ||
|
||
void init(const DirectorPtr<RedrawRequestListener>& listener); | ||
|
||
void stop(); | ||
|
||
bool isIdle() const; | ||
|
||
void operator()(); | ||
|
||
private: | ||
void run(); | ||
|
||
std::map<DirectorPtr<RedrawRequestListener>, std::chrono::steady_clock::time_point> _wakeupMap; | ||
|
||
std::weak_ptr<MapRenderer> _mapRenderer; | ||
std::shared_ptr<RedrawWorker> _worker; | ||
|
||
bool _stop; | ||
bool _idle; | ||
std::condition_variable _condition; | ||
mutable std::mutex _mutex; | ||
}; | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.