[raknet] Implement SimpleMutex

This commit is contained in:
RD42
2023-10-31 21:11:58 +08:00
parent 5550c20f37
commit e7bebd1b9f
2 changed files with 154 additions and 0 deletions
+97
View File
@@ -0,0 +1,97 @@
/// \file
///
/// This file is part of RakNet Copyright 2003 Kevin Jenkins.
///
/// Usage of RakNet is subject to the appropriate license agreement.
/// Creative Commons Licensees are subject to the
/// license found at
/// http://creativecommons.org/licenses/by-nc/2.5/
/// Single application licensees are subject to the license found at
/// http://www.rakkarsoft.com/SingleApplicationLicense.html
/// Custom license users are subject to the terms therein.
/// GPL license users are subject to the GNU General Public
/// License as published by the Free
/// Software Foundation; either version 2 of the License, or (at your
/// option) any later version.
#include "SimpleMutex.h"
#include <assert.h>
SimpleMutex::SimpleMutex()
{
#ifdef _WIN32
// hMutex = CreateMutex(NULL, FALSE, 0);
// assert(hMutex);
InitializeCriticalSection(&criticalSection);
#else
int error = pthread_mutex_init(&hMutex, 0);
assert(error==0);
#endif
}
SimpleMutex::~SimpleMutex()
{
#ifdef _WIN32
// CloseHandle(hMutex);
DeleteCriticalSection(&criticalSection);
#else
pthread_mutex_destroy(&hMutex);
#endif
}
#ifdef _WIN32
#ifdef _DEBUG
#include <stdio.h>
#endif
#endif
void SimpleMutex::Lock(void)
{
#ifdef _WIN32
/*
DWORD d = WaitForSingleObject(hMutex, INFINITE);
#ifdef _DEBUG
if (d==WAIT_FAILED)
{
LPVOID messageBuffer;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &messageBuffer,
0,
NULL
);
// Process any inserts in messageBuffer.
// ...
// Display the string.
//MessageBox( NULL, (LPCTSTR)messageBuffer, "Error", MB_OK | MB_ICONINFORMATION );
printf("SimpleMutex error: %s", messageBuffer);
// Free the buffer.
LocalFree( messageBuffer );
}
assert(d==WAIT_OBJECT_0);
*/
EnterCriticalSection(&criticalSection);
#else
int error = pthread_mutex_lock(&hMutex);
assert(error==0);
#endif
}
void SimpleMutex::Unlock(void)
{
#ifdef _WIN32
// ReleaseMutex(hMutex);
LeaveCriticalSection(&criticalSection);
#else
int error = pthread_mutex_unlock(&hMutex);
assert(error==0);
#endif
}
+57
View File
@@ -0,0 +1,57 @@
/// \file
/// \brief \b [Internal] Encapsulates a mutex
///
/// This file is part of RakNet Copyright 2003 Kevin Jenkins.
///
/// Usage of RakNet is subject to the appropriate license agreement.
/// Creative Commons Licensees are subject to the
/// license found at
/// http://creativecommons.org/licenses/by-nc/2.5/
/// Single application licensees are subject to the license found at
/// http://www.rakkarsoft.com/SingleApplicationLicense.html
/// Custom license users are subject to the terms therein.
/// GPL license users are subject to the GNU General Public
/// License as published by the Free
/// Software Foundation; either version 2 of the License, or (at your
/// option) any later version.
#ifndef __SIMPLE_MUTEX_H
#define __SIMPLE_MUTEX_H
#ifdef _COMPATIBILITY_1
#include "Compatibility1Includes.h"
#elif defined(_WIN32)
#include <windows.h>
#else
#include <pthread.h>
#include <sys/types.h>
#endif
#include "Export.h"
/// \brief An easy to use mutex.
///
/// I wrote this because the version that comes with Windows is too complicated and requires too much code to use.
/// @remark Previously I used this everywhere, and in fact for a year or two RakNet was totally threadsafe. While doing profiling, I saw that this function was incredibly slow compared to the blazing performance of everything else, so switched to single producer / consumer everywhere. Now the user thread of RakNet is not threadsafe, but it's 100X faster than before.
class RAK_DLL_EXPORT SimpleMutex
{
public:
/// Constructor
SimpleMutex();
// Destructor
~SimpleMutex();
// Locks the mutex. Slow!
void Lock(void);
// Unlocks the mutex.
void Unlock(void);
private:
#ifdef _WIN32
CRITICAL_SECTION criticalSection; /// Docs say this is faster than a mutex for single process access
#else
pthread_mutex_t hMutex;
#endif
};
#endif