44namespace Open . Disposable
55{
66 public sealed class ConcurrentQueueObjectPool < T > : TrimmableObjectPoolBase < T >
7- where T : class
8- {
7+ where T : class
8+ {
99
10- public ConcurrentQueueObjectPool ( Func < T > factory , Action < T > recycler , int capacity = DEFAULT_CAPACITY )
11- : base ( factory , recycler , capacity )
12- {
13- Pool = new ConcurrentQueue < T > ( ) ;
14- }
10+ public ConcurrentQueueObjectPool ( Func < T > factory , Action < T > recycler , int capacity = DEFAULT_CAPACITY )
11+ : base ( factory , recycler , capacity )
12+ {
13+ Pool = new ConcurrentQueue < T > ( ) ;
14+ }
1515
16- public ConcurrentQueueObjectPool ( Func < T > factory , int capacity = DEFAULT_CAPACITY )
17- : this ( factory , null , capacity )
18- {
19-
20- }
16+ public ConcurrentQueueObjectPool ( Func < T > factory , int capacity = DEFAULT_CAPACITY )
17+ : this ( factory , null , capacity )
18+ {
2119
22- ConcurrentQueue < T > Pool ;
20+ }
2321
24- public override int Count => Pool ? . Count ?? 0 ;
22+ ConcurrentQueue < T > Pool ;
23+
24+ public override int Count => Pool ? . Count ?? 0 ;
2525
2626 // For ConcurrentQueue disable using the Pocket. It's fast enough as it is...
2727 protected override bool SaveToPocket ( T item )
@@ -35,40 +35,55 @@ protected override T TakeFromPocket()
3535 }
3636
3737 protected override bool Receive ( T item )
38- {
39- var p = Pool ;
40- if ( p == null ) return false ;
41- p . Enqueue ( item ) ; // It's possible that the count could exceed MaxSize here, but the risk is negligble as a few over the limit won't hurt.
42- return true ;
43- }
44-
45- protected override T TryRelease ( )
46- {
47- var p = Pool ;
48- if ( p == null ) return null ;
49- p . TryDequeue ( out T item ) ;
50- return item ;
51- }
52-
53- protected override void OnDispose ( bool calledExplicitly )
54- {
55- Pool = null ;
56- }
57-
58- }
59-
60- public static class ConcurrentQueueObjectPool
61- {
62- public static ConcurrentQueueObjectPool < T > Create < T > ( Func < T > factory , int capacity = Constants . DEFAULT_CAPACITY )
63- where T : class
64- {
65- return new ConcurrentQueueObjectPool < T > ( factory , capacity ) ;
66- }
67-
68- public static ConcurrentQueueObjectPool < T > Create < T > ( int capacity = Constants . DEFAULT_CAPACITY )
69- where T : class , new ( )
70- {
71- return Create ( ( ) => new T ( ) , capacity ) ;
72- }
73- }
38+ {
39+ var p = Pool ;
40+ if ( p == null ) return false ;
41+ p . Enqueue ( item ) ; // It's possible that the count could exceed MaxSize here, but the risk is negligble as a few over the limit won't hurt.
42+ return true ;
43+ }
44+
45+ protected override T TryRelease ( )
46+ {
47+ var p = Pool ;
48+ if ( p == null ) return null ;
49+ p . TryDequeue ( out T item ) ;
50+ return item ;
51+ }
52+
53+ protected override void OnDispose ( bool calledExplicitly )
54+ {
55+ Pool = null ;
56+ }
57+
58+ }
59+
60+ public static class ConcurrentQueueObjectPool
61+ {
62+ public static ConcurrentQueueObjectPool < T > Create < T > ( Func < T > factory , int capacity = Constants . DEFAULT_CAPACITY )
63+ where T : class
64+ {
65+ return new ConcurrentQueueObjectPool < T > ( factory , capacity ) ;
66+ }
67+
68+
69+ public static ConcurrentQueueObjectPool < T > Create < T > ( int capacity = Constants . DEFAULT_CAPACITY )
70+ where T : class , new ( )
71+ {
72+ return Create ( ( ) => new T ( ) , capacity ) ;
73+ }
74+
75+ public static ConcurrentQueueObjectPool < T > Create < T > ( Func < T > factory , bool autoRecycle , int capacity = Constants . DEFAULT_CAPACITY )
76+ where T : class , IRecyclable
77+ {
78+ Action < T > recycler = null ;
79+ if ( autoRecycle ) recycler = Recycler . Recycle ;
80+ return new ConcurrentQueueObjectPool < T > ( factory , recycler , capacity ) ;
81+ }
82+
83+ public static ConcurrentQueueObjectPool < T > Create < T > ( bool autoRecycle , int capacity = Constants . DEFAULT_CAPACITY )
84+ where T : class , IRecyclable , new ( )
85+ {
86+ return Create ( ( ) => new T ( ) , autoRecycle , capacity ) ;
87+ }
88+ }
7489}
0 commit comments