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