util: Fix up indentation in the generated format tables code.
[mesa.git] / src / util / u_thread.h
1 /**************************************************************************
2 *
3 * Copyright 1999-2006 Brian Paul
4 * Copyright 2008 VMware, Inc.
5 * All Rights Reserved.
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
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR 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
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 #ifndef U_THREAD_H_
28 #define U_THREAD_H_
29
30 #include <stdint.h>
31 #include <stdbool.h>
32
33 #include "c11/threads.h"
34 #include "detect_os.h"
35
36 #ifdef HAVE_PTHREAD
37 #include <signal.h>
38 #ifdef PTHREAD_SETAFFINITY_IN_NP_HEADER
39 #include <pthread_np.h>
40 #endif
41 #endif
42
43 #ifdef __HAIKU__
44 #include <OS.h>
45 #endif
46
47 #ifdef __FreeBSD__
48 /* pthread_np.h -> sys/param.h -> machine/param.h
49 * - defines ALIGN which clashes with our ALIGN
50 */
51 #undef ALIGN
52 #define cpu_set_t cpuset_t
53 #endif
54
55 static inline thrd_t u_thread_create(int (*routine)(void *), void *param)
56 {
57 thrd_t thread;
58 #ifdef HAVE_PTHREAD
59 sigset_t saved_set, new_set;
60 int ret;
61
62 sigfillset(&new_set);
63 sigdelset(&new_set, SIGSYS);
64 pthread_sigmask(SIG_BLOCK, &new_set, &saved_set);
65 ret = thrd_create( &thread, routine, param );
66 pthread_sigmask(SIG_SETMASK, &saved_set, NULL);
67 #else
68 int ret;
69 ret = thrd_create( &thread, routine, param );
70 #endif
71 if (ret)
72 return 0;
73
74 return thread;
75 }
76
77 static inline void u_thread_setname( const char *name )
78 {
79 #if defined(HAVE_PTHREAD)
80 #if DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS
81 pthread_setname_np(pthread_self(), name);
82 #elif DETECT_OS_FREEBSD || DETECT_OS_OPENBSD
83 pthread_set_name_np(pthread_self(), name);
84 #elif DETECT_OS_NETBSD
85 pthread_setname_np(pthread_self(), "%s", (void *)name);
86 #elif DETECT_OS_APPLE
87 pthread_setname_np(name);
88 #elif DETECT_OS_HAIKU
89 rename_thread(find_thread(NULL), name);
90 #else
91 #warning Not sure how to call pthread_setname_np
92 #endif
93 #endif
94 (void)name;
95 }
96
97 /**
98 * An AMD Zen CPU consists of multiple modules where each module has its own L3
99 * cache. Inter-thread communication such as locks and atomics between modules
100 * is very expensive. It's desirable to pin a group of closely cooperating
101 * threads to one group of cores sharing L3.
102 *
103 * \param thread thread
104 * \param L3_index index of the L3 cache
105 * \param cores_per_L3 number of CPU cores shared by one L3
106 */
107 static inline void
108 util_pin_thread_to_L3(thrd_t thread, unsigned L3_index, unsigned cores_per_L3)
109 {
110 #if defined(HAVE_PTHREAD_SETAFFINITY)
111 cpu_set_t cpuset;
112
113 CPU_ZERO(&cpuset);
114 for (unsigned i = 0; i < cores_per_L3; i++)
115 CPU_SET(L3_index * cores_per_L3 + i, &cpuset);
116 pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset);
117 #endif
118 }
119
120 /**
121 * Return the index of L3 that the thread is pinned to. If the thread is
122 * pinned to multiple L3 caches, return -1.
123 *
124 * \param thread thread
125 * \param cores_per_L3 number of CPU cores shared by one L3
126 */
127 static inline int
128 util_get_L3_for_pinned_thread(thrd_t thread, unsigned cores_per_L3)
129 {
130 #if defined(HAVE_PTHREAD_SETAFFINITY)
131 cpu_set_t cpuset;
132
133 if (pthread_getaffinity_np(thread, sizeof(cpuset), &cpuset) == 0) {
134 int L3_index = -1;
135
136 for (unsigned i = 0; i < CPU_SETSIZE; i++) {
137 if (CPU_ISSET(i, &cpuset)) {
138 int x = i / cores_per_L3;
139
140 if (L3_index != x) {
141 if (L3_index == -1)
142 L3_index = x;
143 else
144 return -1; /* multiple L3s are set */
145 }
146 }
147 }
148 return L3_index;
149 }
150 #endif
151 return -1;
152 }
153
154 /*
155 * Thread statistics.
156 */
157
158 /* Return the time of a thread's CPU time clock. */
159 static inline int64_t
160 u_thread_get_time_nano(thrd_t thread)
161 {
162 #if defined(HAVE_PTHREAD) && !defined(__APPLE__) && !defined(__HAIKU__)
163 struct timespec ts;
164 clockid_t cid;
165
166 pthread_getcpuclockid(thread, &cid);
167 clock_gettime(cid, &ts);
168 return (int64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
169 #else
170 return 0;
171 #endif
172 }
173
174 static inline bool u_thread_is_self(thrd_t thread)
175 {
176 #if defined(HAVE_PTHREAD)
177 return pthread_equal(pthread_self(), thread);
178 #endif
179 return false;
180 }
181
182 /*
183 * util_barrier
184 */
185
186 #if defined(HAVE_PTHREAD) && !defined(__APPLE__)
187
188 typedef pthread_barrier_t util_barrier;
189
190 static inline void util_barrier_init(util_barrier *barrier, unsigned count)
191 {
192 pthread_barrier_init(barrier, NULL, count);
193 }
194
195 static inline void util_barrier_destroy(util_barrier *barrier)
196 {
197 pthread_barrier_destroy(barrier);
198 }
199
200 static inline void util_barrier_wait(util_barrier *barrier)
201 {
202 pthread_barrier_wait(barrier);
203 }
204
205
206 #else /* If the OS doesn't have its own, implement barriers using a mutex and a condvar */
207
208 typedef struct {
209 unsigned count;
210 unsigned waiters;
211 uint64_t sequence;
212 mtx_t mutex;
213 cnd_t condvar;
214 } util_barrier;
215
216 static inline void util_barrier_init(util_barrier *barrier, unsigned count)
217 {
218 barrier->count = count;
219 barrier->waiters = 0;
220 barrier->sequence = 0;
221 (void) mtx_init(&barrier->mutex, mtx_plain);
222 cnd_init(&barrier->condvar);
223 }
224
225 static inline void util_barrier_destroy(util_barrier *barrier)
226 {
227 assert(barrier->waiters == 0);
228 mtx_destroy(&barrier->mutex);
229 cnd_destroy(&barrier->condvar);
230 }
231
232 static inline void util_barrier_wait(util_barrier *barrier)
233 {
234 mtx_lock(&barrier->mutex);
235
236 assert(barrier->waiters < barrier->count);
237 barrier->waiters++;
238
239 if (barrier->waiters < barrier->count) {
240 uint64_t sequence = barrier->sequence;
241
242 do {
243 cnd_wait(&barrier->condvar, &barrier->mutex);
244 } while (sequence == barrier->sequence);
245 } else {
246 barrier->waiters = 0;
247 barrier->sequence++;
248 cnd_broadcast(&barrier->condvar);
249 }
250
251 mtx_unlock(&barrier->mutex);
252 }
253
254 #endif
255
256 #endif /* U_THREAD_H_ */