Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
mutex.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include "mutex.h"
20#include "wwdebug.h"
21#include <windows.h>
22
23
24// ----------------------------------------------------------------------------
25
26MutexClass::MutexClass(const char* name) : handle(NULL), locked(false)
27{
28 #ifdef _UNIX
29 //assert(0);
30 #else
31 handle=CreateMutex(NULL,false,name);
32 WWASSERT(handle);
33 #endif
34}
35
37{
38 #ifdef _UNIX
39 //assert(0);
40 #else
41 WWASSERT(!locked); // Can't delete locked mutex!
42 CloseHandle(handle);
43 #endif
44}
45
46bool MutexClass::Lock(int time)
47{
48 #ifdef _UNIX
49 //assert(0);
50 return true;
51 #else
52 int res = WaitForSingleObject(handle,time==WAIT_INFINITE ? INFINITE : time);
53 if (res!=WAIT_OBJECT_0) return false;
54 locked++;
55 return true;
56 #endif
57}
58
59void MutexClass::Unlock()
60{
61 #ifdef _UNIX
62 //assert(0);
63 #else
64 WWASSERT(locked);
65 locked--;
66 int res=ReleaseMutex(handle);
67 res; // silence compiler warnings
68 WWASSERT(res);
69 #endif
70}
71
72// ----------------------------------------------------------------------------
73
74MutexClass::LockClass::LockClass(MutexClass& mutex_,int time) : mutex(mutex_)
75{
76 failed=!mutex.Lock(time);
77}
78
80{
81 if (!failed) mutex.Unlock();
82}
83
84
85
86
87
88
89
90// ----------------------------------------------------------------------------
91
93{
94 #ifdef _UNIX
95 //assert(0);
96 #else
97 handle=W3DNEWARRAY char[sizeof(CRITICAL_SECTION)];
98 InitializeCriticalSection((CRITICAL_SECTION*)handle);
99 #endif
100}
101
103{
104 #ifdef _UNIX
105 //assert(0);
106 #else
107 WWASSERT(!locked); // Can't delete locked mutex!
108 DeleteCriticalSection((CRITICAL_SECTION*)handle);
109 delete[] handle;
110 #endif
111}
112
113void CriticalSectionClass::Lock()
114{
115 #ifdef _UNIX
116 //assert(0);
117 #else
118 EnterCriticalSection((CRITICAL_SECTION*)handle);
119 locked++;
120 #endif
121}
122
123void CriticalSectionClass::Unlock()
124{
125 #ifdef _UNIX
126 //assert(0);
127 #else
128 WWASSERT(locked);
129 locked--;
130 LeaveCriticalSection((CRITICAL_SECTION*)handle);
131 #endif
132}
133
134// ----------------------------------------------------------------------------
135
136CriticalSectionClass::LockClass::LockClass(CriticalSectionClass& critical_section) : CriticalSection(critical_section)
137{
138 CriticalSection.Lock();
139}
140
142{
143 CriticalSection.Unlock();
144}
145
146
#define NULL
Definition BaseType.h:92
#define WWASSERT
#define W3DNEWARRAY
Definition always.h:110
@ false
Definition bool.h:59
LockClass(CriticalSectionClass &c)
LockClass(MutexClass &m, int time=MutexClass::WAIT_INFINITE)
Definition mutex.cpp:74
~MutexClass()
Definition mutex.cpp:36
MutexClass(const char *name=NULL)
Definition mutex.cpp:26
@ WAIT_INFINITE
Definition mutex.h:55