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 SCAtomicCompareAndSwap(addr, tv, nv)   __sync_bool_compare_and_swap((addr), (tv), (nv))
 wrapper for OS/compiler specific atomic compare and swap (CAS) function. More...
 
#define SCAtomicFetchAndAdd(addr, value)   __sync_fetch_and_add((addr), (value))
 wrapper for OS/compiler specific atomic fetch and add function. More...
 
#define SCAtomicFetchAndSub(addr, value)   __sync_fetch_and_sub((addr), (value))
 wrapper for OS/compiler specific atomic fetch and sub function. More...
 
#define SCAtomicAddAndFetch(addr, value)   __sync_add_and_fetch((addr), (value))
 wrapper for OS/compiler specific atomic fetch and add function. More...
 
#define SCAtomicSubAndFetch(addr, value)   __sync_sub_and_fetch((addr), (value))
 wrapper for OS/compiler specific atomic fetch and sub function. More...
 
#define SCAtomicFetchAndAnd(addr, value)   __sync_fetch_and_and((addr), (value))
 wrapper for OS/compiler specific atomic fetch and "AND" function. More...
 
#define SCAtomicFetchAndNand(addr, value)   __sync_fetch_and_nand((addr), (value))
 wrapper for OS/compiler specific atomic fetch and "NAND" function. More...
 
#define SCAtomicFetchAndXor(addr, value)   __sync_fetch_and_xor((addr), (value))
 wrapper for OS/compiler specific atomic fetch and "XOR" function. More...
 
#define SCAtomicFetchAndOr(addr, value)   __sync_fetch_and_or((addr), (value))
 wrapper for OS/compiler specific atomic fetch and or function. More...
 
#define SC_ATOMIC_DECLARE(type, name)   type name ## _sc_atomic__
 wrapper for declaring atomic variables. More...
 
#define SC_ATOMIC_EXTERN(type, name)   extern type name ## _sc_atomic__
 wrapper for referencing an atomic variable declared on another file. More...
 
#define SC_ATOMIC_DECL_AND_INIT(type, name)   type name ## _sc_atomic__ = 0
 wrapper for declaring an atomic variable and initializing it. More...
 
#define SC_ATOMIC_INIT(name)   (name ## _sc_atomic__) = 0
 wrapper for initializing an atomic variable. More...
 
#define SC_ATOMIC_INITPTR(name)   (name ## _sc_atomic__) = NULL
 
#define SC_ATOMIC_RESET(name)   (name ## _sc_atomic__) = 0
 wrapper for reinitializing an atomic variable. More...
 
#define SC_ATOMIC_ADD(name, val)   SCAtomicFetchAndAdd(&(name ## _sc_atomic__), (val))
 add a value to our atomic variable More...
 
#define SC_ATOMIC_SUB(name, val)   SCAtomicFetchAndSub(&(name ## _sc_atomic__), (val))
 sub a value from our atomic variable More...
 
#define SC_ATOMIC_OR(name, val)   SCAtomicFetchAndOr(&(name ## _sc_atomic__), (val))
 Bitwise OR a value to our atomic variable. More...
 
#define SC_ATOMIC_AND(name, val)   SCAtomicFetchAndAnd(&(name ## _sc_atomic__), (val))
 Bitwise AND a value to our atomic variable. More...
 
#define SC_ATOMIC_CAS(name, cmpval, newval)   SCAtomicCompareAndSwap((name ## _sc_atomic__), cmpval, newval)
 atomic Compare and Switch More...
 
#define SC_ATOMIC_GET(name)   (name ## _sc_atomic__)
 Get the value from the atomic variable. More...
 
#define SC_ATOMIC_SET(name, val)
 Set the value for the atomic variable. 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 C11 atomic instructions where available, GCC/clang specific (gnu99) 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.

Definition in file util-atomic.h.

Macro Definition Documentation

◆ SC_ATOMIC_ADD

