views:

52

answers:

1

I'm totally blown away from the quality of windows SRW implementation. Its faster then critical sections and its just a few bytes memory overhead.

Unfortunately it is only Windows Vista/Windows 7.

As this is a pure user land implementation, does anybody know if there is a cross platform implementation for it? Has anybody reverse-engineered there solution?

And please i don't want to add stuff like boost just to pull in a less then 100 LOC solution.

+1  A: 

If you want something "portable" in the sense of conforming to some standard... If you are using POSIX threads there is pthread_rwlock_init() and friends. These are of course not typically used on Windows but rather Unix-type OSes.

But if you mean "portable" in the sense of "portable to multiple versions of Windows..." There are some undocumented calls in ntdll which implement RW locks. RtlAcquireResourceShared() and RtlAcquireResourceExclusive().

Here are some prototypes from WINE's implementation:

void WINAPI RtlInitializeResource(LPRTL_RWLOCK rwl);
void WINAPI RtlDeleteResource(LPRTL_RWLOCK rwl);
BYTE WINAPI RtlAcquireResourceExclusive(LPRTL_RWLOCK rwl, BYTE fWait);
BYTE WINAPI RtlAcquireResourceShared(LPRTL_RWLOCK rwl, BYTE fWait);
void WINAPI RtlReleaseResource(LPRTL_RWLOCK rwl);

Note you may have to GetProcAddress() these from ntdll.dll yourself.

As for the structure referenced... Here's what WINE declares:

typedef struct _RTL_RWLOCK {
   RTL_CRITICAL_SECTION rtlCS;

   HANDLE hSharedReleaseSemaphore;
   UINT   uSharedWaiters;

   HANDLE hExclusiveReleaseSemaphore;
   UINT   uExclusiveWaiters;

   INT    iNumberActive;
   HANDLE hOwningThreadId;
   DWORD  dwTimeoutBoost;
   PVOID  pDebugInfo;
} RTL_RWLOCK, *LPRTL_RWLOCK;

If you don't want to use pthreads and you don't want to link to sketchy undocumented functionality... You can look up a rwlock implementation and implement it yourself in terms of other operations... Say InterlockedCompareExchange(), or perhaps higher level primitives such as semaphores and events.

asveikau
Well i don't want System calls and this is not the code for SRW. They are fast. All i want is some bare bone C/Assembler code which is doing this InterlockedCompareExchange magic. This should be by definition portable across Intel i386 or amd64 platforms if the same assembler syntax is used.
Lothar
In that case: http://www.google.com/search?q=rwlock+interlockedcompareexchange -- Note also the x86 instruction for InterlockedCompareExchange() is "lock cmpxchg".
asveikau