Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
threadfac.h
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//
20// Platform independent thread creation (Win32 & POSIX)
21//
22
23#ifndef THREADFAC_HEADER
24#define THREADFAC_HEADER
25
26#ifdef _WIN32
27 #include <process.h>
28#endif
29#include "wstypes.h"
30#include <stdlib.h>
31
32#ifdef _WIN32
33 #include <wtypes.h>
34#else // UNIX
35 #include <pthread.h>
36#endif
37
38// Windows headers have a tendency to redefine IN
39#ifdef IN
40#undef IN
41#endif
42#define IN const
43
44#include "critsec.h"
45
46
47
48#ifdef THREADFAC_CODE
49 // This is the fake thread entry point for functions
50 #ifdef _WIN32
51 static unsigned __stdcall threadFuncLauncher(void *temp);
52 #else // UNIX
53 static void *threadFuncLauncher(void *temp);
54 #endif
55
56 // Fake entry point for classes
57 #ifdef _WIN32
58 static unsigned __stdcall threadClassLauncher(void *temp);
59 #else // UNIX
60 static void *threadClassLauncher(void *temp);
61 #endif
62#endif
63
64
65
66
67
68// Forward definition of base class for threaded classes
69class Runnable;
70
71//
72// Call the static method startThread to begin a new thread.
73//
74class ThreadFactory
75{
76 public:
77 static bit8 startThread(void (*start_func)(void *), void *data);
78 static bit8 startThread(Runnable &runable, void *data, bit8 destroy=FALSE);
79};
80
81
82
83//
84// Base class for when you want a thread to execute inside a class
85// instead of a function.
86//
87class Runnable
88{
89 public:
91 virtual ~Runnable();
92
93
94 // ThreadFactory needs to be able to access the private
95 // IsRunning_ field.
96 friend class ThreadFactory;
97
98 // So do the threadClassLaunchers
99 #ifdef _WIN32
100 friend static unsigned __stdcall threadClassLauncher(void *temp);
101 #else // UNIX
102 friend void *threadClassLauncher(void *temp);
103 #endif
104
105 virtual void run(void *data)=0; // Thread entry point
106
107 void startThread(void *data,bit8 destroy=FALSE) // nice way to start a thread
108 {
109 ThreadFactory::startThread(*this,data,destroy);
110 };
111
112 // Is there a thread running in this class?
113 static bit8 isRunning(void);
114
115 // Get the count of threads running inside this class
116 static int getThreadCount();
117
118
119 private:
120 static int ThreadCount_;
121 static CritSec CritSec_; // to protect ThreadCount_
122};
123
124#endif
#define FALSE
Definition BaseType.h:113
char bit8
Definition wstypes.h:61
void startThread(void *data, bit8 destroy=FALSE)
Definition threadfac.h:107
virtual ~Runnable()
static bit8 isRunning(void)
friend class ThreadFactory
Definition threadfac.h:96
virtual void run(void *data)=0
friend void * threadClassLauncher(void *temp)
static int getThreadCount()
static bit8 startThread(void(*start_func)(void *), void *data)
static bit8 startThread(Runnable &runable, void *data, bit8 destroy=FALSE)
static bit8 startThread(void(*start_func)(void *), void *data)
void * threadClassLauncher(void *temp)
void * threadFuncLauncher(void *temp)