Small compile-time optimization for locks
This commit is contained in:
parent
4e19f58bbe
commit
66c290c331
|
@ -32,30 +32,34 @@ bool bookmarkset(uint addr, bool manual)
|
|||
return true;
|
||||
}
|
||||
|
||||
bool bookmarkget(uint addr)
|
||||
bool bookmarkget(uint Address)
|
||||
{
|
||||
// CHECK: Export call
|
||||
if(!DbgIsDebugging())
|
||||
return false;
|
||||
|
||||
SHARED_ACQUIRE(LockBookmarks);
|
||||
return (bookmarks.count(ModHashFromAddr(addr)) > 0);
|
||||
return (bookmarks.count(ModHashFromAddr(Address)) > 0);
|
||||
}
|
||||
|
||||
bool bookmarkdel(uint addr)
|
||||
bool bookmarkdel(uint Address)
|
||||
{
|
||||
// CHECK: Export call
|
||||
if(!DbgIsDebugging())
|
||||
return false;
|
||||
|
||||
EXCLUSIVE_ACQUIRE(LockBookmarks);
|
||||
return (bookmarks.erase(ModHashFromAddr(addr)) > 0);
|
||||
return (bookmarks.erase(ModHashFromAddr(Address)) > 0);
|
||||
}
|
||||
|
||||
void bookmarkdelrange(uint start, uint end)
|
||||
void bookmarkdelrange(uint Start, uint End)
|
||||
{
|
||||
// CHECK: Export call
|
||||
if(!DbgIsDebugging())
|
||||
return;
|
||||
|
||||
// Are all bookmarks going to be deleted?
|
||||
if(start == 0x00000000 && end == 0xFFFFFFFF)
|
||||
if(Start == 0x00000000 && End == 0xFFFFFFFF)
|
||||
{
|
||||
EXCLUSIVE_ACQUIRE(LockBookmarks);
|
||||
bookmarks.clear();
|
||||
|
@ -63,13 +67,13 @@ void bookmarkdelrange(uint start, uint end)
|
|||
else
|
||||
{
|
||||
// Make sure 'start' and 'end' reference the same module
|
||||
uint modbase = ModBaseFromAddr(start);
|
||||
uint modbase = ModBaseFromAddr(Start);
|
||||
|
||||
if(modbase != ModBaseFromAddr(end))
|
||||
if(modbase != ModBaseFromAddr(End))
|
||||
return;
|
||||
|
||||
start -= modbase;
|
||||
end -= modbase;
|
||||
Start -= modbase;
|
||||
End -= modbase;
|
||||
|
||||
EXCLUSIVE_ACQUIRE(LockBookmarks);
|
||||
for(auto itr = bookmarks.begin(); itr != bookmarks.end();)
|
||||
|
@ -81,7 +85,7 @@ void bookmarkdelrange(uint start, uint end)
|
|||
continue;
|
||||
}
|
||||
|
||||
if(itr->second.addr >= start && itr->second.addr < end)
|
||||
if(itr->second.addr >= Start && itr->second.addr < End)
|
||||
bookmarks.erase(itr);
|
||||
|
||||
itr++;
|
||||
|
@ -192,6 +196,6 @@ bool bookmarkenum(BOOKMARKSINFO* bookmarklist, size_t* cbsize)
|
|||
void bookmarkclear()
|
||||
{
|
||||
EXCLUSIVE_ACQUIRE(LockBookmarks);
|
||||
|
||||
bookmarks.clear();
|
||||
EXCLUSIVE_RELEASE();
|
||||
}
|
|
@ -11,7 +11,7 @@ struct BOOKMARKSINFO
|
|||
};
|
||||
|
||||
bool bookmarkset(uint addr, bool manual);
|
||||
bool bookmarkget(uint addr);
|
||||
bool bookmarkget(uint Address);
|
||||
bool bookmarkdel(uint addr);
|
||||
void bookmarkdelrange(uint start, uint end);
|
||||
void bookmarkcachesave(JSON root);
|
||||
|
|
|
@ -208,6 +208,6 @@ bool loopenum(LOOPSINFO* looplist, size_t* cbsize)
|
|||
void loopclear()
|
||||
{
|
||||
EXCLUSIVE_ACQUIRE(LockLoops);
|
||||
|
||||
loops.clear();
|
||||
EXCLUSIVE_RELEASE();
|
||||
}
|
|
@ -67,9 +67,9 @@ bool SymGetModuleList(std::vector<SYMBOLMODULEINFO>* List)
|
|||
SYMBOLMODULEINFO curModule;
|
||||
curModule.base = BaseOfDll;
|
||||
|
||||
// Zero module name if one isn't found
|
||||
// Terminate module name if one isn't found
|
||||
if(!ModNameFromAddr(BaseOfDll, curModule.name, true))
|
||||
memset(curModule.name, 0, MAX_MODULE_SIZE);
|
||||
curModule.name[0] = '\0';
|
||||
|
||||
((std::vector<SYMBOLMODULEINFO>*)UserContext)->push_back(curModule);
|
||||
return TRUE;
|
||||
|
|
|
@ -28,10 +28,10 @@ bool waitislocked(WAIT_ID id)
|
|||
return waitarray[id];
|
||||
}
|
||||
|
||||
bool CriticalSectionLocker::m_Initialized = false;
|
||||
SRWLOCK CriticalSectionLocker::m_Locks[LockLast];
|
||||
bool ExclusiveSectionLocker::m_Initialized = false;
|
||||
SRWLOCK ExclusiveSectionLocker::m_Locks[SectionLock::LockLast];
|
||||
|
||||
void CriticalSectionLocker::Initialize()
|
||||
void ExclusiveSectionLocker::Initialize()
|
||||
{
|
||||
if(m_Initialized)
|
||||
return;
|
||||
|
@ -39,18 +39,18 @@ void CriticalSectionLocker::Initialize()
|
|||
// Destroy previous data if any existed
|
||||
memset(m_Locks, 0, sizeof(m_Locks));
|
||||
|
||||
for(int i = 0; i < LockLast; i++)
|
||||
for(int i = 0; i < ARRAYSIZE(m_Locks); i++)
|
||||
InitializeSRWLock(&m_Locks[i]);
|
||||
|
||||
m_Initialized = true;
|
||||
}
|
||||
|
||||
void CriticalSectionLocker::Deinitialize()
|
||||
void ExclusiveSectionLocker::Deinitialize()
|
||||
{
|
||||
if(!m_Initialized)
|
||||
return;
|
||||
|
||||
for(int i = 0; i < LockLast; i++)
|
||||
for(int i = 0; i < ARRAYSIZE(m_Locks); i++)
|
||||
{
|
||||
// Wait for the lock's ownership to be released
|
||||
AcquireSRWLockExclusive(&m_Locks[i]);
|
||||
|
@ -63,39 +63,56 @@ void CriticalSectionLocker::Deinitialize()
|
|||
m_Initialized = false;
|
||||
}
|
||||
|
||||
CriticalSectionLocker::CriticalSectionLocker(CriticalSectionLock LockIndex, bool Shared)
|
||||
ExclusiveSectionLocker::ExclusiveSectionLocker(SectionLock LockIndex)
|
||||
{
|
||||
m_Lock = &m_Locks[LockIndex];
|
||||
m_LockCount = 0;
|
||||
|
||||
Lock(Shared);
|
||||
Lock();
|
||||
}
|
||||
|
||||
CriticalSectionLocker::~CriticalSectionLocker()
|
||||
ExclusiveSectionLocker::~ExclusiveSectionLocker()
|
||||
{
|
||||
if(m_LockCount > 0)
|
||||
Unlock();
|
||||
|
||||
// TODO: Assert that the lock count is zero on destructor
|
||||
#ifdef _DEBUG
|
||||
if(m_LockCount > 0)
|
||||
__debugbreak();
|
||||
#endif
|
||||
}
|
||||
|
||||
void CriticalSectionLocker::Unlock()
|
||||
void ExclusiveSectionLocker::Lock()
|
||||
{
|
||||
AcquireSRWLockExclusive(m_Lock);
|
||||
|
||||
m_LockCount++;
|
||||
}
|
||||
|
||||
void ExclusiveSectionLocker::Unlock()
|
||||
{
|
||||
m_LockCount--;
|
||||
|
||||
if(m_Shared)
|
||||
ReleaseSRWLockShared(m_Lock);
|
||||
else
|
||||
ReleaseSRWLockExclusive(m_Lock);
|
||||
ReleaseSRWLockExclusive(m_Lock);
|
||||
}
|
||||
|
||||
void CriticalSectionLocker::Lock(bool Shared)
|
||||
SharedSectionLocker::SharedSectionLocker(SectionLock LockIndex)
|
||||
: ExclusiveSectionLocker(LockIndex)
|
||||
{
|
||||
if(Shared)
|
||||
AcquireSRWLockShared(m_Lock);
|
||||
else
|
||||
AcquireSRWLockExclusive(m_Lock);
|
||||
// Nothing to do here; parent class constructor is called
|
||||
}
|
||||
|
||||
void SharedSectionLocker::Lock()
|
||||
{
|
||||
AcquireSRWLockShared(m_Lock);
|
||||
|
||||
m_Shared = Shared;
|
||||
m_LockCount++;
|
||||
}
|
||||
|
||||
void SharedSectionLocker::Unlock()
|
||||
{
|
||||
m_LockCount--;
|
||||
|
||||
ReleaseSRWLockShared(m_Lock);
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
#ifndef _THREADING_H
|
||||
#define _THREADING_H
|
||||
#pragma once
|
||||
|
||||
#include "_global.h"
|
||||
|
||||
|
@ -24,13 +23,13 @@ bool waitislocked(WAIT_ID id);
|
|||
// Better, but requires VISTA+
|
||||
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa904937%28v=vs.85%29.aspx
|
||||
//
|
||||
#define EXCLUSIVE_ACQUIRE(Index) CriticalSectionLocker __ThreadLock(Index, false);
|
||||
#define EXCLUSIVE_ACQUIRE(Index) ExclusiveSectionLocker __ThreadLock(SectionLock::##Index);
|
||||
#define EXCLUSIVE_RELEASE() __ThreadLock.Unlock();
|
||||
|
||||
#define SHARED_ACQUIRE(Index) CriticalSectionLocker __SThreadLock(Index, true);
|
||||
#define SHARED_ACQUIRE(Index) SharedSectionLocker __SThreadLock(SectionLock::##Index);
|
||||
#define SHARED_RELEASE() __SThreadLock.Unlock();
|
||||
|
||||
enum CriticalSectionLock
|
||||
enum SectionLock
|
||||
{
|
||||
LockMemoryPages,
|
||||
LockVariables,
|
||||
|
@ -54,25 +53,33 @@ enum CriticalSectionLock
|
|||
LockLast
|
||||
};
|
||||
|
||||
class CriticalSectionLocker
|
||||
class ExclusiveSectionLocker
|
||||
{
|
||||
public:
|
||||
static void Initialize();
|
||||
static void Deinitialize();
|
||||
|
||||
CriticalSectionLocker(CriticalSectionLock LockIndex, bool Shared);
|
||||
~CriticalSectionLocker();
|
||||
ExclusiveSectionLocker(SectionLock LockIndex);
|
||||
~ExclusiveSectionLocker();
|
||||
|
||||
void Lock();
|
||||
void Unlock();
|
||||
void Lock(bool Shared);
|
||||
|
||||
private:
|
||||
static bool m_Initialized;
|
||||
static SRWLOCK m_Locks[LockLast];
|
||||
static bool m_Initialized;
|
||||
static SRWLOCK m_Locks[SectionLock::LockLast];
|
||||
|
||||
SRWLOCK* m_Lock;
|
||||
bool m_Shared;
|
||||
BYTE m_LockCount;
|
||||
protected:
|
||||
SRWLOCK* m_Lock;
|
||||
BYTE m_LockCount;
|
||||
};
|
||||
|
||||
#endif // _THREADING_H
|
||||
class SharedSectionLocker : public ExclusiveSectionLocker
|
||||
{
|
||||
public:
|
||||
SharedSectionLocker(SectionLock LockIndex);
|
||||
~SharedSectionLocker();
|
||||
|
||||
void Lock();
|
||||
void Unlock();
|
||||
};
|
Loading…
Reference in New Issue