atomics: change SC_ATOMIC_ADD to 'fetch_add'

Until this point the SC_ATOMIC_ADD macro pointed to a 'add_fetch'
intrinsic. This patch changes it to a 'fetch_add'.

There are 2 reasons for this:

1. C11 stdatomics.h has only 'atomic_fetch_add' and no 'add_fetch'
   So this patch prepares for adding support for C11 atomics.

2. It was not consistent with SC_ATOMIC_SUB, which did use 'fetch_sub'
   and not 'sub_fetch'.

Most callers are not using the return value, so these are unaffected.
The callers that do use the return value are updated.
pull/4832/head
Victor Julien 5 years ago
parent 109b2ae551
commit 531ff3ddec

@ -550,7 +550,7 @@ static TmEcode DetectLoaderThreadInit(ThreadVars *t, const void *initdata, void
if (ftd == NULL) if (ftd == NULL)
return TM_ECODE_FAILED; return TM_ECODE_FAILED;
ftd->instance = SC_ATOMIC_ADD(detect_loader_cnt, 1) - 1; /* id's start at 0 */ ftd->instance = SC_ATOMIC_ADD(detect_loader_cnt, 1); /* id's start at 0 */
SCLogDebug("detect loader instance %u", ftd->instance); SCLogDebug("detect loader instance %u", ftd->instance);
/* pass thread data back to caller */ /* pass thread data back to caller */

@ -660,9 +660,9 @@ static TmEcode FlowManagerThreadInit(ThreadVars *t, const void *initdata, void *
/* set the min and max value used for hash row walking /* set the min and max value used for hash row walking
* each thread has it's own section of the flow hash */ * each thread has it's own section of the flow hash */
uint32_t range = flow_config.hash_size / flowmgr_number; uint32_t range = flow_config.hash_size / flowmgr_number;
if (ftd->instance == 1) if (ftd->instance == 0)
ftd->max = range; ftd->max = range;
else if (ftd->instance == flowmgr_number) { else if ((ftd->instance + 1) == flowmgr_number) {
ftd->min = (range * (ftd->instance - 1)); ftd->min = (range * (ftd->instance - 1));
ftd->max = flow_config.hash_size; ftd->max = flow_config.hash_size;
} else { } else {

@ -209,6 +209,7 @@ void PcapLogRegister(void)
PcapLogDataDeinit, NULL); PcapLogDataDeinit, NULL);
PcapLogProfileSetup(); PcapLogProfileSetup();
SC_ATOMIC_INIT(thread_cnt); SC_ATOMIC_INIT(thread_cnt);
SC_ATOMIC_SET(thread_cnt, 1); /* first id is 1 */
return; return;
} }

@ -418,6 +418,7 @@ void LogTlsStoreRegister (void)
LogTlsStoreLogThreadDeinit, LogTlsStoreLogExitPrintStats); LogTlsStoreLogThreadDeinit, LogTlsStoreLogExitPrintStats);
SC_ATOMIC_INIT(cert_id); SC_ATOMIC_INIT(cert_id);
SC_ATOMIC_SET(cert_id, 1);
SCLogDebug("registered"); SCLogDebug("registered");
} }

@ -258,7 +258,7 @@ static void LogFiledataLogLoadWaldo(const char *path)
if (fgets(line, (int)sizeof(line), fp) != NULL) { if (fgets(line, (int)sizeof(line), fp) != NULL) {
if (sscanf(line, "%10u", &id) == 1) { if (sscanf(line, "%10u", &id) == 1) {
SCLogInfo("id %u", id); SCLogInfo("id %u", id);
(void) SC_ATOMIC_CAS(&g_file_store_id, 0, id); SC_ATOMIC_SET(g_file_store_id, id);
} }
} }
fclose(fp); fclose(fp);
@ -275,7 +275,7 @@ static void LogFiledataLogStoreWaldo(const char *path)
{ {
char line[16] = ""; char line[16] = "";
if (SC_ATOMIC_GET(g_file_store_id) == 0) { if (SC_ATOMIC_GET(g_file_store_id) == 1) {
SCReturn; SCReturn;
} }
@ -448,6 +448,7 @@ void OutputFiledataLoggerRegister(void)
OutputFiledataLogThreadDeinit, OutputFiledataLogExitPrintStats, OutputFiledataLogThreadDeinit, OutputFiledataLogExitPrintStats,
OutputFiledataLog, OutputFiledataLoggerGetActiveCount); OutputFiledataLog, OutputFiledataLoggerGetActiveCount);
SC_ATOMIC_INIT(g_file_store_id); SC_ATOMIC_INIT(g_file_store_id);
SC_ATOMIC_SET(g_file_store_id, 1);
} }
void OutputFiledataShutdown(void) void OutputFiledataShutdown(void)

@ -88,7 +88,7 @@ static void AFPDerefConfig(void *conf)
{ {
AFPIfaceConfig *pfp = (AFPIfaceConfig *)conf; AFPIfaceConfig *pfp = (AFPIfaceConfig *)conf;
/* Pcap config is used only once but cost of this low. */ /* Pcap config is used only once but cost of this low. */
if (SC_ATOMIC_SUB(pfp->ref, 1) == 0) { if (SC_ATOMIC_SUB(pfp->ref, 1) == 1) {
SCFree(pfp); SCFree(pfp);
} }
} }

