From 3ab7014871c1a2f1567d2cb7663d3390d441c10d Mon Sep 17 00:00:00 2001
From: Florian Fischer <florian.fl.fischer@fau.de>
Date: Sun, 11 Apr 2021 15:25:07 +0200
Subject: [PATCH] [IO] fix IoCompleterBehavior::none

Only create, initiateTermination and waitUntilFinished if the
completer thread is actually used.
---
 emper/Runtime.cpp | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/emper/Runtime.cpp b/emper/Runtime.cpp
index 5c36d3b4..d8d98c58 100644
--- a/emper/Runtime.cpp
+++ b/emper/Runtime.cpp
@@ -87,15 +87,13 @@ Runtime::Runtime(workerid_t workerCount, RuntimeStrategyFactory& strategyFactory
 		currentRuntime = this;
 	}
 
-	// initialize the global and all worker IoContexts
-	if constexpr (emper::IO) {
+	// initialize the global IoContext if a completer is used
+	if constexpr (emper::IO && emper::IO_COMPLETER_BEHAVIOR != emper::IoCompleterBehavior::none) {
 		// The global io_uring needs at least workerCount entries in its SQ because
 		// for each worker's IoContext one eventfd read is prepared before the
 		// globalCompleter is started and submits all previously prepared sqes.
 		globalIo = new GlobalIoContext(*this, workerCount);
-		if constexpr (emper::IO_COMPLETER_BEHAVIOR != emper::IoCompleterBehavior::none) {
-			globalIo->startGlobalCompleter();
-		}
+		globalIo->startGlobalCompleter();
 
 		if constexpr (emper::STATS) {
 			globalIo->stats.workerId = emper::io::Stats::GLOBAL_COMPLETER_ID;
@@ -175,10 +173,12 @@ Runtime::~Runtime() {
 	delete[] threads;
 
 	if constexpr (emper::IO) {
-		// It is safer to destroy the globalIo before the worker IoContexts
-		// because pointer to the worker IoContexts can outlife their objects
-		// in the globalCompleter
-		delete globalIo;
+		if constexpr (emper::IO_COMPLETER_BEHAVIOR != emper::IoCompleterBehavior::none) {
+			// It is safer to destroy the globalIo before the worker IoContexts
+			// because pointer to the worker IoContexts can outlife their objects
+			// in the globalCompleter
+			delete globalIo;
+		}
 
 		for (unsigned int i = 0; i < workerCount; ++i) {
 			delete ioContexts[i];
@@ -283,7 +283,7 @@ void Runtime::initiateTermination() {
 
 	wakeupSem.notify_many(workerCount);
 
-	if constexpr (emper::IO) {
+	if constexpr (emper::IO && emper::IO_COMPLETER_BEHAVIOR != emper::IoCompleterBehavior::none) {
 		globalIo->initiateTermination();
 	}
 }
@@ -296,7 +296,7 @@ void Runtime::waitUntilFinished() {
 		}
 	}
 
-	if constexpr (emper::IO) {
+	if constexpr (emper::IO && emper::IO_COMPLETER_BEHAVIOR != emper::IoCompleterBehavior::none) {
 		globalIo->waitUntilFinished();
 	}
 
-- 
GitLab