Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
thread.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#ifndef THREAD_H
20#define THREAD_H
21
22#if defined(_MSC_VER)
23#pragma once
24#endif
25#ifdef _UNIX
26#include "osdep.h"
27#endif
28
29#include "always.h"
30#include "vector.h"
31
32struct _EXCEPTION_POINTERS;
33
34
35// ****************************************************************************
36//
37// To create a new thread just derive a new class from this and define
38// Thread_Function. Creating the TheadClass object doesn't start the
39// thread. To start the thread you must call Execute().
40//
41// In your own thread remember to check for "running" flag of the base class.
42// If the flag is false you must exit the asap. Stop() is the function that
43// will clear the flag and expect you to exit from the thread. If you are
44// not exiting in certain time (defined as a parameter to Stop()) it will
45// force-kill the thread to prevent the program from halting.
46//
47// ****************************************************************************
48
50{
51public:
52 typedef int (*ExceptionHandlerType)(int exception_code, struct _EXCEPTION_POINTERS *e_info);
53
54 ThreadClass(const char *name = NULL, ExceptionHandlerType exception_handler = NULL);
55 virtual ~ThreadClass();
56
57 // Execute Thread_Function(). Note that only one instance can be executed at a time.
58 void Execute();
59
60 // Thread priority 0 is normal, positive numbers are higher and normal and negative are lower.
61 void Set_Priority(int priority);
62
63 // Stop thread execution. Kill after ms milliseconds if not responding.
64 void Stop(unsigned ms=3000);
65
66 // Put current thread sleep for ms milliseconds (can be called from any thread, ThreadClass or other)
67 static void Sleep_Ms(unsigned ms=0);
68
69 // Put current thread in sleep and switch to next one (Useful for balansing the thread switches with game update)
70 static void Switch_Thread();
71
72 // Return calling thread's unique thread id
73 static unsigned _Get_Current_Thread_ID();
74
75 // Returns true if the thread is running.
76 bool Is_Running();
77
78 // Gets the name of the thread.
79 const char *Get_Name(void) {return(ThreadName);};
80
81 // Get info about a registered thread by it's index.
82 static int Get_Thread_By_Index(int index, char *name_ptr = NULL);
83
84protected:
85
86 // User defined thread function. The thread function should check for "running" flag every now and then
87 // and exit the thread if running is false.
88 virtual void Thread_Function() = 0;
89 volatile bool running;
90
91 // Name of thread.
92 char ThreadName[64];
93
94 // ID of thread.
95 unsigned ThreadID;
96
97 // Exception handler for this thread.
99
100private:
101 static void __cdecl Internal_Thread_Function(void*);
102 volatile unsigned long handle;
103 int thread_priority;
104};
105
106#endif
#define NULL
Definition BaseType.h:92
#define __cdecl
Definition IFF.H:44
ThreadClass(const char *name=NULL, ExceptionHandlerType exception_handler=NULL)
Definition thread.cpp:32
static int Get_Thread_By_Index(int index, char *name_ptr=NULL)
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
const char * Get_Name(void)
Definition thread.h:79