From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: LekKit <50500857+LekKit@users.noreply.github.com>
Date: Sat, 23 Mar 2024 13:31:42 +0200
Subject: [PATCH] Disable threaded IO on Minerva

Due to sloppy scheduler/threading behavior on Minerva,
threaded IO is disabled in this port for now.
Otherwise U-Boot randomly fails to read data from NVMe,
or fails to initialize NVMe altogether, along with other IO
issues in guests - all due to threaded tasks being randomly
delayed for very long.

I am not an expert on how scheduler works in Minerva,
so I am unable to fix it yet.
This problem was also visible in previous v0.5 version of this port,
but back then I thought it's some kind of a temporary problem.
Couldn't reproduce this on any other host OS.
---
 src/threading.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/threading.c b/src/threading.c
index 98883ede4fe63faf6b328e427fc1be9e5114c445..705749ca60092ddc42f07ff19d8a66bce797dc80 100644
--- a/src/threading.c
+++ b/src/threading.c
@@ -318,6 +318,12 @@ void condvar_free(cond_var_t* cond)
 
 // Threadpool task offloading
 
+// Minerva has serious scheduler issues that cause threadpool to
+// perform very poorly when vCPU is not sleeping.
+// Basically a thread that uses 100% of CPU on Minerva somehow
+// monopolizes host CPU so threadpool/eventloop tasks are timing out.
+
+#ifndef __minerva__
 #define WORKER_THREADS 4
 #define WORKQUEUE_SIZE 2048
 #define WORKQUEUE_MASK (WORKQUEUE_SIZE - 1)
@@ -440,8 +446,11 @@ static void* threadpool_worker(void* ptr)
     return ptr;
 }
 
+#endif
+
 static bool thread_queue_task(thread_func_t func, void** arg, unsigned arg_count, bool va)
 {
+#ifndef __minerva__
     DO_ONCE ({
         atomic_store_uint32_ex(&pool_run, 1, ATOMIC_RELAXED);
         workqueue_init(&pool_wq);
@@ -461,6 +470,9 @@ static bool thread_queue_task(thread_func_t func, void** arg, unsigned arg_count
     // Still not queued!
     // Assuming entire threadpool is busy, just do a blocking task
     DO_ONCE(rvvm_warn("Blocking on workqueue task %p", func));
+#else
+    UNUSED(func); UNUSED(arg); UNUSED(arg_count); UNUSED(va);
+#endif
     return false;
 }
 
