| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 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 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 public int getCounter() | 232 public int getCounter() |
| 233 { | 233 { |
| 234 return referenceCounter.get(); | 234 return referenceCounter.get(); |
| 235 } | 235 } |
| 236 | 236 |
| 237 /** | 237 /** |
| 238 * Register AdblockHelper engine client | 238 * Register AdblockHelper engine client |
| 239 * @param asynchronous If `true` engines will be created in background thread without locking of | 239 * @param asynchronous If `true` engines will be created in background thread without locking of |
| 240 * current thread. Use waitForReady() before getEngine() l ater. | 240 * current thread. Use waitForReady() before getEngine() l ater. |
| 241 * If `false` locks current thread. | 241 * If `false` locks current thread. |
| 242 * @return if a new instance is allocated | |
| 242 */ | 243 */ |
| 243 public synchronized void retain(boolean asynchronous) | 244 public synchronized boolean retain(boolean asynchronous) |
|
sergei
2017/09/28 08:29:03
Could you please point to where and how it's actua
anton
2017/09/28 09:08:39
Java's `synchronized` token excludes mutiple threa
sergei
2017/09/28 09:33:05
BTW, I plan to add an asynchronous version of crea
| |
| 244 { | 245 { |
| 246 boolean firstInstance = false; | |
| 247 | |
| 245 if (referenceCounter.getAndIncrement() == 0) | 248 if (referenceCounter.getAndIncrement() == 0) |
| 246 { | 249 { |
| 250 firstInstance = true; | |
| 251 | |
| 247 if (!asynchronous) | 252 if (!asynchronous) |
| 248 { | 253 { |
| 249 createAdblock(); | 254 createAdblock(); |
| 250 } | 255 } |
| 251 else | 256 else |
| 252 { | 257 { |
| 253 // latch is required for async (see `waitForReady()`) | 258 // latch is required for async (see `waitForReady()`) |
| 254 engineCreated = new CountDownLatch(1); | 259 engineCreated = new CountDownLatch(1); |
| 255 | 260 |
| 256 new Thread(new Runnable() | 261 new Thread(new Runnable() |
| 257 { | 262 { |
| 258 @Override | 263 @Override |
| 259 public void run() | 264 public void run() |
| 260 { | 265 { |
| 261 createAdblock(); | 266 createAdblock(); |
| 262 | 267 |
| 263 // unlock waiting client thread | 268 // unlock waiting client thread |
| 264 engineCreated.countDown(); | 269 engineCreated.countDown(); |
| 265 } | 270 } |
| 266 }).start(); | 271 }).start(); |
| 267 } | 272 } |
| 268 } | 273 } |
| 274 return firstInstance; | |
| 269 } | 275 } |
| 270 | 276 |
| 271 /** | 277 /** |
| 272 * Unregister AdblockHelper engine client | 278 * Unregister AdblockHelper engine client |
| 279 * @return `true` if the last instance is destroyed | |
| 273 */ | 280 */ |
| 274 public synchronized void release() | 281 public synchronized boolean release() |
| 275 { | 282 { |
| 283 boolean lastInstance = false; | |
| 284 | |
| 276 if (referenceCounter.decrementAndGet() == 0) | 285 if (referenceCounter.decrementAndGet() == 0) |
| 277 { | 286 { |
| 287 lastInstance = true; | |
| 288 | |
| 278 if (engineCreated != null) | 289 if (engineCreated != null) |
| 279 { | 290 { |
| 280 // retained asynchronously | 291 // retained asynchronously |
| 281 waitForReady(); | 292 waitForReady(); |
| 282 disposeAdblock(); | 293 disposeAdblock(); |
| 283 | 294 |
| 284 // to unlock waiting client in waitForReady() | 295 // to unlock waiting client in waitForReady() |
| 285 engineCreated.countDown(); | 296 engineCreated.countDown(); |
| 286 engineCreated = null; | 297 engineCreated = null; |
| 287 } | 298 } |
| 288 else | 299 else |
| 289 { | 300 { |
| 290 disposeAdblock(); | 301 disposeAdblock(); |
| 291 } | 302 } |
| 292 } | 303 } |
| 304 return lastInstance; | |
| 293 } | 305 } |
| 294 } | 306 } |
| OLD | NEW |