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.