ac/nir: don't put lod into args if it's zero.
[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
40 static inline thrd_t u_thread_create(int (*routine)(void *), void *param)
41 {
42 thrd_t thread;
43 #ifdef HAVE_PTHREAD
44 sigset_t saved_set, new_set;
45 int ret;
46
47 sigfillset(&new_set);
48 pthread_sigmask(SIG_SETMASK, &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 # if defined(__GNU_LIBRARY__) && defined(__GLIBC__) && defined(__GLIBC_MINOR__) && \
65 (__GLIBC__ >= 3 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 12)) && \
66 defined(__linux__)
67 pthread_setname_np(pthread_self(), name);
68 # endif
69 #endif
70 (void)name;
71 }
72
73 /*
74 * Thread statistics.
75 */
76
77 /* Return the time of a thread's CPU time clock. */
78 static inline int64_t
79 u_thread_get_time_nano(thrd_t thread)
80 {
81 #if defined(__linux__) && defined(HAVE_PTHREAD)
82 struct timespec ts;
83 clockid_t cid;
84
85 pthread_getcpuclockid(thread, &cid);
86 clock_gettime(cid, &ts);
87 return (int64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
88 #else
89 return 0;
90 #endif
91 }
92
93 static inline bool u_thread_is_self(thrd_t thread)
94 {
95 #if defined(HAVE_PTHREAD)
96 # if defined(__GNU_LIBRARY__) && defined(__GLIBC__) && defined(__GLIBC_MINOR__) && \
97 (__GLIBC__ >= 3 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 12))
98 return pthread_equal(pthread_self(), thread);
99 # endif
100 #endif
101 return false;
102 }
103
104 /*
105 * util_barrier
106 */
107
108 #if defined(HAVE_PTHREAD) && !defined(__APPLE__)
109
110 typedef pthread_barrier_t util_barrier;
111
112 static inline void util_barrier_init(util_barrier *barrier, unsigned count)
113 {
114 pthread_barrier_init(barrier, NULL, count);
115 }
116
117 static inline void util_barrier_destroy(util_barrier *barrier)
118 {
119 pthread_barrier_destroy(barrier);
120 }
121
122 static inline void util_barrier_wait(util_barrier *barrier)
123 {
124 pthread_barrier_wait(barrier);
125 }
126
127
128 #else /* If the OS doesn't have its own, implement barriers using a mutex and a condvar */
129
130 typedef struct {
131 unsigned count;
132 unsigned waiters;
133 uint64_t sequence;
134 mtx_t mutex;
135 cnd_t condvar;
136 } util_barrier;
137
138 static inline void util_barrier_init(util_barrier *barrier, unsigned count)
139 {
140 barrier->count = count;
141 barrier->waiters = 0;
142 barrier->sequence = 0;
143 (void) mtx_init(&barrier->mutex, mtx_plain);
144 cnd_init(&barrier->condvar);
145 }
146
147 static inline void util_barrier_destroy(util_barrier *barrier)
148 {
149 assert(barrier->waiters == 0);
150 mtx_destroy(&barrier->mutex);
151 cnd_destroy(&barrier->condvar);
152 }
153
154 static inline void util_barrier_wait(util_barrier *barrier)
155 {
156 mtx_lock(&barrier->mutex);
157
158 assert(barrier->waiters < barrier->count);
159 barrier->waiters++;
160
161 if (barrier->waiters < barrier->count) {
162 uint64_t sequence = barrier->sequence;
163
164 do {
165 cnd_wait(&barrier->condvar, &barrier->mutex);
166 } while (sequence == barrier->sequence);
167 } else {
168 barrier->waiters = 0;
169 barrier->sequence++;
170 cnd_broadcast(&barrier->condvar);
171 }
172
173 mtx_unlock(&barrier->mutex);
174 }
175
176 #endif
177
178 #endif /* U_THREAD_H_ */