31 #define thread_local _Thread_local
32 #elif defined(TLS_GNU)
33 #define thread_local __thread
35 #error "No supported thread local type found"
45 #ifdef PROFILE_LOCKING
50 #if defined OS_FREEBSD || __OpenBSD__
52 #if ! defined __OpenBSD__
63 #include <mach/mach_init.h>
81 #if HAVE_SYS_SYSCALL_H
82 #include <sys/syscall.h>
85 #include <sys/prctl.h>
86 #define THREAD_NAME_LEN 16
108 #if defined DBG_THREADS
109 #ifdef PROFILE_LOCKING
110 #error "Cannot mix DBG_THREADS and PROFILE_LOCKING"
113 #elif defined PROFILE_LOCKING
118 #define SCMutex pthread_mutex_t
119 #define SCMutexAttr pthread_mutexattr_t
120 #define SCMutexInit(mut, mutattr ) pthread_mutex_init(mut, mutattr)
121 #define SCMutexLock(mut) pthread_mutex_lock(mut)
122 #define SCMutexTrylock(mut) pthread_mutex_trylock(mut)
123 #define SCMutexUnlock(mut) pthread_mutex_unlock(mut)
124 #define SCMutexDestroy pthread_mutex_destroy
125 #define SCMUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
128 #define SCRWLock pthread_rwlock_t
129 #define SCRWLockInit(rwl, rwlattr ) pthread_rwlock_init(rwl, rwlattr)
130 #define SCRWLockWRLock(rwl) pthread_rwlock_wrlock(rwl)
131 #define SCRWLockRDLock(rwl) pthread_rwlock_rdlock(rwl)
132 #define SCRWLockTryWRLock(rwl) pthread_rwlock_trywrlock(rwl)
133 #define SCRWLockTryRDLock(rwl) pthread_rwlock_tryrdlock(rwl)
134 #define SCRWLockUnlock(rwl) pthread_rwlock_unlock(rwl)
135 #define SCRWLockDestroy pthread_rwlock_destroy
138 #define SCCondT pthread_cond_t
139 #define SCCondInit pthread_cond_init
140 #define SCCondSignal pthread_cond_signal
141 #define SCCondDestroy pthread_cond_destroy
142 #define SCCondWait(cond, mut) pthread_cond_wait(cond, mut)
145 #define SCCtrlMutex pthread_mutex_t
146 #define SCCtrlMutexAttr pthread_mutexattr_t
147 #define SCCtrlMutexInit(mut, mutattr ) pthread_mutex_init(mut, mutattr)
148 #define SCCtrlMutexLock(mut) pthread_mutex_lock(mut)
149 #define SCCtrlMutexTrylock(mut) pthread_mutex_trylock(mut)
150 #define SCCtrlMutexUnlock(mut) pthread_mutex_unlock(mut)
151 #define SCCtrlMutexDestroy pthread_mutex_destroy
154 #define SCCtrlCondT pthread_cond_t
155 #define SCCtrlCondInit pthread_cond_init
156 #define SCCtrlCondSignal pthread_cond_signal
157 #define SCCtrlCondTimedwait pthread_cond_timedwait
158 #define SCCtrlCondWait pthread_cond_wait
159 #define SCCtrlCondDestroy pthread_cond_destroy
162 #if ((_POSIX_SPIN_LOCKS - 200112L) < 0L) || defined HELGRIND || !defined(HAVE_PTHREAD_SPIN_UNLOCK)
163 #define SCSpinlock SCMutex
164 #define SCSpinLock(spin) SCMutexLock((spin))
165 #define SCSpinTrylock(spin) SCMutexTrylock((spin))
166 #define SCSpinUnlock(spin) SCMutexUnlock((spin))
167 #define SCSpinInit(spin, spin_attr) SCMutexInit((spin), NULL)
168 #define SCSpinDestroy(spin) SCMutexDestroy((spin))
170 #define SCSpinlock pthread_spinlock_t
171 #define SCSpinLock(spin) pthread_spin_lock(spin)
172 #define SCSpinTrylock(spin) pthread_spin_trylock(spin)
173 #define SCSpinUnlock(spin) pthread_spin_unlock(spin)
174 #define SCSpinInit(spin, spin_attr) pthread_spin_init(spin, spin_attr)
175 #define SCSpinDestroy(spin) pthread_spin_destroy(spin)
180 #if (!defined SCMutex || !defined SCMutexAttr || !defined SCMutexInit || \
181 !defined SCMutexLock || !defined SCMutexTrylock || \
182 !defined SCMutexUnlock || !defined SCMutexDestroy || \
183 !defined SCMUTEX_INITIALIZER)
184 #error "Mutex types and/or macro's not properly defined"
186 #if (!defined SCCtrlMutex || !defined SCCtrlMutexAttr || !defined SCCtrlMutexInit || \
187 !defined SCCtrlMutexLock || !defined SCCtrlMutexTrylock || \
188 !defined SCCtrlMutexUnlock || !defined SCCtrlMutexDestroy)
189 #error "SCCtrlMutex types and/or macro's not properly defined"
192 #if (!defined SCSpinlock || !defined SCSpinLock || \
193 !defined SCSpinTrylock || !defined SCSpinUnlock || \
194 !defined SCSpinInit || !defined SCSpinDestroy)
195 #error "Spinlock types and/or macro's not properly defined"
198 #if (!defined SCRWLock || !defined SCRWLockInit || !defined SCRWLockWRLock || \
199 !defined SCRWLockRDLock || !defined SCRWLockTryWRLock || \
200 !defined SCRWLockTryRDLock || !defined SCRWLockUnlock || !defined SCRWLockDestroy)
201 #error "SCRWLock types and/or macro's not properly defined"
204 #if (!defined SCCondT || !defined SCCondInit || !defined SCCondSignal || \
205 !defined SCCondDestroy || !defined SCCondWait)
206 #error "SCCond types and/or macro's not properly defined"
209 #if (!defined SCCtrlCondT || !defined SCCtrlCondInit || !defined SCCtrlCondSignal ||\
210 !defined SCCtrlCondDestroy || !defined SCCtrlCondTimedwait)
211 #error "SCCtrlCond types and/or macro's not properly defined"
216 #include <pthread_np.h>
218 #define SCGetThreadIdLong(...) ({ \
220 thr_self(&tmpthid); \
221 unsigned long _scgetthread_tid = (unsigned long)tmpthid; \
225 #define SCGetThreadIdLong(...) ({ \
228 unsigned long _scgetthread_tid = (unsigned long)tpid; \
232 #define SCGetThreadIdLong(...) ({ \
233 unsigned long _scgetthread_tid = (unsigned long)GetCurrentThreadId(); \
237 #define SCGetThreadIdLong(...) ({ \
238 unsigned long _scgetthread_tid = (unsigned long)GetCurrentThreadId(); \
242 #define SCGetThreadIdLong(...) ({ \
243 thread_port_t tpid; \
244 tpid = mach_thread_self(); \
245 unsigned long _scgetthread_tid = (unsigned long)tpid; \
250 #define SCGetThreadIdLong(...) ({ \
251 thread_t tmpthid = thr_self(); \
252 unsigned long _scgetthread_tid = (unsigned long)tmpthid; \
257 #define SCGetThreadIdLong(...) ({ \
259 tmpthid = syscall(SYS_gettid); \
260 unsigned long _scgetthread_tid = (unsigned long)tmpthid; \
269 #if defined OS_FREEBSD
271 #define SCSetThreadName(n) ({ \
272 char tname[16] = ""; \
273 if (strlen(n) > 16) \
274 SCLogDebug("Thread name is too long, truncating it..."); \
275 strlcpy(tname, n, 16); \
276 pthread_set_name_np(pthread_self(), tname); \
279 #elif defined __OpenBSD__
281 #define SCSetThreadName(n) (0)
282 #elif defined OS_WIN32
284 #define SCSetThreadName(n) (0)
285 #elif defined OS_DARWIN
287 #define SCSetThreadName(n) (0)
288 #elif defined PR_SET_NAME
292 #define SCSetThreadName(n) ({ \
293 char tname[THREAD_NAME_LEN + 1] = ""; \
294 if (strlen(n) > THREAD_NAME_LEN) \
295 SCLogDebug("Thread name is too long, truncating it..."); \
296 strlcpy(tname, n, THREAD_NAME_LEN); \
298 if ((ret = prctl(PR_SET_NAME, tname, 0, 0, 0)) < 0) \
299 SCLogDebug("Error setting thread name \"%s\": %s", tname, strerror(errno)); \
303 #define SCSetThreadName(n) (0)