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 && !defined(ANDROID)
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 USE_ELF_TLS
79 #ifdef _MSC_VER
80 #define __THREAD_INITIAL_EXEC __declspec(thread)
81 #elif defined(__GLIBC__)
82 #define __THREAD_INITIAL_EXEC __thread __attribute__((tls_model("initial-exec")))
83 #define REALLY_INITIAL_EXEC
84 #else
85 #define __THREAD_INITIAL_EXEC __thread
86 #endif
87 #endif
88 
89 static inline int
util_get_current_cpu(void)90 util_get_current_cpu(void)
91 {
92 #if DETECT_OS_LINUX && !defined(ANDROID)
93    return sched_getcpu();
94 
95 #elif defined(_WIN32) && !defined(__CYGWIN__) && _WIN32_WINNT >= 0x0600
96    return GetCurrentProcessorNumber();
97 
98 #else
99    return -1;
100 #endif
101 }
102 
u_thread_create(int (* routine)(void *),void * param)103 static inline thrd_t u_thread_create(int (*routine)(void *), void *param)
104 {
105    thrd_t thread;
106 #ifdef HAVE_PTHREAD
107    sigset_t saved_set, new_set;
108    int ret;
109 
110    sigfillset(&new_set);
111    sigdelset(&new_set, SIGSYS);
112    pthread_sigmask(SIG_BLOCK, &new_set, &saved_set);
113    ret = thrd_create( &thread, routine, param );
114    pthread_sigmask(SIG_SETMASK, &saved_set, NULL);
115 #else
116    int ret;
117    ret = thrd_create( &thread, routine, param );
118 #endif
119    if (ret)
120       return 0;
121 
122    return thread;
123 }
124 
u_thread_setname(const char * name)125 static inline void u_thread_setname( const char *name )
126 {
127 #if defined(HAVE_PTHREAD)
128 #if DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS
129    int ret = pthread_setname_np(pthread_self(), name);
130    if (ret == ERANGE) {
131       char buf[16];
132       const size_t len = MIN2(strlen(name), ARRAY_SIZE(buf) - 1);
133       memcpy(buf, name, len);
134       buf[len] = '\0';
135       pthread_setname_np(pthread_self(), buf);
136    }
137 #elif DETECT_OS_FREEBSD || DETECT_OS_OPENBSD || DETECT_OS_DRAGONFLY
138    pthread_set_name_np(pthread_self(), name);
139 #elif DETECT_OS_NETBSD
140    pthread_setname_np(pthread_self(), "%s", (void *)name);
141 #elif DETECT_OS_APPLE
142    pthread_setname_np(name);
143 #elif DETECT_OS_HAIKU
144    rename_thread(find_thread(NULL), name);
145 #else
146 #warning Not sure how to call pthread_setname_np
147 #endif
148 #endif
149    (void)name;
150 }
151 
152 /**
153  * Set thread affinity.
154  *
155  * \param thread         Thread
156  * \param mask           Set this affinity mask
157  * \param old_mask       Previous affinity mask returned if not NULL
158  * \param num_mask_bits  Number of bits in both masks
159  * \return  true on success
160  */
161 static inline bool
util_set_thread_affinity(thrd_t thread,const uint32_t * mask,uint32_t * old_mask,unsigned num_mask_bits)162 util_set_thread_affinity(thrd_t thread,
163                          const uint32_t *mask,
164                          uint32_t *old_mask,
165                          unsigned num_mask_bits)
166 {
167 #if defined(HAVE_PTHREAD_SETAFFINITY)
168    cpu_set_t cpuset;
169 
170    if (old_mask) {
171       if (pthread_getaffinity_np(thread, sizeof(cpuset), &cpuset) != 0)
172          return false;
173 
174       memset(old_mask, 0, num_mask_bits / 8);
175       for (unsigned i = 0; i < num_mask_bits && i < CPU_SETSIZE; i++) {
176          if (CPU_ISSET(i, &cpuset))
177             old_mask[i / 32] |= 1u << (i % 32);
178       }
179    }
180 
181    CPU_ZERO(&cpuset);
182    for (unsigned i = 0; i < num_mask_bits && i < CPU_SETSIZE; i++) {
183       if (mask[i / 32] & (1u << (i % 32)))
184          CPU_SET(i, &cpuset);
185    }
186    return pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset) == 0;
187 
188 #elif defined(_WIN32) && !defined(__CYGWIN__)
189    DWORD_PTR m = mask[0];
190 
191    if (sizeof(m) > 4 && num_mask_bits > 32)
192       m |= (uint64_t)mask[1] << 32;
193 
194    m = SetThreadAffinityMask(thread, m);
195    if (!m)
196       return false;
197 
198    if (old_mask) {
199       memset(old_mask, 0, num_mask_bits / 8);
200 
201       old_mask[0] = m;
202 #ifdef _WIN64
203       old_mask[1] = m >> 32;
204 #endif
205    }
206 
207    return true;
208 #else
209    return false;
210 #endif
211 }
212 
213 static inline bool
util_set_current_thread_affinity(const uint32_t * mask,uint32_t * old_mask,unsigned num_mask_bits)214 util_set_current_thread_affinity(const uint32_t *mask,
215                                  uint32_t *old_mask,
216                                  unsigned num_mask_bits)
217 {
218 #if defined(HAVE_PTHREAD_SETAFFINITY)
219    return util_set_thread_affinity(pthread_self(), mask, old_mask,
220                                    num_mask_bits);
221 
222 #elif defined(_WIN32) && !defined(__CYGWIN__)
223    /* The GetCurrentThreadId() handle is only valid within the current thread. */
224    return util_set_thread_affinity(GetCurrentThread(), mask, old_mask,
225                                    num_mask_bits);
226 
227 #else
228    return false;
229 #endif
230 }
231 
232 
233 /*
234  * Thread statistics.
235  */
236 
237 /* Return the time of a thread's CPU time clock. */
238 static inline int64_t
util_thread_get_time_nano(thrd_t thread)239 util_thread_get_time_nano(thrd_t thread)
240 {
241 #if defined(HAVE_PTHREAD) && !defined(__APPLE__) && !defined(__HAIKU__)
242    struct timespec ts;
243    clockid_t cid;
244 
245    pthread_getcpuclockid(thread, &cid);
246    clock_gettime(cid, &ts);
247    return (int64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
248 #else
249    (void)thread;
250    return 0;
251 #endif
252 }
253 
254 /* Return the time of the current thread's CPU time clock. */
255 static inline int64_t
util_current_thread_get_time_nano(void)256 util_current_thread_get_time_nano(void)
257 {
258 #if defined(HAVE_PTHREAD)
259    return util_thread_get_time_nano(pthread_self());
260 
261 #elif defined(_WIN32) && !defined(__CYGWIN__)
262    /* The GetCurrentThreadId() handle is only valid within the current thread. */
263    return util_thread_get_time_nano(GetCurrentThread());
264 
265 #else
266    return 0;
267 #endif
268 }
269 
u_thread_is_self(thrd_t thread)270 static inline bool u_thread_is_self(thrd_t thread)
271 {
272 #if defined(HAVE_PTHREAD)
273    return pthread_equal(pthread_self(), thread);
274 #endif
275    return false;
276 }
277 
278 /*
279  * util_barrier
280  */
281 
282 #if defined(HAVE_PTHREAD) && !defined(__APPLE__) && !defined(__HAIKU__)
283 
284 typedef pthread_barrier_t util_barrier;
285 
util_barrier_init(util_barrier * barrier,unsigned count)286 static inline void util_barrier_init(util_barrier *barrier, unsigned count)
287 {
288    pthread_barrier_init(barrier, NULL, count);
289 }
290 
util_barrier_destroy(util_barrier * barrier)291 static inline void util_barrier_destroy(util_barrier *barrier)
292 {
293    pthread_barrier_destroy(barrier);
294 }
295 
util_barrier_wait(util_barrier * barrier)296 static inline bool util_barrier_wait(util_barrier *barrier)
297 {
298    return pthread_barrier_wait(barrier) == PTHREAD_BARRIER_SERIAL_THREAD;
299 }
300 
301 
302 #else /* If the OS doesn't have its own, implement barriers using a mutex and a condvar */
303 
304 typedef struct {
305    unsigned count;
306    unsigned waiters;
307    uint64_t sequence;
308    mtx_t mutex;
309    cnd_t condvar;
310 } util_barrier;
311 
util_barrier_init(util_barrier * barrier,unsigned count)312 static inline void util_barrier_init(util_barrier *barrier, unsigned count)
313 {
314    barrier->count = count;
315    barrier->waiters = 0;
316    barrier->sequence = 0;
317    (void) mtx_init(&barrier->mutex, mtx_plain);
318    cnd_init(&barrier->condvar);
319 }
320 
util_barrier_destroy(util_barrier * barrier)321 static inline void util_barrier_destroy(util_barrier *barrier)
322 {
323    assert(barrier->waiters == 0);
324    mtx_destroy(&barrier->mutex);
325    cnd_destroy(&barrier->condvar);
326 }
327 
util_barrier_wait(util_barrier * barrier)328 static inline bool util_barrier_wait(util_barrier *barrier)
329 {
330    mtx_lock(&barrier->mutex);
331 
332    assert(barrier->waiters < barrier->count);
333    barrier->waiters++;
334 
335    if (barrier->waiters < barrier->count) {
336       uint64_t sequence = barrier->sequence;
337 
338       do {
339          cnd_wait(&barrier->condvar, &barrier->mutex);
340       } while (sequence == barrier->sequence);
341    } else {
342       barrier->waiters = 0;
343       barrier->sequence++;
344       cnd_broadcast(&barrier->condvar);
345    }
346 
347    mtx_unlock(&barrier->mutex);
348 
349    return true;
350 }
351 
352 #endif
353 
354 /*
355  * Thread-id's.
356  *
357  * thrd_current() is not portable to windows (or at least not in a desirable
358  * way), so thread_id's provide an alternative mechanism
359  */
360 
361 #ifdef _WIN32
362 typedef DWORD thread_id;
363 #else
364 typedef thrd_t thread_id;
365 #endif
366 
367 static inline thread_id
util_get_thread_id(void)368 util_get_thread_id(void)
369 {
370    /*
371     * XXX: Callers of of this function assume it is a lightweight function.
372     * But unfortunately C11's thrd_current() gives no such guarantees.  In
373     * fact, it's pretty hard to have a compliant implementation of
374     * thrd_current() on Windows with such characteristics.  So for now, we
375     * side-step this mess and use Windows thread primitives directly here.
376     */
377 #ifdef _WIN32
378    return GetCurrentThreadId();
379 #else
380    return thrd_current();
381 #endif
382 }
383 
384 
385 static inline int
util_thread_id_equal(thread_id t1,thread_id t2)386 util_thread_id_equal(thread_id t1, thread_id t2)
387 {
388 #ifdef _WIN32
389    return t1 == t2;
390 #else
391    return thrd_equal(t1, t2);
392 #endif
393 }
394 
395 #endif /* U_THREAD_H_ */
396