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