From 7310e8e1740e0d1bf4cd5890026068ded4546982 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Wed, 16 Sep 2020 12:30:57 +0200 Subject: [PATCH] MISC: Introduce a thing that modules have to include - move queue.c as a single header lib. Update Makefiles - introduce the module's header --- inc/lektor/lktmodule.h | 24 ++++++++++ inc/lektor/queue.h | 100 +++++++++++++++++++++++++++++++++++++-- src/Makefile.am | 8 ++-- src/Makefile.in | 44 ++++++----------- src/base/queue.c | 99 -------------------------------------- src/module/module_repo.c | 8 +--- src/module/module_sdl2.c | 8 +--- src/module/mpv.c | 1 - 8 files changed, 142 insertions(+), 150 deletions(-) create mode 100644 inc/lektor/lktmodule.h delete mode 100644 src/base/queue.c diff --git a/inc/lektor/lktmodule.h b/inc/lektor/lktmodule.h new file mode 100644 index 00000000..7ddba4bd --- /dev/null +++ b/inc/lektor/lktmodule.h @@ -0,0 +1,24 @@ +#if ! defined(__LKT_LKTMODULE_H__) +#define __LKT_LKTMODULE_H__ + +/* The single header modules can use from lektor */ + +/* Include header */ + +#include <lektor/lktconfig.h> +#include <lektor/common.h> +#include <lektor/queue.h> +#include <lektor/database.h> +#include <lektor/net.h> +#include <lektor/reg.h> + +#include <lektor/thread.h> /* To be moved in module_repo... */ + +/* Include source files, only do it once per module ! */ +#if defined(__LKT_MODULE_MAIN_SOURCE__) && ! defined(LKT_STATIC_MODULE) +#include "../../../src/base/thread.c" +#include "../../../src/base/common.c" +#include "../../../src/database/queue.c" +#endif /* __LKT_MODULE_MAIN_SOURCE__ && ! LKT_STATIC_MODULE*/ + +#endif /* __LKT_LKTMODULE_H__ */ diff --git a/inc/lektor/queue.h b/inc/lektor/queue.h index f62bad11..1332a2e6 100644 --- a/inc/lektor/queue.h +++ b/inc/lektor/queue.h @@ -1,7 +1,16 @@ #if ! defined(__LKT_QUEUE_H__) #define __LKT_QUEUE_H__ +#define _POSIX_C_SOURCE 200809L + +/* Everything in this file is implemented in the header, so that modules don't + have to compile the lib with another .c file. */ #include <pthread.h> +#include <errno.h> +#include <stdlib.h> +#include <stddef.h> +#include <string.h> +#include <stdio.h> #include <lektor/common.h> enum { @@ -59,12 +68,93 @@ struct queue { volatile int available; }; -int lkt_queue_new(struct queue *); -void lkt_queue_free(struct queue *); +static inline int +lkt_queue_new(struct queue *ret) +{ + if (!ret) + return 1; + + pthread_mutex_t mxt = PTHREAD_MUTEX_INITIALIZER; + struct queue _ret = { + .contents = malloc(LKT_DEFAULT_LIST_SIZE * sizeof(lkt_event)), + .size = LKT_DEFAULT_LIST_SIZE, + .last = 0, + .available = 0, + .lock = mxt, + }; + + if (_ret.contents == NULL) + return 1; + + *ret = _ret; + return 0; +} + +static inline void +lkt_queue_free(struct queue *queue) +{ + pthread_mutex_lock(&queue->lock); + if (queue && queue->contents) + free((void *) queue->contents); + pthread_mutex_unlock(&queue->lock); +} + +static inline void +lkt_queue_send(struct queue *queue, enum lkt_event_type _type, void *_attr) +{ + pthread_mutex_lock(&queue->lock); + GOTO_UNLESS(queue, "Invalid argument", end) + + volatile lkt_event *new; + if (queue->size == queue->last) { + new = realloc((void *) queue->contents, queue->size * 2 * sizeof(lkt_event)); + + if (NULL == new) + goto end; + + queue->contents = new; + queue->size *= 2; + } + + lkt_event evt = { + .type = _type, + .attr = _attr, + }; + queue->contents[(queue->last)++] = evt; + + if (! (_type & queue->available)) + LOG_WARN("QUEUE", "The event %d is not available, push it anyway", _type); +end: + pthread_mutex_unlock(&queue->lock); +} + +static inline lkt_event +lkt_queue_handle(struct queue *queue) +{ + pthread_mutex_lock(&queue->lock); + lkt_event ret = {0}; + if (!queue || !queue->last) + goto end; + if (! (queue->contents[0].type & queue->available) && + queue->contents[0].type) { + LOG_WARN("QUEUE", "Event %d is not available", queue->contents[0].type); + goto end; + } -void lkt_queue_send(struct queue *, enum lkt_event_type, void *attr); -lkt_event lkt_queue_handle(struct queue *); + ret = queue->contents[0]; + memmove((void *) queue->contents, (void *) (queue->contents + 1), + --(queue->last)); +end: + pthread_mutex_unlock(&queue->lock); + return ret; +} -void lkt_queue_make_available(struct queue *, enum lkt_event_type); +static inline void +lkt_queue_make_available(struct queue *queue, enum lkt_event_type type) +{ + pthread_mutex_lock(&queue->lock); + queue->available |= type; + pthread_mutex_unlock(&queue->lock); +} #endif /* __LKT_QUEUE_H__ */ diff --git a/src/Makefile.am b/src/Makefile.am index 11f477f7..cd8897b4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -14,7 +14,7 @@ endif ## REPO module liblktmodrepo_la_SOURCES = module/module_repo.c module/worker.c liblktmodrepo_la_CFLAGS = -fPIC -liblktmodrepo_la_LDFLAGS = -avoid-version -pthread +liblktmodrepo_la_LDFLAGS = -avoid-version -pthread -ljson-c -lcurl if LKT_STATIC_MODULE liblktmodrepo_la_LDFLAGS += -static else @@ -35,8 +35,8 @@ endif ## Lib lektor # Base sources liblektor_la_SOURCES = base/bufferfd.c base/cmd.c base/common.c base/config.c -liblektor_la_SOURCES += base/queue.c base/reg.c base/stack.c base/uri.c -liblektor_la_SOURCES += base/thread.c base/commands.c +liblektor_la_SOURCES += base/reg.c base/stack.c base/uri.c base/thread.c +liblektor_la_SOURCES += base/commands.c # Database sources liblektor_la_SOURCES += database/disk.c database/memory.c @@ -52,7 +52,7 @@ liblektor_la_SOURCES += net/command.c net/listen.c net/message.c # Liblektor configuration liblektor_la_CFLAGS = -fPIC -liblektor_la_LDFLAGS = -avoid-version -shared -pthread -ljson-c -lsqlite3 -lcurl +liblektor_la_LDFLAGS = -avoid-version -shared -pthread -lsqlite3 if LKT_STATIC_MODULE liblektor_la_LDFLAGS += -lSDL2 -lmpv liblektor_la_LIBADD = liblktmodsdl.la liblktmodrepo.la diff --git a/src/Makefile.in b/src/Makefile.in index 94c2556c..05891296 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -145,11 +145,11 @@ LTLIBRARIES = $(lib_LTLIBRARIES) $(noinst_LTLIBRARIES) am__dirstamp = $(am__leading_dot)dirstamp am_liblektor_la_OBJECTS = base/liblektor_la-bufferfd.lo \ base/liblektor_la-cmd.lo base/liblektor_la-common.lo \ - base/liblektor_la-config.lo base/liblektor_la-queue.lo \ - base/liblektor_la-reg.lo base/liblektor_la-stack.lo \ - base/liblektor_la-uri.lo base/liblektor_la-thread.lo \ - base/liblektor_la-commands.lo database/liblektor_la-disk.lo \ - database/liblektor_la-memory.lo database/liblektor_la-open.lo \ + base/liblektor_la-config.lo base/liblektor_la-reg.lo \ + base/liblektor_la-stack.lo base/liblektor_la-uri.lo \ + base/liblektor_la-thread.lo base/liblektor_la-commands.lo \ + database/liblektor_la-disk.lo database/liblektor_la-memory.lo \ + database/liblektor_la-open.lo \ database/liblektor_la-playlist.lo \ database/liblektor_la-queue.lo \ database/liblektor_la-stickers.lo \ @@ -216,7 +216,6 @@ am__depfiles_remade = base/$(DEPDIR)/cmd.Po base/$(DEPDIR)/common.Po \ base/$(DEPDIR)/liblektor_la-commands.Plo \ base/$(DEPDIR)/liblektor_la-common.Plo \ base/$(DEPDIR)/liblektor_la-config.Plo \ - base/$(DEPDIR)/liblektor_la-queue.Plo \ base/$(DEPDIR)/liblektor_la-reg.Plo \ base/$(DEPDIR)/liblektor_la-stack.Plo \ base/$(DEPDIR)/liblektor_la-thread.Plo \ @@ -427,8 +426,8 @@ AM_CPPFLAGS = -I$(abs_top_srcdir)/inc/ -I$(abs_top_builddir)/inc/ @LKT_STATIC_MODULE_TRUE@lib_LTLIBRARIES = liblektor.la liblktmodrepo_la_SOURCES = module/module_repo.c module/worker.c liblktmodrepo_la_CFLAGS = -fPIC -liblktmodrepo_la_LDFLAGS = -avoid-version -pthread $(am__append_1) \ - $(am__append_2) +liblktmodrepo_la_LDFLAGS = -avoid-version -pthread -ljson-c -lcurl \ + $(am__append_1) $(am__append_2) liblktmodsdl_la_SOURCES = module/module_sdl2.c module/mpv.c liblktmodsdl_la_CFLAGS = -fPIC `sdl2-config --cflags` liblktmodsdl_la_LDFLAGS = -avoid-version -pthread -lmpv \ @@ -443,18 +442,17 @@ liblktmodsdl_la_LIBADD = -lSDL2 -lmpv # Net sources liblektor_la_SOURCES = base/bufferfd.c base/cmd.c base/common.c \ - base/config.c base/queue.c base/reg.c base/stack.c base/uri.c \ - base/thread.c base/commands.c database/disk.c \ - database/memory.c database/open.c database/playlist.c \ - database/queue.c database/stickers.c database/update.c \ - database/user.c database/config.c database/find.c mkv/mkv.c \ - mkv/utils.c mkv/write.c net/command.c net/listen.c \ - net/message.c + base/config.c base/reg.c base/stack.c base/uri.c base/thread.c \ + base/commands.c database/disk.c database/memory.c \ + database/open.c database/playlist.c database/queue.c \ + database/stickers.c database/update.c database/user.c \ + database/config.c database/find.c mkv/mkv.c mkv/utils.c \ + mkv/write.c net/command.c net/listen.c net/message.c # Liblektor configuration liblektor_la_CFLAGS = -fPIC -liblektor_la_LDFLAGS = -avoid-version -shared -pthread -ljson-c \ - -lsqlite3 -lcurl $(am__append_5) +liblektor_la_LDFLAGS = -avoid-version -shared -pthread -lsqlite3 \ + $(am__append_5) @LKT_STATIC_MODULE_TRUE@liblektor_la_LIBADD = liblktmodsdl.la liblktmodrepo.la CLEANFILES = database/disk.c database/memory.c EXTRA_DIST = database/disk.sql database/memory.sql @@ -619,8 +617,6 @@ base/liblektor_la-common.lo: base/$(am__dirstamp) \ base/$(DEPDIR)/$(am__dirstamp) base/liblektor_la-config.lo: base/$(am__dirstamp) \ base/$(DEPDIR)/$(am__dirstamp) -base/liblektor_la-queue.lo: base/$(am__dirstamp) \ - base/$(DEPDIR)/$(am__dirstamp) base/liblektor_la-reg.lo: base/$(am__dirstamp) \ base/$(DEPDIR)/$(am__dirstamp) base/liblektor_la-stack.lo: base/$(am__dirstamp) \ @@ -751,7 +747,6 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@base/$(DEPDIR)/liblektor_la-commands.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@base/$(DEPDIR)/liblektor_la-common.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@base/$(DEPDIR)/liblektor_la-config.Plo@am__quote@ # am--include-marker -@AMDEP_TRUE@@am__include@ @am__quote@base/$(DEPDIR)/liblektor_la-queue.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@base/$(DEPDIR)/liblektor_la-reg.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@base/$(DEPDIR)/liblektor_la-stack.Plo@am__quote@ # am--include-marker @AMDEP_TRUE@@am__include@ @am__quote@base/$(DEPDIR)/liblektor_la-thread.Plo@am__quote@ # am--include-marker @@ -837,13 +832,6 @@ base/liblektor_la-config.lo: base/config.c @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(liblektor_la_CFLAGS) $(CFLAGS) -c -o base/liblektor_la-config.lo `test -f 'base/config.c' || echo '$(srcdir)/'`base/config.c -base/liblektor_la-queue.lo: base/queue.c -@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(liblektor_la_CFLAGS) $(CFLAGS) -MT base/liblektor_la-queue.lo -MD -MP -MF base/$(DEPDIR)/liblektor_la-queue.Tpo -c -o base/liblektor_la-queue.lo `test -f 'base/queue.c' || echo '$(srcdir)/'`base/queue.c -@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) base/$(DEPDIR)/liblektor_la-queue.Tpo base/$(DEPDIR)/liblektor_la-queue.Plo -@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='base/queue.c' object='base/liblektor_la-queue.lo' libtool=yes @AMDEPBACKSLASH@ -@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ -@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(liblektor_la_CFLAGS) $(CFLAGS) -c -o base/liblektor_la-queue.lo `test -f 'base/queue.c' || echo '$(srcdir)/'`base/queue.c - base/liblektor_la-reg.lo: base/reg.c @am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(liblektor_la_CFLAGS) $(CFLAGS) -MT base/liblektor_la-reg.lo -MD -MP -MF base/$(DEPDIR)/liblektor_la-reg.Tpo -c -o base/liblektor_la-reg.lo `test -f 'base/reg.c' || echo '$(srcdir)/'`base/reg.c @am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) base/$(DEPDIR)/liblektor_la-reg.Tpo base/$(DEPDIR)/liblektor_la-reg.Plo @@ -1180,7 +1168,6 @@ distclean: distclean-am -rm -f base/$(DEPDIR)/liblektor_la-commands.Plo -rm -f base/$(DEPDIR)/liblektor_la-common.Plo -rm -f base/$(DEPDIR)/liblektor_la-config.Plo - -rm -f base/$(DEPDIR)/liblektor_la-queue.Plo -rm -f base/$(DEPDIR)/liblektor_la-reg.Plo -rm -f base/$(DEPDIR)/liblektor_la-stack.Plo -rm -f base/$(DEPDIR)/liblektor_la-thread.Plo @@ -1259,7 +1246,6 @@ maintainer-clean: maintainer-clean-am -rm -f base/$(DEPDIR)/liblektor_la-commands.Plo -rm -f base/$(DEPDIR)/liblektor_la-common.Plo -rm -f base/$(DEPDIR)/liblektor_la-config.Plo - -rm -f base/$(DEPDIR)/liblektor_la-queue.Plo -rm -f base/$(DEPDIR)/liblektor_la-reg.Plo -rm -f base/$(DEPDIR)/liblektor_la-stack.Plo -rm -f base/$(DEPDIR)/liblektor_la-thread.Plo diff --git a/src/base/queue.c b/src/base/queue.c deleted file mode 100644 index ae9be02a..00000000 --- a/src/base/queue.c +++ /dev/null @@ -1,99 +0,0 @@ -#define _POSIX_C_SOURCE 200809L - -#include <errno.h> -#include <stdlib.h> -#include <stddef.h> -#include <string.h> -#include <stdio.h> -#include <lektor/common.h> -#include <lektor/queue.h> -#include <pthread.h> - -int -lkt_queue_new(struct queue *ret) -{ - if (!ret) - return 1; - - pthread_mutex_t mxt = PTHREAD_MUTEX_INITIALIZER; - struct queue _ret = { - .contents = malloc(LKT_DEFAULT_LIST_SIZE * sizeof(lkt_event)), - .size = LKT_DEFAULT_LIST_SIZE, - .last = 0, - .available = 0, - .lock = mxt, - }; - - if (_ret.contents == NULL) - return 1; - - *ret = _ret; - return 0; -} - -void -lkt_queue_free(struct queue *queue) -{ - pthread_mutex_lock(&queue->lock); - if (queue && queue->contents) - free((void *) queue->contents); - pthread_mutex_unlock(&queue->lock); -} - -void -lkt_queue_send(struct queue *queue, enum lkt_event_type _type, void *_attr) -{ - pthread_mutex_lock(&queue->lock); - GOTO_UNLESS(queue, "Invalid argument", end) - - volatile lkt_event *new; - if (queue->size == queue->last) { - new = realloc((void *) queue->contents, queue->size * 2 * sizeof(lkt_event)); - - if (NULL == new) - goto end; - - queue->contents = new; - queue->size *= 2; - } - - lkt_event evt = { - .type = _type, - .attr = _attr, - }; - queue->contents[(queue->last)++] = evt; - - if (! (_type & queue->available)) - LOG_WARN("QUEUE", "The event %d is not available, push it anyway", _type); -end: - pthread_mutex_unlock(&queue->lock); -} - -lkt_event -lkt_queue_handle(struct queue *queue) -{ - pthread_mutex_lock(&queue->lock); - lkt_event ret = {0}; - if (!queue || !queue->last) - goto end; - if (! (queue->contents[0].type & queue->available) && - queue->contents[0].type) { - LOG_WARN("QUEUE", "Event %d is not available", queue->contents[0].type); - goto end; - } - - ret = queue->contents[0]; - memmove((void *) queue->contents, (void *) (queue->contents + 1), - --(queue->last)); -end: - pthread_mutex_unlock(&queue->lock); - return ret; -} - -void -lkt_queue_make_available(struct queue *queue, enum lkt_event_type type) -{ - pthread_mutex_lock(&queue->lock); - queue->available |= type; - pthread_mutex_unlock(&queue->lock); -} diff --git a/src/module/module_repo.c b/src/module/module_repo.c index e11cc710..53ca2975 100644 --- a/src/module/module_repo.c +++ b/src/module/module_repo.c @@ -1,11 +1,7 @@ #define _POSIX_C_SOURCE 200809L -#include <lektor/lktconfig.h> -#include <lektor/common.h> -#include <lektor/queue.h> -#include <lektor/database.h> -#include <lektor/net.h> -#include <lektor/reg.h> +#define __LKT_MODULE_MAIN_SOURCE__ +#include <lektor/lktmodule.h> #include "worker.h" diff --git a/src/module/module_sdl2.c b/src/module/module_sdl2.c index 490a0fed..5dfcd790 100644 --- a/src/module/module_sdl2.c +++ b/src/module/module_sdl2.c @@ -1,11 +1,7 @@ #define _POSIX_C_SOURCE 200809L -#include <lektor/lktconfig.h> -#include <lektor/common.h> -#include <lektor/queue.h> -#include <lektor/thread.h> -#include <lektor/reg.h> -#include <lektor/net.h> +#define __LKT_MODULE_MAIN_SOURCE__ +#include <lektor/lktmodule.h> #include "mpv.h" diff --git a/src/module/mpv.c b/src/module/mpv.c index 4d92ecc0..7186bd20 100644 --- a/src/module/mpv.c +++ b/src/module/mpv.c @@ -3,7 +3,6 @@ #include "mpv.h" #include <lektor/common.h> -#include <lektor/commands.h> #include <lektor/database.h> #include <strings.h> #include <stdio.h> -- GitLab