suricata
threads.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2013 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  * \author Pablo Rincon Crespo <pablo.rincon.crespo@gmail.com>
23  *
24  * Threading functions defined as macros
25  */
26 
27 #ifndef __THREADS_H__
28 #define __THREADS_H__
29 
30 #if HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 
34 /* need this for the _POSIX_SPIN_LOCKS define */
35 #if HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 
39 #ifdef PROFILING
40 #include "util-cpu.h"
41 #ifdef PROFILE_LOCKING
42 #include "util-profiling-locks.h"
43 #endif /* PROFILE_LOCKING */
44 #endif /* PROFILING */
45 
46 #if defined OS_FREEBSD || __OpenBSD__
47 
48 #if ! defined __OpenBSD__
49 #include <sys/thr.h>
50 #endif
51 enum {
52  PRIO_LOW = 2,
53  PRIO_MEDIUM = 0,
54  PRIO_HIGH = -2,
55 };
56 
57 #elif OS_DARWIN
58 
59 #include <mach/mach_init.h>
60 enum {
61  PRIO_LOW = 2,
62  PRIO_MEDIUM = 0,
63  PRIO_HIGH = -2,
64 };
65 
66 #elif OS_WIN32
67 
68 #include <windows.h>
69 enum {
70  PRIO_LOW = THREAD_PRIORITY_LOWEST,
71  PRIO_MEDIUM = THREAD_PRIORITY_NORMAL,
72  PRIO_HIGH = THREAD_PRIORITY_HIGHEST,
73 };
74 
75 #else /* LINUX */
76 
77 #if HAVE_SYS_SYSCALL_H
78 #include <sys/syscall.h>
79 #endif
80 #if HAVE_SYS_PRCTL_H
81 #include <sys/prctl.h>
82 #define THREAD_NAME_LEN 16
83 #endif
84 
85 enum {
86  PRIO_LOW = 2,
88  PRIO_HIGH = -2,
89 };
90 
91 #endif /* OS_FREEBSD */
92 
93 #include <pthread.h>
94 
95 /* The mutex/spinlock/condition definitions and functions are used
96  * in the same way as the POSIX definitions; Anyway we are centralizing
97  * them here to make an easier portability process and debugging process;
98  * Please, make sure you initialize mutex and spinlocks before using them
99  * because, some OS doesn't initialize them for you :)
100  */
101 
102 //#define DBG_THREADS
103 
104 #ifdef __tile__
105  #include "threads-arch-tile.h"
106 #elif defined DBG_THREADS
107  #ifdef PROFILE_LOCKING
108  #error "Cannot mix DBG_THREADS and PROFILE_LOCKING"
109  #endif
110  #include "threads-debug.h"
111 #elif defined PROFILE_LOCKING
112  #include "threads-profile.h"
113 #else /* normal */
114 
115 /* mutex */
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
124 
125 /* rwlocks */
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
134 
135 /* conditions */
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)
141 
142 /* ctrl mutex */
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
150 
151 /* ctrl conditions */
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
158 
159 /* spinlocks */
160 #if ((_POSIX_SPIN_LOCKS - 200112L) < 0L) || defined HELGRIND
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))
167 #else /* no spinlocks */
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)
174 #endif /* no spinlocks */
175 
176 #endif /* __tile__ */
177 
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"
183 #endif
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"
188 #endif
189 
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"
194 #endif
195 
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"
200 #endif
201 
202 #if (!defined SCCondT || !defined SCCondInit || !defined SCCondSignal || \
203  !defined SCCondDestroy || !defined SCCondWait)
204 #error "SCCond types and/or macro's not properly defined"
205 #endif
206 
207 #if (!defined SCCtrlCondT || !defined SCCtrlCondInit || !defined SCCtrlCondSignal ||\
208  !defined SCCtrlCondDestroy || !defined SCCtrlCondTimedwait)
209 #error "SCCtrlCond types and/or macro's not properly defined"
210 #endif
211 
212 /** Get the Current Thread Id */
213 #ifdef OS_FREEBSD
214 #include <pthread_np.h>
215 
216 #define SCGetThreadIdLong(...) ({ \
217  long tmpthid; \
218  thr_self(&tmpthid); \
219  u_long _scgetthread_tid = (u_long)tmpthid; \
220  _scgetthread_tid; \
221 })
222 #elif __OpenBSD__
223 #define SCGetThreadIdLong(...) ({ \
224  pid_t tpid; \
225  tpid = getpid(); \
226  u_long _scgetthread_tid = (u_long)tpid; \
227  _scgetthread_tid; \
228 })
229 #elif __CYGWIN__
230 #define SCGetThreadIdLong(...) ({ \
231  u_long _scgetthread_tid = (u_long)GetCurrentThreadId(); \
232  _scgetthread_tid; \
233 })
234 #elif OS_WIN32
235 #define SCGetThreadIdLong(...) ({ \
236  u_long _scgetthread_tid = (u_long)GetCurrentThreadId(); \
237  _scgetthread_tid; \
238 })
239 #elif OS_DARWIN
240 #define SCGetThreadIdLong(...) ({ \
241  thread_port_t tpid; \
242  tpid = mach_thread_self(); \
243  u_long _scgetthread_tid = (u_long)tpid; \
244  _scgetthread_tid; \
245 })
246 #elif defined(sun)
247 #include <thread.h>
248 #define SCGetThreadIdLong(...) ({ \
249  thread_t tmpthid = thr_self(); \
250  u_long _scgetthread_tid = (u_long)tmpthid; \
251  _scgetthread_tid; \
252 })
253 
254 #else
255 #define SCGetThreadIdLong(...) ({ \
256  pid_t tmpthid; \
257  tmpthid = syscall(SYS_gettid); \
258  u_long _scgetthread_tid = (u_long)tmpthid; \
259  _scgetthread_tid; \
260 })
261 #endif /* OS FREEBSD */
262 
263 /*
264  * OS specific macro's for setting the thread name. "top" can display
265  * this name.
266  */
267 #if defined OS_FREEBSD /* FreeBSD */
268 /** \todo Add implementation for FreeBSD */
269 #define SCSetThreadName(n) ({ \
270  char tname[16] = ""; \
271  if (strlen(n) > 16) \
272  SCLogDebug("Thread name is too long, truncating it..."); \
273  strlcpy(tname, n, 16); \
274  pthread_set_name_np(pthread_self(), tname); \
275  0; \
276 })
277 #elif defined __OpenBSD__ /* OpenBSD */
278 /** \todo Add implementation for OpenBSD */
279 #define SCSetThreadName(n) (0)
280 #elif defined OS_WIN32 /* Windows */
281 /** \todo Add implementation for Windows */
282 #define SCSetThreadName(n) (0)
283 #elif defined OS_DARWIN /* Mac OS X */
284 /** \todo Add implementation for MacOS */
285 #define SCSetThreadName(n) (0)
286 #elif defined PR_SET_NAME /* PR_SET_NAME */
287 /**
288  * \brief Set the threads name
289  */
290 #define SCSetThreadName(n) ({ \
291  char tname[THREAD_NAME_LEN + 1] = ""; \
292  if (strlen(n) > THREAD_NAME_LEN) \
293  SCLogDebug("Thread name is too long, truncating it..."); \
294  strlcpy(tname, n, THREAD_NAME_LEN); \
295  int ret = 0; \
296  if ((ret = prctl(PR_SET_NAME, tname, 0, 0, 0)) < 0) \
297  SCLogDebug("Error setting thread name \"%s\": %s", tname, strerror(errno)); \
298  ret; \
299 })
300 #else
301 #define SCSetThreadName(n) (0)
302 #endif
303 
304 
305 void ThreadMacrosRegisterTests(void);
306 
307 #endif /* __THREADS_H__ */
308 
void ThreadMacrosRegisterTests(void)
this function registers unit tests for DetectId
Definition: threads.c:144