From 0c8232561f94cbf801c8c5415841d05fac9402f7 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Wed, 10 Apr 2013 08:57:15 +0000 Subject: write locks for MDatabaseCache git-svn-id: http://svn.miranda-ng.org/main/trunk@4410 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- src/modules/database/database.h | 1 + src/modules/database/mdatabasecache.cpp | 25 +++++++++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/modules/database/database.h b/src/modules/database/database.h index e484d341e5..dbecd9dc08 100644 --- a/src/modules/database/database.h +++ b/src/modules/database/database.h @@ -26,6 +26,7 @@ class MDatabaseCache : public MIDatabaseCache HANDLE m_hCacheHeap; char* m_lastSetting; DBCachedContact *m_lastVL; + CRITICAL_SECTION m_cs; LIST m_lContacts; LIST m_lGlobalSettings; diff --git a/src/modules/database/mdatabasecache.cpp b/src/modules/database/mdatabasecache.cpp index 24dda4f9ee..6074d85bf9 100644 --- a/src/modules/database/mdatabasecache.cpp +++ b/src/modules/database/mdatabasecache.cpp @@ -36,26 +36,29 @@ static int compareGlobals(const DBCachedGlobalValue* p1, const DBCachedGlobalVal MDatabaseCache::MDatabaseCache() : m_lSettings(100, stringCompare), - m_lContacts(50, LIST::FTSortFunc(HandleKeySort)), + m_lContacts(50, HandleKeySortT), m_lGlobalSettings(50, compareGlobals) { m_hCacheHeap = HeapCreate(0, 0, 0); + InitializeCriticalSection(&m_cs); } MDatabaseCache::~MDatabaseCache() { - HeapDestroy(m_hCacheHeap); m_lContacts.destroy(); m_lSettings.destroy(); m_lGlobalSettings.destroy(); + HeapDestroy(m_hCacheHeap); + DeleteCriticalSection(&m_cs); } ///////////////////////////////////////////////////////////////////////////////////////// DBCachedContact* MDatabaseCache::AddContactToCache(HANDLE hContact) { - DBCachedContact VLtemp = { hContact }; - int index = m_lContacts.getIndex(&VLtemp); + mir_cslock lck(m_cs); + + int index = m_lContacts.getIndex((DBCachedContact*)&hContact); if (index == -1) { DBCachedContact* VL = (DBCachedContact*)HeapAlloc(m_hCacheHeap, HEAP_ZERO_MEMORY, sizeof(DBCachedContact)); VL->hContact = hContact; @@ -68,15 +71,15 @@ DBCachedContact* MDatabaseCache::AddContactToCache(HANDLE hContact) DBCachedContact* MDatabaseCache::GetCachedContact(HANDLE hContact) { - DBCachedContact VLtemp = { hContact }; - int index = m_lContacts.getIndex(&VLtemp); + int index = m_lContacts.getIndex((DBCachedContact*)&hContact); return (index == -1) ? NULL : m_lContacts[index]; } void MDatabaseCache::FreeCachedContact(HANDLE hContact) { - DBCachedContact VLtemp = { hContact }; - int index = m_lContacts.getIndex(&VLtemp); + mir_cslock lck(m_cs); + + int index = m_lContacts.getIndex((DBCachedContact*)&hContact); if (index == -1) return; @@ -91,6 +94,12 @@ void MDatabaseCache::FreeCachedContact(HANDLE hContact) HeapFree( m_hCacheHeap, 0, VL ); m_lContacts.remove(index); + + for (int i=0; i < m_lContacts.getCount(); i++) { + DBCachedContact *cc = m_lContacts[i]; + if (cc->hNext == hContact) + cc->hNext = NULL; + } } ///////////////////////////////////////////////////////////////////////////////////////// -- cgit v1.2.3