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 __THREADS_H__
28 #define __THREADS_H__
29 
30 #ifndef THREAD_NAME_LEN
31 #define THREAD_NAME_LEN 16
32 #endif
33 
34 #if defined(TLS_C11)
35 #define thread_local _Thread_local
36 #elif defined(TLS_GNU)
37 #define thread_local __thread
38 #else
39 #error "No supported thread local type found"
40 #endif
41 
42 /* need this for the _POSIX_SPIN_LOCKS define */
43 #if HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
46 
47 #ifdef PROFILING
48 #include "util-cpu.h"
49 #ifdef PROFILE_LOCKING
50 #include "util-profiling-locks.h"
51 #endif /* PROFILE_LOCKING */
52 #endif /* PROFILING */
53 
54 #if defined OS_FREEBSD || __OpenBSD__
55 
56 #if ! defined __OpenBSD__
57 #include <sys/thr.h>
58 #endif
59 enum {
60  PRIO_LOW = 2,
61  PRIO_MEDIUM = 0,
62  PRIO_HIGH = -2,
63 };
64 
65 #elif OS_DARWIN
66 
67 #include <mach/mach_init.h>
68 enum {
69  PRIO_LOW = 2,
70  PRIO_MEDIUM = 0,
71  PRIO_HIGH = -2,
72 };
73 
74 #elif OS_WIN32
75 
76 #include <windows.h>
77 enum {
78  PRIO_LOW = THREAD_PRIORITY_LOWEST,
79  PRIO_MEDIUM = THREAD_PRIORITY_NORMAL,
80  PRIO_HIGH = THREAD_PRIORITY_HIGHEST,
81 };
82 
83 #else /* LINUX */
84 
85 #if HAVE_SYS_SYSCALL_H
86 #include <sys/syscall.h>
87 #endif
88 #if HAVE_SYS_PRCTL_H
89 #include <sys/prctl.h>
90 #endif
91 
92 enum {
93  PRIO_LOW = 2,
95  PRIO_HIGH = -2,
96 };
97 
98 #endif /* OS_FREEBSD */
99 
100 #include <pthread.h>
101 
102 /* The mutex/spinlock/condition definitions and functions are used
103  * in the same way as the POSIX definitions; Anyway we are centralizing
104  * them here to make an easier portability process and debugging process;
105  * Please, make sure you initialize mutex and spinlocks before using them
106  * because, some OS doesn't initialize them for you :)
107  */
108 
109 //#define DBG_THREADS
110 
111 #if defined DBG_THREADS
112  #ifdef PROFILE_LOCKING
113  #error "Cannot mix DBG_THREADS and PROFILE_LOCKING"
114  #endif
115  #include "threads-debug.h"
116 #elif defined PROFILE_LOCKING
117  #include "threads-profile.h"
118 #else /* normal */
119 
120 /* mutex */
121 #define SCMutex pthread_mutex_t
122 #define SCMutexAttr pthread_mutexattr_t
123 #define SCMutexInit(mut, mutattr ) pthread_mutex_init(mut, mutattr)
124 #define SCMutexLock(mut) pthread_mutex_lock(mut)
125 #define SCMutexTrylock(mut) pthread_mutex_trylock(mut)
126 #define SCMutexUnlock(mut) pthread_mutex_unlock(mut)
127 #define SCMutexDestroy pthread_mutex_destroy
128 #define SCMUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
129 
130 /* rwlocks */
131 #define SCRWLock pthread_rwlock_t
132 #define SCRWLockInit(rwl, rwlattr ) pthread_rwlock_init(rwl, rwlattr)
133 #define SCRWLockWRLock(rwl) pthread_rwlock_wrlock(rwl)
134 #define SCRWLockRDLock(rwl) pthread_rwlock_rdlock(rwl)
135 #define SCRWLockTryWRLock(rwl) pthread_rwlock_trywrlock(rwl)
136 #define SCRWLockTryRDLock(rwl) pthread_rwlock_tryrdlock(rwl)
137 #define SCRWLockUnlock(rwl) pthread_rwlock_unlock(rwl)
138 #define SCRWLockDestroy pthread_rwlock_destroy
139 
140 /* conditions */
141 #define SCCondT pthread_cond_t
142 #define SCCondInit pthread_cond_init
143 #define SCCondSignal pthread_cond_signal
144 #define SCCondDestroy pthread_cond_destroy
145 #define SCCondWait(cond, mut) pthread_cond_wait(cond, mut)
146 
147 /* ctrl mutex */
148 #define SCCtrlMutex pthread_mutex_t
149 #define SCCtrlMutexAttr pthread_mutexattr_t
150 #define SCCtrlMutexInit(mut, mutattr ) pthread_mutex_init(mut, mutattr)
151 #define SCCtrlMutexLock(mut) pthread_mutex_lock(mut)
152 #define SCCtrlMutexTrylock(mut) pthread_mutex_trylock(mut)
153 #define SCCtrlMutexUnlock(mut) pthread_mutex_unlock(mut)
154 #define SCCtrlMutexDestroy pthread_mutex_destroy
155 
156 /* ctrl conditions */
157 #define SCCtrlCondT pthread_cond_t
158 #define SCCtrlCondInit pthread_cond_init
159 #define SCCtrlCondSignal pthread_cond_signal
160 #define SCCtrlCondTimedwait pthread_cond_timedwait
161 #define SCCtrlCondWait pthread_cond_wait
162 #define SCCtrlCondDestroy pthread_cond_destroy
163 
164 /* spinlocks */
165 #if ((_POSIX_SPIN_LOCKS - 200112L) < 0L) || defined HELGRIND || !defined(HAVE_PTHREAD_SPIN_UNLOCK)
166 #define SCSpinlock SCMutex
167 #define SCSpinLock(spin) SCMutexLock((spin))
168 #define SCSpinTrylock(spin) SCMutexTrylock((spin))
169 #define SCSpinUnlock(spin) SCMutexUnlock((spin))
170 #define SCSpinInit(spin, spin_attr) SCMutexInit((spin), NULL)
171 #define SCSpinDestroy(spin) SCMutexDestroy((spin))
172 #else /* no spinlocks */
173 #define SCSpinlock pthread_spinlock_t
174 #define SCSpinLock(spin) pthread_spin_lock(spin)
175 #define SCSpinTrylock(spin) pthread_spin_trylock(spin)
176 #define SCSpinUnlock(spin) pthread_spin_unlock(spin)
177 #define SCSpinInit(spin, spin_attr) pthread_spin_init(spin, spin_attr)
178 #define SCSpinDestroy(spin) pthread_spin_destroy(spin)
179 #endif /* no spinlocks */
180 
181 #endif
182 
183 #if (!defined SCMutex || !defined SCMutexAttr || !defined SCMutexInit || \
184  !defined SCMutexLock || !defined SCMutexTrylock || \
185  !defined SCMutexUnlock || !defined SCMutexDestroy || \
186  !defined SCMUTEX_INITIALIZER)
187 #error "Mutex types and/or macro's not properly defined"
188 #endif
189 #if (!defined SCCtrlMutex || !defined SCCtrlMutexAttr || !defined SCCtrlMutexInit || \
190  !defined SCCtrlMutexLock || !defined SCCtrlMutexTrylock || \
191  !defined SCCtrlMutexUnlock || !defined SCCtrlMutexDestroy)
192 #error "SCCtrlMutex types and/or macro's not properly defined"
193 #endif
194 
195 #if (!defined SCSpinlock || !defined SCSpinLock || \
196  !defined SCSpinTrylock || !defined SCSpinUnlock || \
197  !defined SCSpinInit || !defined SCSpinDestroy)
198 #error "Spinlock types and/or macro's not properly defined"
199 #endif
200 
201 #if (!defined SCRWLock || !defined SCRWLockInit || !defined SCRWLockWRLock || \
202  !defined SCRWLockRDLock || !defined SCRWLockTryWRLock || \
203  !defined SCRWLockTryRDLock || !defined SCRWLockUnlock || !defined SCRWLockDestroy)
204 #error "SCRWLock types and/or macro's not properly defined"
205 #endif
206 
207 #if (!defined SCCondT || !defined SCCondInit || !defined SCCondSignal || \
208  !defined SCCondDestroy || !defined SCCondWait)
209 #error "SCCond types and/or macro's not properly defined"
210 #endif
211 
212 #if (!defined SCCtrlCondT || !defined SCCtrlCondInit || !defined SCCtrlCondSignal ||\
213  !defined SCCtrlCondDestroy || !defined SCCtrlCondTimedwait)
214 #error "SCCtrlCond types and/or macro's not properly defined"
215 #endif
216 
217 /** Get the Current Thread Id */
218 #ifdef OS_FREEBSD
219 #include <pthread_np.h>
220 
221 #define SCGetThreadIdLong(...) ({ \
222  long tmpthid; \
223  thr_self(&tmpthid); \
224  unsigned long _scgetthread_tid = (unsigned long)tmpthid; \
225  _scgetthread_tid; \
226 })
227 #elif __OpenBSD__
228 #define SCGetThreadIdLong(...) ({ \
229  pid_t tpid; \
230  tpid = getpid(); \
231  unsigned long _scgetthread_tid = (unsigned long)tpid; \
232  _scgetthread_tid; \
233 })
234 #elif __CYGWIN__
235 #define SCGetThreadIdLong(...) ({ \
236  unsigned long _scgetthread_tid = (unsigned long)GetCurrentThreadId(); \
237  _scgetthread_tid; \
238 })
239 #elif OS_WIN32
240 #define SCGetThreadIdLong(...) ({ \
241  unsigned long _scgetthread_tid = (unsigned long)GetCurrentThreadId(); \
242  _scgetthread_tid; \
243 })
244 #elif OS_DARWIN
245 #define SCGetThreadIdLong(...) ({ \
246  thread_port_t tpid; \
247  tpid = mach_thread_self(); \
248  unsigned long _scgetthread_tid = (unsigned long)tpid; \
249  _scgetthread_tid; \
250 })
251 #elif defined(sun)
252 #include <thread.h>
253 #define SCGetThreadIdLong(...) ({ \
254  thread_t tmpthid = thr_self(); \
255  unsigned long _scgetthread_tid = (unsigned long)tmpthid; \
256  _scgetthread_tid; \
257 })
258 
259 #else
260 #define SCGetThreadIdLong(...) ({ \
261  pid_t tmpthid; \
262  tmpthid = syscall(SYS_gettid); \
263  unsigned long _scgetthread_tid = (unsigned long)tmpthid; \
264  _scgetthread_tid; \
265 })
266 #endif /* OS FREEBSD */
267 
268 extern thread_local char t_thread_name[THREAD_NAME_LEN + 1];
269 /*
270  * OS specific macro's for setting the thread name. "top" can display
271  * this name.
272  */
273 #if defined OS_FREEBSD /* FreeBSD */
274 /** \todo Add implementation for FreeBSD */
275 #define SCSetThreadName(n) \
276  ({ \
277  char tname[THREAD_NAME_LEN] = ""; \
278  if (strlen(n) > THREAD_NAME_LEN) \
279  SCLogDebug("Thread name is too long, truncating it..."); \
280  strlcpy(tname, n, THREAD_NAME_LEN); \
281  strlcpy(t_thread_name, n, sizeof(t_thread_name)); \
282  pthread_set_name_np(pthread_self(), tname); \
283  })
284 #elif defined __OpenBSD__ /* OpenBSD */
285 /** \todo Add implementation for OpenBSD */
286 #define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); })
287 #elif defined OS_WIN32 /* Windows */
288 /** \todo Add implementation for Windows */
289 #define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); })
290 #elif defined OS_DARWIN /* Mac OS X */
291 /** \todo Add implementation for MacOS */
292 #define SCSetThreadName(n) ({ strlcpy(t_thread_name, n, sizeof(t_thread_name)); })
293 #elif defined PR_SET_NAME /* PR_SET_NAME */
294 /**
295  * \brief Set the threads name
296  */
297 #define SCSetThreadName(n) \
298  ({ \
299  char tname[THREAD_NAME_LEN + 1] = ""; \
300  if (strlen(n) > THREAD_NAME_LEN) \
301  SCLogDebug("Thread name is too long, truncating it..."); \
302  strlcpy(tname, n, THREAD_NAME_LEN); \
303  strlcpy(t_thread_name, n, sizeof(t_thread_name)); \
304  if (prctl(PR_SET_NAME, tname, 0, 0, 0) < 0) \
305  SCLogDebug("Error setting thread name \"%s\": %s", tname, strerror(errno)); \
306  })
307 #else
308 #define SCSetThreadName(n) ({ \
309  strlcpy(t_thread_name, n, sizeof(t_thread_name)); \
310 }
311 #endif
312 
313 
314 void ThreadMacrosRegisterTests(void);
315 
316 #endif /* __THREADS_H__ */
317 
PRIO_HIGH
@ PRIO_HIGH
Definition: threads.h:95
util-cpu.h
THREAD_NAME_LEN
#define THREAD_NAME_LEN
Definition: threads.h:31
ThreadMacrosRegisterTests
void ThreadMacrosRegisterTests(void)
this function registers unit tests for DetectId
Definition: threads.c:145
t_thread_name
thread_local char t_thread_name[THREAD_NAME_LEN+1]
Definition: threads.c:33
PRIO_LOW
@ PRIO_LOW
Definition: threads.h:93
PRIO_MEDIUM
@ PRIO_MEDIUM
Definition: threads.h:94
util-profiling-locks.h
threads-debug.h
threads-profile.h