@@ -172,10 +172,9 @@ FbConnectionInternal CreateNewConnectionIfPossibleImpl(ConnectionString connecti
172172 }
173173 }
174174
175- bool _disposed ;
175+ int _disposed ;
176176 ConcurrentDictionary < string , Pool > _pools ;
177177 Timer _cleanupTimer ;
178- object _syncRootDisposeTimerCallback ;
179178
180179 static FbConnectionPoolManager ( )
181180 {
@@ -185,10 +184,9 @@ static FbConnectionPoolManager()
185184
186185 FbConnectionPoolManager ( )
187186 {
188- _disposed = false ;
187+ _disposed = 0 ;
189188 _pools = new ConcurrentDictionary < string , Pool > ( ) ;
190189 _cleanupTimer = new Timer ( CleanupCallback , null , TimeSpan . FromSeconds ( 2 ) , Timeout . InfiniteTimeSpan ) ;
191- _syncRootDisposeTimerCallback = new object ( ) ;
192190 }
193191
194192 internal FbConnectionInternal Get ( ConnectionString connectionString , FbConnection owner )
@@ -224,31 +222,27 @@ internal void ClearPool(ConnectionString connectionString)
224222
225223 public void Dispose ( )
226224 {
227- lock ( _syncRootDisposeTimerCallback )
225+ if ( Interlocked . Exchange ( ref _disposed , 1 ) == 1 )
226+ return ;
227+ using ( var mre = new ManualResetEvent ( false ) )
228228 {
229- if ( _disposed )
230- return ;
231- _disposed = true ;
232- // when NS1.6 is dropped it can be switched to Dispose(WaitHandle) and Volatile/Interlocked
233- _cleanupTimer . Dispose ( ) ;
234- _pools . Values . AsParallel ( ) . ForAll ( x => x . Dispose ( ) ) ;
229+ _cleanupTimer . Dispose ( mre ) ;
230+ mre . WaitOne ( ) ;
235231 }
232+ _pools . Values . AsParallel ( ) . ForAll ( x => x . Dispose ( ) ) ;
236233 }
237234
238235 void CleanupCallback ( object o )
239236 {
240- lock ( _syncRootDisposeTimerCallback )
241- {
242- if ( _disposed )
243- return ;
244- _pools . Values . AsParallel ( ) . ForAll ( x => x . CleanupPool ( ) ) ;
245- _cleanupTimer . Change ( TimeSpan . FromSeconds ( 2 ) , Timeout . InfiniteTimeSpan ) ;
246- }
237+ if ( Volatile . Read ( ref _disposed ) == 1 )
238+ return ;
239+ _pools . Values . AsParallel ( ) . ForAll ( x => x . CleanupPool ( ) ) ;
240+ _cleanupTimer . Change ( TimeSpan . FromSeconds ( 2 ) , Timeout . InfiniteTimeSpan ) ;
247241 }
248242
249243 void CheckDisposed ( )
250244 {
251- if ( Volatile . Read ( ref _disposed ) )
245+ if ( Volatile . Read ( ref _disposed ) == 1 )
252246 throw new ObjectDisposedException ( nameof ( FbConnectionPoolManager ) ) ;
253247 }
254248 }
0 commit comments