1010
1111static  volatile  long  initialized ;
1212static  DWORD  dwTlsIndex ;
13- static   CRITICAL_SECTION  mutex ;
13+ CRITICAL_SECTION  fscache_cs ;
1414
1515/* 
1616 * Store one fscache per thread to avoid thread contention and locking. 
@@ -388,12 +388,12 @@ int fscache_enable(size_t initial_size)
388388	 * opendir and lstat function pointers are redirected if 
389389	 * any threads are using the fscache. 
390390	 */ 
391+ 	EnterCriticalSection (& fscache_cs );
391392	if  (!initialized ) {
392- 		InitializeCriticalSection (& mutex );
393393		if  (!dwTlsIndex ) {
394394			dwTlsIndex  =  TlsAlloc ();
395395			if  (dwTlsIndex  ==  TLS_OUT_OF_INDEXES ) {
396- 				LeaveCriticalSection (& mutex );
396+ 				LeaveCriticalSection (& fscache_cs );
397397				return  0 ;
398398			}
399399		}
@@ -402,12 +402,13 @@ int fscache_enable(size_t initial_size)
402402		opendir  =  fscache_opendir ;
403403		lstat  =  fscache_lstat ;
404404	}
405- 	InterlockedIncrement (& initialized );
405+ 	initialized ++ ;
406+ 	LeaveCriticalSection (& fscache_cs );
406407
407408	/* refcount the thread specific initialization */ 
408409	cache  =  fscache_getcache ();
409410	if  (cache ) {
410- 		InterlockedIncrement ( & cache -> enabled ) ;
411+ 		cache -> enabled ++ ;
411412	} else  {
412413		cache  =  (struct  fscache  * )xcalloc (1 , sizeof (* cache ));
413414		cache -> enabled  =  1 ;
@@ -441,7 +442,7 @@ void fscache_disable(void)
441442		BUG ("fscache_disable() called on a thread where fscache has not been initialized" );
442443	if  (!cache -> enabled )
443444		BUG ("fscache_disable() called on an fscache that is already disabled" );
444- 	InterlockedDecrement ( & cache -> enabled ) ;
445+ 	cache -> enabled -- ;
445446	if  (!cache -> enabled ) {
446447		TlsSetValue (dwTlsIndex , NULL );
447448		trace_printf_key (& trace_fscache , "fscache_disable: lstat %u, opendir %u, " 
@@ -454,12 +455,14 @@ void fscache_disable(void)
454455	}
455456
456457	/* update the global fscache initialization */ 
457- 	InterlockedDecrement (& initialized );
458+ 	EnterCriticalSection (& fscache_cs );
459+ 	initialized -- ;
458460	if  (!initialized ) {
459461		/* reset opendir and lstat to the original implementations */ 
460462		opendir  =  dirent_opendir ;
461463		lstat  =  mingw_lstat ;
462464	}
465+ 	LeaveCriticalSection (& fscache_cs );
463466
464467	trace_printf_key (& trace_fscache , "fscache: disable\n" );
465468	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