12 #include "../stdafx.h" 15 #include "../core/alloc_func.hpp" 19 #include "../os/windows/win32.h" 21 #include "../safeguards.h" 44 self_destruct(self_destruct),
47 this->thread = (HANDLE)_beginthreadex(NULL, 0, &
stThreadProc,
this, CREATE_SUSPENDED, &this->
id);
48 if (this->thread == NULL)
return;
49 ResumeThread(this->thread);
54 if (this->thread != NULL) {
55 CloseHandle(this->thread);
62 assert(GetCurrentThreadId() == this->
id);
70 assert(GetCurrentThreadId() != this->
id);
71 WaitForSingleObject(this->thread, INFINITE);
93 SetWin32ThreadName(-1, this->name);
96 this->
proc(this->param);
102 if (self_destruct)
delete this;
109 if (thread != NULL) *thread = to;
125 InitializeCriticalSection(&this->critical_section);
126 this->
event = CreateEvent(NULL, FALSE, FALSE, NULL);
131 DeleteCriticalSection(&this->critical_section);
132 CloseHandle(this->event);
138 EnterCriticalSection(&this->critical_section);
139 this->recursive_count++;
140 if (!allow_recursive && this->recursive_count != 1) NOT_REACHED();
145 if (!allow_recursive && this->recursive_count != 1) NOT_REACHED();
146 this->recursive_count--;
147 LeaveCriticalSection(&this->critical_section);
152 assert(this->recursive_count == 1);
154 WaitForSingleObject(this->event, INFINITE);
155 this->BeginCritical();
160 SetEvent(this->event);
HANDLE thread
System thread identifier.
void(* OTTDThreadFunc)(void *)
Definition of all thread entry functions.
void * param
Parameter for the external thread procedure.
void SendSignal()
Send a signal and wake the 'thread' that was waiting for it.
void ThreadProc()
A new thread is created, and this function is called.
const char * name
Thread name.
ThreadObject_Win32(OTTDThreadFunc proc, void *param, bool self_destruct, const char *name)
Create a win32 thread and start it, calling proc(param).
static ThreadMutex * New()
Create a new mutex.
CRITICAL_SECTION critical_section
The critical section we would enter.
OTTDThreadFunc proc
External thread procedure.
void EndCritical(bool allow_recursive=false)
End of the critical section.
uint id
Thread identifier.
bool Exit()
Exit this thread.
HANDLE event
Event for signalling.
Win32 thread version for ThreadObject.
Win32 thread version of ThreadMutex.
static uint CALLBACK stThreadProc(void *thr)
On thread creation, this function is called, which calls the real startup function.
bool self_destruct
Free ourselves when done?
A Thread Object which works on all our supported OSes.
Signal used for signalling we knowingly want to end the thread.
static bool New(OTTDThreadFunc proc, void *param, ThreadObject **thread=NULL, const char *name=NULL)
Create a thread; proc will be called as first function inside the thread, with optional params...
void BeginCritical(bool allow_recursive=false)
Begin the critical section.
void WaitForSignal()
Wait for a signal to be send.
void Join()
Join this thread.
uint recursive_count
Recursive lock count.