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

Side by Side Diff: src/Platform.cpp

Issue 29727558: Issue 6489 - fix initialization moment of the default scheduler of DefaultPlatformBuilder (Closed) Base URL: https://github.com/adblockplus/libadblockplus@cd1cd70ea3122fb4b8a96c40d96ce9b1ae5a3ae9
Patch Set: Created March 19, 2018, 10:17 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 } 110 }
111 111
112 void Platform::WithLogSystem(const WithLogSystemCallback& callback) 112 void Platform::WithLogSystem(const WithLogSystemCallback& callback)
113 { 113 {
114 if (logSystem && callback) 114 if (logSystem && callback)
115 callback(*logSystem); 115 callback(*logSystem);
116 } 116 }
117 117
118 namespace 118 namespace
119 { 119 {
120 class SharedAsyncExecutor {
121 public:
122 SharedAsyncExecutor()
123 : executor(new AsyncExecutor())
124 {
125 }
126 void Dispatch(const std::function<void()>& task) {
127 std::lock_guard<std::mutex> lock(asyncExecutorMutex);
128 if (!executor)
129 return;
130 executor->Dispatch(task);
131 }
132 void Invalidate() {
133 std::unique_ptr<AsyncExecutor> tmp;
134 {
135 std::lock_guard<std::mutex> lock(asyncExecutorMutex);
136 tmp = move(executor);
137 }
138 }
139 private:
140 std::mutex asyncExecutorMutex;
141 std::unique_ptr<AsyncExecutor> executor;
142 };
143
144 class DefaultPlatform : public Platform 120 class DefaultPlatform : public Platform
145 { 121 {
146 public: 122 public:
147 typedef std::shared_ptr<SharedAsyncExecutor> AsyncExecutorPtr; 123 explicit DefaultPlatform(DefaultPlatformBuilder::AsyncExecutorPtr asyncExecu tor, CreationParameters&& creationParams)
148 explicit DefaultPlatform(const AsyncExecutorPtr& asyncExecutor, CreationPara meters&& creationParams)
149 : Platform(std::move(creationParams)), asyncExecutor(asyncExecutor) 124 : Platform(std::move(creationParams)), asyncExecutor(asyncExecutor)
150 { 125 {
151 } 126 }
152 ~DefaultPlatform(); 127 ~DefaultPlatform();
153 128
154 void WithTimer(const WithTimerCallback&) override; 129 void WithTimer(const WithTimerCallback&) override;
155 void WithFileSystem(const WithFileSystemCallback&) override; 130 void WithFileSystem(const WithFileSystemCallback&) override;
156 void WithWebRequest(const WithWebRequestCallback&) override; 131 void WithWebRequest(const WithWebRequestCallback&) override;
157 void WithLogSystem(const WithLogSystemCallback&) override; 132 void WithLogSystem(const WithLogSystemCallback&) override;
158 133
159 private: 134 private:
160 AsyncExecutorPtr asyncExecutor; 135 DefaultPlatformBuilder::AsyncExecutorPtr asyncExecutor;
161 std::recursive_mutex interfacesMutex; 136 std::recursive_mutex interfacesMutex;
162 }; 137 };
163 138
164 DefaultPlatform::~DefaultPlatform() 139 DefaultPlatform::~DefaultPlatform()
165 { 140 {
166 asyncExecutor->Invalidate(); 141 asyncExecutor->Invalidate();
167 LogSystemPtr tmpLogSystem; 142 LogSystemPtr tmpLogSystem;
168 TimerPtr tmpTimer; 143 TimerPtr tmpTimer;
169 FileSystemPtr tmpFileSystem; 144 FileSystemPtr tmpFileSystem;
170 WebRequestPtr tmpWebRequest; 145 WebRequestPtr tmpWebRequest;
(...skipping 24 matching lines...) Expand all
195 Platform::WithWebRequest(callback); 170 Platform::WithWebRequest(callback);
196 } 171 }
197 172
198 void DefaultPlatform::WithLogSystem(const WithLogSystemCallback& callback) 173 void DefaultPlatform::WithLogSystem(const WithLogSystemCallback& callback)
199 { 174 {
200 std::lock_guard<std::recursive_mutex> lock(interfacesMutex); 175 std::lock_guard<std::recursive_mutex> lock(interfacesMutex);
201 Platform::WithLogSystem(callback); 176 Platform::WithLogSystem(callback);
202 } 177 }
203 } 178 }
204 179
180 DefaultPlatformBuilder::DefaultPlatformBuilder()
181 {
182 auto sharedAsyncExecutor = this->sharedAsyncExecutor = std::make_shared<Option alAsyncExecutor>();
183 defaultScheduler = [sharedAsyncExecutor](const SchedulerTask& task)
184 {
185 sharedAsyncExecutor->Dispatch(task);
186 };
187 }
188
205 Scheduler DefaultPlatformBuilder::GetDefaultAsyncExecutor() 189 Scheduler DefaultPlatformBuilder::GetDefaultAsyncExecutor()
206 { 190 {
207 return defaultScheduler; 191 return defaultScheduler;
208 } 192 }
209 193
210 void DefaultPlatformBuilder::CreateDefaultTimer() 194 void DefaultPlatformBuilder::CreateDefaultTimer()
211 { 195 {
212 timer.reset(new DefaultTimer()); 196 timer.reset(new DefaultTimer());
213 } 197 }
214 198
215 void DefaultPlatformBuilder::CreateDefaultFileSystem(const std::string& basePath ) 199 void DefaultPlatformBuilder::CreateDefaultFileSystem(const std::string& basePath )
216 { 200 {
217 fileSystem.reset(new DefaultFileSystem(GetDefaultAsyncExecutor(), std::unique_ ptr<DefaultFileSystemSync>(new DefaultFileSystemSync(basePath)))); 201 fileSystem.reset(new DefaultFileSystem(GetDefaultAsyncExecutor(), std::unique_ ptr<DefaultFileSystemSync>(new DefaultFileSystemSync(basePath))));
218 } 202 }
219 203
220 void DefaultPlatformBuilder::CreateDefaultWebRequest(std::unique_ptr<IWebRequest Sync> webRequest) 204 void DefaultPlatformBuilder::CreateDefaultWebRequest(std::unique_ptr<IWebRequest Sync> webRequest)
221 { 205 {
222 if (!webRequest) 206 if (!webRequest)
223 webRequest.reset(new DefaultWebRequestSync()); 207 webRequest.reset(new DefaultWebRequestSync());
224 this->webRequest.reset(new DefaultWebRequest(GetDefaultAsyncExecutor(), std::m ove(webRequest))); 208 this->webRequest.reset(new DefaultWebRequest(GetDefaultAsyncExecutor(), std::m ove(webRequest)));
225 } 209 }
226 210
227 void DefaultPlatformBuilder::CreateDefaultLogSystem() 211 void DefaultPlatformBuilder::CreateDefaultLogSystem()
228 { 212 {
229 logSystem.reset(new DefaultLogSystem()); 213 logSystem.reset(new DefaultLogSystem());
230 } 214 }
231 215
232 std::unique_ptr<Platform> DefaultPlatformBuilder::CreatePlatform() 216 std::unique_ptr<Platform> DefaultPlatformBuilder::CreatePlatform()
233 { 217 {
234 auto sharedAsyncExecutor = std::make_shared<SharedAsyncExecutor>(); 218
235 defaultScheduler = [sharedAsyncExecutor](const SchedulerTask& task)
236 {
237 sharedAsyncExecutor->Dispatch(task);
238 };
239 if (!logSystem) 219 if (!logSystem)
240 CreateDefaultLogSystem(); 220 CreateDefaultLogSystem();
241 if (!timer) 221 if (!timer)
242 CreateDefaultTimer(); 222 CreateDefaultTimer();
243 if (!fileSystem) 223 if (!fileSystem)
244 CreateDefaultFileSystem(); 224 CreateDefaultFileSystem();
245 if (!webRequest) 225 if (!webRequest)
246 CreateDefaultWebRequest(); 226 CreateDefaultWebRequest();
247 227
248 std::unique_ptr<Platform> platform(new DefaultPlatform(sharedAsyncExecutor, st d::move(*this))); 228 std::unique_ptr<Platform> platform(new DefaultPlatform(std::move(sharedAsyncEx ecutor), std::move(*this)));
249 return platform; 229 return platform;
250 } 230 }
OLDNEW
« include/AdblockPlus/Platform.h ('K') | « include/AdblockPlus/Platform.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld