88
99static volatile long initialized ;
1010static DWORD dwTlsIndex ;
11- static CRITICAL_SECTION mutex ;
11+ CRITICAL_SECTION fscache_cs ;
1212
1313/*
1414 * Store one fscache per thread to avoid thread contention and locking.
@@ -386,12 +386,12 @@ int fscache_enable(size_t initial_size)
386386 * opendir and lstat function pointers are redirected if
387387 * any threads are using the fscache.
388388 */
389+ EnterCriticalSection (& fscache_cs );
389390 if (!initialized ) {
390- InitializeCriticalSection (& mutex );
391391 if (!dwTlsIndex ) {
392392 dwTlsIndex = TlsAlloc ();
393393 if (dwTlsIndex == TLS_OUT_OF_INDEXES ) {
394- LeaveCriticalSection (& mutex );
394+ LeaveCriticalSection (& fscache_cs );
395395 return 0 ;
396396 }
397397 }
@@ -400,12 +400,13 @@ int fscache_enable(size_t initial_size)
400400 opendir = fscache_opendir ;
401401 lstat = fscache_lstat ;
402402 }
403- InterlockedIncrement (& initialized );
403+ initialized ++ ;
404+ LeaveCriticalSection (& fscache_cs );
404405
405406 /* refcount the thread specific initialization */
406407 cache = fscache_getcache ();
407408 if (cache ) {
408- InterlockedIncrement ( & cache -> enabled ) ;
409+ cache -> enabled ++ ;
409410 } else {
410411 cache = (struct fscache * )xcalloc (1 , sizeof (* cache ));
411412 cache -> enabled = 1 ;
@@ -439,7 +440,7 @@ void fscache_disable(void)
439440 BUG ("fscache_disable() called on a thread where fscache has not been initialized" );
440441 if (!cache -> enabled )
441442 BUG ("fscache_disable() called on an fscache that is already disabled" );
442- InterlockedDecrement ( & cache -> enabled ) ;
443+ cache -> enabled -- ;
443444 if (!cache -> enabled ) {
444445 TlsSetValue (dwTlsIndex , NULL );
445446 trace_printf_key (& trace_fscache , "fscache_disable: lstat %u, opendir %u, "
@@ -452,12 +453,14 @@ void fscache_disable(void)
452453 }
453454
454455 /* update the global fscache initialization */
455- InterlockedDecrement (& initialized );
456+ EnterCriticalSection (& fscache_cs );
457+ initialized -- ;
456458 if (!initialized ) {
457459 /* reset opendir and lstat to the original implementations */
458460 opendir = dirent_opendir ;
459461 lstat = mingw_lstat ;
460462 }
463+ LeaveCriticalSection (& fscache_cs );
461464
462465 trace_printf_key (& trace_fscache , "fscache: disable\n" );
463466 return ;
@@ -626,7 +629,7 @@ void fscache_merge(struct fscache *dest)
626629 * isn't being used so the critical section only needs to prevent
627630 * the the child threads from stomping on each other.
628631 */
629- EnterCriticalSection (& mutex );
632+ EnterCriticalSection (& fscache_cs );
630633
631634 hashmap_iter_init (& cache -> map , & iter );
632635 while ((e = hashmap_iter_next (& iter )))
@@ -638,9 +641,9 @@ void fscache_merge(struct fscache *dest)
638641 dest -> opendir_requests += cache -> opendir_requests ;
639642 dest -> fscache_requests += cache -> fscache_requests ;
640643 dest -> fscache_misses += cache -> fscache_misses ;
641- LeaveCriticalSection (& mutex );
644+ initialized -- ;
645+ LeaveCriticalSection (& fscache_cs );
642646
643647 free (cache );
644648
645- InterlockedDecrement (& initialized );
646649}
0 commit comments