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
38 #elif defined(TLS_GNU)
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>
80 #if HAVE_SYS_SYSCALL_H
81 #include <sys/syscall.h>
84 #include <sys/prctl.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 SCMutexIsLocked(mut) (SCMutexTrylock(mut) == EBUSY)
122 #define SCMutexUnlock(mut) pthread_mutex_unlock(mut)
123 #define SCMutexDestroy pthread_mutex_destroy
124 #define SCMUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
127 #define SCRWLock pthread_rwlock_t
128 #define SCRWLockInit(rwl, rwlattr ) pthread_rwlock_init(rwl, rwlattr)
129 #define SCRWLockWRLock(rwl) pthread_rwlock_wrlock(rwl)
130 #define SCRWLockRDLock(rwl) pthread_rwlock_rdlock(rwl)
131 #define SCRWLockTryWRLock(rwl) pthread_rwlock_trywrlock(rwl)
132 #define SCRWLockTryRDLock(rwl) pthread_rwlock_tryrdlock(rwl)
133 #define SCRWLockUnlock(rwl) pthread_rwlock_unlock(rwl)
134 #define SCRWLockDestroy pthread_rwlock_destroy
137 #define SCCondT pthread_cond_t
138 #define SCCondInit pthread_cond_init
139 #define SCCondSignal pthread_cond_signal
140 #define SCCondDestroy pthread_cond_destroy
141 #define SCCondWait(cond, mut) pthread_cond_wait(cond, mut)
144 #define SCCtrlMutex pthread_mutex_t
145 #define SCCtrlMutexAttr pthread_mutexattr_t
146 #define SCCtrlMutexInit(mut, mutattr ) pthread_mutex_init(mut, mutattr)
147 #define SCCtrlMutexLock(mut) pthread_mutex_lock(mut)
148 #define SCCtrlMutexTrylock(mut) pthread_mutex_trylock(mut)
149 #define SCCtrlMutexUnlock(mut) pthread_mutex_unlock(mut)
150 #define SCCtrlMutexDestroy pthread_mutex_destroy
153 #define SCCtrlCondT pthread_cond_t
154 #define SCCtrlCondInit pthread_cond_init
155 #define SCCtrlCondSignal pthread_cond_signal
156 #define SCCtrlCondTimedwait pthread_cond_timedwait
157 #define SCCtrlCondWait pthread_cond_wait
158 #define SCCtrlCondDestroy pthread_cond_destroy
161 #if ((_POSIX_SPIN_LOCKS - 200112L) < 0L) || defined HELGRIND || !defined(HAVE_PTHREAD_SPIN_UNLOCK)
162 #define SCSpinlock SCMutex
163 #define SCSpinLock(spin) SCMutexLock((spin))
164 #define SCSpinTrylock(spin) SCMutexTrylock((spin))
165 #define SCSpinUnlock(spin) SCMutexUnlock((spin))
166 #define SCSpinInit(spin, spin_attr) SCMutexInit((spin), NULL)
167 #define SCSpinDestroy(spin) SCMutexDestroy((spin))
169 #define SCSpinlock pthread_spinlock_t
170 #define SCSpinLock(spin) pthread_spin_lock(spin)
171 #define SCSpinTrylock(spin) pthread_spin_trylock(spin)
172 #define SCSpinUnlock(spin) pthread_spin_unlock(spin)
173 #define SCSpinInit(spin, spin_attr) pthread_spin_init(spin, spin_attr)
174 #define SCSpinDestroy(spin) pthread_spin_destroy(spin)
179 #if (!defined SCMutex || !defined SCMutexAttr || !defined SCMutexInit || \
180 !defined SCMutexLock || !defined SCMutexTrylock || \
181 !defined SCMutexUnlock || !defined SCMutexDestroy || \
182 !defined SCMUTEX_INITIALIZER)
183 #error "Mutex types and/or macro's not properly defined"
185 #if (!defined SCCtrlMutex || !defined SCCtrlMutexAttr || !defined SCCtrlMutexInit || \
186 !defined SCCtrlMutexLock || !defined SCCtrlMutexTrylock || \
187 !defined SCCtrlMutexUnlock || !defined SCCtrlMutexDestroy)
188 #error "SCCtrlMutex types and/or macro's not properly defined"
191 #if (!defined SCSpinlock || !defined SCSpinLock || \
192 !defined SCSpinTrylock || !defined SCSpinUnlock || \
193 !defined SCSpinInit || !defined SCSpinDestroy)
194 #error "Spinlock types and/or macro's not properly defined"
197 #if (!defined SCRWLock || !defined SCRWLockInit || !defined SCRWLockWRLock || \
198 !defined SCRWLockRDLock || !defined SCRWLockTryWRLock || \
199 !defined SCRWLockTryRDLock || !defined SCRWLockUnlock || !defined SCRWLockDestroy)
200 #error "SCRWLock types and/or macro's not properly defined"
203 #if (!defined SCCondT || !defined SCCondInit || !defined SCCondSignal || \
204 !defined SCCondDestroy || !defined SCCondWait)
205 #error "SCCond types and/or macro's not properly defined"
208 #if (!defined SCCtrlCondT || !defined SCCtrlCondInit || !defined SCCtrlCondSignal ||\
209 !defined SCCtrlCondDestroy || !defined SCCtrlCondTimedwait)
210 #error "SCCtrlCond types and/or macro's not properly defined"
215 #include <pthread_np.h>
217 #define SCGetThreadIdLong(...) ({ \
219 thr_self(&tmpthid); \
220 unsigned long _scgetthread_tid = (unsigned long)tmpthid; \
224 #define SCGetThreadIdLong(...) ({ \
227 unsigned long _scgetthread_tid = (unsigned long)tpid; \
231 #define SCGetThreadIdLong(...) ({ \
232 unsigned long _scgetthread_tid = (unsigned long)GetCurrentThreadId(); \
236 #define SCGetThreadIdLong(...) ({ \
237 unsigned long _scgetthread_tid = (unsigned long)GetCurrentThreadId(); \
241 #define SCGetThreadIdLong(...) ({ \
242 thread_port_t tpid; \
243 tpid = mach_thread_self(); \
244 unsigned long _scgetthread_tid = (unsigned long)tpid; \
249 #define SCGetThreadIdLong(...) ({ \
250 thread_t tmpthid = thr_self(); \
251 unsigned long _scgetthread_tid = (unsigned long)tmpthid; \
256 #define SCGetThreadIdLong(...) \
259 tmpthid = (pid_t)syscall(SYS_gettid); \
260 unsigned long _scgetthread_tid = (unsigned long)tmpthid; \
270 #if defined OS_FREEBSD
272 #define SCSetThreadName(n) \
274 char tname[THREAD_NAME_LEN] = ""; \
275 if (strlen(n) > THREAD_NAME_LEN) \
276 SCLogDebug("Thread name is too long, truncating it..."); \
277 strlcpy(tname, n, THREAD_NAME_LEN); \
278 strlcpy(t_thread_name, n, sizeof(t_thread_name)); \
279 pthread_set_name_np(pthread_self(), tname); \
281 #elif defined __OpenBSD__
283 #define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); })
284 #elif defined OS_WIN32
286 #define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); })
287 #elif defined OS_DARWIN
289 #define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); })
290 #elif defined PR_SET_NAME
294 #define SCSetThreadName(n) \
296 char tname[THREAD_NAME_LEN + 1] = ""; \
297 if (strlen(n) > THREAD_NAME_LEN) \
298 SCLogDebug("Thread name is too long, truncating it..."); \
299 strlcpy(tname, n, THREAD_NAME_LEN); \
300 strlcpy(t_thread_name, n, sizeof(t_thread_name)); \
301 if (prctl(PR_SET_NAME, tname, 0, 0, 0) < 0) \
302 SCLogDebug("Error setting thread name \"%s\": %s", tname, strerror(errno)); \
305 #define SCSetThreadName(n) ({ \
306 strlcpy(t_thread_name, n, sizeof(t_thread_name)); \