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