• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <errno.h>
31 #include <stdint.h>
32 #include <stdbool.h>
33 #include <string.h>
34 
35 #include "c11/threads.h"
36 #include "detect_os.h"
37 #include "macros.h"
38 
39 #ifdef HAVE_PTHREAD
40 #include <signal.h>
41 #ifdef HAVE_PTHREAD_NP_H
42 #include <pthread_np.h>
43 #endif
44 #endif
45 
46 #ifdef __HAIKU__
47 #include <OS.h>
48 #endif
49 
50 #if DETECT_OS_LINUX
51 #include <sched.h>
52 #elif defined(_WIN32) && !defined(__CYGWIN__) && _WIN32_WINNT >= 0x0600
53 #include <windows.h>
54 #endif
55 
56 #ifdef __FreeBSD__
57 /* pthread_np.h -> sys/param.h -> machine/param.h
58  * - defines ALIGN which clashes with our ALIGN
59  */
60 #undef ALIGN
61 #define cpu_set_t cpuset_t
62 #endif
63 
64 /* For util_set_thread_affinity to size the mask. */
65 #define UTIL_MAX_CPUS               1024  /* this should be enough */
66 #define UTIL_MAX_L3_CACHES          UTIL_MAX_CPUS
67 
68 /* Some highly performance-sensitive thread-local variables like the current GL
69  * context are declared with the initial-exec model on Linux.  glibc allocates a
70  * fixed number of extra slots for initial-exec TLS variables at startup, and
71  * Mesa relies on (even if it's dlopen()ed after init) being able to fit into
72  * those.  This model saves the call to look up the address of the TLS variable.
73  *
74  * However, if we don't have this TLS model available on the platform, then we
75  * still want to use normal TLS (which involves a function call, but not the
76  * expensive pthread_getspecific() or its equivalent).
77  */
78 #ifdef _MSC_VER
79 #define __THREAD_INITIAL_EXEC __declspec(thread)
80 #elif defined(__ANDROID__)
81 /* Android 29 gained ELF TLS support, but it doesn't support initial-exec and
82  * it will throw:
83  *
84  *     dlopen failed: TLS symbol "(null)" in dlopened
85  *     "/vendor/lib64/egl/libEGL_mesa.so" referenced from
86  *     "/vendor/lib64/egl/libEGL_mesa.so" using IE access model.
87  */
88 #define __THREAD_INITIAL_EXEC __thread
89 #else
90 #define __THREAD_INITIAL_EXEC __thread __attribute__((tls_model("initial-exec")))
91 #endif
92 
93 static inline int
util_get_current_cpu(void)94 util_get_current_cpu(void)
95 {
96 #if DETECT_OS_LINUX && !defined(__ANDROID__)
97    return sched_getcpu();
98 
99 #elif defined(_WIN32) && !defined(__CYGWIN__) && _WIN32_WINNT >= 0x0600
100    return GetCurrentProcessorNumber();
101 
102 #else
103    return -1;
104 #endif
105 }
106 
u_thread_create(int (* routine)(void *),void * param)107 static inline thrd_t u_thread_create(int (*routine)(void *), void *param)
108 {
109    thrd_t thread;
110 #ifdef HAVE_PTHREAD
111    sigset_t saved_set, new_set;
112    int ret;
113 
114    sigfillset(&new_set);
115    sigdelset(&new_set, SIGSYS);
116    pthread_sigmask(SIG_BLOCK, &new_set, &saved_set);
117    ret = thrd_create( &thread, routine, param );
118    pthread_sigmask(SIG_SETMASK, &saved_set, NULL);
119 #else
120    int ret;
121    ret = thrd_create( &thread, routine, param );
122 #endif
123    if (ret)
124       return 0;
125 
126    return thread;
127 }
128 
u_thread_setname(const char * name)129 static inline void u_thread_setname( const char *name )
130 {
131 #if defined(HAVE_PTHREAD)
132 #if DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS
133    int ret = pthread_setname_np(pthread_self(), name);
134    if (ret == ERANGE) {
135       char buf[16];
136       const size_t len = MIN2(strlen(name), ARRAY_SIZE(buf) - 1);
137       memcpy(buf, name, len);
138       buf[len] = '\0';
139       pthread_setname_np(pthread_self(), buf);
140    }
141 #elif DETECT_OS_FREEBSD || DETECT_OS_OPENBSD
142    pthread_set_name_np(pthread_self(), name);
143 #elif DETECT_OS_NETBSD
144    pthread_setname_np(pthread_self(), "%s", (void *)name);
145 #elif DETECT_OS_APPLE
146    pthread_setname_np(name);
147 #elif DETECT_OS_HAIKU
148    rename_thread(find_thread(NULL), name);
149 #else
150 #warning Not sure how to call pthread_setname_np
151 #endif
152 #endif
153    (void)name;
154 }
155 
156 /**
157  * Set thread affinity.
158  *
159  * \param thread         Thread
160  * \param mask           Set this affinity mask
161  * \param old_mask       Previous affinity mask returned if not NULL
162  * \param num_mask_bits  Number of bits in both masks
163  * \return  true on success
164  */
165 static inline bool
util_set_thread_affinity(thrd_t thread,const uint32_t * mask,uint32_t * old_mask,unsigned num_mask_bits)166 util_set_thread_affinity(thrd_t thread,
167                          const uint32_t *mask,
168                          uint32_t *old_mask,
169                          unsigned num_mask_bits)
170 {
171 #if defined(HAVE_PTHREAD_SETAFFINITY)
172    cpu_set_t cpuset;
173 
174    if (old_mask) {
175       if (pthread_getaffinity_np(thread, sizeof(cpuset), &cpuset) != 0)
176          return false;
177 
178       memset(old_mask, 0, num_mask_bits / 8);
179       for (unsigned i = 0; i < num_mask_bits && i < CPU_SETSIZE; i++) {
180          if (CPU_ISSET(i, &cpuset))
181             old_mask[i / 32] |= 1u << (i % 32);
182       }
183    }
184 
185    CPU_ZERO(&cpuset);
186    for (unsigned i = 0; i < num_mask_bits && i < CPU_SETSIZE; i++) {
187       if (mask[i / 32] & (1u << (i % 32)))
188          CPU_SET(i, &cpuset);
189    }
190    return pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset) == 0;
191 
192 #elif defined(_WIN32) && !defined(__CYGWIN__)
193    DWORD_PTR m = mask[0];
194 
195    if (sizeof(m) > 4 && num_mask_bits > 32)
196       m |= (uint64_t)mask[1] << 32;
197 
198    m = SetThreadAffinityMask(thread, m);
199    if (!m)
200       return false;
201 
202    if (old_mask) {
203       memset(old_mask, 0, num_mask_bits / 8);
204 
205       old_mask[0] = m;
206 #ifdef _WIN64
207       old_mask[1] = m >> 32;
208 #endif
209    }
210 
211    return true;
212 #else
213    return false;
214 #endif
215 }
216 
217 static inline bool
util_set_current_thread_affinity(const uint32_t * mask,uint32_t * old_mask,unsigned num_mask_bits)218 util_set_current_thread_affinity(const uint32_t *mask,
219                                  uint32_t *old_mask,
220                                  unsigned num_mask_bits)
221 {
222 #if defined(HAVE_PTHREAD_SETAFFINITY)
223    return util_set_thread_affinity(pthread_self(), mask, old_mask,
224                                    num_mask_bits);
225 
226 #elif defined(_WIN32) && !defined(__CYGWIN__)
227    /* The GetCurrentThreadId() handle is only valid within the current thread. */
228    return util_set_thread_affinity(GetCurrentThread(), mask, old_mask,
229                                    num_mask_bits);
230 
231 #else
232    return false;
233 #endif
234 }
235 
236 
237 /*
238  * Thread statistics.
239  */
240 
241 /* Return the time of a thread's CPU time clock. */
242 static inline int64_t
util_thread_get_time_nano(thrd_t thread)243 util_thread_get_time_nano(thrd_t thread)
244 {
245 #if defined(HAVE_PTHREAD) && !defined(__APPLE__) && !defined(__HAIKU__)
246    struct timespec ts;
247    clockid_t cid;
248 
249    pthread_getcpuclockid(thread, &cid);
250    clock_gettime(cid, &ts);
251    return (int64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
252 #else
253    return 0;
254 #endif
255 }
256 
257 /* Return the time of the current thread's CPU time clock. */
258 static inline int64_t
util_current_thread_get_time_nano(void)259 util_current_thread_get_time_nano(void)
260 {
261 #if defined(HAVE_PTHREAD)
262    return util_thread_get_time_nano(pthread_self());
263 
264 #elif defined(_WIN32) && !defined(__CYGWIN__)
265    /* The GetCurrentThreadId() handle is only valid within the current thread. */
266    return util_thread_get_time_nano(GetCurrentThread());
267 
268 #else
269    return 0;
270 #endif
271 }
272 
u_thread_is_self(thrd_t thread)273 static inline bool u_thread_is_self(thrd_t thread)
274 {
275 #if defined(HAVE_PTHREAD)
276    return pthread_equal(pthread_self(), thread);
277 #endif
278    return false;
279 }
280 
281 /*
282  * util_barrier
283  */
284 
285 #if defined(HAVE_PTHREAD) && !defined(__APPLE__) && !defined(__HAIKU__)
286 
287 typedef pthread_barrier_t util_barrier;
288 
util_barrier_init(util_barrier * barrier,unsigned count)289 static inline void util_barrier_init(util_barrier *barrier, unsigned count)
290 {
291    pthread_barrier_init(barrier, NULL, count);
292 }
293 
util_barrier_destroy(util_barrier * barrier)294 static inline void util_barrier_destroy(util_barrier *barrier)
295 {
296    pthread_barrier_destroy(barrier);
297 }
298 
util_barrier_wait(util_barrier * barrier)299 static inline void util_barrier_wait(util_barrier *barrier)
300 {
301    pthread_barrier_wait(barrier);
302 }
303 
304 
305 #else /* If the OS doesn't have its own, implement barriers using a mutex and a condvar */
306 
307 typedef struct {
308    unsigned count;
309    unsigned waiters;
310    uint64_t sequence;
311    mtx_t mutex;
312    cnd_t condvar;
313 } util_barrier;
314 
util_barrier_init(util_barrier * barrier,unsigned count)315 static inline void util_barrier_init(util_barrier *barrier, unsigned count)
316 {
317    barrier->count = count;
318    barrier->waiters = 0;
319    barrier->sequence = 0;
320    (void) mtx_init(&barrier->mutex, mtx_plain);
321    cnd_init(&barrier->condvar);
322 }
323 
util_barrier_destroy(util_barrier * barrier)324 static inline void util_barrier_destroy(util_barrier *barrier)
325 {
326    assert(barrier->waiters == 0);
327    mtx_destroy(&barrier->mutex);
328    cnd_destroy(&barrier->condvar);
329 }
330 
util_barrier_wait(util_barrier * barrier)331 static inline void util_barrier_wait(util_barrier *barrier)
332 {
333    mtx_lock(&barrier->mutex);
334 
335    assert(barrier->waiters < barrier->count);
336    barrier->waiters++;
337 
338    if (barrier->waiters < barrier->count) {
339       uint64_t sequence = barrier->sequence;
340 
341       do {
342          cnd_wait(&barrier->condvar, &barrier->mutex);
343       } while (sequence == barrier->sequence);
344    } else {
345       barrier->waiters = 0;
346       barrier->sequence++;
347       cnd_broadcast(&barrier->condvar);
348    }
349 
350    mtx_unlock(&barrier->mutex);
351 }
352 
353 #endif
354 
355 /*
356  * Thread-id's.
357  *
358  * thrd_current() is not portable to windows (or at least not in a desirable
359  * way), so thread_id's provide an alternative mechanism
360  */
361 
362 #ifdef _WIN32
363 typedef DWORD thread_id;
364 #else
365 typedef thrd_t thread_id;
366 #endif
367 
368 static inline thread_id
util_get_thread_id(void)369 util_get_thread_id(void)
370 {
371    /*
372     * XXX: Callers of of this function assume it is a lightweight function.
373     * But unfortunately C11's thrd_current() gives no such guarantees.  In
374     * fact, it's pretty hard to have a compliant implementation of
375     * thrd_current() on Windows with such characteristics.  So for now, we
376     * side-step this mess and use Windows thread primitives directly here.
377     */
378 #ifdef _WIN32
379    return GetCurrentThreadId();
380 #else
381    return thrd_current();
382 #endif
383 }
384 
385 
386 static inline int
util_thread_id_equal(thread_id t1,thread_id t2)387 util_thread_id_equal(thread_id t1, thread_id t2)
388 {
389 #ifdef _WIN32
390    return t1 == t2;
391 #else
392    return thrd_equal(t1, t2);
393 #endif
394 }
395 
396 #endif /* U_THREAD_H_ */
397