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