@@ -217,7 +217,8 @@ private boolean allocate(int size, int maxCapacity, Thread currentThread, Adapti |
217 | 217 | if (threadLocalMagazine != null && currentThread instanceof FastThreadLocalThread) { |
218 | 218 | Object mag = threadLocalMagazine.get(); |
219 | 219 | if (mag != NO_MAGAZINE) { |
220 | | - ((Magazine) mag).allocate(size, sizeBucket, maxCapacity, buf); |
| 220 | + boolean allocated = ((Magazine) mag).tryAllocate(size, sizeBucket, maxCapacity, buf); |
| 221 | + assert allocated : "Allocation of threadLocalMagazine must always succeed"; |
221 | 222 | return true; |
222 | 223 | } |
223 | 224 | } |
@@ -230,14 +231,9 @@ private boolean allocate(int size, int maxCapacity, Thread currentThread, Adapti |
230 | 231 | int index = (int) (threadId & mask); |
231 | 232 | for (int i = 0, m = Integer.numberOfTrailingZeros(~mask); i < m; i++) { |
232 | 233 | Magazine mag = mags[index + i & mask]; |
233 | | - long writeLock = mag.tryWriteLock(); |
234 | | - if (writeLock != 0) { |
235 | | - try { |
236 | | - mag.allocate(size, sizeBucket, maxCapacity, buf); |
237 | | - return true; |
238 | | - } finally { |
239 | | - mag.unlockWrite(writeLock); |
240 | | - } |
| 234 | + if (mag.tryAllocate(size, sizeBucket, maxCapacity, buf)) { |
| 235 | + // Was able to allocate. |
| 236 | + return true; |
241 | 237 | } |
242 | 238 | } |
243 | 239 | expansions++; |
@@ -339,8 +335,7 @@ static int sizeBucket(int size) { |
339 | 335 | } |
340 | 336 |
|
341 | 337 | @SuppressWarnings("checkstyle:finalclass") // Checkstyle mistakenly believes this class should be final. |
342 | | - private static class AllocationStatistics extends StampedLock { |
343 | | - private static final long serialVersionUID = -8319929980932269688L; |
| 338 | + private static class AllocationStatistics { |
344 | 339 | private static final int MIN_DATUM_TARGET = 1024; |
345 | 340 | private static final int MAX_DATUM_TARGET = 65534; |
346 | 341 | private static final int INIT_DATUM_TARGET = 9; |
@@ -442,28 +437,55 @@ protected int preferredChunkSize() { |
442 | 437 | } |
443 | 438 | } |
444 | 439 |
|
| 440 | + @SuppressJava6Requirement(reason = "Guarded by version check") |
445 | 441 | private static final class Magazine extends AllocationStatistics { |
446 | | - private static final long serialVersionUID = -4068223712022528165L; |
447 | 442 | private static final AtomicReferenceFieldUpdater<Magazine, Chunk> NEXT_IN_LINE; |
448 | | - |
449 | 443 | static { |
450 | 444 | NEXT_IN_LINE = AtomicReferenceFieldUpdater.newUpdater(Magazine.class, Chunk.class, "nextInLine"); |
451 | 445 | } |
452 | 446 | private Chunk current; |
453 | 447 | @SuppressWarnings("unused") // updated via NEXT_IN_LINE |
454 | 448 | private volatile Chunk nextInLine; |
455 | 449 | private final AtomicLong usedMemory; |
| 450 | + private final StampedLock allocationLock; |
456 | 451 |
|
457 | 452 | Magazine(AdaptivePoolingAllocator parent) { |
458 | 453 | this(parent, true); |
459 | 454 | } |
460 | 455 |
|
461 | 456 | Magazine(AdaptivePoolingAllocator parent, boolean shareable) { |
462 | 457 | super(parent, shareable); |
| 458 | + |
| 459 | + // We only need the StampedLock if this Magazine will be shared across threads. |
| 460 | + if (shareable) { |
| 461 | + allocationLock = new StampedLock(); |
| 462 | + } else { |
| 463 | + allocationLock = null; |
| 464 | + } |
463 | 465 | usedMemory = new AtomicLong(); |
464 | 466 | } |
465 | 467 |
|
466 | | - public void allocate(int size, int sizeBucket, int maxCapacity, AdaptiveByteBuf buf) { |
| 468 | + public boolean tryAllocate(int size, int sizeBucket, int maxCapacity, AdaptiveByteBuf buf) { |
| 469 | + if (allocationLock == null) { |
| 470 | + // This magazine is not shared across threads, just allocate directly. |
| 471 | + allocate(size, sizeBucket, maxCapacity, buf); |
| 472 | + return true; |
| 473 | + } |
| 474 | + |
| 475 | + // Try to retrieve the lock and if successful allocate. |
| 476 | + long writeLock = allocationLock.tryWriteLock(); |
| 477 | + if (writeLock != 0) { |
| 478 | + try { |
| 479 | + allocate(size, sizeBucket, maxCapacity, buf); |
| 480 | + return true; |
| 481 | + } finally { |
| 482 | + allocationLock.unlockWrite(writeLock); |
| 483 | + } |
| 484 | + } |
| 485 | + return false; |
| 486 | + } |
| 487 | + |
| 488 | + private void allocate(int size, int sizeBucket, int maxCapacity, AdaptiveByteBuf buf) { |
467 | 489 | recordAllocationSize(sizeBucket); |
468 | 490 | Chunk curr = current; |
469 | 491 | if (curr != null && curr.remainingCapacity() >= size) { |
|