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