static int CreateMutexHandle(bool initiallyOwned, String name, Win32Native.SECURITY_ATTRIBUTES securityAttribute, out SafeWaitHandle mutexHandle) {
int errorCode;
bool fReservedMutexObtained = false;
bool fAffinity = false;
while(true) {
mutexHandle = Win32Native.CreateMutex(securityAttribute, initiallyOwned, name);
errorCode = Marshal.GetLastWin32Error();
if( !mutexHandle.IsInvalid) {
break;
}
if( errorCode == Win32Native.ERROR_ACCESS_DENIED) {
// If a mutex with the name already exists, OS will try to open it with FullAccess.
// It might fail if we don't have enough access. In that case, we try to open the mutex will modify and synchronize access.
//
RuntimeHelpers.PrepareConstrainedRegions();
try
{
try
{
}
finally
{
Thread.BeginThreadAffinity();
fAffinity = true;
}
AcquireReservedMutex(ref fReservedMutexObtained);
mutexHandle = Win32Native.OpenMutex(Win32Native.MUTEX_MODIFY_STATE | Win32Native.SYNCHRONIZE, false, name);
if(!mutexHandle.IsInvalid)
{
errorCode = Win32Native.ERROR_ALREADY_EXISTS;
}
else
{
errorCode = Marshal.GetLastWin32Error();
}
}
finally
{
if (fReservedMutexObtained)
ReleaseReservedMutex();
if (fAffinity)
Thread.EndThreadAffinity();
}
// There could be a race here, the other owner of the mutex can free the mutex,
// We need to retry creation in that case.
if( errorCode != Win32Native.ERROR_FILE_NOT_FOUND) {
if( errorCode == Win32Native.ERROR_SUCCESS) {
errorCode = Win32Native.ERROR_ALREADY_EXISTS;
}
break;
}
}
else {
break;
}
}
return errorCode;
}