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