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