Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
thread.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#define _WIN32_WINNT 0x0400
20
21#include "thread.h"
22#include "except.h"
23#include "wwdebug.h"
24#include <process.h>
25#include <windows.h>
26#pragma warning ( push )
27#pragma warning ( disable : 4201 )
28#include "systimer.h"
29#pragma warning ( pop )
30
31
32ThreadClass::ThreadClass(const char *thread_name, ExceptionHandlerType exception_handler) : handle(0), running(false), thread_priority(0)
33{
34 if (thread_name) {
35 assert(strlen(thread_name) < sizeof(ThreadName) - 1);
36 strcpy(ThreadName, thread_name);
37 } else {
38 strcpy(ThreadName, "No name");;
39 }
40
41 ExceptionHandler = exception_handler;
42}
43
48
49void __cdecl ThreadClass::Internal_Thread_Function(void* params)
50{
51 ThreadClass* tc=reinterpret_cast<ThreadClass*>(params);
52 tc->running=true;
53 tc->ThreadID = GetCurrentThreadId();
54
55#ifdef _WIN32
56 Register_Thread_ID(tc->ThreadID, tc->ThreadName);
57
58 if (tc->ExceptionHandler != NULL) {
59 __try {
60 tc->Thread_Function();
61 } __except(tc->ExceptionHandler(GetExceptionCode(), GetExceptionInformation())) {};
62 } else {
63 tc->Thread_Function();
64 }
65
66#else //_WIN32
67 tc->Thread_Function();
68#endif //_WIN32
69
70#ifdef _WIN32
71 Unregister_Thread_ID(tc->ThreadID, tc->ThreadName);
72#endif // _WIN32
73 tc->handle=0;
74 tc->ThreadID = 0;
75}
76
78{
79 WWASSERT(!handle); // Only one thread at a time!
80 #ifdef _UNIX
81 // assert(0);
82 return;
83 #else
84 handle=_beginthread(&Internal_Thread_Function,0,this);
85 SetThreadPriority((HANDLE)handle,THREAD_PRIORITY_NORMAL+thread_priority);
86 WWDEBUG_SAY(("ThreadClass::Execute: Started thread %s, thread ID is %X\n", ThreadName, handle));
87 #endif
88}
89
90void ThreadClass::Set_Priority(int priority)
91{
92 #ifdef _UNIX
93 // assert(0);
94 return;
95 #else
96 thread_priority=priority;
97 if (handle) SetThreadPriority((HANDLE)handle,THREAD_PRIORITY_NORMAL+thread_priority);
98 #endif
99}
100
101void ThreadClass::Stop(unsigned ms)
102{
103 #ifdef _UNIX
104 // assert(0);
105 return;
106 #else
107 running=false;
108 unsigned time=TIMEGETTIME();
109 while (handle) {
110 if ((TIMEGETTIME()-time)>ms) {
111 int res=TerminateThread((HANDLE)handle,0);
112 res; // just to silence compiler warnings
113 WWASSERT(res); // Thread still not killed!
114 handle=0;
115 }
116 Sleep(0);
117 }
118 #endif
119}
120
121void ThreadClass::Sleep_Ms(unsigned ms)
122{
123 Sleep(ms);
124}
125
126#ifndef _UNIX
127HANDLE test_event = ::CreateEvent (NULL, FALSE, FALSE, "");
128#endif
129
131{
132 #ifdef _UNIX
133 return;
134 #else
135 // ::SwitchToThread ();
136 ::WaitForSingleObject (test_event, 1);
137 // Sleep(1); // Note! Parameter can not be 0 (or the thread switch doesn't occur)
138 #endif
139}
140
141// Return calling thread's unique thread id
143{
144 #ifdef _UNIX
145 return 0;
146 #else
147 return GetCurrentThreadId();
148 #endif
149}
150
152{
153 return !!handle;
154}
#define NULL
Definition BaseType.h:92
#define FALSE
Definition BaseType.h:113
#define WWASSERT
#define __cdecl
Definition IFF.H:44
@ false
Definition bool.h:59
ThreadClass(const char *name=NULL, ExceptionHandlerType exception_handler=NULL)
Definition thread.cpp:32
void Execute()
Definition thread.cpp:77
char ThreadName[64]
Definition thread.h:92
unsigned ThreadID
Definition thread.h:95
virtual ~ThreadClass()
Definition thread.cpp:44
virtual void Thread_Function()=0
void Set_Priority(int priority)
Definition thread.cpp:90
static void Switch_Thread()
Definition thread.cpp:130
int(* ExceptionHandlerType)(int exception_code, struct _EXCEPTION_POINTERS *e_info)
Definition thread.h:52
bool Is_Running()
Definition thread.cpp:151
static void Sleep_Ms(unsigned ms=0)
Definition thread.cpp:121
static unsigned _Get_Current_Thread_ID()
Definition thread.cpp:142
volatile bool running
Definition thread.h:89
ExceptionHandlerType ExceptionHandler
Definition thread.h:98
void Stop(unsigned ms=3000)
Definition thread.cpp:101
#define TIMEGETTIME
Definition systimer.h:44
HANDLE test_event
Definition thread.cpp:127
#define WWDEBUG_SAY(x)
Definition wwdebug.h:114