diff --git a/sycl/include/sycl/detail/spinlock.hpp b/sycl/include/sycl/detail/spinlock.hpp index 5743e6ee5e797..c41397609c1d6 100644 --- a/sycl/include/sycl/detail/spinlock.hpp +++ b/sycl/include/sycl/detail/spinlock.hpp @@ -26,6 +26,8 @@ namespace detail { /// std::mutex, that doesn't provide such guarantees). class SpinLock { public: + bool try_lock() { return !MLock.test_and_set(std::memory_order_acquire); } + void lock() { while (MLock.test_and_set(std::memory_order_acquire)) std::this_thread::yield(); diff --git a/sycl/source/detail/kernel_program_cache.hpp b/sycl/source/detail/kernel_program_cache.hpp index d1832da1c59f6..1d55fb56b9dd4 100644 --- a/sycl/source/detail/kernel_program_cache.hpp +++ b/sycl/source/detail/kernel_program_cache.hpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -421,7 +422,7 @@ class KernelProgramCache { template KernelFastCacheValT tryToGetKernelFast(KeyT &&CacheKey) { - std::unique_lock Lock(MKernelFastCacheMutex); + KernelFastCacheReadLockT Lock(MKernelFastCacheMutex); auto It = MKernelFastCache.find(CacheKey); if (It != MKernelFastCache.end()) { traceKernel("Kernel fetched.", CacheKey.second, true); @@ -445,7 +446,7 @@ class KernelProgramCache { return; } // Save reference between the program and the fast cache key. - std::unique_lock Lock(MKernelFastCacheMutex); + KernelFastCacheWriteLockT Lock(MKernelFastCacheMutex); MProgramToKernelFastCacheKeyMap[Program].emplace_back(CacheKey); // if no insertion took place, thus some other thread has already inserted @@ -483,7 +484,7 @@ class KernelProgramCache { { // Remove corresponding entries from KernelFastCache. - std::unique_lock Lock(MKernelFastCacheMutex); + KernelFastCacheWriteLockT Lock(MKernelFastCacheMutex); if (auto FastCacheKeyItr = MProgramToKernelFastCacheKeyMap.find(NativePrg); FastCacheKeyItr != MProgramToKernelFastCacheKeyMap.end()) { @@ -630,7 +631,7 @@ class KernelProgramCache { std::lock_guard EvictionListLock(MProgramEvictionListMutex); std::lock_guard L1(MProgramCacheMutex); std::lock_guard L2(MKernelsPerProgramCacheMutex); - std::lock_guard L3(MKernelFastCacheMutex); + KernelFastCacheWriteLockT L3(MKernelFastCacheMutex); MCachedPrograms = ProgramCache{}; MKernelsPerProgramCache = KernelCacheT{}; MKernelFastCache = KernelFastCacheT{}; @@ -758,7 +759,10 @@ class KernelProgramCache { KernelCacheT MKernelsPerProgramCache; ContextPtr MParentContext; - std::mutex MKernelFastCacheMutex; + using KernelFastCacheMutexT = SpinLock; + using KernelFastCacheReadLockT = std::lock_guard; + using KernelFastCacheWriteLockT = std::lock_guard; + KernelFastCacheMutexT MKernelFastCacheMutex; KernelFastCacheT MKernelFastCache; // Map between fast kernel cache keys and program handle.