#define SC_ATOMIC_ADD (   name,
  val 
)    SCAtomicFetchAndAdd(&(name ## _sc_atomic__), (val))

add a value to our atomic variable

Parameters
namethe atomic variable
valthe value to add to the variable

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

◆ SC_ATOMIC_AND

#define SC_ATOMIC_AND (   name,
  val 
)    SCAtomicFetchAndAnd(&(name ## _sc_atomic__), (val))

Bitwise AND a value to our atomic variable.

Parameters
namethe atomic variable
valthe value to AND to the variable

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

◆ SC_ATOMIC_CAS

#define SC_ATOMIC_CAS (   name,
  cmpval,
  newval 
)    SCAtomicCompareAndSwap((name ## _sc_atomic__), cmpval, newval)

atomic Compare and Switch

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

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

◆ SC_ATOMIC_DECL_AND_INIT

#define SC_ATOMIC_DECL_AND_INIT (   type,
  name 
)    type name ## _sc_atomic__ = 0

wrapper for declaring an atomic variable and initializing it.

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

◆ SC_ATOMIC_DECLARE

#define SC_ATOMIC_DECLARE (   type,
  name 
)    type name ## _sc_atomic__

wrapper for declaring atomic variables.

Warning
Only char, short, int, long, long long and their unsigned versions are supported.
Parameters
typeType of the variable (char, short, int, long, long long)
nameName of the variable.

We just declare the variable here as we rely on atomic operations to modify it, so no need for locks.

Warning
variable is not initialized

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

◆ SC_ATOMIC_EXTERN

#define SC_ATOMIC_EXTERN (   type,
  name 
)    extern type name ## _sc_atomic__

wrapper for referencing an atomic variable declared on another file.

Warning
Only char, short, int, long, long long and their unsigned versions are supported.
Parameters
typeType of the variable (char, short, int, long, long long)
nameName of the variable.

We just declare the variable here as we rely on atomic operations to modify it, so no need for locks.

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

◆ SC_ATOMIC_GET

#define SC_ATOMIC_GET (   name)    (name ## _sc_atomic__)

Get the value from the atomic variable.

Return values
varvalue

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

◆ SC_ATOMIC_INIT

#define SC_ATOMIC_INIT (   name)    (name ## _sc_atomic__) = 0

wrapper for initializing an atomic variable.

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

◆ SC_ATOMIC_INITPTR

#define SC_ATOMIC_INITPTR (   name)    (name ## _sc_atomic__) = NULL

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

◆ SC_ATOMIC_OR

#define SC_ATOMIC_OR (   name,
  val 
)    SCAtomicFetchAndOr(&(name ## _sc_atomic__), (val))

Bitwise OR a value to our atomic variable.

Parameters
namethe atomic variable
valthe value to OR to the variable

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

◆ SC_ATOMIC_RESET

#define SC_ATOMIC_RESET (   name)    (name ## _sc_atomic__) = 0

wrapper for reinitializing an atomic variable.

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

◆ SC_ATOMIC_SET

#define SC_ATOMIC_SET (   name,
  val 
)
Value:
({ \
while (SC_ATOMIC_CAS(&name, SC_ATOMIC_GET(name), val) == 0) \
; \
})

Set the value for the atomic variable.

Return values
varvalue

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

◆ SC_ATOMIC_SUB

#define SC_ATOMIC_SUB (   name,
  val 
)    SCAtomicFetchAndSub(&(name ## _sc_atomic__), (val))

sub a value from our atomic variable

Parameters
namethe atomic variable
valthe value to sub from the variable

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

◆ SCAtomicAddAndFetch

#define SCAtomicAddAndFetch (   addr,
  value 
)    __sync_add_and_fetch((addr), (value))

wrapper for OS/compiler specific atomic fetch and add function.

Parameters
addrAddress of the variable to add to
valueValue to add to the variable at addr

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

◆ SCAtomicCompareAndSwap

#define SCAtomicCompareAndSwap (   addr,
  tv,
  nv 
)    __sync_bool_compare_and_swap((addr), (tv), (nv))

wrapper for OS/compiler specific atomic compare and swap (CAS) function.

Parameters
addrAddress of the variable to CAS
tvTest value to compare the value at address against
nvNew value to set the variable at addr to
Return values
0CAS failed
1CAS succeeded

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

◆ SCAtomicFetchAndAdd

#define SCAtomicFetchAndAdd (   addr,
  value 
)    __sync_fetch_and_add((addr), (value))

wrapper for OS/compiler specific atomic fetch and add function.

Parameters
addrAddress of the variable to add to
valueValue to add to the variable at addr

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

◆ SCAtomicFetchAndAnd

#define SCAtomicFetchAndAnd (   addr,
  value 
)    __sync_fetch_and_and((addr), (value))

wrapper for OS/compiler specific atomic fetch and "AND" function.

Parameters
addrAddress of the variable to AND to
valueValue to add to the variable at addr

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

◆ SCAtomicFetchAndNand

#define SCAtomicFetchAndNand (   addr,
  value 
)    __sync_fetch_and_nand((addr), (value))

wrapper for OS/compiler specific atomic fetch and "NAND" function.

Parameters
addrAddress of the variable to NAND to
valueValue to add to the variable at addr

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

◆ SCAtomicFetchAndOr

#define SCAtomicFetchAndOr (   addr,
  value 
)    __sync_fetch_and_or((addr), (value))

wrapper for OS/compiler specific atomic fetch and or function.

Parameters
addrAddress of the variable to or to
valueValue to add to the variable at addr

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

◆ SCAtomicFetchAndSub

#define SCAtomicFetchAndSub (   addr,
  value 
)    __sync_fetch_and_sub((addr), (value))

wrapper for OS/compiler specific atomic fetch and sub function.

Parameters
addrAddress of the variable to add to
valueValue to sub from the variable at addr

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

◆ SCAtomicFetchAndXor

#define SCAtomicFetchAndXor (   addr,
  value 
)    __sync_fetch_and_xor((addr), (value))

wrapper for OS/compiler specific atomic fetch and "XOR" function.

Parameters
addrAddress of the variable to XOR to
valueValue to add to the variable at addr

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

◆ SCAtomicSubAndFetch

#define SCAtomicSubAndFetch (   addr,
  value 
)    __sync_sub_and_fetch((addr), (value))

wrapper for OS/compiler specific atomic fetch and sub function.

Parameters
addrAddress of the variable to add to
valueValue to sub from the variable at addr

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

Function Documentation

◆ SCAtomicRegisterTests()

void SCAtomicRegisterTests ( void  )

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

References UtRegisterTest().

Here is the call graph for this function:
SC_ATOMIC_CAS
#define SC_ATOMIC_CAS(name, cmpval, newval)
atomic Compare and Switch
Definition: util-atomic.h:339
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:347