Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/Platform.cpp

Issue 29543810: Issue 5118 - Lock the platform interfaces before use (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Review feedback Created Sept. 13, 2017, 7:49 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/JsEngine.cpp ('k') | src/WebRequestJsObject.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/Platform.cpp
===================================================================
--- a/src/Platform.cpp
+++ b/src/Platform.cpp
@@ -90,53 +90,105 @@
}
FilterEngine& Platform::GetFilterEngine()
{
CreateFilterEngineAsync();
return *std::shared_future<FilterEnginePtr>(filterEngine).get();
}
-ITimer& Platform::GetTimer()
+void Platform::WithTimer(const WithTimerCallback& callback)
{
- return *timer;
+ if (timer && callback)
+ callback(*timer);
}
-IFileSystem& Platform::GetFileSystem()
+void Platform::WithFileSystem(const WithFileSystemCallback& callback)
{
- return *fileSystem;
+ if (fileSystem && callback)
+ callback(*fileSystem);
}
-IWebRequest& Platform::GetWebRequest()
+void Platform::WithWebRequest(const WithWebRequestCallback& callback)
{
- return *webRequest;
+ if (webRequest && callback)
+ callback(*webRequest);
}
-LogSystem& Platform::GetLogSystem()
+void Platform::WithLogSystem(const WithLogSystemCallback& callback)
{
- return *logSystem;
+ if (logSystem && callback)
+ callback(*logSystem);
}
namespace
{
class DefaultPlatform : public Platform
{
public:
typedef std::shared_ptr<Scheduler> AsyncExecutorPtr;
explicit DefaultPlatform(const AsyncExecutorPtr& asyncExecutor, CreationParameters&& creationParams)
: Platform(std::move(creationParams)), asyncExecutor(asyncExecutor)
{
}
- ~DefaultPlatform()
- {
- asyncExecutor.reset();
- }
+ ~DefaultPlatform();
+
+ virtual void WithTimer(const WithTimerCallback&) override;
sergei 2017/09/13 20:06:37 Could you please remove the `virtual` keyword here
hub 2017/09/13 20:17:51 Done.
+ virtual void WithFileSystem(const WithFileSystemCallback&) override;
+ virtual void WithWebRequest(const WithWebRequestCallback&) override;
+ virtual void WithLogSystem(const WithLogSystemCallback&) override;
+
private:
AsyncExecutorPtr asyncExecutor;
+ std::recursive_mutex interfacesMutex;
};
+
+ DefaultPlatform::~DefaultPlatform()
+ {
+ asyncExecutor.reset();
+ LogSystemPtr tmpLogSystem;
+ TimerPtr tmpTimer;
+ FileSystemPtr tmpFileSystem;
+ WebRequestPtr tmpWebRequest;
+ {
+ std::lock_guard<std::recursive_mutex> lock(interfacesMutex);
+ tmpLogSystem = std::move(logSystem);
+ tmpTimer = std::move(timer);
+ tmpFileSystem = std::move(fileSystem);
+ tmpWebRequest = std::move(webRequest);
+ }
+ tmpLogSystem.reset();
+ tmpTimer.reset();
+ tmpFileSystem.reset();
+ tmpWebRequest.reset();
sergei 2017/09/13 20:06:37 I think this calls of reset are not required.
hub 2017/09/13 20:17:51 Done.
+ }
+
+ void DefaultPlatform::WithTimer(const WithTimerCallback& callback)
+ {
+ std::lock_guard<std::recursive_mutex> lock(interfacesMutex);
+ Platform::WithTimer(callback);
+ }
+
+ void DefaultPlatform::WithFileSystem(const WithFileSystemCallback& callback)
+ {
+ std::lock_guard<std::recursive_mutex> lock(interfacesMutex);
+ Platform::WithFileSystem(callback);
+ }
+
+ void DefaultPlatform::WithWebRequest(const WithWebRequestCallback& callback)
+ {
+ std::lock_guard<std::recursive_mutex> lock(interfacesMutex);
+ Platform::WithWebRequest(callback);
+ }
+
+ void DefaultPlatform::WithLogSystem(const WithLogSystemCallback& callback)
+ {
+ std::lock_guard<std::recursive_mutex> lock(interfacesMutex);
+ Platform::WithLogSystem(callback);
+ }
}
Scheduler DefaultPlatformBuilder::GetDefaultAsyncExecutor()
{
if (!defaultScheduler)
{
asyncExecutor = std::make_shared<Scheduler>(::DummyScheduler);
std::weak_ptr<Scheduler> weakExecutor = asyncExecutor;
@@ -182,9 +234,9 @@
if (!fileSystem)
CreateDefaultFileSystem();
if (!webRequest)
CreateDefaultWebRequest();
std::unique_ptr<Platform> platform(new DefaultPlatform(asyncExecutor, std::move(*this)));
asyncExecutor.reset();
return platform;
-}
+}
« no previous file with comments | « src/JsEngine.cpp ('k') | src/WebRequestJsObject.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld