@@ -282,32 +282,37 @@ void PersistentDeviceCodeCache::evictItemsFromCache(
282282 const std::string SrcFile = FileNameWOExt + " .src" ;
283283
284284 while (OSUtil::isPathPresent (BinFile) || OSUtil::isPathPresent (SrcFile)) {
285- // Remove the file and subtract its size from the cache size.
286- auto RemoveFileAndSubtractSize =
287- [&CurrCacheSize](const std::string &FileName) {
288- // If the file is not present, return.
289- if (!OSUtil::isPathPresent (FileName))
290- return ;
291-
292- auto FileSize = getFileSize (FileName);
293- if (std::remove (FileName.c_str ())) {
294- throw sycl::exception (make_error_code (errc::runtime),
295- " Failed to evict cache entry: " + FileName);
296- } else {
297- PersistentDeviceCodeCache::trace (" File removed: " + FileName);
298- CurrCacheSize -= FileSize;
299- }
300- };
301-
302- // If removal fails due to a race, retry.
303- // Races are rare, but can happen if another process is reading the file.
304- // Locking down the entire cache and blocking all readers would be
305- // inefficient.
306- try {
307- RemoveFileAndSubtractSize (SrcFile);
308- RemoveFileAndSubtractSize (BinFile);
309- } catch (...) {
310- continue ;
285+
286+ // Lock to prevent race between writer and eviction thread.
287+ LockCacheItem Lock{FileNameWOExt};
288+ if (Lock.isOwned ()) {
289+ // Remove the file and subtract its size from the cache size.
290+ auto RemoveFileAndSubtractSize = [&CurrCacheSize](
291+ const std::string &FileName) {
292+ // If the file is not present, return.
293+ if (!OSUtil::isPathPresent (FileName))
294+ return ;
295+
296+ auto FileSize = getFileSize (FileName);
297+ if (std::remove (FileName.c_str ())) {
298+ throw sycl::exception (make_error_code (errc::runtime),
299+ " Failed to evict cache entry: " + FileName);
300+ } else {
301+ PersistentDeviceCodeCache::trace (" File removed: " + FileName);
302+ CurrCacheSize -= FileSize;
303+ }
304+ };
305+
306+ // If removal fails due to a race, retry.
307+ // Races are rare, but can happen if another process is reading the
308+ // file. Locking down the entire cache and blocking all readers would be
309+ // inefficient.
310+ try {
311+ RemoveFileAndSubtractSize (SrcFile);
312+ RemoveFileAndSubtractSize (BinFile);
313+ } catch (...) {
314+ continue ;
315+ }
311316 }
312317 }
313318
@@ -426,7 +431,6 @@ void PersistentDeviceCodeCache::putItemToDisc(
426431 return ;
427432
428433 std::string FileName;
429- bool IsWriteSuccess = false ;
430434 try {
431435 OSUtil::makeDir (DirName.c_str ());
432436 FileName = getUniqueFilename (DirName);
@@ -437,7 +441,10 @@ void PersistentDeviceCodeCache::putItemToDisc(
437441 trace (" device binary has been cached: " + FullFileName);
438442 writeSourceItem (FileName + " .src" , Devices[DeviceIndex], SortedImgs,
439443 SpecConsts, BuildOptionsString);
440- IsWriteSuccess = true ;
444+
445+ // Update Total cache size after adding the new items.
446+ TotalSize += getFileSize (FileName + " .src" );
447+ TotalSize += getFileSize (FileName + " .bin" );
441448 } else {
442449 PersistentDeviceCodeCache::trace (" cache lock not owned " + FileName);
443450 }
@@ -450,11 +457,6 @@ void PersistentDeviceCodeCache::putItemToDisc(
450457 std::string (" error outputting persistent cache: " ) +
451458 std::strerror (errno));
452459 }
453-
454- if (IsWriteSuccess) {
455- TotalSize += getFileSize (FileName + " .src" );
456- TotalSize += getFileSize (FileName + " .bin" );
457- }
458460 }
459461
460462 // Update the cache size file and trigger cache eviction if needed.
0 commit comments