suricata
util-atomic.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define SC_ATOMIC_DECLARE(type, name)
 wrapper to declare an atomic variable including a (spin) lock to protect it. More...
 
#define SC_ATOMIC_EXTERN(type, name)
 wrapper to reference an atomic variable already declared on another file (including the spin lock) More...
 
#define SC_ATOMIC_DECL_AND_INIT(type, name)
 wrapper to declare an atomic variable including a (spin) lock to protect it and initialize them. More...
 
#define SC_ATOMIC_INIT(name)
 Initialize the previously declared atomic variable and it's lock. More...
 
#define SC_ATOMIC_RESET(name)
 Initialize the previously declared atomic variable and it's lock. More...
 
#define SC_ATOMIC_DESTROY(name)
 Destroy the lock used to protect this variable. More...
 
#define SC_ATOMIC_ADD(name, val)
 add a value to our atomic variable More...
 
#define SC_ATOMIC_SUB(name, val)
 sub a value from our atomic variable More...
 
#define SC_ATOMIC_AND(name, val)
 Bitwise AND a value from our atomic variable. More...
 
#define SC_ATOMIC_OR(name, val)
 Bitwise OR a value from our atomic variable. More...
 
#define SC_ATOMIC_NAND(name, val)
 Bitwise NAND a value from our atomic variable. More...
 
#define SC_ATOMIC_XOR(name, val)
 Bitwise XOR a value from our atomic variable. More...
 
#define SC_ATOMIC_GET(name)
 Get the value from the atomic variable. More...
 
#define SC_ATOMIC_SET(name, val)
 Set the value for the atomic variable. More...
 
#define SC_ATOMIC_CAS(name, cmpval, newval)
 atomic Compare and Switch More...
 

Functions

void SCAtomicRegisterTests (void)
 

Detailed Description

Author
Victor Julien victo.nosp@m.r@in.nosp@m.linia.nosp@m.c.ne.nosp@m.t
Pablo Rincon pablo.nosp@m..rin.nosp@m.con.c.nosp@m.resp.nosp@m.o@gma.nosp@m.il.c.nosp@m.om

API for atomic operations. Uses atomic instructions (GCC only at this time) where available, falls back to (spin)locked* operations otherwise.

To prevent developers from accidentally working with the atomic variables directly instead of through the proper macro's, a marco trick is performed that exposes different variable names than the developer uses. So if the dev uses "somevar", internally "somevar_sc_atomic__" is used.

Where available, we use sync_fetch_and_add and __sync_bool_compare_and_swap. If those are unavailable, the API transparently created a matching (spin)lock for each atomic variable. The lock will be named "somevar_sc_lock</strong>"

(*) where spinlocks are unavailable, the threading api falls back to mutex

Definition in file util-atomic.h.

Macro Definition Documentation

◆ SC_ATOMIC_ADD

