OpenTTD Source  20241121-master-g67a0fccfad
thread.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #ifndef THREAD_H
11 #define THREAD_H
12 
13 #include "debug.h"
14 #include "crashlog.h"
15 #include "error_func.h"
16 #include <system_error>
17 #include <thread>
18 #include <mutex>
19 
24 inline void CSleep(int milliseconds)
25 {
26  std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
27 }
28 
33 void SetCurrentThreadName(const char *name);
34 
35 
46 template<class TFn, class... TArgs>
47 inline bool StartNewThread(std::thread *thr, const char *name, TFn&& _Fx, TArgs&&... _Ax)
48 {
49  try {
50  static std::mutex thread_startup_mutex;
51  std::lock_guard<std::mutex> lock(thread_startup_mutex);
52 
53  std::thread t([] (const char *name, TFn&& F, TArgs&&... A) {
54  /* Delay starting the thread till the main thread is finished
55  * with the administration. This prevent race-conditions on
56  * startup. */
57  {
58  std::lock_guard<std::mutex> lock(thread_startup_mutex);
59  }
60 
63  try {
64  /* Call user function with the given arguments. */
65  F(A...);
66  } catch (std::exception &e) {
67  FatalError("Unhandled exception in {} thread: {}", name, e.what());
68  } catch (...) {
69  NOT_REACHED();
70  }
71  }, name, std::forward<TFn>(_Fx), std::forward<TArgs>(_Ax)...);
72 
73  if (thr != nullptr) {
74  *thr = std::move(t);
75  } else {
76  t.detach();
77  }
78 
79  return true;
80  } catch (const std::system_error &e) {
81  /* Something went wrong, the system we are running on might not support threads. */
82  Debug(misc, 1, "Can't create thread '{}': {}", name, e.what());
83  }
84 
85  return false;
86 }
87 
88 #endif /* THREAD_H */
static void InitThread()
Prepare crash log handler for a newly started thread.
Functions to be called to log a crash.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
Error reporting related functions.
void CSleep(int milliseconds)
Sleep on the current thread for a defined time.
Definition: thread.h:24
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
Definition: thread.h:47
void SetCurrentThreadName(const char *name)
Name the thread this function is called on for the debugger.
Definition: win32.cpp:540
std::mutex lock
synchronization for playback status fields
Definition: win32_m.cpp:35