From 1d6a4b4b61e73763acf77ba09a5945d16f72d744 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Wed, 29 Apr 2020 18:30:24 +0200 Subject: [PATCH] Name correction: lkt_thread -> poller_thread, using mthread and not pthread, corrections in mthread --- inc/lektor/thread.h | 32 ++++++++++---------- meson.build | 4 +-- src/module/module_sdl2.c | 11 +++---- src/mthread/mthread.c | 17 +++++------ src/mthread/mthread_mutex.c | 6 +--- src/repo/async.c | 29 ++++++++++--------- src/thread.c | 58 ++++++++++++++++++------------------- 7 files changed, 76 insertions(+), 81 deletions(-) diff --git a/inc/lektor/thread.h b/inc/lektor/thread.h index 6efc832e..ac1f52ad 100644 --- a/inc/lektor/thread.h +++ b/inc/lektor/thread.h @@ -1,45 +1,45 @@ #pragma once #include <lektor/define.h> -#include <pthread.h> +#include <mthread/mthread.h> #include <sys/types.h> -struct lkt_thread { +struct poller_thread { /* The input poll. */ volatile unsigned int input_len; volatile unsigned int input_size; volatile void *volatile *volatile input_cells; - pthread_mutex_t input_lock; + mthread_mutex_t input_lock; - pthread_t th; + mthread_t th; unsigned int initial_size; /* The output pool. */ volatile unsigned int output_len; volatile unsigned int output_size; volatile void *volatile *volatile output_cells; - pthread_mutex_t output_lock; + mthread_mutex_t output_lock; }; -struct lkt_thread_arg { - struct lkt_thread *self; +struct poller_thread_arg { + struct poller_thread *self; void *args; }; /* Create a new thread. Returns 0 on success, a non zero value on error. */ -int lkt_th_new(struct lkt_thread *th, unsigned int init_sizes, - void *(*func)(struct lkt_thread_arg *), void *args); +int lkt_th_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 lkt_thread *th, void **ret); +int lkt_th_join(struct poller_thread *th, void **ret); /* Input qeueu manipulations. */ -int lkt_th_append_input(struct lkt_thread *th, void *ptr); -int lkt_th_pop_input(struct lkt_thread *th, void **ptr); -int lkt_th_head_input(struct lkt_thread *th, void **ptr); +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); /* Output qeueu manipulations. */ -int lkt_th_append_output(struct lkt_thread *th, void *ptr); -int lkt_th_pop_output(struct lkt_thread *th, void **ptr); -int lkt_th_head_output(struct lkt_thread *th, void **ptr); +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); diff --git a/meson.build b/meson.build index 406b4040..f0ab5baa 100644 --- a/meson.build +++ b/meson.build @@ -121,7 +121,7 @@ if dep_x11.found() and dep_mpv.found() lib_mod_x11 = shared_library ( '_module_x11' , files(['src/module/module_x11.c', 'src/module/mpv.c']) , include_directories : includes - , dependencies : [ dep_x11, dep_mpv ] + , dependencies : [ dep_x11, dep_mpv, mthread_deps ] , link_with : lib , install : true , install_dir : 'lib/lektor' @@ -133,7 +133,7 @@ if dep_sdl.found() and dep_mpv.found() lib_mod_sdl = shared_library ( '_module_sdl2' , files(['src/module/module_sdl2.c', 'src/module/mpv.c']) , include_directories : includes - , dependencies : [ dep_sdl, dep_mpv ] + , dependencies : [ dep_sdl, dep_mpv, mthread_deps ] , link_with : lib , install : true , install_dir : 'lib/lektor' diff --git a/src/module/module_sdl2.c b/src/module/module_sdl2.c index 1c5f693a..6b8ccfd4 100644 --- a/src/module/module_sdl2.c +++ b/src/module/module_sdl2.c @@ -3,6 +3,7 @@ #include <common/common.h> #include <lektor/module/module_sdl2.h> #include <lektor/module/mpv.h> +#include <mthread/mthread.h> #include <lektor/thread.h> #include <sched.h> @@ -32,8 +33,8 @@ struct module_sdl2_window { volatile int mpv_duration; /* Because don't need to be persistent */ /* Thread related */ - pthread_mutex_t mtx; - struct lkt_thread self; + mthread_mutex_t mtx; + struct poller_thread self; volatile int launched; /* SDL you sucks */ }; @@ -94,7 +95,7 @@ init_mpv_gl__(mpv_handle *mpv, mpv_render_context **mpv_gl, mpv_render_param *pa /* Thread related functions */ static void * -sdl_thread__(struct lkt_thread_arg *arg) +sdl_thread__(struct poller_thread_arg *arg) { volatile struct lkt_win *const win = arg->args; volatile struct module_sdl2_window *sdl2 = win->window; @@ -242,11 +243,11 @@ module_sdl2_new(struct lkt_win *const win) memset(win->window, 0, sizeof(struct module_sdl2_window)); /* Yeah, this is how it is done. */ - pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; + mthread_mutex_t mtx = MTHREAD_MUTEX_INITIALIZER; ((struct module_sdl2_window *) win->window)->mtx = mtx; /* Start the SDL thread */ - struct lkt_thread_arg *arg = calloc(1, sizeof(struct lkt_thread_arg)); + 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, diff --git a/src/mthread/mthread.c b/src/mthread/mthread.c index aad399c2..898f8401 100644 --- a/src/mthread/mthread.c +++ b/src/mthread/mthread.c @@ -1,5 +1,6 @@ #include <mthread/mthread_internal.h> #include <sched.h> +#include <string.h> #define TWO_LEVEL @@ -378,16 +379,12 @@ mthread_create(mthread_t *__threadp, const mthread_attr_t *__attr, mthread_virtual_processor_t *vp = mthread_get_vp(); if (__attr == NULL) { - struct mthread_s *mctx; - char *stack; - - mctx = mthread_remove_first(&(joined_list)); - if (mctx == NULL) - mctx = (struct mthread_s *)safe_malloc(sizeof(struct mthread_s)); - if (mctx->stack == NULL) - stack = (char *)safe_malloc(MTHREAD_DEFAULT_STACK); - else - stack = mctx->stack; + struct mthread_s *mctx = mthread_remove_first(&(joined_list)); + if (mctx == NULL) { + mctx = safe_malloc(sizeof(struct mthread_s)); + memset(mctx, 0, sizeof(struct mthread_s)); + } + char *stack = mctx->stack ? mctx->stack : safe_malloc(MTHREAD_DEFAULT_STACK); mthread_init_thread(mctx); LOG_INFO_SCT("THREAD INIT", "Create thread %p", (void *) mctx); diff --git a/src/mthread/mthread_mutex.c b/src/mthread/mthread_mutex.c index 0805f5e7..85fbc6ad 100644 --- a/src/mthread/mthread_mutex.c +++ b/src/mthread/mthread_mutex.c @@ -54,7 +54,6 @@ mthread_mutex_trylock(mthread_mutex_t *__mutex) __mutex->nb_thread = 1; mthread_spinlock_unlock(&__mutex->lock); retval = 0; - LOG_INFO("%s", "MUTEX acquired"); goto end; } @@ -92,7 +91,6 @@ mthread_mutex_lock(mthread_mutex_t *__mutex) mthread_yield(); } - LOG_INFO("%s", "MUTEX acquired"); return 0; } @@ -100,7 +98,6 @@ mthread_mutex_lock(mthread_mutex_t *__mutex) int mthread_mutex_unlock(mthread_mutex_t *__mutex) { - int retval = EINVAL; mthread_t first; mthread_virtual_processor_t *vp; @@ -119,6 +116,5 @@ mthread_mutex_unlock(mthread_mutex_t *__mutex) __mutex->nb_thread = 0; mthread_spinlock_unlock(&__mutex->lock); - LOG_INFO("%s", "MUTEX released"); - return retval; + return 0; } diff --git a/src/repo/async.c b/src/repo/async.c index 5019b543..d883b4ce 100644 --- a/src/repo/async.c +++ b/src/repo/async.c @@ -4,15 +4,16 @@ #include <stdio.h> #include <unistd.h> #include <string.h> +#include <limits.h> +#include <mthread/mthread.h> #include <lektor/macro.h> #include <lektor/repo.h> #include <lektor/thread.h> -#include <limits.h> -static struct lkt_thread repo_thread; +static struct poller_thread repo_thread; -static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; +static mthread_mutex_t mtx = MTHREAD_MUTEX_INITIALIZER; static volatile int init = 0; static volatile int stop = 0; static volatile int all_json = 0; @@ -21,14 +22,14 @@ int repo_join_thread(void) { int ret = 1; - RETURN_IF(pthread_mutex_lock(&mtx), "Failed to lock mutex", 3); + RETURN_IF(mthread_mutex_lock(&mtx), "Failed to lock mutex", 3); GOTO_UNLESS(init, "Repo thread no launched, can't join", error); stop = 1; - GOTO_IF(pthread_join(repo_thread.th, NULL), "Failed to join repo thread", error); + GOTO_IF(mthread_join(repo_thread.th, NULL), "Failed to join repo thread", error); LOG_INFO("%s", "repo thread joined"); ret = 0; error: - RETURN_IF(pthread_mutex_unlock(&mtx), "Failed to unlock mutex", 3); + RETURN_IF(mthread_mutex_unlock(&mtx), "Failed to unlock mutex", 3); return ret; } @@ -39,7 +40,7 @@ extern int safe_json_get_int32(struct json_object *json, const char *key, int32_t *ret); static inline void -__handle_got_json(struct lkt_thread *self, struct lkt_repo *repo, struct json_object *json) +__handle_got_json(struct poller_thread *self, struct lkt_repo *repo, struct json_object *json) { size_t i, len = json_object_array_length(json); struct json_object *kara_json; @@ -99,11 +100,11 @@ err: } static void * -__repo_thread_function(struct lkt_thread_arg *arg) +__repo_thread_function(struct poller_thread_arg *arg) { size_t head; struct lkt_repo *repo = arg->args; - struct lkt_thread *self = arg->self; + struct poller_thread *self = arg->self; struct kara *kara; struct json_object *json = NULL; char path[PATH_MAX]; @@ -112,7 +113,7 @@ __repo_thread_function(struct lkt_thread_arg *arg) LOG_INFO("%s", "Starting the repo thread"); for (;;) { - GOTO_IF(pthread_mutex_lock(&mtx), "Failed to lock mutex", end_loop); + GOTO_IF(mthread_mutex_lock(&mtx), "Failed to lock mutex", end_loop); if (all_json) { repo_get_alljson_sync(repo, &json); @@ -121,7 +122,7 @@ __repo_thread_function(struct lkt_thread_arg *arg) } if (stop) { - if (pthread_mutex_unlock(&mtx)) + if (mthread_mutex_unlock(&mtx)) LOG_ERROR("Failed to unlock mutex: %s", strerror(errno)); break; } @@ -188,7 +189,7 @@ int repo_new_thread(struct lkt_repo *const repo) { RETURN_IF(init, "Already running", 1); - struct lkt_thread_arg *arg = calloc(1, sizeof(struct lkt_thread_arg)); + 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_thread, LKT_DEFAULT_LIST_SIZE, __repo_thread_function, arg), "Thread error", 1); @@ -224,8 +225,8 @@ err: inline int repo_get_allid_async(void) { - RETURN_IF(pthread_mutex_lock(&mtx), "Failed to lock mutex", 3); + RETURN_IF(mthread_mutex_lock(&mtx), "Failed to lock mutex", 3); all_json = 1; - RETURN_IF(pthread_mutex_unlock(&mtx), "Failed to lock mutex", 3); + RETURN_IF(mthread_mutex_unlock(&mtx), "Failed to lock mutex", 3); return 0; } diff --git a/src/thread.c b/src/thread.c index 96e749f1..82112f27 100644 --- a/src/thread.c +++ b/src/thread.c @@ -2,7 +2,7 @@ #include <common/common.h> #include <lektor/thread.h> -#include <pthread.h> +#include <mthread/mthread.h> #include <sys/types.h> #include <stdlib.h> #include <errno.h> @@ -11,28 +11,28 @@ #include <string.h> struct __args { - void *(*start)(struct lkt_thread_arg *); - struct lkt_thread_arg *arg; + void *(*start)(struct poller_thread_arg *); + struct poller_thread_arg *arg; }; static void * __start(void *args__) { struct __args *args = (struct __args *) args__; - void *(*start)(struct lkt_thread_arg *) = args->start; - struct lkt_thread_arg *arg = args->arg; + void *(*start)(struct poller_thread_arg *) = args->start; + struct poller_thread_arg *arg = args->arg; free(args__); return start(arg); } int -lkt_th_new(struct lkt_thread *th, unsigned int init_sizes, - void *(*func)(struct lkt_thread_arg *), void *args) +lkt_th_new(struct poller_thread *th, unsigned int init_sizes, + void *(*func)(struct poller_thread_arg *), void *args) { int ret = 1; - pthread_mutex_t mtx1 = PTHREAD_MUTEX_INITIALIZER; - pthread_mutex_t mtx2 = PTHREAD_MUTEX_INITIALIZER; - struct lkt_thread th_ = { + mthread_mutex_t mtx1 = MTHREAD_MUTEX_INITIALIZER; + mthread_mutex_t mtx2 = MTHREAD_MUTEX_INITIALIZER; + struct poller_thread th_ = { .initial_size = init_sizes, .input_lock = mtx1, .output_lock = mtx2, @@ -53,7 +53,7 @@ lkt_th_new(struct lkt_thread *th, unsigned int init_sizes, __args->arg = args; __args->arg->self = th; - ret = pthread_create(&(th->th), NULL, __start, __args); + ret = mthread_create(&(th->th), NULL, __start, __args); if (ret) goto end; @@ -75,9 +75,9 @@ out_of_memory: } int -lkt_th_join(struct lkt_thread *th, void **ret) +lkt_th_join(struct poller_thread *th, void **ret) { - int sta = pthread_join(th->th, ret); + int sta = mthread_join(th->th, ret); if (sta) LOG_ERROR("%s", "Failed to join thread"); @@ -90,18 +90,18 @@ lkt_th_join(struct lkt_thread *th, void **ret) if (!sta) LOG_ERROR("%s", "Thread joined"); - memset(th, 0, sizeof(struct lkt_thread)); + memset(th, 0, sizeof(struct poller_thread)); return sta; } static inline int th_append(unsigned int *len, unsigned int *size, void ***cells, - pthread_mutex_t *lock, void *ptr) + mthread_mutex_t *lock, void *ptr) { void *new; int ret = 0; - GOTO_IF(pthread_mutex_lock(lock), "Failed to lock", end); + GOTO_IF(mthread_mutex_lock(lock), "Failed to lock", end); if (*len == *size) { new = realloc((void *) *cells, (*len + *size) * sizeof(void *)); @@ -119,16 +119,16 @@ th_append(unsigned int *len, unsigned int *size, void ***cells, (*cells)[(*len)++] = ptr; ret = 0; end: - RETURN_IF(pthread_mutex_unlock(lock), "Failed to lock", 1); + RETURN_IF(mthread_mutex_unlock(lock), "Failed to lock", 1); return ret; } static inline int -th_pop(unsigned int *len, void **cells, pthread_mutex_t *lock, void **ptr) +th_pop(unsigned int *len, void **cells, mthread_mutex_t *lock, void **ptr) { int ret = 1; - GOTO_IF(pthread_mutex_lock(lock), "Failed to lock", end); + GOTO_IF(mthread_mutex_lock(lock), "Failed to lock", end); if (*len > 0) *ptr = cells[--(*len)]; @@ -137,15 +137,15 @@ th_pop(unsigned int *len, void **cells, pthread_mutex_t *lock, void **ptr) ret = 0; end: - RETURN_IF(pthread_mutex_unlock(lock), "Failed to lock", 1); + RETURN_IF(mthread_mutex_unlock(lock), "Failed to lock", 1); return ret; } static inline int -th_head(unsigned int len, void **cells, pthread_mutex_t *lock, void **ptr) +th_head(unsigned int len, void **cells, mthread_mutex_t *lock, void **ptr) { int ret = 1; - GOTO_IF(pthread_mutex_lock(lock), "Failed to lock", end); + GOTO_IF(mthread_mutex_lock(lock), "Failed to lock", end); if (len > 0) *ptr = cells[len - 1]; @@ -154,26 +154,26 @@ th_head(unsigned int len, void **cells, pthread_mutex_t *lock, void **ptr) ret = 0; end: - RETURN_IF(pthread_mutex_unlock(lock), "Failed to lock", 1); + RETURN_IF(mthread_mutex_unlock(lock), "Failed to lock", 1); return ret; } int -lkt_th_append_input(struct lkt_thread *th, void *ptr) +lkt_th_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 lkt_thread *th, void *ptr) +lkt_th_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 lkt_thread *th, void **ptr) +lkt_th_pop_input(struct poller_thread *th, void **ptr) { return th_pop((unsigned int *) &th->input_len, (void **) th->input_cells, &th->input_lock, ptr); @@ -181,20 +181,20 @@ lkt_th_pop_input(struct lkt_thread *th, void **ptr) int -lkt_th_pop_output(struct lkt_thread *th, void **ptr) +lkt_th_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 lkt_thread *th, void **ptr) +lkt_th_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 lkt_thread *th, void **ptr) +lkt_th_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