| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- /*
- ** 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();
- }
|