From 6bbd0099b5504eb2e5e7857aca0aef0141b58e11 Mon Sep 17 00:00:00 2001
From: Florian Fischer <florian.fl.fischer@fau.de>
Date: Wed, 10 Feb 2021 12:15:07 +0100
Subject: [PATCH] [IO] add option to use a common async backend for all
 io_urings

---
 emper/Emper.hpp        | 8 ++++++++
 emper/Runtime.hpp      | 2 +-
 emper/io/IoContext.cpp | 8 ++++++++
 meson.build            | 1 +
 meson_options.txt      | 6 ++++++
 5 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/emper/Emper.hpp b/emper/Emper.hpp
index bbebc22a..8a049800 100644
--- a/emper/Emper.hpp
+++ b/emper/Emper.hpp
@@ -89,4 +89,12 @@ static const bool IO_URING_SQPOLL =
 		false
 #endif
 		;
+
+static const bool IO_URING_SHARED_WQ =
+#ifdef EMPER_IO_URING_SHARED_WQ
+		true
+#else
+		false
+#endif
+		;
 }	 // namespace emper
diff --git a/emper/Runtime.hpp b/emper/Runtime.hpp
index 05c0ea51..803d935d 100644
--- a/emper/Runtime.hpp
+++ b/emper/Runtime.hpp
@@ -58,7 +58,7 @@ class Runtime : public Logger<LogSubsystem::RUNTI> {
 	ContextManager& contextManager;
 	pthread_t* threads;
 	Worker** workers;
-	IoContext* globalIo;
+	IoContext* globalIo = nullptr;
 	IoContext* ioContexts;
 
 	std::default_random_engine randomEngine;
diff --git a/emper/io/IoContext.cpp b/emper/io/IoContext.cpp
index b7c1c599..6768524f 100644
--- a/emper/io/IoContext.cpp
+++ b/emper/io/IoContext.cpp
@@ -351,6 +351,14 @@ IoContext::IoContext(size_t uring_entries) {
 		params.flags |= IORING_SETUP_SQPOLL;
 	}
 
+	if constexpr (emper::IO_URING_SHARED_WQ) {
+		auto *gio = getGlobalIo();
+		if (gio) {
+			params.flags |= IORING_SETUP_ATTACH_WQ;
+			params.wq_fd = gio->ring.ring_fd;
+		}
+	}
+
 	auto ret = io_uring_queue_init_params(uring_entries, &ring, &params);
 	if (ret < 0) {
 		DIE_MSG_ERRNO("io_uring_queue_init failed");
diff --git a/meson.build b/meson.build
index 00d12552..8385425d 100644
--- a/meson.build
+++ b/meson.build
@@ -56,6 +56,7 @@ endif
 
 io_bool_options = [
 	'uring_sqpoll',
+	'uring_shared_wq',
 ]
 
 io_raw_options = [
diff --git a/meson_options.txt b/meson_options.txt
index 3fa7046e..ab067837 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -92,3 +92,9 @@ option(
   value: false,
   description: 'Enable io_urings SQPOLL feature (start a separate kernel thread which polls the sq reducing the amount of syscalls to submit new requests. This is a privileged operation.).'
 )
+option(
+  'io_uring_shared_wq',
+  type: 'boolean',
+  value: false,
+  description: 'Share a common async backend between all io_urings'
+)
-- 
GitLab