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