www.pudn.com > claar.zip > RWLOCK.CPP


// Multiple-reader/Single-writer Lock in Windows NT 
// Author: Jeff Claar 
 
#define STRICT 
#define WIN32_LEAN_AND_MEAN 
#include  
#include "rwlock.hpp" 
 
// Function names exported from ntdll.dll. 
LPCSTR SyRWLock::sInitName = "RtlInitializeResource"; 
LPCSTR SyRWLock::sDeleteName = "RtlDeleteResource"; 
LPCSTR SyRWLock::sGetExclusiveName = "RtlAcquireResourceExclusive"; 
LPCSTR SyRWLock::sGetSharedName = "RtlAcquireResourceShared"; 
LPCSTR SyRWLock::sReleaseName = "RtlReleaseResource"; 
LPCSTR SyRWLock::sDumpName = "RtlDumpResource"; 
 
// Function pointers into ntdll.dll. 
SyRWLock::RtlInitializeResource SyRWLock::sInitProc; 
SyRWLock::RtlDeleteResource SyRWLock::sDelProc; 
SyRWLock::RtlAcqResourceExclusive SyRWLock::sGetExclusiveProc; 
SyRWLock::RtlAcqResourceShared SyRWLock::sGetSharedProc; 
SyRWLock::RtlReleaseResource SyRWLock::sReleaseProc; 
SyRWLock::RtlDumpResource SyRWLock::sDumpProc; 
 
SyRWLock::SyRWLock() 
{ 
    // If this is the first object constructed, fill in the 
    // statics. 
    static HINSTANCE hNtdll = NULL; 
    if (hNtdll == NULL) 
    { 
        hNtdll = LoadLibraryA("ntdll.dll"); 
        sInitProc = (RtlInitializeResource) 
            GetProcAddress(hNtdll, sInitName); 
        sDelProc = (RtlDeleteResource) 
            GetProcAddress(hNtdll, sDeleteName); 
        sGetExclusiveProc = (RtlAcqResourceExclusive) 
            GetProcAddress(hNtdll, sGetExclusiveName); 
        sGetSharedProc = (RtlAcqResourceShared) 
            GetProcAddress(hNtdll, sGetSharedName); 
        sReleaseProc = (RtlReleaseResource) 
            GetProcAddress(hNtdll, sReleaseName); 
        sDumpProc = (RtlDumpResource) 
            GetProcAddress(hNtdll, sDumpName); 
    } 
    sInitProc(&mRWLock); 
} 
 
SyRWLock::~SyRWLock() 
{ 
    sDelProc(&mRWLock); 
} 
 
void SyRWLock::Dump() 
{ 
    sDumpProc(&mRWLock); 
} 
 
BYTE SyRWLock::GetExclusive(BYTE fWait) 
{ 
    return sGetExclusiveProc(&mRWLock, fWait); 
} 
 
BYTE SyRWLock::GetShared(BYTE fWait) 
{ 
    return sGetSharedProc(&mRWLock, fWait); 
} 
 
void SyRWLock::Release() 
{ 
    sReleaseProc(&mRWLock); 
}