27#ifndef SURICATA_THREADS_H
28#define SURICATA_THREADS_H
32#ifndef THREAD_NAME_LEN
33#define THREAD_NAME_LEN 16
37#define thread_local _Thread_local
39#define thread_local __thread
41#error "No supported thread local type found"
49#if defined OS_FREEBSD || __OpenBSD__
51#if ! defined __OpenBSD__
62#include <mach/mach_init.h>
81#include <sys/syscall.h>
106#if defined DBG_THREADS
107 #ifdef PROFILE_LOCKING
108 #error "Cannot mix DBG_THREADS and PROFILE_LOCKING"
111#elif defined PROFILE_LOCKING
116#define SCMutex pthread_mutex_t
117#define SCMutexAttr pthread_mutexattr_t
118#define SCMutexInit(mut, mutattr ) pthread_mutex_init(mut, mutattr)
119#define SCMutexLock(mut) pthread_mutex_lock(mut)
120#define SCMutexTrylock(mut) pthread_mutex_trylock(mut)
121#define SCMutexUnlock(mut) pthread_mutex_unlock(mut)
122#define SCMutexDestroy pthread_mutex_destroy
123#define SCMUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
126#define SCRWLock pthread_rwlock_t
127#define SCRWLockInit(rwl, rwlattr ) pthread_rwlock_init(rwl, rwlattr)
128#define SCRWLockWRLock(rwl) pthread_rwlock_wrlock(rwl)
129#define SCRWLockRDLock(rwl) pthread_rwlock_rdlock(rwl)
130#define SCRWLockTryWRLock(rwl) pthread_rwlock_trywrlock(rwl)
131#define SCRWLockTryRDLock(rwl) pthread_rwlock_tryrdlock(rwl)
132#define SCRWLockUnlock(rwl) pthread_rwlock_unlock(rwl)
133#define SCRWLockDestroy pthread_rwlock_destroy
136#define SCCondT pthread_cond_t
137#define SCCondInit pthread_cond_init
138#define SCCondSignal pthread_cond_signal
139#define SCCondDestroy pthread_cond_destroy
140#define SCCondWait(cond, mut) pthread_cond_wait(cond, mut)
143#define SCCtrlMutex pthread_mutex_t
144#define SCCtrlMutexAttr pthread_mutexattr_t
145#define SCCtrlMutexInit(mut, mutattr ) pthread_mutex_init(mut, mutattr)
146#define SCCtrlMutexLock(mut) pthread_mutex_lock(mut)
147#define SCCtrlMutexTrylock(mut) pthread_mutex_trylock(mut)
148#define SCCtrlMutexUnlock(mut) pthread_mutex_unlock(mut)
149#define SCCtrlMutexDestroy pthread_mutex_destroy
152#define SCCtrlCondT pthread_cond_t
153#define SCCtrlCondInit pthread_cond_init
154#define SCCtrlCondSignal pthread_cond_signal
155#define SCCtrlCondTimedwait pthread_cond_timedwait
156#define SCCtrlCondWait pthread_cond_wait
157#define SCCtrlCondDestroy pthread_cond_destroy
160#if ((_POSIX_SPIN_LOCKS - 200112L) < 0L) || defined HELGRIND || !defined(HAVE_PTHREAD_SPIN_UNLOCK)
161#define SCSpinlock SCMutex
162#define SCSpinLock(spin) SCMutexLock((spin))
163#define SCSpinTrylock(spin) SCMutexTrylock((spin))
164#define SCSpinUnlock(spin) SCMutexUnlock((spin))
165#define SCSpinInit(spin, spin_attr) SCMutexInit((spin), NULL)
166#define SCSpinDestroy(spin) SCMutexDestroy((spin))
168#define SCSpinlock pthread_spinlock_t
169#define SCSpinLock(spin) pthread_spin_lock(spin)
170#define SCSpinTrylock(spin) pthread_spin_trylock(spin)
171#define SCSpinUnlock(spin) pthread_spin_unlock(spin)
172#define SCSpinInit(spin, spin_attr) pthread_spin_init(spin, spin_attr)
173#define SCSpinDestroy(spin) pthread_spin_destroy(spin)
178#if (!defined SCMutex || !defined SCMutexAttr || !defined SCMutexInit || \
179 !defined SCMutexLock || !defined SCMutexTrylock || \
180 !defined SCMutexUnlock || !defined SCMutexDestroy || \
181 !defined SCMUTEX_INITIALIZER)
182#error "Mutex types and/or macro's not properly defined"
184#if (!defined SCCtrlMutex || !defined SCCtrlMutexAttr || !defined SCCtrlMutexInit || \
185 !defined SCCtrlMutexLock || !defined SCCtrlMutexTrylock || \
186 !defined SCCtrlMutexUnlock || !defined SCCtrlMutexDestroy)
187#error "SCCtrlMutex types and/or macro's not properly defined"
190#if (!defined SCSpinlock || !defined SCSpinLock || \
191 !defined SCSpinTrylock || !defined SCSpinUnlock || \
192 !defined SCSpinInit || !defined SCSpinDestroy)
193#error "Spinlock types and/or macro's not properly defined"
196#if (!defined SCRWLock || !defined SCRWLockInit || !defined SCRWLockWRLock || \
197 !defined SCRWLockRDLock || !defined SCRWLockTryWRLock || \
198 !defined SCRWLockTryRDLock || !defined SCRWLockUnlock || !defined SCRWLockDestroy)
199#error "SCRWLock types and/or macro's not properly defined"
202#if (!defined SCCondT || !defined SCCondInit || !defined SCCondSignal || \
203 !defined SCCondDestroy || !defined SCCondWait)
204#error "SCCond types and/or macro's not properly defined"
207#if (!defined SCCtrlCondT || !defined SCCtrlCondInit || !defined SCCtrlCondSignal ||\
208 !defined SCCtrlCondDestroy || !defined SCCtrlCondTimedwait)
209#error "SCCtrlCond types and/or macro's not properly defined"
214#include <pthread_np.h>
216#define SCGetThreadIdLong(...) ({ \
218 thr_self(&tmpthid); \
219 unsigned long _scgetthread_tid = (unsigned long)tmpthid; \
223#define SCGetThreadIdLong(...) ({ \
226 unsigned long _scgetthread_tid = (unsigned long)tpid; \
230#define SCGetThreadIdLong(...) ({ \
231 unsigned long _scgetthread_tid = (unsigned long)GetCurrentThreadId(); \
235#define SCGetThreadIdLong(...) ({ \
236 unsigned long _scgetthread_tid = (unsigned long)GetCurrentThreadId(); \
240#define SCGetThreadIdLong(...) ({ \
241 thread_port_t tpid; \
242 tpid = mach_thread_self(); \
243 unsigned long _scgetthread_tid = (unsigned long)tpid; \
248#define SCGetThreadIdLong(...) ({ \
249 thread_t tmpthid = thr_self(); \
250 unsigned long _scgetthread_tid = (unsigned long)tmpthid; \
255#define SCGetThreadIdLong(...) \
258 tmpthid = (pid_t)syscall(SYS_gettid); \
259 unsigned long _scgetthread_tid = (unsigned long)tmpthid; \
269#if defined OS_FREEBSD
271#define SCSetThreadName(n) \
273 char tname[THREAD_NAME_LEN] = ""; \
274 if (strlen(n) > THREAD_NAME_LEN) \
275 SCLogDebug("Thread name is too long, truncating it..."); \
276 strlcpy(tname, n, THREAD_NAME_LEN); \
277 strlcpy(t_thread_name, n, sizeof(t_thread_name)); \
278 pthread_set_name_np(pthread_self(), tname); \
280#elif defined __OpenBSD__
282#define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); })
283#elif defined OS_WIN32
285#define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); })
286#elif defined OS_DARWIN
288#define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); })
289#elif defined PR_SET_NAME
293#define SCSetThreadName(n) \
295 char tname[THREAD_NAME_LEN + 1] = ""; \
296 if (strlen(n) > THREAD_NAME_LEN) \
297 SCLogDebug("Thread name is too long, truncating it..."); \
298 strlcpy(tname, n, THREAD_NAME_LEN); \
299 strlcpy(t_thread_name, n, sizeof(t_thread_name)); \
300 if (prctl(PR_SET_NAME, tname, 0, 0, 0) < 0) \
301 SCLogDebug("Error setting thread name \"%s\": %s", tname, strerror(errno)); \
304#define SCSetThreadName(n) ({ \
305 strlcpy(t_thread_name, n, sizeof(t_thread_name)); \
thread_local char t_thread_name[THREAD_NAME_LEN+1]
void ThreadMacrosRegisterTests(void)
this function registers unit tests for DetectId