suricata
threads.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2020 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 SURICATA_THREADS_H
28 #define SURICATA_THREADS_H
29 
30 #include "suricata-common.h"
31 
32 #ifndef THREAD_NAME_LEN
33 #define THREAD_NAME_LEN 16
34 #endif
35 
36 #if defined(TLS_C11)
37 #define thread_local _Thread_local
38 #elif defined(TLS_GNU)
39 #define thread_local __thread
40 #else
41 #error "No supported thread local type found"
42 #endif
43 
44 /* need this for the _POSIX_SPIN_LOCKS define */
45 #if HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48 
49 #if defined OS_FREEBSD || __OpenBSD__
50 
51 #if ! defined __OpenBSD__
52 #include <sys/thr.h>
53 #endif
54 enum {
55  PRIO_LOW = 2,
56  PRIO_MEDIUM = 0,
57  PRIO_HIGH = -2,
58 };
59 
60 #elif OS_DARWIN
61 
62 #include <mach/mach_init.h>
63 enum {
64  PRIO_LOW = 2,
65  PRIO_MEDIUM = 0,
66  PRIO_HIGH = -2,
67 };
68 
69 #elif OS_WIN32
70 
71 #include <windows.h>
72 enum {
73  PRIO_LOW = THREAD_PRIORITY_LOWEST,
74  PRIO_MEDIUM = THREAD_PRIORITY_NORMAL,
75  PRIO_HIGH = THREAD_PRIORITY_HIGHEST,
76 };
77 
78 #else /* LINUX */
79 
80 #if HAVE_SYS_SYSCALL_H
81 #include <sys/syscall.h>
82 #endif
83 #if HAVE_SYS_PRCTL_H
84 #include <sys/prctl.h>
85 #endif
86 
87 enum {
88  PRIO_LOW = 2,
90  PRIO_HIGH = -2,
91 };
92 
93 #endif /* OS_FREEBSD */
94 
95 #include <pthread.h>
96 
97 /* The mutex/spinlock/condition definitions and functions are used
98  * in the same way as the POSIX definitions; Anyway we are centralizing
99  * them here to make an easier portability process and debugging process;
100  * Please, make sure you initialize mutex and spinlocks before using them
101  * because, some OS doesn't initialize them for you :)
102  */
103 
104 //#define DBG_THREADS
105 
106 #if 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 || !defined(HAVE_PTHREAD_SPIN_UNLOCK)
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
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  unsigned long _scgetthread_tid = (unsigned long)tmpthid; \
220  _scgetthread_tid; \
221 })
222 #elif __OpenBSD__
223 #define SCGetThreadIdLong(...) ({ \
224  pid_t tpid; \
225  tpid = getpid(); \
226  unsigned long _scgetthread_tid = (unsigned long)tpid; \
227  _scgetthread_tid; \
228 })
229 #elif __CYGWIN__
230 #define SCGetThreadIdLong(...) ({ \
231  unsigned long _scgetthread_tid = (unsigned long)GetCurrentThreadId(); \
232  _scgetthread_tid; \
233 })
234 #elif OS_WIN32
235 #define SCGetThreadIdLong(...) ({ \
236  unsigned long _scgetthread_tid = (unsigned long)GetCurrentThreadId(); \
237  _scgetthread_tid; \
238 })
239 #elif OS_DARWIN
240 #define SCGetThreadIdLong(...) ({ \
241  thread_port_t tpid; \
242  tpid = mach_thread_self(); \
243  unsigned long _scgetthread_tid = (unsigned long)tpid; \
244  _scgetthread_tid; \
245 })
246 #elif defined(sun)
247 #include <thread.h>
248 #define SCGetThreadIdLong(...) ({ \
249  thread_t tmpthid = thr_self(); \
250  unsigned long _scgetthread_tid = (unsigned long)tmpthid; \
251  _scgetthread_tid; \
252 })
253 
254 #else
255 #define SCGetThreadIdLong(...) ({ \
256  pid_t tmpthid; \
257  tmpthid = syscall(SYS_gettid); \
258  unsigned long _scgetthread_tid = (unsigned long)tmpthid; \
259  _scgetthread_tid; \
260 })
261 #endif /* OS FREEBSD */
262 
263 extern thread_local char t_thread_name[THREAD_NAME_LEN + 1];
264 /*
265  * OS specific macro's for setting the thread name. "top" can display
266  * this name.
267  */
268 #if defined OS_FREEBSD /* FreeBSD */
269 /** \todo Add implementation for FreeBSD */
270 #define SCSetThreadName(n) \
271  ({ \
272  char tname[THREAD_NAME_LEN] = ""; \
273  if (strlen(n) > THREAD_NAME_LEN) \
274  SCLogDebug("Thread name is too long, truncating it..."); \
275  strlcpy(tname, n, THREAD_NAME_LEN); \
276  strlcpy(t_thread_name, n, sizeof(t_thread_name)); \
277  pthread_set_name_np(pthread_self(), tname); \
278  })
279 #elif defined __OpenBSD__ /* OpenBSD */
280 /** \todo Add implementation for OpenBSD */
281 #define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); })
282 #elif defined OS_WIN32 /* Windows */
283 /** \todo Add implementation for Windows */
284 #define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); })
285 #elif defined OS_DARWIN /* Mac OS X */
286 /** \todo Add implementation for MacOS */
287 #define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); })
288 #elif defined PR_SET_NAME /* PR_SET_NAME */
289 /**
290  * \brief Set the threads name
291  */
292 #define SCSetThreadName(n) \
293  ({ \
294  char tname[THREAD_NAME_LEN + 1] = ""; \
295  if (strlen(n) > THREAD_NAME_LEN) \
296  SCLogDebug("Thread name is too long, truncating it..."); \
297  strlcpy(tname, n, THREAD_NAME_LEN); \
298  strlcpy(t_thread_name, n, sizeof(t_thread_name)); \
299  if (prctl(PR_SET_NAME, tname, 0, 0, 0) < 0) \
300  SCLogDebug("Error setting thread name \"%s\": %s", tname, strerror(errno)); \
301  })
302 #else
303 #define SCSetThreadName(n) ({ \
304  strlcpy(t_thread_name, n, sizeof(t_thread_name)); \
305 }
306 #endif
307 
308 
309 void ThreadMacrosRegisterTests(void);
310 
311 #endif /* SURICATA_THREADS_H */
THREAD_NAME_LEN
#define THREAD_NAME_LEN
Definition: threads.h:33
ThreadMacrosRegisterTests
void ThreadMacrosRegisterTests(void)
this function registers unit tests for DetectId
Definition: threads.c:144
t_thread_name
thread_local char t_thread_name[THREAD_NAME_LEN+1]
Definition: threads.c:32
suricata-common.h
PRIO_MEDIUM
@ PRIO_MEDIUM
Definition: threads.h:89
PRIO_LOW
@ PRIO_LOW
Definition: threads.h:88
threads-debug.h
PRIO_HIGH
@ PRIO_HIGH
Definition: threads.h:90
threads-profile.h