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_MEMORY_ORDER_RELAXED
 
#define SC_ATOMIC_MEMORY_ORDER_CONSUME
 
#define SC_ATOMIC_MEMORY_ORDER_ACQUIRE
 
#define SC_ATOMIC_MEMORY_ORDER_RELEASE
 
#define SC_ATOMIC_MEMORY_ORDER_ACQ_REL
 
#define SC_ATOMIC_MEMORY_ORDER_SEQ_CST
 
#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_WITH_VAL(type, name, val)   type name##_sc_atomic__ = val
 wrapper for declaring an atomic variable and initializing it to a specific value 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_LOAD_EXPLICIT(name, order)   (name ## _sc_atomic__)
 
#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 333 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 360 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 368 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 309 of file util-atomic.h.

◆ SC_ATOMIC_DECL_AND_INIT_WITH_VAL

#define SC_ATOMIC_DECL_AND_INIT_WITH_VAL (   type,
  name,
  val 
)    type name##_sc_atomic__ = val

wrapper for declaring an atomic variable and initializing it to a specific value

Definition at line 304 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 281 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 297 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 376 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 315 of file util-atomic.h.

◆ SC_ATOMIC_INITPTR

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

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

◆ SC_ATOMIC_LOAD_EXPLICIT

#define SC_ATOMIC_LOAD_EXPLICIT (   name,
  order 
)    (name ## _sc_atomic__)

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

◆ SC_ATOMIC_MEMORY_ORDER_ACQ_REL

#define SC_ATOMIC_MEMORY_ORDER_ACQ_REL

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

◆ SC_ATOMIC_MEMORY_ORDER_ACQUIRE

#define SC_ATOMIC_MEMORY_ORDER_ACQUIRE

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

◆ SC_ATOMIC_MEMORY_ORDER_CONSUME

#define SC_ATOMIC_MEMORY_ORDER_CONSUME

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

◆ SC_ATOMIC_MEMORY_ORDER_RELAXED

#define SC_ATOMIC_MEMORY_ORDER_RELAXED

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

◆ SC_ATOMIC_MEMORY_ORDER_RELEASE

#define SC_ATOMIC_MEMORY_ORDER_RELEASE

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

◆ SC_ATOMIC_MEMORY_ORDER_SEQ_CST

#define SC_ATOMIC_MEMORY_ORDER_SEQ_CST

Definition at line 171 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 351 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 324 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 387 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 342 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 214 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 184 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 194 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 234 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 244 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 264 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 204 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 254 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 224 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:367
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:375