mirror of
https://github.com/electronicarts/CnC_Renegade.git
synced 2025-12-16 07:31:40 -05:00
Initial commit of Command & Conquer Renegade source code.
This commit is contained in:
145
Code/wwlib/mutex.cpp
Normal file
145
Code/wwlib/mutex.cpp
Normal file
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
** Command & Conquer Renegade(tm)
|
||||
** Copyright 2025 Electronic Arts Inc.
|
||||
**
|
||||
** This program is free software: you can redistribute it and/or modify
|
||||
** it under the terms of the GNU General Public License as published by
|
||||
** the Free Software Foundation, either version 3 of the License, or
|
||||
** (at your option) any later version.
|
||||
**
|
||||
** This program is distributed in the hope that it will be useful,
|
||||
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
** GNU General Public License for more details.
|
||||
**
|
||||
** You should have received a copy of the GNU General Public License
|
||||
** along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "mutex.h"
|
||||
#include "wwdebug.h"
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
MutexClass::MutexClass(const char* name) : handle(NULL), locked(false)
|
||||
{
|
||||
#ifdef _UNIX
|
||||
//assert(0);
|
||||
#else
|
||||
handle=CreateMutex(NULL,false,name);
|
||||
WWASSERT(handle);
|
||||
#endif
|
||||
}
|
||||
|
||||
MutexClass::~MutexClass()
|
||||
{
|
||||
#ifdef _UNIX
|
||||
//assert(0);
|
||||
#else
|
||||
WWASSERT(!locked); // Can't delete locked mutex!
|
||||
CloseHandle(handle);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool MutexClass::Lock(int time)
|
||||
{
|
||||
#ifdef _UNIX
|
||||
//assert(0);
|
||||
return true;
|
||||
#else
|
||||
int res = WaitForSingleObject(handle,time==WAIT_INFINITE ? INFINITE : time);
|
||||
if (res!=WAIT_OBJECT_0) return false;
|
||||
locked++;
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
void MutexClass::Unlock()
|
||||
{
|
||||
#ifdef _UNIX
|
||||
//assert(0);
|
||||
#else
|
||||
WWASSERT(locked);
|
||||
locked--;
|
||||
int res=ReleaseMutex(handle);
|
||||
WWASSERT(res);
|
||||
#endif
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
MutexClass::LockClass::LockClass(MutexClass& mutex_,int time) : mutex(mutex_)
|
||||
{
|
||||
failed=!mutex.Lock(time);
|
||||
}
|
||||
|
||||
MutexClass::LockClass::~LockClass()
|
||||
{
|
||||
if (!failed) mutex.Unlock();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
CriticalSectionClass::CriticalSectionClass() : handle(NULL), locked(false)
|
||||
{
|
||||
#ifdef _UNIX
|
||||
//assert(0);
|
||||
#else
|
||||
handle=new char[sizeof(CRITICAL_SECTION)];
|
||||
InitializeCriticalSection((CRITICAL_SECTION*)handle);
|
||||
#endif
|
||||
}
|
||||
|
||||
CriticalSectionClass::~CriticalSectionClass()
|
||||
{
|
||||
#ifdef _UNIX
|
||||
//assert(0);
|
||||
#else
|
||||
WWASSERT(!locked); // Can't delete locked mutex!
|
||||
DeleteCriticalSection((CRITICAL_SECTION*)handle);
|
||||
delete[] handle;
|
||||
#endif
|
||||
}
|
||||
|
||||
void CriticalSectionClass::Lock()
|
||||
{
|
||||
#ifdef _UNIX
|
||||
//assert(0);
|
||||
#else
|
||||
EnterCriticalSection((CRITICAL_SECTION*)handle);
|
||||
locked++;
|
||||
#endif
|
||||
}
|
||||
|
||||
void CriticalSectionClass::Unlock()
|
||||
{
|
||||
#ifdef _UNIX
|
||||
//assert(0);
|
||||
#else
|
||||
WWASSERT(locked);
|
||||
locked--;
|
||||
LeaveCriticalSection((CRITICAL_SECTION*)handle);
|
||||
#endif
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
CriticalSectionClass::LockClass::LockClass(CriticalSectionClass& critical_section) : CriticalSection(critical_section)
|
||||
{
|
||||
CriticalSection.Lock();
|
||||
}
|
||||
|
||||
CriticalSectionClass::LockClass::~LockClass()
|
||||
{
|
||||
CriticalSection.Unlock();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user