Skip to content

Commit b1a0f3f

Browse files
kbleesdscho
authored andcommitted
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 9c8f453 commit b1a0f3f

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
@@ -23,6 +23,8 @@ struct fsentry {
2323
union {
2424
/* Reference count of the directory listing. */
2525
volatile long refcnt;
26+
/* Handle to wait on the loading thread. */
27+
HANDLE hwait;
2628
struct {
2729
/* More stat members (only used for file entries). */
2830
off64_t st_size;
@@ -254,24 +256,51 @@ static inline int fscache_enabled(const char *path)
254256
return enabled > 0 && !is_absolute_path(path);
255257
}
256258

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

264293
EnterCriticalSection(&mutex);
265294
/* check if entry is in cache */
266-
fse = hashmap_get_entry(&map, key, ent, NULL);
295+
fse = fscache_get_wait(key);
267296
if (fse) {
268297
fsentry_addref(fse);
269298
LeaveCriticalSection(&mutex);
270299
return fse;
271300
}
272301
/* if looking for a file, check if directory listing is in cache */
273302
if (!fse && key->list) {
274-
fse = hashmap_get_entry(&map, key->list, ent, NULL);
303+
fse = fscache_get_wait(key->list);
275304
if (fse) {
276305
LeaveCriticalSection(&mutex);
277306
/* dir entry without file entry -> file doesn't exist */
@@ -280,16 +309,34 @@ static struct fsentry *fscache_get(struct fsentry *key)
280309
}
281310
}
282311

312+
/* add future entry to indicate that we're loading it */
313+
future = key->list ? key->list : key;
314+
future->next = NULL;
315+
future->u.refcnt = 0;
316+
hashmap_add(&map, &future->ent);
317+
283318
/* create the directory listing (outside mutex!) */
284319
LeaveCriticalSection(&mutex);
285-
fse = fsentry_create_list(key->list ? key->list : key);
286-
if (!fse)
320+
fse = fsentry_create_list(future);
321+
EnterCriticalSection(&mutex);
322+
323+
/* remove future entry and signal waiting threads */
324+
hashmap_remove(&map, &future->ent, NULL);
325+
waiter = future->next;
326+
while (waiter) {
327+
HANDLE h = waiter->u.hwait;
328+
waiter = waiter->next;
329+
SetEvent(h);
330+
}
331+
332+
/* leave on error (errno set by fsentry_create_list) */
333+
if (!fse) {
334+
LeaveCriticalSection(&mutex);
287335
return NULL;
336+
}
288337

289-
EnterCriticalSection(&mutex);
290-
/* add directory listing if it hasn't been added by some other thread */
291-
if (!hashmap_get_entry(&map, key, ent, NULL))
292-
fscache_add(fse);
338+
/* add directory listing to the cache */
339+
fscache_add(fse);
293340

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

0 commit comments

Comments
 (0)