@@ -172,10 +172,9 @@ FbConnectionInternal CreateNewConnectionIfPossibleImpl(ConnectionString connecti
172
172
}
173
173
}
174
174
175
- bool _disposed ;
175
+ int _disposed ;
176
176
ConcurrentDictionary < string , Pool > _pools ;
177
177
Timer _cleanupTimer ;
178
- object _syncRootDisposeTimerCallback ;
179
178
180
179
static FbConnectionPoolManager ( )
181
180
{
@@ -185,10 +184,9 @@ static FbConnectionPoolManager()
185
184
186
185
FbConnectionPoolManager ( )
187
186
{
188
- _disposed = false ;
187
+ _disposed = 0 ;
189
188
_pools = new ConcurrentDictionary < string , Pool > ( ) ;
190
189
_cleanupTimer = new Timer ( CleanupCallback , null , TimeSpan . FromSeconds ( 2 ) , Timeout . InfiniteTimeSpan ) ;
191
- _syncRootDisposeTimerCallback = new object ( ) ;
192
190
}
193
191
194
192
internal FbConnectionInternal Get ( ConnectionString connectionString , FbConnection owner )
@@ -224,31 +222,27 @@ internal void ClearPool(ConnectionString connectionString)
224
222
225
223
public void Dispose ( )
226
224
{
227
- lock ( _syncRootDisposeTimerCallback )
225
+ if ( Interlocked . Exchange ( ref _disposed , 1 ) == 1 )
226
+ return ;
227
+ using ( var mre = new ManualResetEvent ( false ) )
228
228
{
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 ( ) ;
235
231
}
232
+ _pools . Values . AsParallel ( ) . ForAll ( x => x . Dispose ( ) ) ;
236
233
}
237
234
238
235
void CleanupCallback ( object o )
239
236
{
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 ) ;
247
241
}
248
242
249
243
void CheckDisposed ( )
250
244
{
251
- if ( Volatile . Read ( ref _disposed ) )
245
+ if ( Volatile . Read ( ref _disposed ) == 1 )
252
246
throw new ObjectDisposedException ( nameof ( FbConnectionPoolManager ) ) ;
253
247
}
254
248
}
0 commit comments