summaryrefslogtreecommitdiff
path: root/src/thread
diff options
context:
space:
mode:
authorMichael Lutz <michi@icosahedron.de>2019-03-17 01:59:46 +0100
committerMichael Lutz <michi@icosahedron.de>2019-04-06 11:27:39 +0200
commit05bc2ed7cbe07cb4cd535932f10778b35f72e944 (patch)
tree0faaf12fd1bafb0786236ffc82052e8b83dfca60 /src/thread
parent05f4e7360886e36b221ef5c3af4426625a3de686 (diff)
downloadopenttd-05bc2ed7cbe07cb4cd535932f10778b35f72e944.tar.xz
Codechange: Replace custom thread code with C++11 thread objects.
We assume a conforming C++11 compiler environment that has a valid <thread>-header. Failure to run a real thread is handled gracefully.
Diffstat (limited to 'src/thread')
-rw-r--r--src/thread/thread.h59
-rw-r--r--src/thread/thread_none.cpp30
-rw-r--r--src/thread/thread_os2.cpp91
-rw-r--r--src/thread/thread_pthread.cpp110
-rw-r--r--src/thread/thread_win32.cpp111
5 files changed, 0 insertions, 401 deletions
diff --git a/src/thread/thread.h b/src/thread/thread.h
deleted file mode 100644
index eca825e25..000000000
--- a/src/thread/thread.h
+++ /dev/null
@@ -1,59 +0,0 @@
-/* $Id$ */
-
-/*
- * This file is part of OpenTTD.
- * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
- * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/** @file thread.h Base of all threads. */
-
-#ifndef THREAD_H
-#define THREAD_H
-
-/** Definition of all thread entry functions. */
-typedef void (*OTTDThreadFunc)(void *);
-
-/** Signal used for signalling we knowingly want to end the thread. */
-class OTTDThreadExitSignal { };
-
-/**
- * A Thread Object which works on all our supported OSes.
- */
-class ThreadObject {
-public:
- /**
- * Virtual destructor to allow 'delete' operator to work properly.
- */
- virtual ~ThreadObject() {};
-
- /**
- * Exit this thread.
- */
- virtual bool Exit() = 0;
-
- /**
- * Join this thread.
- */
- virtual void Join() = 0;
-
- /**
- * Create a thread; proc will be called as first function inside the thread,
- * with optional params.
- * @param proc The procedure to call inside the thread.
- * @param param The params to give with 'proc'.
- * @param thread Place to store a pointer to the thread in. May be NULL.
- * @param name A name for the thread. May be NULL.
- * @return True if the thread was started correctly.
- */
- static bool New(OTTDThreadFunc proc, void *param, ThreadObject **thread = NULL, const char *name = NULL);
-};
-
-/**
- * Get number of processor cores in the system, including HyperThreading or similar.
- * @return Total number of processor cores.
- */
-uint GetCPUCoreCount();
-
-#endif /* THREAD_H */
diff --git a/src/thread/thread_none.cpp b/src/thread/thread_none.cpp
deleted file mode 100644
index 83ae52d46..000000000
--- a/src/thread/thread_none.cpp
+++ /dev/null
@@ -1,30 +0,0 @@
-/* $Id$ */
-
-/*
- * This file is part of OpenTTD.
- * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
- * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/** @file thread_none.cpp No-Threads-Available implementation of Threads */
-
-#include "../stdafx.h"
-#include "thread.h"
-
-#include "../safeguards.h"
-
-/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread, const char *name)
-{
- if (thread != NULL) *thread = NULL;
- return false;
-}
-
-/** Mutex that doesn't do locking because it ain't needed when there're no threads */
-class ThreadMutex_None : public ThreadMutex {
-public:
- virtual void BeginCritical(bool allow_recursive = false) {}
- virtual void EndCritical(bool allow_recursive = false) {}
- virtual void WaitForSignal() {}
- virtual void SendSignal() {}
-};
diff --git a/src/thread/thread_os2.cpp b/src/thread/thread_os2.cpp
deleted file mode 100644
index 72ee080d6..000000000
--- a/src/thread/thread_os2.cpp
+++ /dev/null
@@ -1,91 +0,0 @@
-/* $Id$ */
-
-/*
- * This file is part of OpenTTD.
- * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
- * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/** @file thread_os2.cpp OS/2 implementation of Threads. */
-
-#include "../stdafx.h"
-#include "thread.h"
-
-#define INCL_DOS
-#include <os2.h>
-#include <process.h>
-
-#include "../safeguards.h"
-
-/**
- * OS/2 version for ThreadObject.
- */
-class ThreadObject_OS2 : public ThreadObject {
-private:
- TID thread; ///< System thread identifier.
- OTTDThreadFunc proc; ///< External thread procedure.
- void *param; ///< Parameter for the external thread procedure.
- bool self_destruct; ///< Free ourselves when done?
-
-public:
- /**
- * Create a thread and start it, calling proc(param).
- */
- ThreadObject_OS2(OTTDThreadFunc proc, void *param, bool self_destruct) :
- thread(0),
- proc(proc),
- param(param),
- self_destruct(self_destruct)
- {
- thread = _beginthread(stThreadProc, NULL, 1048576, this);
- }
-
- bool Exit() override
- {
- _endthread();
- return true;
- }
-
- void Join() override
- {
- DosWaitThread(&this->thread, DCWW_WAIT);
- this->thread = 0;
- }
-private:
- /**
- * On thread creation, this function is called, which calls the real startup
- * function. This to get back into the correct instance again.
- */
- static void stThreadProc(void *thr)
- {
- ((ThreadObject_OS2 *)thr)->ThreadProc();
- }
-
- /**
- * A new thread is created, and this function is called. Call the custom
- * function of the creator of the thread.
- */
- void ThreadProc()
- {
- /* Call the proc of the creator to continue this thread */
- try {
- this->proc(this->param);
- } catch (OTTDThreadExitSignal e) {
- } catch (...) {
- NOT_REACHED();
- }
-
- if (self_destruct) {
- this->Exit();
- delete this;
- }
- }
-};
-
-/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread, const char *name)
-{
- ThreadObject *to = new ThreadObject_OS2(proc, param, thread == NULL);
- if (thread != NULL) *thread = to;
- return true;
-}
diff --git a/src/thread/thread_pthread.cpp b/src/thread/thread_pthread.cpp
deleted file mode 100644
index 50fefb531..000000000
--- a/src/thread/thread_pthread.cpp
+++ /dev/null
@@ -1,110 +0,0 @@
-/* $Id$ */
-
-/*
- * This file is part of OpenTTD.
- * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
- * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/** @file thread_pthread.cpp POSIX pthread implementation of Threads. */
-
-#include "../stdafx.h"
-#include "thread.h"
-#include <pthread.h>
-#include <errno.h>
-
-#if defined(__APPLE__)
-#include "../os/macosx/macos.h"
-#endif
-
-#include "../safeguards.h"
-
-/**
- * POSIX pthread version for ThreadObject.
- */
-class ThreadObject_pthread : public ThreadObject {
-private:
- pthread_t thread; ///< System thread identifier.
- OTTDThreadFunc proc; ///< External thread procedure.
- void *param; ///< Parameter for the external thread procedure.
- bool self_destruct; ///< Free ourselves when done?
- const char *name; ///< Name for the thread
-
-public:
- /**
- * Create a pthread and start it, calling proc(param).
- */
- ThreadObject_pthread(OTTDThreadFunc proc, void *param, bool self_destruct, const char *name) :
- thread(0),
- proc(proc),
- param(param),
- self_destruct(self_destruct),
- name(name)
- {
- pthread_create(&this->thread, NULL, &stThreadProc, this);
- }
-
- bool Exit() override
- {
- assert(pthread_self() == this->thread);
- /* For now we terminate by throwing an error, gives much cleaner cleanup */
- throw OTTDThreadExitSignal();
- }
-
- void Join() override
- {
- /* You cannot join yourself */
- assert(pthread_self() != this->thread);
- pthread_join(this->thread, NULL);
- this->thread = 0;
- }
-private:
- /**
- * On thread creation, this function is called, which calls the real startup
- * function. This to get back into the correct instance again.
- */
- static void *stThreadProc(void *thr)
- {
- ThreadObject_pthread *self = (ThreadObject_pthread *) thr;
-#if defined(__GLIBC__)
-#if __GLIBC_PREREQ(2, 12)
- if (self->name) {
- pthread_setname_np(pthread_self(), self->name);
- }
-#endif
-#endif
-#if defined(__APPLE__)
- MacOSSetThreadName(self->name);
-#endif
- self->ThreadProc();
- pthread_exit(NULL);
- }
-
- /**
- * A new thread is created, and this function is called. Call the custom
- * function of the creator of the thread.
- */
- void ThreadProc()
- {
- /* Call the proc of the creator to continue this thread */
- try {
- this->proc(this->param);
- } catch (OTTDThreadExitSignal) {
- } catch (...) {
- NOT_REACHED();
- }
-
- if (self_destruct) {
- pthread_detach(pthread_self());
- delete this;
- }
- }
-};
-
-/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread, const char *name)
-{
- ThreadObject *to = new ThreadObject_pthread(proc, param, thread == NULL, name);
- if (thread != NULL) *thread = to;
- return true;
-}
diff --git a/src/thread/thread_win32.cpp b/src/thread/thread_win32.cpp
deleted file mode 100644
index fc7a85a91..000000000
--- a/src/thread/thread_win32.cpp
+++ /dev/null
@@ -1,111 +0,0 @@
-/* $Id$ */
-
-/*
- * This file is part of OpenTTD.
- * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
- * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/** @file thread_win32.cpp Win32 thread implementation of Threads. */
-
-#include "../stdafx.h"
-#include "thread.h"
-#include "../debug.h"
-#include "../core/alloc_func.hpp"
-#include <stdlib.h>
-#include <windows.h>
-#include <process.h>
-#include "../os/windows/win32.h"
-
-#include "../safeguards.h"
-
-/**
- * Win32 thread version for ThreadObject.
- */
-class ThreadObject_Win32 : public ThreadObject {
-private:
- HANDLE thread; ///< System thread identifier.
- uint id; ///< Thread identifier.
- OTTDThreadFunc proc; ///< External thread procedure.
- void *param; ///< Parameter for the external thread procedure.
- bool self_destruct; ///< Free ourselves when done?
- const char *name; ///< Thread name.
-
-public:
- /**
- * Create a win32 thread and start it, calling proc(param).
- */
- ThreadObject_Win32(OTTDThreadFunc proc, void *param, bool self_destruct, const char *name) :
- thread(NULL),
- id(0),
- proc(proc),
- param(param),
- self_destruct(self_destruct),
- name(name)
- {
- this->thread = (HANDLE)_beginthreadex(NULL, 0, &stThreadProc, this, CREATE_SUSPENDED, &this->id);
- if (this->thread == NULL) return;
- ResumeThread(this->thread);
- }
-
- ~ThreadObject_Win32() override
- {
- if (this->thread != NULL) {
- CloseHandle(this->thread);
- this->thread = NULL;
- }
- }
-
- bool Exit() override
- {
- assert(GetCurrentThreadId() == this->id);
- /* For now we terminate by throwing an error, gives much cleaner cleanup */
- throw OTTDThreadExitSignal();
- }
-
- void Join() override
- {
- /* You cannot join yourself */
- assert(GetCurrentThreadId() != this->id);
- WaitForSingleObject(this->thread, INFINITE);
- }
-
-private:
- /**
- * On thread creation, this function is called, which calls the real startup
- * function. This to get back into the correct instance again.
- */
- static uint CALLBACK stThreadProc(void *thr)
- {
- ((ThreadObject_Win32 *)thr)->ThreadProc();
- return 0;
- }
-
- /**
- * A new thread is created, and this function is called. Call the custom
- * function of the creator of the thread.
- */
- void ThreadProc()
- {
-#ifdef _MSC_VER
- /* Set thread name for debuggers. Has to be done from the thread due to a race condition in older MS debuggers. */
- SetWin32ThreadName(-1, this->name);
-#endif
- try {
- this->proc(this->param);
- } catch (OTTDThreadExitSignal) {
- } catch (...) {
- NOT_REACHED();
- }
-
- if (self_destruct) delete this;
- }
-};
-
-/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread, const char *name)
-{
- ThreadObject *to = new ThreadObject_Win32(proc, param, thread == NULL, name);
- if (thread != NULL) *thread = to;
- return true;
-}