]> git.lizzy.rs Git - dragonfireclient.git/blob - src/threading/thread.h
Revert "Make Lint Happy"
[dragonfireclient.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 #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 /*
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
53 class Thread {
54 public:
55         Thread(const std::string &name="");
56         virtual ~Thread();
57         DISABLE_CLASS_COPY(Thread)
58
59         /*
60          * Begins execution of a new thread at the pure virtual method Thread::run().
61          * Execution of the thread is guaranteed to have started after this function
62          * returns.
63          */
64         bool start();
65
66         /*
67          * Requests that the thread exit gracefully.
68          * Returns immediately; thread execution is guaranteed to be complete after
69          * a subsequent call to Thread::wait.
70          */
71         bool stop();
72
73         /*
74          * Immediately terminates the thread.
75          * This should be used with extreme caution, as the thread will not have
76          * any opportunity to release resources it may be holding (such as memory
77          * or locks).
78          */
79         bool kill();
80
81         /*
82          * Waits for thread to finish.
83          * Note:  This does not stop a thread, you have to do this on your own.
84          * Returns false immediately if the thread is not started or has been waited
85          * on before.
86          */
87         bool wait();
88
89         /*
90          * Returns true if the calling thread is this Thread object.
91          */
92         bool isCurrentThread() { return std::this_thread::get_id() == getThreadId(); }
93
94         bool isRunning() { return m_running; }
95         bool stopRequested() { return m_request_stop; }
96
97         std::thread::id getThreadId() { return m_thread_obj->get_id(); }
98
99         /*
100          * Gets the thread return value.
101          * Returns true if the thread has exited and the return value was available,
102          * or false if the thread has yet to finish.
103          */
104         bool getReturnValue(void **ret);
105
106         /*
107          * Binds (if possible, otherwise sets the affinity of) the thread to the
108          * specific processor specified by proc_number.
109          */
110         bool bindToProcessor(unsigned int proc_number);
111
112         /*
113          * Sets the thread priority to the specified priority.
114          *
115          * prio can be one of: THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL,
116          * THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_HIGHEST.
117          * On Windows, any of the other priorites as defined by SetThreadPriority
118          * are supported as well.
119          *
120          * Note that it may be necessary to first set the threading policy or
121          * scheduling algorithm to one that supports thread priorities if not
122          * supported by default, otherwise this call will have no effect.
123          */
124         bool setPriority(int prio);
125
126         /*
127          * Sets the currently executing thread's name to where supported; useful
128          * for debugging.
129          */
130         static void setName(const std::string &name);
131
132         /*
133          * Returns the number of processors/cores configured and active on this machine.
134          */
135         static unsigned int getNumberOfProcessors();
136
137 protected:
138         std::string m_name;
139
140         virtual void *run() = 0;
141
142 private:
143         std::thread::native_handle_type getThreadHandle()
144                 { return m_thread_obj->native_handle(); }
145
146         static void threadProc(Thread *thr);
147
148         void *m_retval = nullptr;
149         bool m_joinable = false;
150         std::atomic<bool> m_request_stop;
151         std::atomic<bool> m_running;
152         std::mutex m_mutex;
153         std::mutex m_start_finished_mutex;
154
155         std::thread *m_thread_obj = nullptr;
156
157
158 #ifdef _AIX
159         // For AIX, there does not exist any mapping from pthread_t to tid_t
160         // available to us, so we maintain one ourselves.  This is set on thread start.
161         tid_t m_kernel_thread_id;
162 #endif
163 };
164