@ -84,7 +84,7 @@ static void NetmapDerefConfig(void *conf)
{ {
NetmapIfaceConfig *pfp = (NetmapIfaceConfig *)conf; NetmapIfaceConfig *pfp = (NetmapIfaceConfig *)conf;
/* config is used only once but cost of this low. */ /* config is used only once but cost of this low. */
if (SC_ATOMIC_SUB(pfp->ref, 1) == 0) { if (SC_ATOMIC_SUB(pfp->ref, 1) == 1) {
SCFree(pfp); SCFree(pfp);
} }
} }

@ -63,7 +63,7 @@ static void PcapDerefConfig(void *conf)
{ {
PcapIfaceConfig *pfp = (PcapIfaceConfig *)conf; PcapIfaceConfig *pfp = (PcapIfaceConfig *)conf;
/* Pcap config is used only once but cost of this low. */ /* Pcap config is used only once but cost of this low. */
if (SC_ATOMIC_SUB(pfp->ref, 1) == 0) { if (SC_ATOMIC_SUB(pfp->ref, 1) == 1) {
SCFree(pfp); SCFree(pfp);
} }
} }

@ -70,7 +70,7 @@ void RunModeIdsPfringRegister(void)
static void PfringDerefConfig(void *conf) static void PfringDerefConfig(void *conf)
{ {
PfringIfaceConfig *pfp = (PfringIfaceConfig *)conf; PfringIfaceConfig *pfp = (PfringIfaceConfig *)conf;
if (SC_ATOMIC_SUB(pfp->ref, 1) == 0) { if (SC_ATOMIC_SUB(pfp->ref, 1) == 1) {
if (pfp->bpf_filter) { if (pfp->bpf_filter) {
SCFree(pfp->bpf_filter); SCFree(pfp->bpf_filter);
} }

@ -507,7 +507,7 @@ static void AFPPeersListReachedInc(void)
if (peerslist.turn == 0) if (peerslist.turn == 0)
return; return;
if (SC_ATOMIC_ADD(peerslist.reached, 1) == peerslist.turn) { if ((SC_ATOMIC_ADD(peerslist.reached, 1) + 1) == peerslist.turn) {
SCLogInfo("All AFP capture threads are running."); SCLogInfo("All AFP capture threads are running.");
(void)SC_ATOMIC_SET(peerslist.reached, 0); (void)SC_ATOMIC_SET(peerslist.reached, 0);
/* Set turn to 0 to skip syncrhonization when ReceiveAFPLoop is /* Set turn to 0 to skip syncrhonization when ReceiveAFPLoop is
@ -1228,7 +1228,7 @@ static int AFPDerefSocket(AFPPeer* peer)
if (peer == NULL) if (peer == NULL)
return 1; return 1;
if (SC_ATOMIC_SUB(peer->sock_usage, 1) == 0) { if (SC_ATOMIC_SUB(peer->sock_usage, 1) == 1) {
if (SC_ATOMIC_GET(peer->state) == AFP_STATE_DOWN) { if (SC_ATOMIC_GET(peer->state) == AFP_STATE_DOWN) {
SCLogInfo("Cleaning socket connected to '%s'", peer->iface); SCLogInfo("Cleaning socket connected to '%s'", peer->iface);
close(SC_ATOMIC_GET(peer->socket)); close(SC_ATOMIC_GET(peer->socket));
@ -1265,7 +1265,7 @@ static void AFPSwitchState(AFPThreadVars *ptv, int state)
#endif #endif
if (ptv->socket != -1) { if (ptv->socket != -1) {
/* we need to wait for all packets to return data */ /* we need to wait for all packets to return data */
if (SC_ATOMIC_SUB(ptv->mpeer->sock_usage, 1) == 0) { if (SC_ATOMIC_SUB(ptv->mpeer->sock_usage, 1) == 1) {
SCLogDebug("Cleaning socket connected to '%s'", ptv->iface); SCLogDebug("Cleaning socket connected to '%s'", ptv->iface);
munmap(ptv->ring_buf, ptv->ring_buflen); munmap(ptv->ring_buf, ptv->ring_buflen);
close(ptv->socket); close(ptv->socket);

@ -1,4 +1,4 @@
/* Copyright (C) 2007-2013 Open Information Security Foundation /* Copyright (C) 2007-2020 Open Information Security Foundation
* *
* You can copy, redistribute or modify this Program under the terms of * You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free * the GNU General Public License version 2 as published by the Free
@ -185,7 +185,7 @@
* \param val the value to add to the variable * \param val the value to add to the variable
*/ */
#define SC_ATOMIC_ADD(name, val) \ #define SC_ATOMIC_ADD(name, val) \
SCAtomicAddAndFetch(&(name ## _sc_atomic__), (val)) SCAtomicFetchAndAdd(&(name ## _sc_atomic__), (val))
/** /**
* \brief sub a value from our atomic variable * \brief sub a value from our atomic variable
@ -194,7 +194,7 @@
* \param val the value to sub from the variable * \param val the value to sub from the variable
*/ */
#define SC_ATOMIC_SUB(name, val) \ #define SC_ATOMIC_SUB(name, val) \
SCAtomicSubAndFetch(&(name ## _sc_atomic__), (val)) SCAtomicFetchAndSub(&(name ## _sc_atomic__), (val))
/** /**
* \brief Bitwise OR a value to our atomic variable * \brief Bitwise OR a value to our atomic variable

Loading…
Cancel
Save