77
88static volatile long initialized ;
99static DWORD dwTlsIndex ;
10- static CRITICAL_SECTION mutex ;
10+ CRITICAL_SECTION fscache_cs ;
1111
1212/*
1313 * Store one fscache per thread to avoid thread contention and locking.
@@ -392,8 +392,8 @@ int fscache_enable(size_t initial_size)
392392 * opendir and lstat function pointers are redirected if
393393 * any threads are using the fscache.
394394 */
395+ EnterCriticalSection (& fscache_cs );
395396 if (!initialized ) {
396- InitializeCriticalSection (& mutex );
397397 if (!dwTlsIndex ) {
398398 dwTlsIndex = TlsAlloc ();
399399 if (dwTlsIndex == TLS_OUT_OF_INDEXES )
@@ -404,12 +404,13 @@ int fscache_enable(size_t initial_size)
404404 opendir = fscache_opendir ;
405405 lstat = fscache_lstat ;
406406 }
407- InterlockedIncrement (& initialized );
407+ initialized ++ ;
408+ LeaveCriticalSection (& fscache_cs );
408409
409410 /* refcount the thread specific initialization */
410411 cache = fscache_getcache ();
411412 if (cache ) {
412- InterlockedIncrement ( & cache -> enabled ) ;
413+ cache -> enabled ++ ;
413414 } else {
414415 cache = (struct fscache * )xcalloc (1 , sizeof (* cache ));
415416 cache -> enabled = 1 ;
@@ -443,7 +444,7 @@ void fscache_disable(void)
443444 BUG ("fscache_disable() called on a thread where fscache has not been initialized" );
444445 if (!cache -> enabled )
445446 BUG ("fscache_disable() called on an fscache that is already disabled" );
446- InterlockedDecrement ( & cache -> enabled ) ;
447+ cache -> enabled -- ;
447448 if (!cache -> enabled ) {
448449 TlsSetValue (dwTlsIndex , NULL );
449450 trace_printf_key (& trace_fscache , "fscache_disable: lstat %u, opendir %u, "
@@ -456,12 +457,14 @@ void fscache_disable(void)
456457 }
457458
458459 /* update the global fscache initialization */
459- InterlockedDecrement (& initialized );
460+ EnterCriticalSection (& fscache_cs );
461+ initialized -- ;
460462 if (!initialized ) {
461463 /* reset opendir and lstat to the original implementations */
462464 opendir = dirent_opendir ;
463465 lstat = mingw_lstat ;
464466 }
467+ LeaveCriticalSection (& fscache_cs );
465468
466469 trace_printf_key (& trace_fscache , "fscache: disable\n" );
467470 return ;
@@ -628,7 +631,7 @@ void fscache_merge(struct fscache *dest)
628631 * isn't being used so the critical section only needs to prevent
629632 * the the child threads from stomping on each other.
630633 */
631- EnterCriticalSection (& mutex );
634+ EnterCriticalSection (& fscache_cs );
632635
633636 hashmap_iter_init (& cache -> map , & iter );
634637 while ((e = hashmap_iter_next (& iter )))
@@ -640,9 +643,9 @@ void fscache_merge(struct fscache *dest)
640643 dest -> opendir_requests += cache -> opendir_requests ;
641644 dest -> fscache_requests += cache -> fscache_requests ;
642645 dest -> fscache_misses += cache -> fscache_misses ;
643- LeaveCriticalSection (& mutex );
646+ initialized -- ;
647+ LeaveCriticalSection (& fscache_cs );
644648
645649 free (cache );
646650
647- InterlockedDecrement (& initialized );
648651}
0 commit comments