Clang format: only show errors on non whitelisted files
[oweals/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 "threading/atomic.h"
31 #include "threading/mutex.h"
32 #include "threads.h"
33
34 #include <string>
35 #ifdef _AIX
36         #include <sys/thread.h> // for tid_t
37 #endif
38
39 /*
40  * On platforms using pthreads, these five priority classes correlate to
41  * even divisions between the minimum and maximum reported thread priority.
42  */
43 #if !defined(_WIN32)
44         #define THREAD_PRIORITY_LOWEST       0
45         #define THREAD_PRIORITY_BELOW_NORMAL 1
46         #define THREAD_PRIORITY_NORMAL       2
47         #define THREAD_PRIORITY_ABOVE_NORMAL 3
48         #define THREAD_PRIORITY_HIGHEST      4
49 #endif
50
51
52 class Thread {
53 public:
54         Thread(const std::string &name="");
55         virtual ~Thread();
56
57         /*
58          * Begins execution of a new thread at the pure virtual method Thread::run().
59          * Execution of the thread is guaranteed to have started after this function
60          * returns.
61          */
62         bool start();
63
64         /*
65          * Requests that the thread exit gracefully.
66          * Returns immediately; thread execution is guaranteed to be complete after
67          * a subsequent call to Thread::wait.
68          */
69         bool stop();
70
71         /*
72          * Immediately terminates the thread.
73          * This should be used with extreme caution, as the thread will not have
74          * any opportunity to release resources it may be holding (such as memory
75          * or locks).
76          */
77         bool kill();
78
79         /*
80          * Waits for thread to finish.
81          * Note:  This does not stop a thread, you have to do this on your own.
82          * Returns false immediately if the thread is not started or has been waited
83          * on before.
84          */
85         bool wait();
86
87         /*
88          * Returns true if the calling thread is this Thread object.
89          */
90         bool isCurrentThread() { return thr_is_current_thread(getThreadId()); }
91
92         inline bool isRunning() { return m_running; }
93         inline bool stopRequested() { return m_request_stop; }
94
95 #if USE_CPP11_THREADS
96         inline threadid_t getThreadId() { return m_thread_obj->get_id(); }
97         inline threadhandle_t getThreadHandle() { return m_thread_obj->native_handle(); }
98 #else
99 #  if USE_WIN_THREADS
100         inline threadid_t getThreadId() { return m_thread_id; }
101 #  else
102         inline threadid_t getThreadId() { return m_thread_handle; }
103 #  endif
104         inline threadhandle_t getThreadHandle() { return m_thread_handle; }
105 #endif
106
107         /*
108          * Gets the thread return value.
109          * Returns true if the thread has exited and the return value was available,
110          * or false if the thread has yet to finish.
111          */
112         bool getReturnValue(void **ret);
113
114         /*
115          * Binds (if possible, otherwise sets the affinity of) the thread to the
116          * specific processor specified by proc_number.
117          */
118         bool bindToProcessor(unsigned int proc_number);
119
120         /*
121          * Sets the thread priority to the specified priority.
122          *
123          * prio can be one of: THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL,
124          * THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_HIGHEST.
125          * On Windows, any of the other priorites as defined by SetThreadPriority
126          * are supported as well.
127          *
128          * Note that it may be necessary to first set the threading policy or
129          * scheduling algorithm to one that supports thread priorities if not
130          * supported by default, otherwise this call will have no effect.
131          */
132         bool setPriority(int prio);
133
134         /*
135          * Sets the currently executing thread's name to where supported; useful
136          * for debugging.
137          */
138         static void setName(const std::string &name);
139
140         /*
141          * Returns the number of processors/cores configured and active on this machine.
142          */
143         static unsigned int getNumberOfProcessors();
144
145 protected:
146         std::string m_name;
147
148         virtual void *run() = 0;
149
150 private:
151         void *m_retval;
152         bool m_joinable;
153         Atomic<bool> m_request_stop;
154         Atomic<bool> m_running;
155         Mutex m_mutex;
156         Mutex m_start_finished_mutex;
157
158 #if USE_CPP11_THREADS
159         std::thread *m_thread_obj;
160 #else
161         threadhandle_t m_thread_handle;
162 #   if USE_WIN_THREADS
163         threadid_t m_thread_id;
164 #   endif
165 #endif
166
167         static ThreadStartFunc threadProc;
168
169 #ifdef _AIX
170         // For AIX, there does not exist any mapping from pthread_t to tid_t
171         // available to us, so we maintain one ourselves.  This is set on thread start.
172         tid_t m_kernel_thread_id;
173 #endif
174
175         DISABLE_CLASS_COPY(Thread);
176 };
177
178 #endif
179