Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 1a62aa8

Browse files
committed
fscache: load directories only once
If multiple threads access a directory that is not yet in the cache, the directory will be loaded by each thread. Only one of the results is added to the cache, all others are leaked. This wastes performance and memory. On cache miss, add a future object to the cache to indicate that the directory is currently being loaded. Subsequent threads register themselves with the future object and wait. When the first thread has loaded the directory, it replaces the future object with the result and notifies waiting threads. Signed-off-by: Karsten Blees <[email protected]>
1 parent 2e646a9 commit 1a62aa8

File tree

1 file changed

+56
-9
lines changed

1 file changed

+56
-9
lines changed

compat/win32/fscache.c

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ struct fsentry {
3333
union {
3434
/* Reference count of the directory listing. */
3535
volatile long refcnt;
36+
/* Handle to wait on the loading thread. */
37+
HANDLE hwait;
3638
struct {
3739
/* More stat members (only used for file entries). */
3840
off64_t st_size;
@@ -259,24 +261,51 @@ static inline int fscache_enabled(const char *path)
259261
return enabled > 0 && !is_absolute_path(path);
260262
}
261263

264+
/*
265+
* Looks up a cache entry, waits if its being loaded by another thread.
266+
* The mutex must be owned by the calling thread.
267+
*/
268+
static struct fsentry *fscache_get_wait(struct fsentry *key)
269+
{
270+
struct fsentry *fse = hashmap_get(&map, key, NULL);
271+
272+
/* return if its a 'real' entry (future entries have refcnt == 0) */
273+
if (!fse || fse->list || fse->refcnt)
274+
return fse;
275+
276+
/* create an event and link our key to the future entry */
277+
key->hwait = CreateEvent(NULL, TRUE, FALSE, NULL);
278+
key->next = fse->next;
279+
fse->next = key;
280+
281+
/* wait for the loading thread to signal us */
282+
LeaveCriticalSection(&mutex);
283+
WaitForSingleObject(key->hwait, INFINITE);
284+
CloseHandle(key->hwait);
285+
EnterCriticalSection(&mutex);
286+
287+
/* repeat cache lookup */
288+
return hashmap_get(&map, key, NULL);
289+
}
290+
262291
/*
263292
* Looks up or creates a cache entry for the specified key.
264293
*/
265294
static struct fsentry *fscache_get(struct fsentry *key)
266295
{
267-
struct fsentry *fse;
296+
struct fsentry *fse, *future, *waiter;
268297

269298
EnterCriticalSection(&mutex);
270299
/* check if entry is in cache */
271-
fse = hashmap_get(&map, key, NULL);
300+
fse = fscache_get_wait(key);
272301
if (fse) {
273302
fsentry_addref(fse);
274303
LeaveCriticalSection(&mutex);
275304
return fse;
276305
}
277306
/* if looking for a file, check if directory listing is in cache */
278307
if (!fse && key->list) {
279-
fse = hashmap_get(&map, key->list, NULL);
308+
fse = fscache_get_wait(key->list);
280309
if (fse) {
281310
LeaveCriticalSection(&mutex);
282311
/* dir entry without file entry -> file doesn't exist */
@@ -285,16 +314,34 @@ static struct fsentry *fscache_get(struct fsentry *key)
285314
}
286315
}
287316

317+
/* add future entry to indicate that we're loading it */
318+
future = key->list ? key->list : key;
319+
future->next = NULL;
320+
future->refcnt = 0;
321+
hashmap_add(&map, future);
322+
288323
/* create the directory listing (outside mutex!) */
289324
LeaveCriticalSection(&mutex);
290-
fse = fsentry_create_list(key->list ? key->list : key);
291-
if (!fse)
325+
fse = fsentry_create_list(future);
326+
EnterCriticalSection(&mutex);
327+
328+
/* remove future entry and signal waiting threads */
329+
hashmap_remove(&map, future, NULL);
330+
waiter = future->next;
331+
while (waiter) {
332+
HANDLE h = waiter->hwait;
333+
waiter = waiter->next;
334+
SetEvent(h);
335+
}
336+
337+
/* leave on error (errno set by fsentry_create_list) */
338+
if (!fse) {
339+
LeaveCriticalSection(&mutex);
292340
return NULL;
341+
}
293342

294-
EnterCriticalSection(&mutex);
295-
/* add directory listing if it hasn't been added by some other thread */
296-
if (!hashmap_get(&map, key, NULL))
297-
fscache_add(fse);
343+
/* add directory listing to the cache */
344+
fscache_add(fse);
298345

299346
/* lookup file entry if requested (fse already points to directory) */
300347
if (key->list)

0 commit comments

Comments
 (0)