From 7ea6349839d80d1dc46225cce97486b0066206b1 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Thu, 30 Apr 2020 10:01:36 +0200
Subject: [PATCH] Name replace for poller threads

---
 inc/lektor/thread.h      | 16 ++++++++--------
 scripts/astyle.sh        |  2 +-
 src/commands.c           |  3 ++-
 src/module/module_sdl2.c |  2 +-
 src/net/listen.c         | 15 ++++++++-------
 src/repo/downloader.c    | 14 +++++++-------
 src/thread.c             | 16 ++++++++--------
 7 files changed, 35 insertions(+), 33 deletions(-)

diff --git a/inc/lektor/thread.h b/inc/lektor/thread.h
index dcd3d75c..8187b561 100644
--- a/inc/lektor/thread.h
+++ b/inc/lektor/thread.h
@@ -28,18 +28,18 @@ struct poller_thread_arg {
 
 
 /* Create a new thread. Returns 0 on success, a non zero value on error. */
-int lkt_th_new(struct poller_thread *th, unsigned int init_sizes,
+int poller_new(struct poller_thread *th, unsigned int init_sizes,
                void *(*func)(struct poller_thread_arg *), void *args);
 
 /* Joins a thread. Returns 0 on success, a non zero value on error. */
-int lkt_th_join(struct poller_thread *th, void **ret);
+int poller_join(struct poller_thread *th, void **ret);
 
 /* Input qeueu manipulations. */
-int lkt_th_append_input(struct poller_thread *th, void *ptr);
-int lkt_th_pop_input(struct poller_thread *th, void **ptr);
-int lkt_th_head_input(struct poller_thread *th, void **ptr);
+int poller_append_input(struct poller_thread *th, void *ptr);
+int poller_pop_input(struct poller_thread *th, void **ptr);
+int poller_head_input(struct poller_thread *th, void **ptr);
 
 /* Output qeueu manipulations. */
-int lkt_th_append_output(struct poller_thread *th, void *ptr);
-int lkt_th_pop_output(struct poller_thread *th, void **ptr);
-int lkt_th_head_output(struct poller_thread *th, void **ptr);
+int poller_append_output(struct poller_thread *th, void *ptr);
+int poller_pop_output(struct poller_thread *th, void **ptr);
+int poller_head_output(struct poller_thread *th, void **ptr);
diff --git a/scripts/astyle.sh b/scripts/astyle.sh
index 234f5330..0a4a45c5 100755
--- a/scripts/astyle.sh
+++ b/scripts/astyle.sh
@@ -10,7 +10,7 @@ OPTIONS='
   --break-return-type
   --convert-tabs
   --close-templates
-  --max-code-length=110
+  --max-code-length=140
   --mode=c
   --lineend=linux
   --quiet'
diff --git a/src/commands.c b/src/commands.c
index 57f40c85..fd48677b 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -931,7 +931,8 @@ command_sticker_get(struct lkt_state *srv, size_t c, char *argv[LKT_MESSAGE_ARGS
         .c = c,
         .call = sticker_send_one_value,
     };
-    RETURN_UNLESS(database_sticker_get((sqlite3 *) srv->db, argv[0], argv[2], uri, &cb), "Failed to get sticker", false);
+    RETURN_UNLESS(database_sticker_get((sqlite3 *) srv->db, argv[0], argv[2], uri, &cb), "Failed to get sticker",
+                  false);
     return true;
 }
 
diff --git a/src/module/module_sdl2.c b/src/module/module_sdl2.c
index 659b5ab7..2432ca9d 100644
--- a/src/module/module_sdl2.c
+++ b/src/module/module_sdl2.c
@@ -261,7 +261,7 @@ module_sdl2_new(struct lkt_win *const win)
         struct poller_thread_arg *arg = calloc(1, sizeof(struct poller_thread_arg));
         RETURN_UNLESS(arg, "Out of memory", false);
         arg->args = win;
-        RETURN_IF(lkt_th_new(&((struct module_sdl2_window *) win->window)->self,
+        RETURN_IF(poller_new(&((struct module_sdl2_window *) win->window)->self,
                              LKT_DEFAULT_LIST_SIZE, sdl_thread__, arg),
                   "Failed to launch the SDL thread", false);
     }
diff --git a/src/net/listen.c b/src/net/listen.c
index 6e526aea..8b52ef84 100644
--- a/src/net/listen.c
+++ b/src/net/listen.c
@@ -185,7 +185,8 @@ handle_simple_command(struct lkt_state *srv, size_t c, struct lkt_command cmd)
         else if (!strcmp(cmd.name, "play"))
             err = ! command_play((sqlite3 *) srv->db, &srv->win, cmd.args, (enum mpd_idle_flag *) &srv->mpd_idle_events);
         else if (!strcmp(cmd.name, "playid"))
-            err = ! command_playid((sqlite3 *) srv->db, &srv->win, cmd.args, (enum mpd_idle_flag *) &srv->mpd_idle_events);
+            err = ! command_playid((sqlite3 *) srv->db, &srv->win, cmd.args,
+                                   (enum mpd_idle_flag *) &srv->mpd_idle_events);
         else if (!strcmp(cmd.name, "stop"))
             err = !command_stop((sqlite3 *) srv->db, &srv->win, (enum mpd_idle_flag *) &srv->mpd_idle_events);
         else if (!strcmp(cmd.name, "clear"))
@@ -227,7 +228,7 @@ handle_simple_command(struct lkt_state *srv, size_t c, struct lkt_command cmd)
         else if (!strcmp(cmd.name, "playlistclear"))
             err = ! command_plt_clear((sqlite3 *) srv->db, cmd.args, (enum mpd_idle_flag *) &srv->mpd_idle_events);
         else if (!strcmp(cmd.name, "rename"))
-            err = ! command_plt_rename((sqlite3* ) srv->db, cmd.args, (enum mpd_idle_flag *) &srv->mpd_idle_events);
+            err = ! command_plt_rename((sqlite3 * ) srv->db, cmd.args, (enum mpd_idle_flag *) &srv->mpd_idle_events);
         else if (!strcmp(cmd.name, "playlistdelete"))
             err = ! command_plt_remove((sqlite3 *) srv->db, cmd.args, (enum mpd_idle_flag *) &srv->mpd_idle_events);
         else if (!strcmp(cmd.name, "playlistadd"))
@@ -759,11 +760,11 @@ lkt_listen(void)
     RETURN_UNLESS(database_open((sqlite3 *) srv.db, db_path), "Can't open database", 1);
 
     /* Read the configuration. */
-    RETURN_UNLESS(database_config_get_int((sqlite3 *) srv.db, "player", "autoclear", &autoclear), "Cfg error", 2);
-    RETURN_UNLESS(database_config_get_text((sqlite3 *) srv.db, "database", "kara_dir", kara_dir, PATH_MAX), "Cfg error", 2);
-    RETURN_UNLESS(database_config_get_text((sqlite3 *) srv.db, "server", "host", host, HOST_NAME_MAX), "Cfg error", 2);
-    RETURN_UNLESS(database_config_get_text((sqlite3 *) srv.db, "server", "port", port, 5), "Cfg error", 2);
-    RETURN_UNLESS(database_config_get_text((sqlite3 *) srv.db, "player", "module", player_mod, INI_MAX_LINE), "Cfg error", 2);
+    RETURN_UNLESS(database_config_get_int( (sqlite3 *) srv.db, "player",   "autoclear", &autoclear),               "Cfg error", 2);
+    RETURN_UNLESS(database_config_get_text((sqlite3 *) srv.db, "database", "kara_dir",  kara_dir, PATH_MAX),       "Cfg error", 2);
+    RETURN_UNLESS(database_config_get_text((sqlite3 *) srv.db, "server",   "host",      host, HOST_NAME_MAX),      "Cfg error", 2);
+    RETURN_UNLESS(database_config_get_text((sqlite3 *) srv.db, "server",   "port",      port, 5),                  "Cfg error", 2);
+    RETURN_UNLESS(database_config_get_text((sqlite3 *) srv.db, "player",   "module",    player_mod, INI_MAX_LINE), "Cfg error", 2);
 
     if (kara_dir[strlen(kara_dir) - 1] != '/')
         strncat(kara_dir, "/", PATH_MAX - 1);
diff --git a/src/repo/downloader.c b/src/repo/downloader.c
index a8036da9..aed1c042 100644
--- a/src/repo/downloader.c
+++ b/src/repo/downloader.c
@@ -81,7 +81,7 @@ __handle_got_json(struct poller_thread *self, struct lkt_repo *repo, struct json
             goto err;
 
         /* Append. */
-        if (lkt_th_append_output(self, kara)) {
+        if (poller_append_output(self, kara)) {
             LOG_ERROR("%s", "Could not append downloaded kara mdt");
             goto err;
         }
@@ -123,7 +123,7 @@ __repo_thread_function(struct poller_thread_arg *arg)
         head = 0;
 
         /* size_t has the size of a pointer (thus of a void *). */
-        if (lkt_th_pop_input(self, (void **) &head)) {
+        if (poller_pop_input(self, (void **) &head)) {
             LOG_ERROR("%s", "Failed to get the head of the input list");
             goto end_loop; /* Just skip all the loop to the yield function. */
         }
@@ -151,7 +151,7 @@ __repo_thread_function(struct poller_thread_arg *arg)
         kara->id = head;
         memcpy(kara->filename, path, (strlen(path) + 1) * sizeof(char));
 
-        if (lkt_th_append_output(self, (void *) kara)) {
+        if (poller_append_output(self, (void *) kara)) {
             LOG_ERROR("Failed to append to output, orphan kara %lu", head);
             free(kara);
             goto end_loop;
@@ -167,7 +167,7 @@ try_later:
             free(kara);
 
         /* Retry later. TODO: Implements a retry counter. */
-        if (lkt_th_append_input(self, (void *) head))
+        if (poller_append_input(self, (void *) head))
             LOG_ERROR("%s", "Failed to get the head of the input list");
 
 end_loop:
@@ -187,7 +187,7 @@ repo_new_thread(struct lkt_repo *const repo)
     struct poller_thread_arg *arg = calloc(1, sizeof(struct poller_thread_arg));
     RETURN_UNLESS(arg, "Out of memory", errno = ENOMEM);
     arg->args = repo;
-    RETURN_IF(lkt_th_new(&repo->self, LKT_DEFAULT_LIST_SIZE, __repo_thread_function, arg), "Thread error", 1);
+    RETURN_IF(poller_new(&repo->self, LKT_DEFAULT_LIST_SIZE, __repo_thread_function, arg), "Thread error", 1);
     repo->init = 1;
     return 0;
 }
@@ -196,7 +196,7 @@ int
 repo_download_id_async(struct lkt_repo *const repo, const size_t id)
 {
     RETURN_IF(id == 0, "Invalid argument", 1);
-    RETURN_IF(lkt_th_append_input(&repo->self, (void *) id), "Failed to push downloaded id", id);
+    RETURN_IF(poller_append_input(&repo->self, (void *) id), "Failed to push downloaded id", id);
     LOG_INFO("Asked to download kara with id %lu", id);
     return 0;
 }
@@ -205,7 +205,7 @@ int
 repo_get_kara_async(struct lkt_repo *const repo, struct kara **downloaded)
 {
     /* Is there a kara that has been downloaded? */
-    if (lkt_th_pop_output(&repo->self, (void **) downloaded))
+    if (poller_pop_output(&repo->self, (void **) downloaded))
         goto err;
 
     if (!*downloaded)
diff --git a/src/thread.c b/src/thread.c
index 4969c933..e4e3f107 100644
--- a/src/thread.c
+++ b/src/thread.c
@@ -26,7 +26,7 @@ __start(void *args__)
 }
 
 int
-lkt_th_new(struct poller_thread *th, unsigned int init_sizes,
+poller_new(struct poller_thread *th, unsigned int init_sizes,
            void *(*func)(struct poller_thread_arg *), void *args)
 {
     pthread_mutex_t mtx1 = PTHREAD_MUTEX_INITIALIZER;
@@ -73,7 +73,7 @@ out_of_memory:
 }
 
 int
-lkt_th_join(struct poller_thread *th, void **ret)
+poller_join(struct poller_thread *th, void **ret)
 {
     int sta = pthread_join(th->th, ret);
 
@@ -157,21 +157,21 @@ end:
 }
 
 int
-lkt_th_append_input(struct poller_thread *th, void *ptr)
+poller_append_input(struct poller_thread *th, void *ptr)
 {
     return th_append((unsigned int *) &th->input_len, (unsigned int *) &th->input_size,
                      (void ***) &th->input_cells, &th->input_lock, ptr);
 }
 
 int
-lkt_th_append_output(struct poller_thread *th, void *ptr)
+poller_append_output(struct poller_thread *th, void *ptr)
 {
     return th_append((unsigned int *) &th->output_len, (unsigned int *) &th->output_size,
                      (void ***) &th->output_cells, &th->output_lock, ptr);
 }
 
 int
-lkt_th_pop_input(struct poller_thread *th, void **ptr)
+poller_pop_input(struct poller_thread *th, void **ptr)
 {
     return th_pop((unsigned int *) &th->input_len, (void **) th->input_cells,
                   &th->input_lock, ptr);
@@ -179,20 +179,20 @@ lkt_th_pop_input(struct poller_thread *th, void **ptr)
 
 
 int
-lkt_th_pop_output(struct poller_thread *th, void **ptr)
+poller_pop_output(struct poller_thread *th, void **ptr)
 {
     return th_pop((unsigned int *) &th->output_len, (void **) th->output_cells,
                   &th->output_lock, ptr);
 }
 
 int
-lkt_th_head_input(struct poller_thread *th, void **ptr)
+poller_head_input(struct poller_thread *th, void **ptr)
 {
     return th_head((unsigned int) th->input_len, (void **) th->input_cells, &th->input_lock, ptr);
 }
 
 int
-lkt_th_head_output(struct poller_thread *th, void **ptr)
+poller_head_output(struct poller_thread *th, void **ptr)
 {
     return th_head((unsigned int) th->output_len, (void **) th->output_cells, &th->output_lock, ptr);
 }
-- 
GitLab