- CacheLock()
- {
- m_count = 0;
- m_count_mutex.Init();
- m_cache_mutex.Init();
- m_waitcache_mutex.Init();
- }
-
- void cacheCreated()
- {
- //dstream<<"cacheCreated() begin"<<std::endl;
- JMutexAutoLock waitcachelock(m_waitcache_mutex);
- JMutexAutoLock countlock(m_count_mutex);
-
- // If this is the first cache, grab the cache lock
- if(m_count == 0)
- m_cache_mutex.Lock();
-
- m_count++;
-
- //dstream<<"cacheCreated() end"<<std::endl;
- }
-
- void cacheRemoved()
- {
- //dstream<<"cacheRemoved() begin"<<std::endl;
- JMutexAutoLock countlock(m_count_mutex);
-
- assert(m_count > 0);
-
- m_count--;
-
- // If this is the last one, release the cache lock
- if(m_count == 0)
- m_cache_mutex.Unlock();
-
- //dstream<<"cacheRemoved() end"<<std::endl;
- }
-
- /*
- This lock should be taken when removing stuff that can be
- pointed by the cache.
-
- You'll want to grab this in a SharedPtr.
- */
- JMutexAutoLock * waitCaches()
- {
- //dstream<<"waitCaches() begin"<<std::endl;
- JMutexAutoLock waitcachelock(m_waitcache_mutex);
- JMutexAutoLock *lock = new JMutexAutoLock(m_cache_mutex);
- //dstream<<"waitCaches() end"<<std::endl;
- return lock;
- }
-
-private:
- // Count of existing caches
- u32 m_count;
- JMutex m_count_mutex;
- // This is locked always when there are some caches
- JMutex m_cache_mutex;
- // Locked so that when waitCaches() is called, no more caches are created
- JMutex m_waitcache_mutex;