Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1/*
2This file is a part of the JThread package, which contains some object-
3oriented thread wrappers for different thread implementations.
4
5Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com)
6
7Permission is hereby granted, free of charge, to any person obtaining a
8copy of this software and associated documentation files (the "Software"),
9to deal in the Software without restriction, including without limitation
10the rights to use, copy, modify, merge, publish, distribute, sublicense,
11and/or sell copies of the Software, and to permit persons to whom the
12Software is furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in
15all copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23DEALINGS IN THE SOFTWARE.
24*/
25
26#pragma once
27
28#include "util/basic_macros.h"
29
30#include <string>
31#include <atomic>
32#include <thread>
33#include <mutex>
34
35#ifdef _AIX
36 #include <sys/thread.h> // for tid_t
37#endif
38
39#ifdef __HAIKU__
40 #include <kernel/OS.h>
41#endif
42
43/*
44 * On platforms using pthreads, these five priority classes correlate to
45 * even divisions between the minimum and maximum reported thread priority.
46 */
47#if !defined(_WIN32)
48 #define THREAD_PRIORITY_LOWEST 0
49 #define THREAD_PRIORITY_BELOW_NORMAL 1
50 #define THREAD_PRIORITY_NORMAL 2
51 #define THREAD_PRIORITY_ABOVE_NORMAL 3
52 #define THREAD_PRIORITY_HIGHEST 4
53#endif
54
55
56
57class Thread {
58public:
59 Thread(const std::string &name="");
60 virtual ~Thread();
62 // Note: class cannot be moved since other references exist
63
64 /*
65 * Begins execution of a new thread at the pure virtual method Thread::run().
66 * Execution of the thread is guaranteed to have started after this function
67 * returns.
68 */
69 bool start();
70
71 /*
72 * Requests that the thread exit gracefully.
73 * Returns immediately; thread execution is guaranteed to be complete after
74 * a subsequent call to Thread::wait.
75 */
76 bool stop();
77
78 /*
79 * Waits for thread to finish.
80 * Note: This does not stop a thread, you have to do this on your own.
81 * Returns false immediately if the thread is not started or has been waited
82 * on before.
83 */
84 bool wait();
85
86 /*
87 * Returns true if the calling thread is this Thread object.
88 */
89 bool isCurrentThread() { return std::this_thread::get_id() == getThreadId(); }
90
91 bool isRunning() { return m_running; }
92 bool stopRequested() { return m_request_stop; }
93
94 std::thread::id getThreadId() { return m_thread_obj->get_id(); }
95
96 /*
97 * Gets the thread return value.
98 * Returns true if the thread has exited and the return value was available,
99 * or false if the thread has yet to finish.
100 */
101 bool getReturnValue(void **ret);
102
103 /*
104 * Binds (if possible, otherwise sets the affinity of) the thread to the
105 * specific processor specified by proc_number.
106 */
107 bool bindToProcessor(unsigned int proc_number);
108
109 /*
110 * Sets the thread priority to the specified priority.
111 *
112 * prio can be one of: THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL,
113 * THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_HIGHEST.
114 * On Windows, any of the other priorites as defined by SetThreadPriority
115 * are supported as well.
116 *
117 * Note that it may be necessary to first set the threading policy or
118 * scheduling algorithm to one that supports thread priorities if not
119 * supported by default, otherwise this call will have no effect.
120 */
121 bool setPriority(int prio);
122
123 /*
124 * Returns the thread object of the current thread if it exists.
125 */
126 static Thread *getCurrentThread();
127
128 /*
129 * Sets the currently executing thread's name to where supported; useful
130 * for debugging.
131 */
132 static void setName(const std::string &name);
133
134 /*
135 * Returns the number of processors/cores configured and active on this machine.
136 */
137 static unsigned int getNumberOfProcessors();
138
139protected:
140 std::string m_name;
141
142 virtual void *run() = 0;
143
144private:
145 std::thread::native_handle_type getThreadHandle()
146 { return m_thread_obj->native_handle(); }
147
148 static void threadProc(Thread *thr);
149
150 void *m_retval = nullptr;
151 bool m_joinable = false;
152 std::atomic<bool> m_request_stop;
153 std::atomic<bool> m_running;
154 std::mutex m_mutex;
156
157 std::thread *m_thread_obj = nullptr;
158
159
160#ifdef _AIX
161 // For AIX, there does not exist any mapping from pthread_t to tid_t
162 // available to us, so we maintain one ourselves. This is set on thread start.
163 tid_t m_kernel_thread_id;
164#endif
165};
166
#define DISABLE_CLASS_COPY(C)
Definition basic_macros.h:26
Definition thread.h:57
bool wait()
Definition thread.cpp:147
virtual ~Thread()
Definition thread.cpp:79
bool stop()
Definition thread.cpp:140
std::mutex m_start_finished_mutex
Definition thread.h:155
void * m_retval
Definition thread.h:150
std::thread::native_handle_type getThreadHandle()
Definition thread.h:145
bool setPriority(int prio)
Definition thread.cpp:329
virtual void * run()=0
static unsigned int getNumberOfProcessors()
Definition thread.cpp:264
bool m_joinable
Definition thread.h:151
std::string m_name
Definition thread.h:140
bool getReturnValue(void **ret)
Definition thread.cpp:167
std::atomic< bool > m_running
Definition thread.h:153
static void threadProc(Thread *thr)
Definition thread.cpp:177
Thread(const std::string &name="")
Definition thread.cpp:68
bool isRunning()
Definition thread.h:91
static Thread * getCurrentThread()
Definition thread.cpp:205
bool bindToProcessor(unsigned int proc_number)
Definition thread.cpp:270
std::atomic< bool > m_request_stop
Definition thread.h:152
bool start()
Definition thread.cpp:109
bool stopRequested()
Definition thread.h:92
std::mutex m_mutex
Definition thread.h:154
std::thread * m_thread_obj
Definition thread.h:157
static void setName(const std::string &name)
Definition thread.cpp:211
std::thread::id getThreadId()
Definition thread.h:94
bool isCurrentThread()
Definition thread.h:89