1 /**************************************************************************
3 * Copyright 1999-2006 Brian Paul
4 * Copyright 2008 VMware, Inc.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
25 **************************************************************************/
33 #include "c11/threads.h"
34 #include "detect_os.h"
38 #ifdef PTHREAD_SETAFFINITY_IN_NP_HEADER
39 #include <pthread_np.h>
44 #define cpu_set_t cpuset_t
47 static inline thrd_t
u_thread_create(int (*routine
)(void *), void *param
)
51 sigset_t saved_set
, new_set
;
55 sigdelset(&new_set
, SIGSYS
);
56 pthread_sigmask(SIG_BLOCK
, &new_set
, &saved_set
);
57 ret
= thrd_create( &thread
, routine
, param
);
58 pthread_sigmask(SIG_SETMASK
, &saved_set
, NULL
);
61 ret
= thrd_create( &thread
, routine
, param
);
69 static inline void u_thread_setname( const char *name
)
71 #if defined(HAVE_PTHREAD)
72 #if DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS
73 pthread_setname_np(pthread_self(), name
);
74 #elif DETECT_OS_FREEBSD || DETECT_OS_OPENBSD
75 pthread_set_name_np(pthread_self(), name
);
76 #elif DETECT_OS_NETBSD
77 pthread_setname_np(pthread_self(), "%s", (void *)name
);
79 pthread_setname_np(name
);
81 #error Not sure how to call pthread_setname_np
88 * An AMD Zen CPU consists of multiple modules where each module has its own L3
89 * cache. Inter-thread communication such as locks and atomics between modules
90 * is very expensive. It's desirable to pin a group of closely cooperating
91 * threads to one group of cores sharing L3.
93 * \param thread thread
94 * \param L3_index index of the L3 cache
95 * \param cores_per_L3 number of CPU cores shared by one L3
98 util_pin_thread_to_L3(thrd_t thread
, unsigned L3_index
, unsigned cores_per_L3
)
100 #if defined(HAVE_PTHREAD_SETAFFINITY)
104 for (unsigned i
= 0; i
< cores_per_L3
; i
++)
105 CPU_SET(L3_index
* cores_per_L3
+ i
, &cpuset
);
106 pthread_setaffinity_np(thread
, sizeof(cpuset
), &cpuset
);
111 * Return the index of L3 that the thread is pinned to. If the thread is
112 * pinned to multiple L3 caches, return -1.
114 * \param thread thread
115 * \param cores_per_L3 number of CPU cores shared by one L3
118 util_get_L3_for_pinned_thread(thrd_t thread
, unsigned cores_per_L3
)
120 #if defined(HAVE_PTHREAD_SETAFFINITY)
123 if (pthread_getaffinity_np(thread
, sizeof(cpuset
), &cpuset
) == 0) {
126 for (unsigned i
= 0; i
< CPU_SETSIZE
; i
++) {
127 if (CPU_ISSET(i
, &cpuset
)) {
128 int x
= i
/ cores_per_L3
;
134 return -1; /* multiple L3s are set */
148 /* Return the time of a thread's CPU time clock. */
149 static inline int64_t
150 u_thread_get_time_nano(thrd_t thread
)
152 #if defined(__linux__) && defined(HAVE_PTHREAD)
156 pthread_getcpuclockid(thread
, &cid
);
157 clock_gettime(cid
, &ts
);
158 return (int64_t)ts
.tv_sec
* 1000000000 + ts
.tv_nsec
;
164 static inline bool u_thread_is_self(thrd_t thread
)
166 #if defined(HAVE_PTHREAD)
167 return pthread_equal(pthread_self(), thread
);
176 #if defined(HAVE_PTHREAD) && !defined(__APPLE__)
178 typedef pthread_barrier_t util_barrier
;
180 static inline void util_barrier_init(util_barrier
*barrier
, unsigned count
)
182 pthread_barrier_init(barrier
, NULL
, count
);
185 static inline void util_barrier_destroy(util_barrier
*barrier
)
187 pthread_barrier_destroy(barrier
);
190 static inline void util_barrier_wait(util_barrier
*barrier
)
192 pthread_barrier_wait(barrier
);
196 #else /* If the OS doesn't have its own, implement barriers using a mutex and a condvar */
206 static inline void util_barrier_init(util_barrier
*barrier
, unsigned count
)
208 barrier
->count
= count
;
209 barrier
->waiters
= 0;
210 barrier
->sequence
= 0;
211 (void) mtx_init(&barrier
->mutex
, mtx_plain
);
212 cnd_init(&barrier
->condvar
);
215 static inline void util_barrier_destroy(util_barrier
*barrier
)
217 assert(barrier
->waiters
== 0);
218 mtx_destroy(&barrier
->mutex
);
219 cnd_destroy(&barrier
->condvar
);
222 static inline void util_barrier_wait(util_barrier
*barrier
)
224 mtx_lock(&barrier
->mutex
);
226 assert(barrier
->waiters
< barrier
->count
);
229 if (barrier
->waiters
< barrier
->count
) {
230 uint64_t sequence
= barrier
->sequence
;
233 cnd_wait(&barrier
->condvar
, &barrier
->mutex
);
234 } while (sequence
== barrier
->sequence
);
236 barrier
->waiters
= 0;
238 cnd_broadcast(&barrier
->condvar
);
241 mtx_unlock(&barrier
->mutex
);
246 #endif /* U_THREAD_H_ */