suricata
util-atomic.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2012 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 Anoop Saldanha <anoopsaldanha@gmail.com>
22  */
23 
24 #include "suricata-common.h"
25 #include "suricata.h"
26 #include "util-atomic.h"
27 #include "util-unittest.h"
28 
29 #ifdef UNITTESTS
30 
31 static int SCAtomicTest01(void)
32 {
33  int result = 0;
34  int a = 10;
35  int b = 20;
36  int *temp_int = NULL;
37 
38  SC_ATOMIC_DECL_AND_INIT(void *, temp);
39 
40  temp_int = SC_ATOMIC_GET(temp);
41  if (temp_int != NULL)
42  goto end;
43 
44  (void)SC_ATOMIC_SET(temp, &a);
45  temp_int = SC_ATOMIC_GET(temp);
46  if (temp_int == NULL)
47  goto end;
48  if (*temp_int != a)
49  goto end;
50 
51  (void)SC_ATOMIC_SET(temp, &b);
52  temp_int = SC_ATOMIC_GET(temp);
53  if (temp_int == NULL)
54  goto end;
55  if (*temp_int != b)
56  goto end;
57 
58  result = 1;
59 
60  end:
61  return result;
62 }
63 
64 #endif /* UNITTESTS */
65 
67 {
68 #ifdef UNITTESTS
69  UtRegisterTest("SCAtomicTest01", SCAtomicTest01);
70 #endif
71 
72  return;
73 }
void SCAtomicRegisterTests(void)
Definition: util-atomic.c:66
#define SC_ATOMIC_DECL_AND_INIT(type, name)
wrapper to declare an atomic variable including a (spin) lock to protect it and initialize them...
Definition: util-atomic.h:73
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
Definition: util-atomic.h:208
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:193