util: add BITSET_LAST_BIT() helper
[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 #define cpu_set_t cpuset_t
49 #endif
50
51 static inline thrd_t u_thread_create(int (*routine)(void *), void *param)
52 {
53 thrd_t thread;
54 #ifdef HAVE_PTHREAD
55 sigset_t saved_set, new_set;
56 int ret;
57
58 sigfillset(&new_set);
59 sigdelset(&new_set, SIGSYS);
60 pthread_sigmask(SIG_BLOCK, &new_set, &saved_set);
61 ret = thrd_create( &thread, routine, param );
62 pthread_sigmask(SIG_SETMASK, &saved_set, NULL);
63 #else
64 int ret;
65 ret = thrd_create( &thread, routine, param );
66 #endif
67 if (ret)
68 return 0;
69
70 return thread;
71 }
72
73 static inline void u_thread_setname( const char *name )
74 {
75 #if defined(HAVE_PTHREAD)
76 #if DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS
77 pthread_setname_np(pthread_self(), name);
78 #elif DETECT_OS_FREEBSD || DETECT_OS_OPENBSD
79 pthread_set_name_np(pthread_self(), name);
80 #elif DETECT_OS_NETBSD
81 pthread_setname_np(pthread_self(), "%s", (void *)name);
82 #elif DETECT_OS_APPLE
83 pthread_setname_np(name);
84 #elif DETECT_OS_HAIKU
85 rename_thread(find_thread(NULL), name);
86 #else
87 #warning Not sure how to call pthread_setname_np
88 #endif
89 #endif
90 (void)name;
91 }
92
93 /**
94 * An AMD Zen CPU consists of multiple modules where each module has its own L3
95 * cache. Inter-thread communication such as locks and atomics between modules
96 * is very expensive. It's desirable to pin a group of closely cooperating
97 * threads to one group of cores sharing L3.
98 *
99 * \param thread thread
100 * \param L3_index index of the L3 cache
101 * \param cores_per_L3 number of CPU cores shared by one L3
102 */
103 static inline void
104 util_pin_thread_to_L3(thrd_t thread, unsigned L3_index, unsigned cores_per_L3)
105 {
106 #if defined(HAVE_PTHREAD_SETAFFINITY)
107 cpu_set_t cpuset;
108
109 CPU_ZERO(&cpuset);
110 for (unsigned i = 0; i < cores_per_L3; i++)
111 CPU_SET(L3_index * cores_per_L3 + i, &cpuset);
112 pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset);
113 #endif
114 }
115
116 /**
117 * Return the index of L3 that the thread is pinned to. If the thread is
118 * pinned to multiple L3 caches, return -1.
119 *
120 * \param thread thread
121 * \param cores_per_L3 number of CPU cores shared by one L3
122 */
123 static inline int
124 util_get_L3_for_pinned_thread(thrd_t thread, unsigned cores_per_L3)
125 {
126 #if defined(HAVE_PTHREAD_SETAFFINITY)
127 cpu_set_t cpuset;
128
129 if (pthread_getaffinity_np(thread, sizeof(cpuset), &cpuset) == 0) {
130 int L3_index = -1;
131
132 for (unsigned i = 0; i < CPU_SETSIZE; i++) {
133 if (CPU_ISSET(i, &cpuset)) {
134 int x = i / cores_per_L3;
135
136 if (L3_index != x) {
137 if (L3_index == -1)
138 L3_index = x;
139 else
140 return -1; /* multiple L3s are set */
141 }
142 }
143 }
144 return L3_index;
145 }
146 #endif
147 return -1;
148 }
149
150 /*
151 * Thread statistics.
152 */
153
154 /* Return the time of a thread's CPU time clock. */
155 static inline int64_t
156 u_thread_get_time_nano(thrd_t thread)
157 {
158 #if defined(HAVE_PTHREAD) && !defined(__APPLE__) && !defined(__HAIKU__)
159 struct timespec ts;
160 clockid_t cid;
161
162 pthread_getcpuclockid(thread, &cid);
163 clock_gettime(cid, &ts);
164 return (int64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
165 #else
166 return 0;
167 #endif
168 }
169
170 static inline bool u_thread_is_self(thrd_t thread)
171 {
172 #if defined(HAVE_PTHREAD)
173 return pthread_equal(pthread_self(), thread);
174 #endif
175 return false;
176 }
177
178 /*
179 * util_barrier
180 */
181
182 #if defined(HAVE_PTHREAD) && !defined(__APPLE__)
183
184 typedef pthread_barrier_t util_barrier;
185
186 static inline void util_barrier_init(util_barrier *barrier, unsigned count)
187 {
188 pthread_barrier_init(barrier, NULL, count);
189 }
190
191 static inline void util_barrier_destroy(util_barrier *barrier)
192 {
193 pthread_barrier_destroy(barrier);
194 }
195
196 static inline void util_barrier_wait(util_barrier *barrier)
197 {
198 pthread_barrier_wait(barrier);
199 }
200
201
202 #else /* If the OS doesn't have its own, implement barriers using a mutex and a condvar */
203
204 typedef struct {
205 unsigned count;
206 unsigned waiters;
207 uint64_t sequence;
208 mtx_t mutex;
209 cnd_t condvar;
210 } util_barrier;
211
212 static inline void util_barrier_init(util_barrier *barrier, unsigned count)
213 {
214 barrier->count = count;
215 barrier->waiters = 0;
216 barrier->sequence = 0;
217 (void) mtx_init(&barrier->mutex, mtx_plain);
218 cnd_init(&barrier->condvar);
219 }
220
221 static inline void util_barrier_destroy(util_barrier *barrier)
222 {
223 assert(barrier->waiters == 0);
224 mtx_destroy(&barrier->mutex);
225 cnd_destroy(&barrier->condvar);
226 }
227
228 static inline void util_barrier_wait(util_barrier *barrier)
229 {
230 mtx_lock(&barrier->mutex);
231
232 assert(barrier->waiters < barrier->count);
233 barrier->waiters++;
234
235 if (barrier->waiters < barrier->count) {
236 uint64_t sequence = barrier->sequence;
237
238 do {
239 cnd_wait(&barrier->condvar, &barrier->mutex);
240 } while (sequence == barrier->sequence);
241 } else {
242 barrier->waiters = 0;
243 barrier->sequence++;
244 cnd_broadcast(&barrier->condvar);
245 }
246
247 mtx_unlock(&barrier->mutex);
248 }
249
250 #endif
251
252 #endif /* U_THREAD_H_ */