#define SC_ATOMIC_ADD (   name,
  val 
)
Value:
({\
typeof(name ## _sc_atomic__) var; \
do { \
SCSpinLock(&(name ## _sc_lock__)); \
(name ## _sc_atomic__) += (val); \
var = (name ## _sc_atomic__); \
SCSpinUnlock(&(name ## _sc_lock__)); \
} while(0); \
var ; \
})

add a value to our atomic variable

Parameters
namethe atomic variable
valthe value to add to the variable

Definition at line 107 of file util-atomic.h.

◆ SC_ATOMIC_AND

#define SC_ATOMIC_AND (   name,
  val 
)
Value:
do { \
SCSpinLock(&(name ## _sc_lock__)); \
(name ## _sc_atomic__) &= (val); \
SCSpinUnlock(&(name ## _sc_lock__)); \
} while(0)

Bitwise AND a value from our atomic variable.

Parameters
namethe atomic variable
valthe value to sub from the variable

Definition at line 141 of file util-atomic.h.

◆ SC_ATOMIC_CAS

#define SC_ATOMIC_CAS (   name,
  cmpval,
  newval 
)
Value:
({ \
char r = 0; \
do { \
SCSpinLock((name ## _sc_lock__)); \
if (*(name ## _sc_atomic__) == (cmpval)) { \
*(name ## _sc_atomic__) = (newval); \
r = 1; \
} \
SCSpinUnlock((name ## _sc_lock__)); \
} while(0); \
r; \
})

atomic Compare and Switch

Warning
"name" is passed to us as "&var"

Definition at line 222 of file util-atomic.h.

◆ SC_ATOMIC_DECL_AND_INIT

#define SC_ATOMIC_DECL_AND_INIT (   type,
  name 
)
Value:
type name ## _sc_atomic__ = 0; \
SCSpinlock name ## _sc_lock__; \
SCSpinInit(&(name ## _sc_lock__), 0)

wrapper to declare an atomic variable including a (spin) lock to protect it and initialize them.

Definition at line 72 of file util-atomic.h.

◆ SC_ATOMIC_DECLARE

#define SC_ATOMIC_DECLARE (   type,
  name 
)
Value:
type name ## _sc_atomic__; \
SCSpinlock name ## _sc_lock__

wrapper to declare an atomic variable including a (spin) lock to protect it.

Warning
Variable and lock are not initialized.

Definition at line 56 of file util-atomic.h.

◆ SC_ATOMIC_DESTROY

#define SC_ATOMIC_DESTROY (   name)
Value:
do { \
SCSpinDestroy(&(name ## _sc_lock__)); \
} while (0)

Destroy the lock used to protect this variable.

Definition at line 97 of file util-atomic.h.

◆ SC_ATOMIC_EXTERN

#define SC_ATOMIC_EXTERN (   type,
  name 
)
Value:
extern type name ## _sc_atomic__; \
extern SCSpinlock name ## _sc_lock__

wrapper to reference an atomic variable already declared on another file (including the spin lock)

Definition at line 64 of file util-atomic.h.

◆ SC_ATOMIC_GET

#define SC_ATOMIC_GET (   name)
Value:
({ \
typeof(name ## _sc_atomic__) var; \
do { \
SCSpinLock(&(name ## _sc_lock__)); \
var = (name ## _sc_atomic__); \
SCSpinUnlock(&(name ## _sc_lock__)); \
} while (0); \
var; \
})

Get the value from the atomic variable.

Return values
varvalue

Definition at line 192 of file util-atomic.h.

◆ SC_ATOMIC_INIT

#define SC_ATOMIC_INIT (   name)
Value:
do { \
SCSpinInit(&(name ## _sc_lock__), 0); \
(name ## _sc_atomic__) = 0; \
} while(0)

Initialize the previously declared atomic variable and it's lock.

Definition at line 81 of file util-atomic.h.

◆ SC_ATOMIC_NAND

#define SC_ATOMIC_NAND (   name,
  val 
)
Value:
do { \
SCSpinLock(&(name ## _sc_lock__)); \
(name ## _sc_atomic__) = ~(name ## _sc_atomic__) & (val); \
SCSpinUnlock(&(name ## _sc_lock__)); \
} while(0)

Bitwise NAND a value from our atomic variable.

Parameters
namethe atomic variable
valthe value to sub from the variable

Definition at line 167 of file util-atomic.h.

◆ SC_ATOMIC_OR

#define SC_ATOMIC_OR (   name,
  val 
)
Value:
do { \
SCSpinLock(&(name ## _sc_lock__)); \
(name ## _sc_atomic__) |= (val); \
SCSpinUnlock(&(name ## _sc_lock__)); \
} while(0)

Bitwise OR a value from our atomic variable.

Parameters
namethe atomic variable
valthe value to sub from the variable

Definition at line 154 of file util-atomic.h.

◆ SC_ATOMIC_RESET

#define SC_ATOMIC_RESET (   name)
Value:
do { \
(name ## _sc_atomic__) = 0; \
} while(0)

Initialize the previously declared atomic variable and it's lock.

Definition at line 90 of file util-atomic.h.

◆ SC_ATOMIC_SET

#define SC_ATOMIC_SET (   name,
  val 
)
Value:
({ \
typeof(name ## _sc_atomic__) var; \
do { \
SCSpinLock(&(name ## _sc_lock__)); \
var = (name ## _sc_atomic__) = val; \
SCSpinUnlock(&(name ## _sc_lock__)); \
} while (0); \
var; \
})

Set the value for the atomic variable.

Return values
varvalue

Definition at line 207 of file util-atomic.h.

◆ SC_ATOMIC_SUB

#define SC_ATOMIC_SUB (   name,
  val 
)
Value:
({ \
typeof(name ## _sc_atomic__) var; \
do { \
SCSpinLock(&(name ## _sc_lock__)); \
(name ## _sc_atomic__) -= (val); \
var = (name ## _sc_atomic__); \
SCSpinUnlock(&(name ## _sc_lock__)); \
} while(0); \
var ; \
})

sub a value from our atomic variable

Parameters
namethe atomic variable
valthe value to sub from the variable

Definition at line 124 of file util-atomic.h.

◆ SC_ATOMIC_XOR

#define SC_ATOMIC_XOR (   name,
  val 
)
Value:
do { \
SCSpinLock(&(name ## _sc_lock__)); \
(name ## _sc_atomic__) ^= (val); \
SCSpinUnlock(&(name ## _sc_lock__)); \
} while(0)

Bitwise XOR a value from our atomic variable.

Parameters
namethe atomic variable
valthe value to sub from the variable

Definition at line 180 of file util-atomic.h.

Function Documentation

◆ SCAtomicRegisterTests()

void SCAtomicRegisterTests ( void  )

Definition at line 66 of file util-atomic.c.

References UtRegisterTest().

Here is the call graph for this function:
type
uint8_t type
Definition: decode-icmpv4.h:2
SCSpinlock
#define SCSpinlock
Definition: threads-debug.h:234