Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
sem4.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/******************************************************************************\
20sem4.cpp Neal Kettler
21
22Simple Posix semaphore class
23This is useful because the constructor will automatically call sem_init
24 and you don't have to worry about it. It also allows for other semaphore
25 libraries if you don't have posix.
26\******************************************************************************/
27
28#include "sem4.h"
29
30#ifdef _REENTRANT
31
33{
34#ifndef _WINDOWS
35 sem_init(&sem,1,1);
36#else
37 sem = CreateSemaphore(NULL, 1, 1, NULL);
38#endif
39}
40
42{
43#ifndef _WINDOWS
44 sem_init(&sem,1,value);
45#else
46 sem = CreateSemaphore(NULL, value, value, NULL);
47#endif
48}
49
51{
52#ifndef _WINDOWS
53 sem_destroy(&sem);
54#else
55 if (sem) CloseHandle(sem);
56#endif
57}
58
59sint32 Sem4::Wait(void) const
60{
61#ifndef _WINDOWS
62 return(sem_wait((sem_t *)&sem));
63#else
64 if (!sem)
65 return -1; // no semaphore!
66
67 DWORD dwWaitResult = WaitForSingleObject(sem, INFINITE);
68 switch (dwWaitResult) {
69 case WAIT_OBJECT_0: // The semaphore object was signaled.
70 return 0;
71 break;
72 case WAIT_TIMEOUT: // Should not happen ;)
73 return -1;
74 break;
75 }
76 return -1;
77#endif
78}
79
80sint32 Sem4::Post(void) const
81{
82#ifndef _WINDOWS
83 return(sem_post((sem_t *)&sem));
84#else
85 if (!sem)
86 return -1;
87 if (!ReleaseSemaphore(sem, 1 ,NULL))
88 return -1;
89 return 0;
90#endif
91}
92
93sint32 Sem4::TryWait(void) const
94{
95#ifndef _WINDOWS
96 return(sem_trywait((sem_t *)&sem));
97#else
98 if (!sem)
99 return -1;
100 DWORD dwWaitResult = WaitForSingleObject(sem, 0L);
101 switch (dwWaitResult) {
102 case WAIT_OBJECT_0: // The semaphore object was signaled.
103 return 0;
104 break;
105 case WAIT_TIMEOUT:
106 return -1;
107 break;
108 }
109 return -1;
110#endif
111}
112
113sint32 Sem4::GetValue(int *sval) const
114{
115#ifndef _WINDOWS
116 return(sem_getvalue((sem_t *)&sem,sval));
117#else
118 if (!sem)
119 return -1;
120 long prev;
121 if (!ReleaseSemaphore(sem, 0, &prev))
122 return -1;
123 if (sval)
124 *sval = prev;
125 return 0;
126#endif
127}
128
130{
131#ifndef _WINDOWS
132 return(sem_destroy(&sem));
133#else
134 return CloseHandle(sem);
135#endif
136}
137
138#else
139
140/****************************************************************************
141non threaded versions that do nothing
142*****************************************************************************/
143
145{
146}
147
149{
150}
151
153{
154}
155
156sint32 Sem4::Wait(void) const
157{
158 return(0);
159}
160
161sint32 Sem4::Post(void) const
162{
163 return(0);
164}
165
166sint32 Sem4::TryWait(void) const
167{
168 return(0);
169}
170
171sint32 Sem4::GetValue(int *) const
172{
173 return(0);
174}
175
177{
178 return(0);
179}
180
181#endif
182
#define NULL
Definition BaseType.h:92
void const char * value
unsigned long uint32
Definition bittype.h:46
signed long sint32
Definition bittype.h:51
unsigned long DWORD
Definition bittype.h:57
Sem4()
Definition sem4.cpp:144
sint32 Wait(void) const
Definition sem4.cpp:156
sint32 GetValue(int *sval) const
Definition sem4.cpp:171
sint32 Destroy(void)
Definition sem4.cpp:176
sint32 Post(void) const
Definition sem4.cpp:161
~Sem4()
Definition sem4.cpp:152
sint32 TryWait(void) const
Definition sem4.cpp:166