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