Skip to content

Commit 984e058

Browse files
dschoGit for Windows Build Agent
authored andcommitted
Merge pull request #1934 from benpeart/fscache-thread-safe-enable-gfw
fscache: make fscache_enable() thread safe
2 parents 68272a7 + eaffe81 commit 984e058

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

compat/mingw.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "win32/lazyload.h"
99
#include "../config.h"
1010
#include "dir.h"
11+
#include "win32/fscache.h"
1112

1213
#define HCAST(type, handle) ((type)(intptr_t)handle)
1314

@@ -3007,6 +3008,9 @@ int wmain(int argc, const wchar_t **wargv)
30073008
/* initialize critical section for waitpid pinfo_t list */
30083009
InitializeCriticalSection(&pinfo_cs);
30093010

3011+
/* initialize critical section for fscache */
3012+
InitializeCriticalSection(&fscache_cs);
3013+
30103014
/* set up default file mode and file modes for stdin/out/err */
30113015
_fmode = _O_BINARY;
30123016
_setmode(_fileno(stdin), _O_BINARY);

compat/win32/fscache.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
static volatile long initialized;
99
static 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.
@@ -381,12 +381,12 @@ int fscache_enable(size_t initial_size)
381381
* opendir and lstat function pointers are redirected if
382382
* any threads are using the fscache.
383383
*/
384+
EnterCriticalSection(&fscache_cs);
384385
if (!initialized) {
385-
InitializeCriticalSection(&mutex);
386386
if (!dwTlsIndex) {
387387
dwTlsIndex = TlsAlloc();
388388
if (dwTlsIndex == TLS_OUT_OF_INDEXES) {
389-
LeaveCriticalSection(&mutex);
389+
LeaveCriticalSection(&fscache_cs);
390390
return 0;
391391
}
392392
}
@@ -395,12 +395,13 @@ int fscache_enable(size_t initial_size)
395395
opendir = fscache_opendir;
396396
lstat = fscache_lstat;
397397
}
398-
InterlockedIncrement(&initialized);
398+
initialized++;
399+
LeaveCriticalSection(&fscache_cs);
399400

400401
/* refcount the thread specific initialization */
401402
cache = fscache_getcache();
402403
if (cache) {
403-
InterlockedIncrement(&cache->enabled);
404+
cache->enabled++;
404405
} else {
405406
cache = (struct fscache *)xcalloc(1, sizeof(*cache));
406407
cache->enabled = 1;
@@ -434,7 +435,7 @@ void fscache_disable(void)
434435
BUG("fscache_disable() called on a thread where fscache has not been initialized");
435436
if (!cache->enabled)
436437
BUG("fscache_disable() called on an fscache that is already disabled");
437-
InterlockedDecrement(&cache->enabled);
438+
cache->enabled--;
438439
if (!cache->enabled) {
439440
TlsSetValue(dwTlsIndex, NULL);
440441
trace_printf_key(&trace_fscache, "fscache_disable: lstat %u, opendir %u, "
@@ -447,12 +448,14 @@ void fscache_disable(void)
447448
}
448449

449450
/* update the global fscache initialization */
450-
InterlockedDecrement(&initialized);
451+
EnterCriticalSection(&fscache_cs);
452+
initialized--;
451453
if (!initialized) {
452454
/* reset opendir and lstat to the original implementations */
453455
opendir = dirent_opendir;
454456
lstat = mingw_lstat;
455457
}
458+
LeaveCriticalSection(&fscache_cs);
456459

457460
trace_printf_key(&trace_fscache, "fscache: disable\n");
458461
return;
@@ -619,7 +622,7 @@ void fscache_merge(struct fscache *dest)
619622
* isn't being used so the critical section only needs to prevent
620623
* the the child threads from stomping on each other.
621624
*/
622-
EnterCriticalSection(&mutex);
625+
EnterCriticalSection(&fscache_cs);
623626

624627
hashmap_iter_init(&cache->map, &iter);
625628
while ((e = hashmap_iter_next(&iter)))
@@ -631,9 +634,9 @@ void fscache_merge(struct fscache *dest)
631634
dest->opendir_requests += cache->opendir_requests;
632635
dest->fscache_requests += cache->fscache_requests;
633636
dest->fscache_misses += cache->fscache_misses;
634-
LeaveCriticalSection(&mutex);
637+
initialized--;
638+
LeaveCriticalSection(&fscache_cs);
635639

636640
free(cache);
637641

638-
InterlockedDecrement(&initialized);
639642
}

compat/win32/fscache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* for each thread where caching is desired.
77
*/
88

9+
extern CRITICAL_SECTION fscache_cs;
10+
911
int fscache_enable(size_t initial_size);
1012
#define enable_fscache(initial_size) fscache_enable(initial_size)
1113

0 commit comments

Comments
 (0)