2014-12-18 09:52:29 +00:00
|
|
|
// Wraps an SDL mutex object. (A critical section is a Windows synchronization
|
|
|
|
// object similar to a mutex but optimized for access by threads belonging to
|
|
|
|
// only one process, hence the class name.)
|
|
|
|
|
|
|
|
#include "SDL.h"
|
|
|
|
#include "SDL_thread.h"
|
|
|
|
#include "i_system.h"
|
|
|
|
|
2017-03-10 15:12:52 +00:00
|
|
|
class FInternalCriticalSection
|
2014-12-18 09:52:29 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-03-11 16:01:30 +00:00
|
|
|
FInternalCriticalSection()
|
2014-12-18 09:52:29 +00:00
|
|
|
{
|
2017-03-11 16:04:37 +00:00
|
|
|
CritSec = SDL_CreateMutex();
|
2014-12-18 09:52:29 +00:00
|
|
|
if (CritSec == NULL)
|
|
|
|
{
|
|
|
|
I_FatalError("Failed to create a critical section mutex.");
|
|
|
|
}
|
|
|
|
}
|
2017-03-11 16:01:30 +00:00
|
|
|
~FInternalCriticalSection()
|
2014-12-18 09:52:29 +00:00
|
|
|
{
|
|
|
|
if (CritSec != NULL)
|
|
|
|
{
|
|
|
|
SDL_DestroyMutex(CritSec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Enter()
|
|
|
|
{
|
|
|
|
if (SDL_mutexP(CritSec) != 0)
|
|
|
|
{
|
|
|
|
I_FatalError("Failed entering a critical section.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Leave()
|
|
|
|
{
|
|
|
|
if (SDL_mutexV(CritSec) != 0)
|
|
|
|
{
|
|
|
|
I_FatalError("Failed to leave a critical section.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
SDL_mutex *CritSec;
|
|
|
|
};
|
|
|
|
|
2017-03-10 15:12:52 +00:00
|
|
|
FInternalCriticalSection *CreateCriticalSection()
|
|
|
|
{
|
|
|
|
return new FInternalCriticalSection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeleteCriticalSection(FInternalCriticalSection *c)
|
|
|
|
{
|
|
|
|
delete c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnterCriticalSection(FInternalCriticalSection *c)
|
|
|
|
{
|
|
|
|
c->Enter();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LeaveCriticalSection(FInternalCriticalSection *c)
|
|
|
|
{
|
|
|
|
c->Leave();
|
|
|
|
}
|