]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
atomics: change SC_ATOMIC_ADD to 'fetch_add'
authorVictor Julien <victor@inliniac.net>
Sun, 12 Apr 2020 09:06:32 +0000 (11:06 +0200)
committerVictor Julien <victor@inliniac.net>
Thu, 16 Apr 2020 12:37:34 +0000 (14:37 +0200)
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.

src/detect-engine-loader.c
src/flow-manager.c
src/log-pcap.c
src/log-tlsstore.c
src/output-filedata.c
src/runmode-af-packet.c
src/runmode-netmap.c
src/runmode-pcap.c
src/runmode-pfring.c
src/source-af-packet.c
src/util-atomic.h

index fba37b8fa84d5aa82cddff9068d207b6f9e62025..271c31e9500731752ea316f300c611b0c5e28035 100644 (file)
@@ -550,7 +550,7 @@ static TmEcode DetectLoaderThreadInit(ThreadVars *t, const void *initdata, void
     if (ftd == NULL)
         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);
 
     /* pass thread data back to caller */
index 26a452a23d0bbfb262cce97751b98721e0e267c9..6aecc9c8b3af92b10862121ec995c821da3c6d0c 100644 (file)
@@ -660,9 +660,9 @@ static TmEcode FlowManagerThreadInit(ThreadVars *t, const void *initdata, void *
     /* set the min and max value used for hash row walking
      * each thread has it's own section of the flow hash */
     uint32_t range = flow_config.hash_size / flowmgr_number;
-    if (ftd->instance == 1)
+    if (ftd->instance == 0)
         ftd->max = range;
-    else if (ftd->instance == flowmgr_number) {
+    else if ((ftd->instance + 1) == flowmgr_number) {
         ftd->min = (range * (ftd->instance - 1));
         ftd->max = flow_config.hash_size;
     } else {
index f813d15c5083b6bfa5ac01460f6018210783ba71..dda5fbbc5cf7ee966d96d73b452033114646b022 100644 (file)
@@ -209,6 +209,7 @@ void PcapLogRegister(void)
         PcapLogDataDeinit, NULL);
     PcapLogProfileSetup();
     SC_ATOMIC_INIT(thread_cnt);
+    SC_ATOMIC_SET(thread_cnt, 1); /* first id is 1 */
     return;
 }
 
index 8ada7bcbadb50a1923b690e4a8849d6ee9071707..8bf5d28573844538df4e37b796042b300c714a95 100644 (file)
@@ -418,6 +418,7 @@ void LogTlsStoreRegister (void)
         LogTlsStoreLogThreadDeinit, LogTlsStoreLogExitPrintStats);
 
     SC_ATOMIC_INIT(cert_id);
+    SC_ATOMIC_SET(cert_id, 1);
 
     SCLogDebug("registered");
 }
index 9414513759522375fafd8bd38f3c22c88a88b9ec..42b52d7b126625dc6b3abf5d99ca1e51ec516190 100644 (file)
@@ -258,7 +258,7 @@ static void LogFiledataLogLoadWaldo(const char *path)
     if (fgets(line, (int)sizeof(line), fp) != NULL) {
         if (sscanf(line, "%10u", &id) == 1) {
             SCLogInfo("id %u", id);
-            (void) SC_ATOMIC_CAS(&g_file_store_id, 0, id);
+            SC_ATOMIC_SET(g_file_store_id, id);
         }
     }
     fclose(fp);
@@ -275,7 +275,7 @@ static void LogFiledataLogStoreWaldo(const char *path)
 {
     char line[16] = "";
 
-    if (SC_ATOMIC_GET(g_file_store_id) == 0) {
+    if (SC_ATOMIC_GET(g_file_store_id) == 1) {
         SCReturn;
     }
 
@@ -448,6 +448,7 @@ void OutputFiledataLoggerRegister(void)
         OutputFiledataLogThreadDeinit, OutputFiledataLogExitPrintStats,
         OutputFiledataLog, OutputFiledataLoggerGetActiveCount);
     SC_ATOMIC_INIT(g_file_store_id);
+    SC_ATOMIC_SET(g_file_store_id, 1);
 }
 
 void OutputFiledataShutdown(void)
index 9fd1fa8f76978004db1eb06e9ada4940958cf91a..3e62ad3e86008bfd5e378c166af9c82c528520b7 100644 (file)
@@ -88,7 +88,7 @@ static void AFPDerefConfig(void *conf)
 {
     AFPIfaceConfig *pfp = (AFPIfaceConfig *)conf;
     /* 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);
     }
 }
index 9280764a8155e6f12551bb794ec34b09116d0045..6962a33c8972362be8629ca108275a56ea2ed983 100644 (file)
@@ -84,7 +84,7 @@ static void NetmapDerefConfig(void *conf)
 {
     NetmapIfaceConfig *pfp = (NetmapIfaceConfig *)conf;
     /* 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);
     }
 }
index db22d2f0a178e6a403f6b412857a87010ec935c6..76c25e258f94182ac961afd680d8099bc5a1e6ee 100644 (file)
@@ -63,7 +63,7 @@ static void PcapDerefConfig(void *conf)
 {
     PcapIfaceConfig *pfp = (PcapIfaceConfig *)conf;
     /* 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);
     }
 }
index 0c17a0533ac3736a2ffd5688c62219de4a69a0b1..1ade2024277fa31ec501c6a3cf9ba53ff22fda51 100644 (file)
@@ -70,7 +70,7 @@ void RunModeIdsPfringRegister(void)
 static void PfringDerefConfig(void *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) {
             SCFree(pfp->bpf_filter);
         }
index c38066155877cdfa3f186f476c69c126a2bd4f57..f3cb43b2b78561a1460bf0bc21c63ebcc6afa4e8 100644 (file)
@@ -507,7 +507,7 @@ static void AFPPeersListReachedInc(void)
     if (peerslist.turn == 0)
         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.");
         (void)SC_ATOMIC_SET(peerslist.reached, 0);
         /* Set turn to 0 to skip syncrhonization when ReceiveAFPLoop is
@@ -1228,7 +1228,7 @@ static int AFPDerefSocket(AFPPeer* peer)
     if (peer == NULL)
         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) {
             SCLogInfo("Cleaning socket connected to '%s'", peer->iface);
             close(SC_ATOMIC_GET(peer->socket));
@@ -1265,7 +1265,7 @@ static void AFPSwitchState(AFPThreadVars *ptv, int state)
 #endif
         if (ptv->socket != -1) {
             /* 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);
                 munmap(ptv->ring_buf, ptv->ring_buflen);
                 close(ptv->socket);
index ba38ffc1b137e7d79f7037a1bb18f5afc53e3d24..85d0a02935440e624d63b1eb1c34ad4c50e2548b 100644 (file)
@@ -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
  * the GNU General Public License version 2 as published by the Free
  *  \param val the value to add to the variable
  */
 #define SC_ATOMIC_ADD(name, val) \
-    SCAtomicAddAndFetch(&(name ## _sc_atomic__), (val))
+    SCAtomicFetchAndAdd(&(name ## _sc_atomic__), (val))
 
 /**
  *  \brief sub a value from our atomic variable
  *  \param val the value to sub from the variable
  */
 #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