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