From 2caaa0bcd237aaf272f548b8a852f03a172ec1d5 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 29 Apr 2020 19:04:38 +0200
Subject: [PATCH] Small corrections

---
 src/module/module_sdl2.c |  5 +++--
 src/mthread/mthread.c    |  5 +++--
 src/net/listen.c         |  5 +++--
 src/repo/async.c         |  1 +
 src/thread.c             | 22 ++++++++++------------
 5 files changed, 20 insertions(+), 18 deletions(-)

diff --git a/src/module/module_sdl2.c b/src/module/module_sdl2.c
index 6b8ccfd4..6ec3defe 100644
--- a/src/module/module_sdl2.c
+++ b/src/module/module_sdl2.c
@@ -147,8 +147,7 @@ sdl_thread__(struct poller_thread_arg *arg)
     LOG_INFO_SCT("WINDOW", "%s", "Started SDL thread");
 
 loop:
-    if (SDL_PollEvent(&event))
-        sched_yield();
+    SDL_WaitEvent(&event);
 
     switch (event.type) {
     case SDL_QUIT:
@@ -207,6 +206,8 @@ loop:
         SDL_GL_SwapWindow((SDL_Window *) sdl2->window);
         redraw = 0;
     }
+
+    sched_yield();
     goto loop;  /* A loop without indentation. */
 }
 
diff --git a/src/mthread/mthread.c b/src/mthread/mthread.c
index 898f8401..7f9dae67 100644
--- a/src/mthread/mthread.c
+++ b/src/mthread/mthread.c
@@ -9,7 +9,7 @@
 #endif
 
 #ifdef TWO_LEVEL
-#define MTHREAD_LWP 4
+#define MTHREAD_LWP 3
 #else
 #define MTHREAD_LWP 1
 #endif
@@ -157,6 +157,8 @@ mthread_work_take(mthread_virtual_processor_t *vp)
 void
 __mthread_yield(mthread_virtual_processor_t *vp)
 {
+    sched_yield();
+
     struct mthread_s *current = (struct mthread_s *)vp->current;
     struct mthread_s *next    = mthread_remove_first(&(vp->ready_list));
 
@@ -198,7 +200,6 @@ __mthread_yield(mthread_virtual_processor_t *vp)
         mthread_spinlock_unlock(vp->p);
         vp->p = NULL;
     }
-
 }
 
 static void
diff --git a/src/net/listen.c b/src/net/listen.c
index 6fac8d90..9a6a226d 100644
--- a/src/net/listen.c
+++ b/src/net/listen.c
@@ -10,6 +10,7 @@
 
 #include <sqlite3.h>
 
+#include <sched.h>
 #include <assert.h>
 #include <arpa/inet.h>
 #include <errno.h>
@@ -479,9 +480,9 @@ failure:
     }
 
     if (host)
-        LOG_ERROR_SCT("NETWORK", "Listening on %s:%s", host, port);
+        LOG_INFO_SCT("NETWORK", "Listening on %s:%s", host, port);
     else
-        LOG_ERROR_SCT("NETWORK", "Listening on port %s", port);
+        LOG_INFO_SCT("NETWORK", "Listening on port %s", port);
 
     return fd;
 }
diff --git a/src/repo/async.c b/src/repo/async.c
index d883b4ce..0befc290 100644
--- a/src/repo/async.c
+++ b/src/repo/async.c
@@ -178,6 +178,7 @@ try_later:
             LOG_ERROR("%s", "Failed to get the head of the input list");
 
 end_loop:
+        mthread_yield();
         sleep(1);
     }
 
diff --git a/src/thread.c b/src/thread.c
index 82112f27..31fe5559 100644
--- a/src/thread.c
+++ b/src/thread.c
@@ -29,7 +29,6 @@ int
 lkt_th_new(struct poller_thread *th, unsigned int init_sizes,
            void *(*func)(struct poller_thread_arg *), void *args)
 {
-    int ret = 1;
     mthread_mutex_t mtx1 = MTHREAD_MUTEX_INITIALIZER;
     mthread_mutex_t mtx2 = MTHREAD_MUTEX_INITIALIZER;
     struct poller_thread th_ = {
@@ -53,16 +52,16 @@ lkt_th_new(struct poller_thread *th, unsigned int init_sizes,
     __args->arg = args;
     __args->arg->self = th;
 
-    ret = mthread_create(&(th->th), NULL, __start, __args);
-
-    if (ret)
-        goto end;
+    if(!mthread_create(&(th->th), NULL, __start, __args)) {
+        LOG_INFO_SCT("THREAD", "%s", "Create a new poller thread");
+        return 0;
+    }
 
-end:
-    fprintf(stderr, " %c lkt_th_new: %s\n",
-            ret ? '!' : '*',
-            ret ? "Failed to create a new thread" : "Created a new thread");
-    return ret;
+    else {
+error:
+        LOG_ERROR_SCT("THREAD", "%s", "Failed to create a poller thread");
+        return 1;
+    }
 
 out_of_memory:
     LOG_ERROR_SCT("MEMORY", "%s", "Out of memory");
@@ -70,8 +69,7 @@ out_of_memory:
         free((void *) th_.input_cells);
     if (th_.output_cells)
         free((void *) th_.output_cells);
-    ret = errno = ENOMEM;
-    goto end;
+    goto error;
 }
 
 int
-- 
GitLab