suricata
threads.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 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  * This file now only contains unit tests see macros in threads.h
25  */
26 
27 #include "suricata-common.h"
28 #include "thread-storage.h"
29 #include "util-unittest.h"
30 #include "util-debug.h"
31 #include "threads.h"
32 
33 thread_local char t_thread_name[THREAD_NAME_LEN + 1];
34 #ifdef UNITTESTS /* UNIT TESTS */
35 
36 /**
37  * \brief Test Mutex macros
38  */
39 static int ThreadMacrosTest01Mutex(void)
40 {
41  SCMutex mut;
42  int r = 0;
43  r |= SCMutexInit(&mut, NULL);
44  r |= SCMutexLock(&mut);
45  r |= (SCMutexTrylock(&mut) == EBUSY)? 0 : 1;
46  r |= SCMutexUnlock(&mut);
47  r |= SCMutexDestroy(&mut);
48 
49  return (r == 0)? 1 : 0;
50 }
51 
52 /**
53  * \brief Test Spinlock Macros
54  *
55  * Valgrind's DRD tool (valgrind-3.5.0-Debian) reports:
56  *
57  * ==31156== Recursive locking not allowed: mutex 0x7fefff97c, recursion count 1, owner 1.
58  * ==31156== at 0x4C2C77E: pthread_spin_trylock (drd_pthread_intercepts.c:829)
59  * ==31156== by 0x40EB3E: ThreadMacrosTest02Spinlocks (threads.c:40)
60  * ==31156== by 0x532E8A: UtRunTests (util-unittest.c:182)
61  * ==31156== by 0x4065C3: main (suricata.c:789)
62  *
63  * To me this is a false positive, as the whole point of "trylock" is to see
64  * if a spinlock is actually locked.
65  *
66  */
67 static int ThreadMacrosTest02Spinlocks(void)
68 {
69  SCSpinlock mut;
70  int r = 0;
71  r |= SCSpinInit(&mut, 0);
72  r |= SCSpinLock(&mut);
73 #ifndef __OpenBSD__
74  r |= (SCSpinTrylock(&mut) == EBUSY)? 0 : 1;
75 #else
76  r |= (SCSpinTrylock(&mut) == EDEADLK)? 0 : 1;
77 #endif
78  r |= SCSpinUnlock(&mut);
79  r |= SCSpinDestroy(&mut);
80 
81  return (r == 0)? 1 : 0;
82 }
83 
84 /**
85  * \brief Test RWLock macros
86  */
87 static int ThreadMacrosTest03RWLocks(void)
88 {
89  SCRWLock rwl_write;
90  int r = 0;
91  r |= SCRWLockInit(&rwl_write, NULL);
92  r |= SCRWLockWRLock(&rwl_write);
93 /* OS X/macOS 10.10 (Yosemite) and newer return EDEADLK. Older versions
94  * and other tested OS's return EBUSY. */
95 #if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__>=101000
96  r |= (SCRWLockTryWRLock(&rwl_write) == EDEADLK)? 0 : 1;
97 #else
98  r |= (SCRWLockTryWRLock(&rwl_write) == EBUSY)? 0 : 1;
99 #endif
100  r |= SCRWLockUnlock(&rwl_write);
101  r |= SCRWLockDestroy(&rwl_write);
102 
103  return (r == 0)? 1 : 0;
104 }
105 
106 /**
107  * \brief Test RWLock macros
108  */
109 static int ThreadMacrosTest04RWLocks(void)
110 {
111  SCRWLock rwl_read;
112  int r = 0;
113  r |= SCRWLockInit(&rwl_read, NULL);
114  r |= SCRWLockRDLock(&rwl_read);
115  r |= (SCRWLockTryWRLock(&rwl_read) == EBUSY)? 0 : 1;
116  r |= SCRWLockUnlock(&rwl_read);
117  r |= SCRWLockDestroy(&rwl_read);
118 
119  return (r == 0)? 1 : 0;
120 }
121 
122 #if 0 // broken on OSX
123 /**
124  * \brief Test RWLock macros
125  */
126 static int ThreadMacrosTest05RWLocks(void)
127 {
128  SCRWLock rwl_read;
129  int r = 0;
130  r |= SCRWLockInit(&rwl_read, NULL);
131  r |= SCRWLockWRLock(&rwl_read);
132  r |= (SCRWLockTryRDLock(&rwl_read) == EBUSY)? 0 : 1;
133  r |= SCRWLockUnlock(&rwl_read);
134  r |= SCRWLockDestroy(&rwl_read);
135 
136  return (r == 0)? 1 : 0;
137 }
138 #endif
139 
140 #endif /* UNIT TESTS */
141 
142 /**
143  * \brief this function registers unit tests for DetectId
144  */
146 {
147 #ifdef UNITTESTS /* UNIT TESTS */
148  UtRegisterTest("ThreadMacrosTest01Mutex", ThreadMacrosTest01Mutex);
149  UtRegisterTest("ThreadMacrosTest02Spinlocks", ThreadMacrosTest02Spinlocks);
150  UtRegisterTest("ThreadMacrosTest03RWLocks", ThreadMacrosTest03RWLocks);
151  UtRegisterTest("ThreadMacrosTest04RWLocks", ThreadMacrosTest04RWLocks);
152 // UtRegisterTest("ThreadMacrosTest05RWLocks", ThreadMacrosTest05RWLocks);
154 #endif /* UNIT TESTS */
155 }
SCSpinDestroy
#define SCSpinDestroy
Definition: threads-debug.h:239
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
threads.h
SCSpinTrylock
#define SCSpinTrylock
Definition: threads-debug.h:236
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
SCSpinLock
#define SCSpinLock
Definition: threads-debug.h:235
SCRWLockTryWRLock
#define SCRWLockTryWRLock(rwl)
Definition: threads-debug.h:367
SCRWLockUnlock
#define SCRWLockUnlock(rwl)
Definition: threads-debug.h:369
util-unittest.h
t_thread_name
thread_local char t_thread_name[THREAD_NAME_LEN+1]
Definition: threads.c:33
SCRWLockDestroy
#define SCRWLockDestroy
Definition: threads-debug.h:370
util-debug.h
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
THREAD_NAME_LEN
#define THREAD_NAME_LEN
Definition: threads.h:33
SCSpinUnlock
#define SCSpinUnlock
Definition: threads-debug.h:237
thread-storage.h
SCRWLockInit
#define SCRWLockInit(rwl, rwlattrs)
Definition: threads-debug.h:364
SCRWLockWRLock
#define SCRWLockWRLock(rwl)
Definition: threads-debug.h:366
SCMutexInit
#define SCMutexInit(mut, mutattrs)
Definition: threads-debug.h:116
suricata-common.h
SCSpinInit
#define SCSpinInit
Definition: threads-debug.h:238
SCRWLockTryRDLock
#define SCRWLockTryRDLock(rwl)
Definition: threads-debug.h:368
SCRWLock
#define SCRWLock
Definition: threads-debug.h:363
SCSpinlock
#define SCSpinlock
Definition: threads-debug.h:234
RegisterThreadStorageTests
void RegisterThreadStorageTests(void)
Definition: thread-storage.c:205
ThreadMacrosRegisterTests
void ThreadMacrosRegisterTests(void)
this function registers unit tests for DetectId
Definition: threads.c:145
SCMutexDestroy
#define SCMutexDestroy
Definition: threads-debug.h:120
SCMutex
#define SCMutex
Definition: threads-debug.h:114
SCRWLockRDLock
#define SCRWLockRDLock(rwl)
Definition: threads-debug.h:365
SCMutexTrylock
#define SCMutexTrylock(mut)
Definition: threads-debug.h:118