summaryrefslogtreecommitdiff
path: root/src/linkgraph
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/linkgraph
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/linkgraph')
-rw-r--r--src/linkgraph/linkgraphjob.cpp10
-rw-r--r--src/linkgraph/linkgraphjob.h6
-rw-r--r--src/linkgraph/linkgraphschedule.cpp8
-rw-r--r--src/linkgraph/linkgraphschedule.h2
4 files changed, 10 insertions, 16 deletions
diff --git a/src/linkgraph/linkgraphjob.cpp b/src/linkgraph/linkgraphjob.cpp
index fcc9dce78..5b8683820 100644
--- a/src/linkgraph/linkgraphjob.cpp
+++ b/src/linkgraph/linkgraphjob.cpp
@@ -39,7 +39,6 @@ LinkGraphJob::LinkGraphJob(const LinkGraph &orig) :
* This is on purpose. */
link_graph(orig),
settings(_settings_game.linkgraph),
- thread(NULL),
join_date(_date + _settings_game.linkgraph.recalc_time)
{
}
@@ -61,8 +60,7 @@ void LinkGraphJob::EraseFlows(NodeID from)
*/
void LinkGraphJob::SpawnThread()
{
- if (!ThreadObject::New(&(LinkGraphSchedule::Run), this, &this->thread, "ottd:linkgraph")) {
- this->thread = NULL;
+ if (!StartNewThread(&this->thread, "ottd:linkgraph", &(LinkGraphSchedule::Run), this)) {
/* Of course this will hang a bit.
* On the other hand, if you want to play games which make this hang noticably
* on a platform without threads then you'll probably get other problems first.
@@ -79,10 +77,8 @@ void LinkGraphJob::SpawnThread()
*/
void LinkGraphJob::JoinThread()
{
- if (this->thread != NULL) {
- this->thread->Join();
- delete this->thread;
- this->thread = NULL;
+ if (this->thread.joinable()) {
+ this->thread.join();
}
}
diff --git a/src/linkgraph/linkgraphjob.h b/src/linkgraph/linkgraphjob.h
index b47dd70b9..e52d2412d 100644
--- a/src/linkgraph/linkgraphjob.h
+++ b/src/linkgraph/linkgraphjob.h
@@ -12,7 +12,7 @@
#ifndef LINKGRAPHJOB_H
#define LINKGRAPHJOB_H
-#include "../thread/thread.h"
+#include "../thread.h"
#include "linkgraph.h"
#include <list>
@@ -59,7 +59,7 @@ private:
protected:
const LinkGraph link_graph; ///< Link graph to by analyzed. Is copied when job is started and mustn't be modified later.
const LinkGraphSettings settings; ///< Copy of _settings_game.linkgraph at spawn time.
- ThreadObject *thread; ///< Thread the job is running in or NULL if it's running in the main thread.
+ std::thread thread; ///< Thread the job is running in or a default-constructed thread if it's running in the main thread.
Date join_date; ///< Date when the job is to be joined.
NodeAnnotationVector nodes; ///< Extra node data necessary for link graph calculation.
EdgeAnnotationMatrix edges; ///< Extra edge data necessary for link graph calculation.
@@ -266,7 +266,7 @@ public:
* Bare constructor, only for save/load. link_graph, join_date and actually
* settings have to be brutally const-casted in order to populate them.
*/
- LinkGraphJob() : settings(_settings_game.linkgraph), thread(NULL),
+ LinkGraphJob() : settings(_settings_game.linkgraph),
join_date(INVALID_DATE) {}
LinkGraphJob(const LinkGraph &orig);
diff --git a/src/linkgraph/linkgraphschedule.cpp b/src/linkgraph/linkgraphschedule.cpp
index 8c508d817..c5f797f18 100644
--- a/src/linkgraph/linkgraphschedule.cpp
+++ b/src/linkgraph/linkgraphschedule.cpp
@@ -69,13 +69,11 @@ void LinkGraphSchedule::JoinNext()
}
/**
- * Run all handlers for the given Job. This method is tailored to
- * ThreadObject::New.
- * @param j Pointer to a link graph job.
+ * Run all handlers for the given Job.
+ * @param job Pointer to a link graph job.
*/
-/* static */ void LinkGraphSchedule::Run(void *j)
+/* static */ void LinkGraphSchedule::Run(LinkGraphJob *job)
{
- LinkGraphJob *job = (LinkGraphJob *)j;
for (uint i = 0; i < lengthof(instance.handlers); ++i) {
instance.handlers[i]->Run(*job);
}
diff --git a/src/linkgraph/linkgraphschedule.h b/src/linkgraph/linkgraphschedule.h
index ec22be316..b7dfa9793 100644
--- a/src/linkgraph/linkgraphschedule.h
+++ b/src/linkgraph/linkgraphschedule.h
@@ -53,7 +53,7 @@ public:
static const uint SPAWN_JOIN_TICK = 21; ///< Tick when jobs are spawned or joined every day.
static LinkGraphSchedule instance;
- static void Run(void *j);
+ static void Run(LinkGraphJob *job);
static void Clear();
void SpawnNext();