30 #ifndef THREAD_NAME_LEN
31 #define THREAD_NAME_LEN 16
35 #define thread_local _Thread_local
36 #elif defined(TLS_GNU)
37 #define thread_local __thread
39 #error "No supported thread local type found"
49 #ifdef PROFILE_LOCKING
54 #if defined OS_FREEBSD || __OpenBSD__
56 #if ! defined __OpenBSD__
67 #include <mach/mach_init.h>
85 #if HAVE_SYS_SYSCALL_H
86 #include <sys/syscall.h>
89 #include <sys/prctl.h>
111 #if defined DBG_THREADS
112 #ifdef PROFILE_LOCKING
113 #error "Cannot mix DBG_THREADS and PROFILE_LOCKING"
116 #elif defined PROFILE_LOCKING
121 #define SCMutex pthread_mutex_t
122 #define SCMutexAttr pthread_mutexattr_t
123 #define SCMutexInit(mut, mutattr ) pthread_mutex_init(mut, mutattr)
124 #define SCMutexLock(mut) pthread_mutex_lock(mut)
125 #define SCMutexTrylock(mut) pthread_mutex_trylock(mut)
126 #define SCMutexUnlock(mut) pthread_mutex_unlock(mut)
127 #define SCMutexDestroy pthread_mutex_destroy
128 #define SCMUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
131 #define SCRWLock pthread_rwlock_t
132 #define SCRWLockInit(rwl, rwlattr ) pthread_rwlock_init(rwl, rwlattr)
133 #define SCRWLockWRLock(rwl) pthread_rwlock_wrlock(rwl)
134 #define SCRWLockRDLock(rwl) pthread_rwlock_rdlock(rwl)
135 #define SCRWLockTryWRLock(rwl) pthread_rwlock_trywrlock(rwl)
136 #define SCRWLockTryRDLock(rwl) pthread_rwlock_tryrdlock(rwl)
137 #define SCRWLockUnlock(rwl) pthread_rwlock_unlock(rwl)
138 #define SCRWLockDestroy pthread_rwlock_destroy
141 #define SCCondT pthread_cond_t
142 #define SCCondInit pthread_cond_init
143 #define SCCondSignal pthread_cond_signal
144 #define SCCondDestroy pthread_cond_destroy
145 #define SCCondWait(cond, mut) pthread_cond_wait(cond, mut)
148 #define SCCtrlMutex pthread_mutex_t
149 #define SCCtrlMutexAttr pthread_mutexattr_t
150 #define SCCtrlMutexInit(mut, mutattr ) pthread_mutex_init(mut, mutattr)
151 #define SCCtrlMutexLock(mut) pthread_mutex_lock(mut)
152 #define SCCtrlMutexTrylock(mut) pthread_mutex_trylock(mut)
153 #define SCCtrlMutexUnlock(mut) pthread_mutex_unlock(mut)
154 #define SCCtrlMutexDestroy pthread_mutex_destroy
157 #define SCCtrlCondT pthread_cond_t
158 #define SCCtrlCondInit pthread_cond_init
159 #define SCCtrlCondSignal pthread_cond_signal
160 #define SCCtrlCondTimedwait pthread_cond_timedwait
161 #define SCCtrlCondWait pthread_cond_wait
162 #define SCCtrlCondDestroy pthread_cond_destroy
165 #if ((_POSIX_SPIN_LOCKS - 200112L) < 0L) || defined HELGRIND || !defined(HAVE_PTHREAD_SPIN_UNLOCK)
166 #define SCSpinlock SCMutex
167 #define SCSpinLock(spin) SCMutexLock((spin))
168 #define SCSpinTrylock(spin) SCMutexTrylock((spin))
169 #define SCSpinUnlock(spin) SCMutexUnlock((spin))
170 #define SCSpinInit(spin, spin_attr) SCMutexInit((spin), NULL)
171 #define SCSpinDestroy(spin) SCMutexDestroy((spin))
173 #define SCSpinlock pthread_spinlock_t
174 #define SCSpinLock(spin) pthread_spin_lock(spin)
175 #define SCSpinTrylock(spin) pthread_spin_trylock(spin)
176 #define SCSpinUnlock(spin) pthread_spin_unlock(spin)
177 #define SCSpinInit(spin, spin_attr) pthread_spin_init(spin, spin_attr)
178 #define SCSpinDestroy(spin) pthread_spin_destroy(spin)
183 #if (!defined SCMutex || !defined SCMutexAttr || !defined SCMutexInit || \
184 !defined SCMutexLock || !defined SCMutexTrylock || \
185 !defined SCMutexUnlock || !defined SCMutexDestroy || \
186 !defined SCMUTEX_INITIALIZER)
187 #error "Mutex types and/or macro's not properly defined"
189 #if (!defined SCCtrlMutex || !defined SCCtrlMutexAttr || !defined SCCtrlMutexInit || \
190 !defined SCCtrlMutexLock || !defined SCCtrlMutexTrylock || \
191 !defined SCCtrlMutexUnlock || !defined SCCtrlMutexDestroy)
192 #error "SCCtrlMutex types and/or macro's not properly defined"
195 #if (!defined SCSpinlock || !defined SCSpinLock || \
196 !defined SCSpinTrylock || !defined SCSpinUnlock || \
197 !defined SCSpinInit || !defined SCSpinDestroy)
198 #error "Spinlock types and/or macro's not properly defined"
201 #if (!defined SCRWLock || !defined SCRWLockInit || !defined SCRWLockWRLock || \
202 !defined SCRWLockRDLock || !defined SCRWLockTryWRLock || \
203 !defined SCRWLockTryRDLock || !defined SCRWLockUnlock || !defined SCRWLockDestroy)
204 #error "SCRWLock types and/or macro's not properly defined"
207 #if (!defined SCCondT || !defined SCCondInit || !defined SCCondSignal || \
208 !defined SCCondDestroy || !defined SCCondWait)
209 #error "SCCond types and/or macro's not properly defined"
212 #if (!defined SCCtrlCondT || !defined SCCtrlCondInit || !defined SCCtrlCondSignal ||\
213 !defined SCCtrlCondDestroy || !defined SCCtrlCondTimedwait)
214 #error "SCCtrlCond types and/or macro's not properly defined"
219 #include <pthread_np.h>
221 #define SCGetThreadIdLong(...) ({ \
223 thr_self(&tmpthid); \
224 unsigned long _scgetthread_tid = (unsigned long)tmpthid; \
228 #define SCGetThreadIdLong(...) ({ \
231 unsigned long _scgetthread_tid = (unsigned long)tpid; \
235 #define SCGetThreadIdLong(...) ({ \
236 unsigned long _scgetthread_tid = (unsigned long)GetCurrentThreadId(); \
240 #define SCGetThreadIdLong(...) ({ \
241 unsigned long _scgetthread_tid = (unsigned long)GetCurrentThreadId(); \
245 #define SCGetThreadIdLong(...) ({ \
246 thread_port_t tpid; \
247 tpid = mach_thread_self(); \
248 unsigned long _scgetthread_tid = (unsigned long)tpid; \
253 #define SCGetThreadIdLong(...) ({ \
254 thread_t tmpthid = thr_self(); \
255 unsigned long _scgetthread_tid = (unsigned long)tmpthid; \
260 #define SCGetThreadIdLong(...) ({ \
262 tmpthid = syscall(SYS_gettid); \
263 unsigned long _scgetthread_tid = (unsigned long)tmpthid; \
273 #if defined OS_FREEBSD
275 #define SCSetThreadName(n) \
277 char tname[THREAD_NAME_LEN] = ""; \
278 if (strlen(n) > THREAD_NAME_LEN) \
279 SCLogDebug("Thread name is too long, truncating it..."); \
280 strlcpy(tname, n, THREAD_NAME_LEN); \
281 strlcpy(t_thread_name, n, sizeof(t_thread_name)); \
282 pthread_set_name_np(pthread_self(), tname); \
284 #elif defined __OpenBSD__
286 #define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); })
287 #elif defined OS_WIN32
289 #define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); })
290 #elif defined OS_DARWIN
292 #define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); })
293 #elif defined PR_SET_NAME
297 #define SCSetThreadName(n) \
299 char tname[THREAD_NAME_LEN + 1] = ""; \
300 if (strlen(n) > THREAD_NAME_LEN) \
301 SCLogDebug("Thread name is too long, truncating it..."); \
302 strlcpy(tname, n, THREAD_NAME_LEN); \
303 strlcpy(t_thread_name, n, sizeof(t_thread_name)); \
304 if (prctl(PR_SET_NAME, tname, 0, 0, 0) < 0) \
305 SCLogDebug("Error setting thread name \"%s\": %s", tname, strerror(errno)); \
308 #define SCSetThreadName(n) ({ \
309 strlcpy(t_thread_name, n, sizeof(t_thread_name)); \