/
Pool.java
1043 lines (928 loc) · 32.1 KB
/
Pool.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.util;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.component.Dumpable;
import org.eclipse.jetty.util.component.DumpableCollection;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.thread.Locker;
/**
* <p>A pool of objects, with optional support for multiplexing,
* max usage count and several optimized strategies plus
* an optional {@link ThreadLocal} cache of the last release entry.</p>
* <p>When the method {@link #close()} is called, all {@link Closeable}s
* object pooled by the pool are also closed.</p>
*
* @param <T> the type of the pooled objects
*/
@ManagedObject
public class Pool<T> implements AutoCloseable, Dumpable
{
private static final Logger LOGGER = Log.getLogger(Pool.class);
private final List<Entry> entries = new CopyOnWriteArrayList<>();
private final int maxEntries;
private final StrategyType strategyType;
/*
* The cache is used to avoid hammering on the first index of the entry list.
* Caches can become poisoned (i.e.: containing entries that are in use) when
* the release isn't done by the acquiring thread or when the entry pool is
* undersized compared to the load applied on it.
* When an entry can't be found in the cache, the global list is iterated
* with the configured strategy so the cache has no visible effect besides performance.
*/
private final Locker locker = new Locker();
private final ThreadLocal<Entry> cache;
private final AtomicInteger nextIndex;
private volatile boolean closed;
@Deprecated
private volatile int maxUsage = -1;
@Deprecated
private volatile int maxMultiplex = -1;
/**
* The type of the strategy to use for the pool.
* The strategy primarily determines where iteration over the pool entries begins.
*/
public enum StrategyType
{
/**
* A strategy that looks for an entry always starting from the first entry.
* It will favour the early entries in the pool, but may contend on them more.
*/
FIRST,
/**
* A strategy that looks for an entry by iterating from a random starting
* index. No entries are favoured and contention is reduced.
*/
RANDOM,
/**
* A strategy that uses the {@link Thread#getId()} of the current thread
* to select a starting point for an entry search. Whilst not as performant as
* using the {@link ThreadLocal} cache, it may be suitable when the pool is substantially smaller
* than the number of available threads.
* No entries are favoured and contention is reduced.
*/
THREAD_ID,
/**
* A strategy that looks for an entry by iterating from a starting point
* that is incremented on every search. This gives similar results to the
* random strategy but with more predictable behaviour.
* No entries are favoured and contention is reduced.
*/
ROUND_ROBIN
}
/**
* Construct a Pool with a specified lookup strategy and no
* {@link ThreadLocal} cache.
*
* @param strategyType The strategy to used for looking up entries.
* @param maxEntries the maximum amount of entries that the pool will accept.
*/
public Pool(StrategyType strategyType, int maxEntries)
{
this(strategyType, maxEntries, false);
}
/**
* Construct a Pool with the specified thread-local cache size and
* an optional {@link ThreadLocal} cache.
*
* @param strategyType The strategy to used for looking up entries.
* @param maxEntries the maximum amount of entries that the pool will accept.
* @param cache True if a {@link ThreadLocal} cache should be used to try the most recently released entry.
*/
public Pool(StrategyType strategyType, int maxEntries, boolean cache)
{
this.maxEntries = maxEntries;
this.strategyType = strategyType;
this.cache = cache ? new ThreadLocal<>() : null;
this.nextIndex = strategyType == StrategyType.ROUND_ROBIN ? new AtomicInteger() : null;
}
/**
* @return the number of reserved entries
*/
@ManagedAttribute("The number of reserved entries")
public int getReservedCount()
{
return (int)entries.stream().filter(Entry::isReserved).count();
}
/**
* @return the number of idle entries
*/
@ManagedAttribute("The number of idle entries")
public int getIdleCount()
{
return (int)entries.stream().filter(Entry::isIdle).count();
}
/**
* @return the number of in-use entries
*/
@ManagedAttribute("The number of in-use entries")
public int getInUseCount()
{
return (int)entries.stream().filter(Entry::isInUse).count();
}
/**
* @return the number of closed entries
*/
@ManagedAttribute("The number of closed entries")
public int getClosedCount()
{
return (int)entries.stream().filter(Entry::isClosed).count();
}
/**
* @return the maximum number of entries
*/
@ManagedAttribute("The maximum number of entries")
public int getMaxEntries()
{
return maxEntries;
}
/**
* @return the default maximum multiplex count of entries
* @deprecated Multiplex functionalities will be removed
*/
@ManagedAttribute("The default maximum multiplex count of entries")
@Deprecated
public int getMaxMultiplex()
{
return maxMultiplex == -1 ? 1 : maxMultiplex;
}
/**
* <p>Retrieves the max multiplex count for the given pooled object.</p>
*
* @param pooled the pooled object
* @return the max multiplex count for the given pooled object
* @deprecated Multiplex functionalities will be removed
*/
@Deprecated
protected int getMaxMultiplex(T pooled)
{
return getMaxMultiplex();
}
/**
* <p>Sets the default maximum multiplex count for the Pool's entries.</p>
*
* @param maxMultiplex the default maximum multiplex count of entries
* @deprecated Multiplex functionalities will be removed
*/
@Deprecated
public final void setMaxMultiplex(int maxMultiplex)
{
if (maxMultiplex < 1)
throw new IllegalArgumentException("Max multiplex must be >= 1");
try (Locker.Lock l = locker.lock())
{
if (closed)
return;
if (entries.stream().anyMatch(MonoEntry.class::isInstance))
throw new IllegalStateException("Pool entries do not support multiplexing");
this.maxMultiplex = maxMultiplex;
}
}
/**
* <p>Returns the maximum number of times the entries of the pool
* can be acquired.</p>
*
* @return the default maximum usage count of entries
* @deprecated MaxUsage functionalities will be removed
*/
@ManagedAttribute("The default maximum usage count of entries")
@Deprecated
public int getMaxUsageCount()
{
return maxUsage;
}
/**
* <p>Retrieves the max usage count for the given pooled object.</p>
*
* @param pooled the pooled object
* @return the max usage count for the given pooled object
* @deprecated MaxUsage functionalities will be removed
*/
@Deprecated
protected int getMaxUsageCount(T pooled)
{
return getMaxUsageCount();
}
/**
* <p>Sets the maximum usage count for the Pool's entries.</p>
* <p>All existing idle entries that have a usage count larger
* than this new value are removed from the Pool and closed.</p>
*
* @param maxUsageCount the default maximum usage count of entries
* @deprecated MaxUsage functionalities will be removed
*/
@Deprecated
public final void setMaxUsageCount(int maxUsageCount)
{
if (maxUsageCount == 0)
throw new IllegalArgumentException("Max usage count must be != 0");
// Iterate the entries, remove overused ones and collect a list of the closeable removed ones.
List<Closeable> copy;
try (Locker.Lock l = locker.lock())
{
if (closed)
return;
if (entries.stream().anyMatch(MonoEntry.class::isInstance))
throw new IllegalStateException("Pool entries do not support max usage");
this.maxUsage = maxUsageCount;
copy = entries.stream()
.filter(entry -> entry.isIdleAndOverUsed() && remove(entry) && entry.pooled instanceof Closeable)
.map(entry -> (Closeable)entry.pooled)
.collect(Collectors.toList());
}
// Iterate the copy and close the collected entries.
copy.forEach(IO::close);
}
/**
* <p>Creates a new disabled slot into the pool.</p>
* <p>The returned entry must ultimately have the {@link Entry#enable(Object, boolean)}
* method called or be removed via {@link Pool.Entry#remove()} or
* {@link Pool#remove(Pool.Entry)}.</p>
*
* @param allotment the desired allotment, where each entry handles an allotment of maxMultiplex,
* or a negative number to always trigger the reservation of a new entry.
* @return a disabled entry that is contained in the pool,
* or null if the pool is closed or if the pool already contains
* {@link #getMaxEntries()} entries, or the allotment has already been reserved
* @deprecated Use {@link #reserve()} instead
*/
@Deprecated
public Entry reserve(int allotment)
{
try (Locker.Lock l = locker.lock())
{
if (closed)
return null;
int space = maxEntries - entries.size();
if (space <= 0)
return null;
if (allotment >= 0 && (getReservedCount() * getMaxMultiplex()) >= allotment)
return null;
Entry entry = newEntry();
entries.add(entry);
return entry;
}
}
/**
* <p>Creates a new disabled slot into the pool.</p>
* <p>The returned entry must ultimately have the {@link Entry#enable(Object, boolean)}
* method called or be removed via {@link Pool.Entry#remove()} or
* {@link Pool#remove(Pool.Entry)}.</p>
*
* @return a disabled entry that is contained in the pool,
* or null if the pool is closed or if the pool already contains
* {@link #getMaxEntries()} entries
*/
public Entry reserve()
{
try (Locker.Lock l = locker.lock())
{
if (closed)
return null;
// If we have no space
if (entries.size() >= maxEntries)
return null;
Entry entry = newEntry();
entries.add(entry);
return entry;
}
}
private Entry newEntry()
{
// Do not allow more than 2 implementations of Entry, otherwise call sites in Pool
// referencing Entry methods will become mega-morphic and kill the performance.
if (maxMultiplex >= 0 || maxUsage >= 0)
return new MultiEntry();
return new MonoEntry();
}
/**
* <p>Acquires the entry from the pool at the specified index.</p>
* <p>This method bypasses the thread-local cache mechanism.</p>
*
* @param idx the index of the entry to acquire.
* @return the specified entry or null if there is none at the specified index or if it is not available.
* @deprecated No longer supported. Instead use a {@link StrategyType} to configure the pool.
*/
@Deprecated
public Entry acquireAt(int idx)
{
if (closed)
return null;
try
{
Entry entry = entries.get(idx);
if (entry.tryAcquire())
return entry;
}
catch (IndexOutOfBoundsException e)
{
// no entry at that index
}
return null;
}
/**
* <p>Acquires an entry from the pool.</p>
* <p>Only enabled entries will be returned from this method
* and their {@link Entry#enable(Object, boolean)}
* method must not be called.</p>
*
* @return an entry from the pool or null if none is available.
*/
public Entry acquire()
{
if (closed)
return null;
int size = entries.size();
if (size == 0)
return null;
if (cache != null)
{
Pool<T>.Entry entry = cache.get();
if (entry != null && entry.tryAcquire())
return entry;
}
int index = startIndex(size);
for (int tries = size; tries-- > 0;)
{
try
{
Pool<T>.Entry entry = entries.get(index);
if (entry != null && entry.tryAcquire())
return entry;
}
catch (IndexOutOfBoundsException e)
{
LOGGER.ignore(e);
size = entries.size();
// Size can be 0 when the pool is in the middle of
// acquiring a connection while another thread
// removes the last one from the pool.
if (size == 0)
break;
}
index = (index + 1) % size;
}
return null;
}
private int startIndex(int size)
{
switch (strategyType)
{
case FIRST:
return 0;
case RANDOM:
return ThreadLocalRandom.current().nextInt(size);
case ROUND_ROBIN:
return nextIndex.getAndUpdate(c -> Math.max(0, c + 1)) % size;
case THREAD_ID:
return (int)(Thread.currentThread().getId() % size);
default:
throw new IllegalArgumentException("Unknown strategy type: " + strategyType);
}
}
/**
* <p>Acquires an entry from the pool,
* reserving and creating a new entry if necessary.</p>
*
* @param creator a function to create the pooled value for a reserved entry.
* @return an entry from the pool or null if none is available.
*/
public Entry acquire(Function<Pool<T>.Entry, T> creator)
{
Entry entry = acquire();
if (entry != null)
return entry;
entry = reserve();
if (entry == null)
return null;
T value;
try
{
value = creator.apply(entry);
}
catch (Throwable th)
{
remove(entry);
throw th;
}
if (value == null)
{
remove(entry);
return null;
}
return entry.enable(value, true) ? entry : null;
}
/**
* <p>Releases an {@link #acquire() acquired} entry to the pool.</p>
* <p>Entries that are acquired from the pool but never released
* will result in a memory leak.</p>
*
* @param entry the value to return to the pool
* @return true if the entry was released and could be acquired again,
* false if the entry should be removed by calling {@link #remove(Pool.Entry)}
* and the object contained by the entry should be disposed.
*/
public boolean release(Entry entry)
{
if (closed)
return false;
boolean released = entry.tryRelease();
if (released && cache != null)
cache.set(entry);
return released;
}
/**
* <p>Removes an entry from the pool.</p>
*
* @param entry the value to remove
* @return true if the entry was removed, false otherwise
*/
public boolean remove(Entry entry)
{
if (closed)
return false;
if (!entry.tryRemove())
{
if (LOGGER.isDebugEnabled())
LOGGER.debug("Attempt to remove an object from the pool that is still in use: {}", entry);
return false;
}
boolean removed = entries.remove(entry);
if (!removed && LOGGER.isDebugEnabled())
LOGGER.debug("Attempt to remove an object from the pool that does not exist: {}", entry);
return removed;
}
public boolean isClosed()
{
return closed;
}
@Override
public void close()
{
List<Entry> copy;
try (Locker.Lock l = locker.lock())
{
closed = true;
copy = new ArrayList<>(entries);
entries.clear();
}
// iterate the copy and close its entries
for (Entry entry : copy)
{
boolean removed = entry.tryRemove();
if (removed)
{
if (entry.pooled instanceof Closeable)
IO.close((Closeable)entry.pooled);
}
else
{
if (LOGGER.isDebugEnabled())
LOGGER.debug("Pooled object still in use: {}", entry);
}
}
}
public int size()
{
return entries.size();
}
public Collection<Entry> values()
{
return Collections.unmodifiableCollection(entries);
}
@Override
public void dump(Appendable out, String indent) throws IOException
{
Dumpable.dumpObjects(out, indent, this,
new DumpableCollection("entries", entries));
}
@Override
public String toString()
{
return String.format("%s@%x[inUse=%d,size=%d,capacity=%d,closed=%b]",
getClass().getSimpleName(),
hashCode(),
getInUseCount(),
size(),
getMaxEntries(),
isClosed());
}
/**
* <p>A Pool entry that holds metadata and a pooled object.</p>
*/
public abstract class Entry
{
// The pooled object. This is not volatile as it is set once and then never changed.
// Other threads accessing must check the state field above first, so a good before/after
// relationship exists to make a memory barrier.
private T pooled;
/**
* <p>Enables this, previously {@link #reserve() reserved}, Entry.</p>
* <p>An entry returned from the {@link #reserve()} method must be enabled with this method,
* once and only once, before it is usable by the pool.</p>
* <p>The entry may be enabled and not acquired, in which case it is immediately available to be
* acquired, potentially by another thread; or it can be enabled and acquired atomically so that
* no other thread can acquire it, although the acquire may still fail if the pool has been closed.</p>
*
* @param pooled the pooled object for this Entry
* @param acquire whether this Entry should be atomically enabled and acquired
* @return whether this Entry was enabled
* @throws IllegalStateException if this Entry was already enabled
*/
public boolean enable(T pooled, boolean acquire)
{
Objects.requireNonNull(pooled);
if (!isReserved())
{
if (isClosed())
return false; // Pool has been closed
throw new IllegalStateException("Entry already enabled: " + this);
}
this.pooled = pooled;
if (tryEnable(acquire))
return true;
this.pooled = null;
if (isClosed())
return false; // Pool has been closed
throw new IllegalStateException("Entry already enabled: " + this);
}
/**
* @return the pooled object
*/
public T getPooled()
{
return pooled;
}
/**
* <p>Releases this Entry.</p>
* <p>This is equivalent to calling {@link Pool#release(Pool.Entry)} passing this entry.</p>
*
* @return whether this Entry was released
*/
public boolean release()
{
return Pool.this.release(this);
}
/**
* <p>Removes this Entry from the Pool.</p>
* <p>This is equivalent to calling {@link Pool#remove(Pool.Entry)} passing this entry.</p>
*
* @return whether this Entry was removed
*/
public boolean remove()
{
return Pool.this.remove(this);
}
/**
* <p>Tries to enable, and possible also acquire, this Entry.</p>
*
* @param acquire whether to also acquire this Entry
* @return whether this Entry was enabled
*/
abstract boolean tryEnable(boolean acquire);
/**
* <p>Tries to acquire this Entry.</p>
*
* @return whether this Entry was acquired
*/
abstract boolean tryAcquire();
/**
* <p>Tries to release this Entry.</p>
*
* @return true if this Entry was released,
* false if {@link #tryRemove()} should be called.
*/
abstract boolean tryRelease();
/**
* <p>Tries to remove the entry by marking it as closed.</p>
*
* @return whether the entry can be removed from the containing pool
*/
abstract boolean tryRemove();
/**
* @return whether this Entry is closed
*/
public abstract boolean isClosed();
/**
* @return whether this Entry is reserved
*/
public abstract boolean isReserved();
/**
* @return whether this Entry is idle
*/
public abstract boolean isIdle();
/**
* @return whether this entry is in use.
*/
public abstract boolean isInUse();
/**
* @return whether this entry has been used beyond {@link #getMaxUsageCount()}
* @deprecated MaxUsage functionalities will be removed
*/
@Deprecated
public boolean isOverUsed()
{
return false;
}
boolean isIdleAndOverUsed()
{
return false;
}
// Only for testing.
int getUsageCount()
{
return 0;
}
// Only for testing.
void setUsageCount(int usageCount)
{
}
}
/**
* <p>A Pool entry that holds metadata and a pooled object,
* that can only be acquired concurrently at most once, and
* can be acquired/released multiple times.</p>
*/
private class MonoEntry extends Entry
{
// MIN_VALUE => pending; -1 => closed; 0 => idle; 1 => active;
private final AtomicInteger state = new AtomicInteger(Integer.MIN_VALUE);
@Override
protected boolean tryEnable(boolean acquire)
{
return state.compareAndSet(Integer.MIN_VALUE, acquire ? 1 : 0);
}
@Override
boolean tryAcquire()
{
while (true)
{
int s = state.get();
if (s != 0)
return false;
if (state.compareAndSet(s, 1))
return true;
}
}
@Override
boolean tryRelease()
{
while (true)
{
int s = state.get();
if (s < 0)
return false;
if (s == 0)
throw new IllegalStateException("Cannot release an already released entry");
if (state.compareAndSet(s, 0))
return true;
}
}
@Override
boolean tryRemove()
{
state.set(-1);
return true;
}
@Override
public boolean isClosed()
{
return state.get() < 0;
}
@Override
public boolean isReserved()
{
return state.get() == Integer.MIN_VALUE;
}
@Override
public boolean isIdle()
{
return state.get() == 0;
}
@Override
public boolean isInUse()
{
return state.get() == 1;
}
@Override
public String toString()
{
String s;
switch (state.get())
{
case Integer.MIN_VALUE:
s = "PENDING";
break;
case -1:
s = "CLOSED";
break;
case 0:
s = "IDLE";
break;
default:
s = "ACTIVE";
}
return String.format("%s@%x{%s,pooled=%s}",
getClass().getSimpleName(),
hashCode(),
s,
getPooled());
}
}
/**
* <p>A Pool entry that holds metadata and a pooled object,
* that can be acquired concurrently multiple times, and
* can be acquired/released multiple times.</p>
*/
class MultiEntry extends Entry
{
// hi: MIN_VALUE => pending; -1 => closed; 0+ => usage counter;
// lo: 0 => idle; positive => multiplex counter
private final AtomicBiInteger state;
MultiEntry()
{
this.state = new AtomicBiInteger(Integer.MIN_VALUE, 0);
}
@Override
void setUsageCount(int usageCount)
{
this.state.getAndSetHi(usageCount);
}
@Override
protected boolean tryEnable(boolean acquire)
{
int usage = acquire ? 1 : 0;
return state.compareAndSet(Integer.MIN_VALUE, usage, 0, usage);
}
/**
* <p>Tries to acquire the entry if possible by incrementing both the usage
* count and the multiplex count.</p>
*
* @return true if the usage count is less than {@link #getMaxUsageCount()} and
* the multiplex count is less than {@link #getMaxMultiplex(Object)} and
* the entry is not closed, false otherwise.
*/
@Override
boolean tryAcquire()
{
while (true)
{
long encoded = state.get();
int usageCount = AtomicBiInteger.getHi(encoded);
int multiplexCount = AtomicBiInteger.getLo(encoded);
boolean closed = usageCount < 0;
if (closed)
return false;
T pooled = getPooled();
int maxUsageCount = getMaxUsageCount(pooled);
int maxMultiplexed = getMaxMultiplex(pooled);
if (maxMultiplexed > 0 && multiplexCount >= maxMultiplexed)
return false;
if (maxUsageCount > 0 && usageCount >= maxUsageCount)
return false;
// Prevent overflowing the usage counter by capping it at Integer.MAX_VALUE.
int newUsageCount = usageCount == Integer.MAX_VALUE ? Integer.MAX_VALUE : usageCount + 1;
if (state.compareAndSet(encoded, newUsageCount, multiplexCount + 1))
return true;
}
}
/**
* <p>Tries to release the entry if possible by decrementing the multiplex
* count unless the entity is closed.</p>
*
* @return true if the entry was released,
* false if {@link #tryRemove()} should be called.
*/
@Override
boolean tryRelease()
{
int newMultiplexCount;
int usageCount;
while (true)
{
long encoded = state.get();
usageCount = AtomicBiInteger.getHi(encoded);
boolean closed = usageCount < 0;
if (closed)
return false;
newMultiplexCount = AtomicBiInteger.getLo(encoded) - 1;
if (newMultiplexCount < 0)
throw new IllegalStateException("Cannot release an already released entry");
if (state.compareAndSet(encoded, usageCount, newMultiplexCount))
break;
}
int currentMaxUsageCount = maxUsage;
boolean overUsed = currentMaxUsageCount > 0 && usageCount >= currentMaxUsageCount;
return !(overUsed && newMultiplexCount == 0);
}
/**
* <p>Tries to remove the entry by marking it as closed and decrementing the multiplex counter.</p>
* <p>The multiplex counter will never go below zero and if it reaches zero, the entry is considered removed.</p>
*
* @return true if the entry can be removed from the containing pool, false otherwise.
*/
@Override
boolean tryRemove()
{
while (true)
{
long encoded = state.get();
int usageCount = AtomicBiInteger.getHi(encoded);
int multiplexCount = AtomicBiInteger.getLo(encoded);
int newMultiplexCount = Math.max(multiplexCount - 1, 0);
boolean removed = state.compareAndSet(usageCount, -1, multiplexCount, newMultiplexCount);
if (removed)
return newMultiplexCount == 0;
}
}
@Override
public boolean isClosed()
{
return state.getHi() < 0;
}
@Override
public boolean isReserved()
{
return state.getHi() == Integer.MIN_VALUE;
}
@Override
public boolean isIdle()
{
long encoded = state.get();
return AtomicBiInteger.getHi(encoded) >= 0 && AtomicBiInteger.getLo(encoded) == 0;
}
@Override
public boolean isInUse()
{
long encoded = state.get();
return AtomicBiInteger.getHi(encoded) >= 0 && AtomicBiInteger.getLo(encoded) > 0;
}
@Override
public boolean isOverUsed()