]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
flow: fix condition signalling
authorVictor Julien <vjulien@oisf.net>
Wed, 22 Nov 2023 08:03:09 +0000 (09:03 +0100)
committerVictor Julien <victor@inliniac.net>
Fri, 24 Nov 2023 10:57:11 +0000 (11:57 +0100)
Signal threads while holding lock. This should make the signalling
more reliable.

From PTHREAD_COND(3):

"Unlocking the mutex and suspending on the condition variable is done
 atomically. Thus, if all threads always acquire the mutex before
 signaling the condition, this guarantees that the condition cannot be
 signaled (and thus ignored) between the time a thread locks the
 mutex and the time it waits on the condition variable."

Ticket: #6569.

src/flow-manager.c
src/flow-manager.h

index e5e1aa270276978944834cf3d33c5dbe7d6115d9..bcc1498c8bd880c6f5e639bb08b7a87d4d4a207e 100644 (file)
@@ -84,10 +84,24 @@ SC_ATOMIC_DECLARE(uint32_t, flowrec_cnt);
 SC_ATOMIC_DECLARE(uint32_t, flowrec_busy);
 SC_ATOMIC_EXTERN(unsigned int, flow_flags);
 
-SCCtrlCondT flow_manager_ctrl_cond;
-SCCtrlMutex flow_manager_ctrl_mutex;
-SCCtrlCondT flow_recycler_ctrl_cond;
-SCCtrlMutex flow_recycler_ctrl_mutex;
+static SCCtrlCondT flow_manager_ctrl_cond = PTHREAD_COND_INITIALIZER;
+static SCCtrlMutex flow_manager_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
+static SCCtrlCondT flow_recycler_ctrl_cond = PTHREAD_COND_INITIALIZER;
+static SCCtrlMutex flow_recycler_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+void FlowWakeupFlowManagerThread(void)
+{
+    SCCtrlMutexLock(&flow_manager_ctrl_mutex);
+    SCCtrlCondSignal(&flow_manager_ctrl_cond);
+    SCCtrlMutexUnlock(&flow_manager_ctrl_mutex);
+}
+
+void FlowWakeupFlowRecyclerThread(void)
+{
+    SCCtrlMutexLock(&flow_recycler_ctrl_mutex);
+    SCCtrlCondSignal(&flow_recycler_ctrl_cond);
+    SCCtrlMutexUnlock(&flow_recycler_ctrl_mutex);
+}
 
 void FlowTimeoutsInit(void)
 {
@@ -942,9 +956,6 @@ void FlowManagerThreadSpawn(void)
     }
     flowmgr_number = (uint32_t)setting;
 
-    SCCtrlCondInit(&flow_manager_ctrl_cond, NULL);
-    SCCtrlMutexInit(&flow_manager_ctrl_mutex, NULL);
-
     SCLogConfig("using %u flow manager threads", flowmgr_number);
     StatsRegisterGlobalCounter("flow.memuse", FlowGetMemuse);
 
@@ -1148,9 +1159,6 @@ void FlowRecyclerThreadSpawn(void)
     }
     flowrec_number = (uint32_t)setting;
 
-    SCCtrlCondInit(&flow_recycler_ctrl_cond, NULL);
-    SCCtrlMutexInit(&flow_recycler_ctrl_mutex, NULL);
-
     SCLogConfig("using %u flow recycler threads", flowrec_number);
 
     for (uint32_t u = 0; u < flowrec_number; u++) {
index 157358d170f59dca64d62eadff845a6475103150..7cdd017000aa9b953392ec5ee77bd702207fac7e 100644 (file)
 #ifndef __FLOW_MANAGER_H__
 #define __FLOW_MANAGER_H__
 
-/** flow manager scheduling condition */
-extern SCCtrlCondT flow_manager_ctrl_cond;
-extern SCCtrlMutex flow_manager_ctrl_mutex;
-#define FlowWakeupFlowManagerThread() SCCtrlCondSignal(&flow_manager_ctrl_cond)
-extern SCCtrlCondT flow_recycler_ctrl_cond;
-extern SCCtrlMutex flow_recycler_ctrl_mutex;
-#define FlowWakeupFlowRecyclerThread() SCCtrlCondSignal(&flow_recycler_ctrl_cond)
+void FlowWakeupFlowManagerThread(void);
+void FlowWakeupFlowRecyclerThread(void);
 
 #define FlowTimeoutsReset() FlowTimeoutsInit()
 void FlowTimeoutsInit(void);