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

#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 ; \
})
#define SCSpinLock
#define SCSpinUnlock

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.

Referenced by AFPPeersListCheck(), AppLayerExpectationCreate(), DefragInitConfig(), DefragLookupTrackerFromHash(), DefragTrackerMoveToSpare(), FlowAlloc(), FlowDisableFlowManagerThread(), FlowGetExistingFlowFromHash(), FlowInitConfig(), FTPMemcapGlobalCounter(), HostAlloc(), HostInitConfig(), HostLookupHostFromHash(), HTPMemcapGlobalCounter(), HTPParseMemcap(), IPPairAlloc(), IPPairCleanup(), IPPairInitConfig(), IPPairLookupIPPairFromHash(), LiveDevAddBypassFail(), LiveDevAddBypassStats(), LiveDevAddBypassSuccess(), NapatechPacketLoopZC(), NapatechStreamThreadExitStats(), NoNFLOGSupportExit(), OutputRegisterFiledataLogger(), PacketPoolWait(), PacketPoolWaitForN(), PacketProfileLoggertIdToString(), PcapIncreaseInvalidChecksum(), ReceiveAFPLoop(), RunModeIdsAFPRegister(), RunModeIdsNetmapRegister(), RunModeIdsPcapRegister(), RunModeIdsPfringRegister(), SCProfilePacketStart(), SCProfileRuleStart(), SRepReloadComplete(), StreamTcpIncrMemuse(), StreamTcpPacket(), StreamTcpReassembleIncrMemuse(), TagFlowAdd(), TagHashAddTag(), TmModuleDecodeAFPRegister(), TmModuleDecodeNetmapRegister(), TmModuleDecodePcapRegister(), and TmModuleDecodePfringRegister().

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

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.

Referenced by TmThreadsUnsetFlag().

#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; \
})
#define SCSpinLock
#define SCSpinUnlock

atomic Compare and Switch

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

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

Referenced by main(), OutputRegisterFiledataLogger(), and PacketProfileLoggertIdToString().

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

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.

Referenced by PacketProfileLoggertIdToString().

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

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.

Referenced by TmModuleDecodeNetmapRegister().

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

Destroy the lock used to protect this variable.

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

Referenced by DefragHashShutdown(), DefragTrackerClearMemory(), FlowShutdown(), GlobalsInitPreConfig(), HostFree(), HostShutdown(), HTPDestroyMemcap(), IPPairFree(), IPPairShutdown(), LiveDeviceListClean(), PacketPoolDestroy(), StreamTcpFreeConfig(), and TagDestroyCtx().

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

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

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

#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; \
})
#define SCSpinLock
#define SCSpinUnlock

Get the value from the atomic variable.

Return values
varvalue

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

Referenced by AFPPeersListCheck(), AppLayerExpectationHandle(), CleanupPcapFileFileVars(), DefragHashShutdown(), DefragInitConfig(), DefragLookupTrackerFromHash(), DefragTrackerGetMemcap(), DefragTrackerGetMemuse(), DefragTrackerSetMemcap(), Detect(), DetectEngineInspectPktBufferGeneric(), ExpectationGetCounter(), FlowDisableFlowManagerThread(), FlowGetExistingFlowFromHash(), FlowGetMemcap(), FlowGetMemuse(), FlowHandlePacketUpdate(), FlowInitConfig(), FlowSetMemcap(), FlowSetupPacket(), FlowShutdown(), FlowUpdateState(), FlowWorkerGetDetectCtxPtr(), FTPMemcapGlobalCounter(), FTPMemuseGlobalCounter(), HostCleanup(), HostGetActiveCount(), HostGetMemcap(), HostGetMemuse(), HostInitConfig(), HostLookupHostFromHash(), HostPrintStats(), HostSetMemcap(), HostShutdown(), HTPGetMemcap(), HTPMemcapGlobalCounter(), HTPMemuseGlobalCounter(), HTPParseMemcap(), HTPSetMemcap(), IPPairCleanup(), IPPairGetActiveCount(), IPPairGetMemcap(), IPPairGetMemuse(), IPPairInitConfig(), IPPairLookupIPPairFromHash(), IPPairPrintStats(), IPPairSetMemcap(), IPPairShutdown(), LiveDevAddBypassSuccess(), LiveDeviceListClean(), NapatechPacketLoopZC(), NapatechStreamThreadExitStats(), NapatechStreamThreadInit(), OutputRegisterFiledataLogger(), PacketBypassCallback(), PacketPoolReturnPacket(), PcapFileGlobalInit(), SRepResetVersion(), StreamTcpCheckMemcap(), StreamTcpDecrMemuse(), StreamTcpGetMemcap(), StreamTcpInitConfig(), StreamTcpMemuseCounter(), StreamTcpReassembleCheckMemcap(), StreamTcpReassembleDecrMemuse(), StreamTcpReassembleGetMemcap(), StreamTcpReassembleMemuseGlobalCounter(), StreamTcpReassembleSetMemcap(), StreamTcpSetMemcap(), TagDestroyCtx(), TagHandlePacket(), TmModuleDecodeAFPRegister(), TmModuleDecodePcapRegister(), TmModuleDecodePfringRegister(), TmModuleFlowRecyclerRegister(), TmThreadDisableReceiveThreads(), TmThreadsCheckFlag(), TmThreadsSlotVarRun(), TmThreadWaitOnThreadInit(), VarNameStoreActivateStaging(), VarNameStoreFree(), VarNameStoreLookupById(), VarNameStoreLookupByName(), and VarNameStoreSetupStaging().

#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)
#define SCSpinLock
#define SCSpinUnlock

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.

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

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.

Referenced by AppLayerHtpEnableRequestBodyCallback(), AppLayerHtpEnableResponseBodyCallback(), AppLayerHtpNeedFileInspection(), FlowSetupPacket(), and TmThreadsSetFlag().

#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.

Referenced by PacketPoolReturnPacket(), PacketPoolWaitForN(), RunModeIdsAFPRegister(), RunModeIdsNetmapRegister(), RunModeIdsPfringRegister(), and UTHBuildPacketOfFlows().

#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 ; \
})
#define SCSpinLock
#define SCSpinUnlock

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.

Referenced by AppLayerExpectationGetDataId(), DefragHashShutdown(), DefragTrackerMoveToSpare(), DetectTagDataListFree(), FlowAlloc(), FlowFree(), FlowShutdown(), HostFree(), HostMoveToSpare(), HostShutdown(), HTPParseMemcap(), IPPairFree(), IPPairMoveToSpare(), IPPairShutdown(), LiveDevSubBypassStats(), RunModeIdsAFPRegister(), RunModeIdsNetmapRegister(), RunModeIdsPcapRegister(), RunModeIdsPfringRegister(), StreamTcpDecrMemuse(), StreamTcpReassembleDecrMemuse(), TagHashAddTag(), TagTimeoutCheck(), and TmModuleDecodeAFPRegister().

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

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

void SCAtomicRegisterTests ( void  )

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

References UtRegisterTest().

Here is the call graph for this function: