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