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