Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 public int getCounter() | 243 public int getCounter() |
244 { | 244 { |
245 return referenceCounter.get(); | 245 return referenceCounter.get(); |
246 } | 246 } |
247 | 247 |
248 /** | 248 /** |
249 * Register AdblockHelper engine client | 249 * Register AdblockHelper engine client |
250 * @param asynchronous If `true` engines will be created in background thread without locking of | 250 * @param asynchronous If `true` engines will be created in background thread without locking of |
251 * current thread. Use waitForReady() before getEngine() l ater. | 251 * current thread. Use waitForReady() before getEngine() l ater. |
252 * If `false` locks current thread. | 252 * If `false` locks current thread. |
253 * @return if a new instance is allocated | 253 */ |
254 */ | 254 public synchronized void retain(boolean asynchronous) |
255 public synchronized boolean retain(boolean asynchronous) | 255 { |
sergei
2017/09/26 09:44:13
It seems this code and `lastInstance` are not used
anton
2017/09/26 10:55:06
They are related but i will split it into separate
| |
256 { | |
257 boolean firstInstance = false; | |
258 | |
259 if (referenceCounter.getAndIncrement() == 0) | 256 if (referenceCounter.getAndIncrement() == 0) |
260 { | 257 { |
261 firstInstance = true; | |
262 | |
263 if (!asynchronous) | 258 if (!asynchronous) |
264 { | 259 { |
265 createAdblock(); | 260 createAdblock(); |
266 } | 261 } |
267 else | 262 else |
268 { | 263 { |
269 // latch is required for async (see `waitForReady()`) | 264 // latch is required for async (see `waitForReady()`) |
270 engineCreated = new CountDownLatch(1); | 265 engineCreated = new CountDownLatch(1); |
271 | 266 |
272 new Thread(new Runnable() | 267 new Thread(new Runnable() |
273 { | 268 { |
274 @Override | 269 @Override |
275 public void run() | 270 public void run() |
276 { | 271 { |
277 createAdblock(); | 272 createAdblock(); |
278 | 273 |
279 // unlock waiting client thread | 274 // unlock waiting client thread |
280 engineCreated.countDown(); | 275 engineCreated.countDown(); |
281 } | 276 } |
282 }).start(); | 277 }).start(); |
283 } | 278 } |
284 } | 279 } |
285 return firstInstance; | |
286 } | 280 } |
287 | 281 |
288 /** | 282 /** |
289 * Unregister AdblockHelper engine client | 283 * Unregister AdblockHelper engine client |
290 * @return if the last instance is destroyed | 284 */ |
291 */ | 285 public synchronized void release() |
292 public synchronized boolean release() | 286 { |
293 { | |
294 boolean lastInstance = false; | |
295 | |
296 if (referenceCounter.decrementAndGet() == 0) | 287 if (referenceCounter.decrementAndGet() == 0) |
297 { | 288 { |
298 lastInstance = true; | |
299 | |
300 if (engineCreated != null) | 289 if (engineCreated != null) |
301 { | 290 { |
302 // retained asynchronously | 291 // retained asynchronously |
303 waitForReady(); | 292 waitForReady(); |
304 disposeAdblock(); | 293 disposeAdblock(); |
305 | 294 |
306 // to unlock waiting client in waitForReady() | 295 // to unlock waiting client in waitForReady() |
307 engineCreated.countDown(); | 296 engineCreated.countDown(); |
308 engineCreated = null; | 297 engineCreated = null; |
309 } | 298 } |
310 else | 299 else |
311 { | 300 { |
312 disposeAdblock(); | 301 disposeAdblock(); |
313 } | 302 } |
314 } | 303 } |
315 return lastInstance; | |
316 } | 304 } |
317 } | 305 } |
LEFT | RIGHT |