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 #if defined DBG_THREADS
105  #ifdef PROFILE_LOCKING
106  #error "Cannot mix DBG_THREADS and PROFILE_LOCKING"
107  #endif
108  #include "threads-debug.h"
109 #elif defined PROFILE_LOCKING
110  #include "threads-profile.h"
111 #else /* normal */
112 
113 /* mutex */
114 #define SCMutex pthread_mutex_t
115 #define SCMutexAttr pthread_mutexattr_t
116 #define SCMutexInit(mut, mutattr ) pthread_mutex_init(mut, mutattr)
117 #define SCMutexLock(mut) pthread_mutex_lock(mut)
118 #define SCMutexTrylock(mut) pthread_mutex_trylock(mut)
119 #define SCMutexUnlock(mut) pthread_mutex_unlock(mut)
120 #define SCMutexDestroy pthread_mutex_destroy
121 #define SCMUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
122 
123 /* rwlocks */
124 #define SCRWLock pthread_rwlock_t
125 #define SCRWLockInit(rwl, rwlattr ) pthread_rwlock_init(rwl, rwlattr)
126 #define SCRWLockWRLock(rwl) pthread_rwlock_wrlock(rwl)
127 #define SCRWLockRDLock(rwl) pthread_rwlock_rdlock(rwl)
128 #define SCRWLockTryWRLock(rwl) pthread_rwlock_trywrlock(rwl)
129 #define SCRWLockTryRDLock(rwl) pthread_rwlock_tryrdlock(rwl)
130 #define SCRWLockUnlock(rwl) pthread_rwlock_unlock(rwl)
131 #define SCRWLockDestroy pthread_rwlock_destroy
132 
133 /* conditions */
134 #define SCCondT pthread_cond_t
135 #define SCCondInit pthread_cond_init
136 #define SCCondSignal pthread_cond_signal
137 #define SCCondDestroy pthread_cond_destroy
138 #define SCCondWait(cond, mut) pthread_cond_wait(cond, mut)
139 
140 /* ctrl mutex */
141 #define SCCtrlMutex pthread_mutex_t
142 #define SCCtrlMutexAttr pthread_mutexattr_t
143 #define SCCtrlMutexInit(mut, mutattr ) pthread_mutex_init(mut, mutattr)
144 #define SCCtrlMutexLock(mut) pthread_mutex_lock(mut)
145 #define SCCtrlMutexTrylock(mut) pthread_mutex_trylock(mut)
146 #define SCCtrlMutexUnlock(mut) pthread_mutex_unlock(mut)
147 #define SCCtrlMutexDestroy pthread_mutex_destroy
148 
149 /* ctrl conditions */
150 #define SCCtrlCondT pthread_cond_t
151 #define SCCtrlCondInit pthread_cond_init
152 #define SCCtrlCondSignal pthread_cond_signal
153 #define SCCtrlCondTimedwait pthread_cond_timedwait
154 #define SCCtrlCondWait pthread_cond_wait
155 #define SCCtrlCondDestroy pthread_cond_destroy
156 
157 /* spinlocks */
158 #if ((_POSIX_SPIN_LOCKS - 200112L) < 0L) || defined HELGRIND
159 #define SCSpinlock SCMutex
160 #define SCSpinLock(spin) SCMutexLock((spin))
161 #define SCSpinTrylock(spin) SCMutexTrylock((spin))
162 #define SCSpinUnlock(spin) SCMutexUnlock((spin))
163 #define SCSpinInit(spin, spin_attr) SCMutexInit((spin), NULL)
164 #define SCSpinDestroy(spin) SCMutexDestroy((spin))
165 #else /* no spinlocks */
166 #define SCSpinlock pthread_spinlock_t
167 #define SCSpinLock(spin) pthread_spin_lock(spin)
168 #define SCSpinTrylock(spin) pthread_spin_trylock(spin)
169 #define SCSpinUnlock(spin) pthread_spin_unlock(spin)
170 #define SCSpinInit(spin, spin_attr) pthread_spin_init(spin, spin_attr)
171 #define SCSpinDestroy(spin) pthread_spin_destroy(spin)
172 #endif /* no spinlocks */
173 
174 #endif
175 
176 #if (!defined SCMutex || !defined SCMutexAttr || !defined SCMutexInit || \
177  !defined SCMutexLock || !defined SCMutexTrylock || \
178  !defined SCMutexUnlock || !defined SCMutexDestroy || \
179  !defined SCMUTEX_INITIALIZER)
180 #error "Mutex types and/or macro's not properly defined"
181 #endif
182 #if (!defined SCCtrlMutex || !defined SCCtrlMutexAttr || !defined SCCtrlMutexInit || \
183  !defined SCCtrlMutexLock || !defined SCCtrlMutexTrylock || \
184  !defined SCCtrlMutexUnlock || !defined SCCtrlMutexDestroy)
185 #error "SCCtrlMutex types and/or macro's not properly defined"
186 #endif
187 
188 #if (!defined SCSpinlock || !defined SCSpinLock || \
189  !defined SCSpinTrylock || !defined SCSpinUnlock || \
190  !defined SCSpinInit || !defined SCSpinDestroy)
191 #error "Spinlock types and/or macro's not properly defined"
192 #endif
193 
194 #if (!defined SCRWLock || !defined SCRWLockInit || !defined SCRWLockWRLock || \
195  !defined SCRWLockRDLock || !defined SCRWLockTryWRLock || \
196  !defined SCRWLockTryRDLock || !defined SCRWLockUnlock || !defined SCRWLockDestroy)
197 #error "SCRWLock types and/or macro's not properly defined"
198 #endif
199 
200 #if (!defined SCCondT || !defined SCCondInit || !defined SCCondSignal || \
201  !defined SCCondDestroy || !defined SCCondWait)
202 #error "SCCond types and/or macro's not properly defined"
203 #endif
204 
205 #if (!defined SCCtrlCondT || !defined SCCtrlCondInit || !defined SCCtrlCondSignal ||\
206  !defined SCCtrlCondDestroy || !defined SCCtrlCondTimedwait)
207 #error "SCCtrlCond types and/or macro's not properly defined"
208 #endif
209 
210 /** Get the Current Thread Id */
211 #ifdef OS_FREEBSD
212 #include <pthread_np.h>
213 
214 #define SCGetThreadIdLong(...) ({ \
215  long tmpthid; \
216  thr_self(&tmpthid); \
217  u_long _scgetthread_tid = (u_long)tmpthid; \
218  _scgetthread_tid; \
219 })
220 #elif __OpenBSD__
221 #define SCGetThreadIdLong(...) ({ \
222  pid_t tpid; \
223  tpid = getpid(); \
224  u_long _scgetthread_tid = (u_long)tpid; \
225  _scgetthread_tid; \
226 })
227 #elif __CYGWIN__
228 #define SCGetThreadIdLong(...) ({ \
229  u_long _scgetthread_tid = (u_long)GetCurrentThreadId(); \
230  _scgetthread_tid; \
231 })
232 #elif OS_WIN32
233 #define SCGetThreadIdLong(...) ({ \
234  u_long _scgetthread_tid = (u_long)GetCurrentThreadId(); \
235  _scgetthread_tid; \
236 })
237 #elif OS_DARWIN
238 #define SCGetThreadIdLong(...) ({ \
239  thread_port_t tpid; \
240  tpid = mach_thread_self(); \
241  u_long _scgetthread_tid = (u_long)tpid; \
242  _scgetthread_tid; \
243 })
244 #elif defined(sun)
245 #include <thread.h>
246 #define SCGetThreadIdLong(...) ({ \
247  thread_t tmpthid = thr_self(); \
248  u_long _scgetthread_tid = (u_long)tmpthid; \
249  _scgetthread_tid; \
250 })
251 
252 #else
253 #define SCGetThreadIdLong(...) ({ \
254  pid_t tmpthid; \
255  tmpthid = syscall(SYS_gettid); \
256  u_long _scgetthread_tid = (u_long)tmpthid; \
257  _scgetthread_tid; \
258 })
259 #endif /* OS FREEBSD */
260 
261 /*
262  * OS specific macro's for setting the thread name. "top" can display
263  * this name.
264  */
265 #if defined OS_FREEBSD /* FreeBSD */
266 /** \todo Add implementation for FreeBSD */
267 #define SCSetThreadName(n) ({ \
268  char tname[16] = ""; \
269  if (strlen(n) > 16) \
270  SCLogDebug("Thread name is too long, truncating it..."); \
271  strlcpy(tname, n, 16); \
272  pthread_set_name_np(pthread_self(), tname); \
273  0; \
274 })
275 #elif defined __OpenBSD__ /* OpenBSD */
276 /** \todo Add implementation for OpenBSD */
277 #define SCSetThreadName(n) (0)
278 #elif defined OS_WIN32 /* Windows */
279 /** \todo Add implementation for Windows */
280 #define SCSetThreadName(n) (0)
281 #elif defined OS_DARWIN /* Mac OS X */
282 /** \todo Add implementation for MacOS */
283 #define SCSetThreadName(n) (0)
284 #elif defined PR_SET_NAME /* PR_SET_NAME */
285 /**
286  * \brief Set the threads name
287  */
288 #define SCSetThreadName(n) ({ \
289  char tname[THREAD_NAME_LEN + 1] = ""; \
290  if (strlen(n) > THREAD_NAME_LEN) \
291  SCLogDebug("Thread name is too long, truncating it..."); \
292  strlcpy(tname, n, THREAD_NAME_LEN); \
293  int ret = 0; \
294  if ((ret = prctl(PR_SET_NAME, tname, 0, 0, 0)) < 0) \
295  SCLogDebug("Error setting thread name \"%s\": %s", tname, strerror(errno)); \
296  ret; \
297 })
298 #else
299 #define SCSetThreadName(n) (0)
300 #endif
301 
302 
303 void ThreadMacrosRegisterTests(void);
304 
305 #endif /* __THREADS_H__ */
306 
void ThreadMacrosRegisterTests(void)
this function registers unit tests for DetectId
Definition: threads.c:144