From 2e228d21a4643c28db7ba87dbc5f4fc523ae4fe1 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Mon, 11 Dec 2006 23:43:37 +0100 Subject: [PATCH] Added threading infrastructure to libucw and CONFIG_UCW_THREADS. Finally decided that we need threading support in too many libucw modules, so the only sane thing is to link all programs with -lpthread. On the other hand, I would like to keep the possibility of building non-threaded libucw, so I've introduced a new config switch, which is always set for Sherlock. --- lib/Makefile | 10 +++++- lib/threads-conf.c | 27 +++++++++++++++ lib/threads.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++ lib/threads.h | 34 +++++++++++++++++++ 4 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 lib/threads-conf.c create mode 100644 lib/threads.c create mode 100644 lib/threads.h diff --git a/lib/Makefile b/lib/Makefile index 15dc0a24..14018b1c 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -7,6 +7,7 @@ PROGS+=$(o)/lib/db-tool endif LIBUCW_MODS= \ + threads \ alloc alloc_str realloc mempool mempool-str mempool-fmt \ mmap pagecache partmap hashfunc \ lists slists simple-lists sorter bitsig \ @@ -32,7 +33,7 @@ LIBUCW_MODS= \ bbuf LIBUCW_INCLUDES= \ - lib.h config.h math.h \ + lib.h config.h threads.h math.h \ mempool.h pagecache.h \ sorter.h arraysort.h \ lists.h clists.h \ @@ -54,6 +55,13 @@ LIBUCW_INCLUDES= \ base64.h base224.h \ qache.h +ifdef CONFIG_UCW_THREADS +# Some modules require threading +LIBS+=-lpthread +LIBUCW_MODS+=threads-conf workqueue +LIBUCW_INCLUDES+=workqueue.h +endif + ifdef CONFIG_OWN_REGEX include $(s)/lib/regex/Makefile endif diff --git a/lib/threads-conf.c b/lib/threads-conf.c new file mode 100644 index 00000000..f3ac5a2f --- /dev/null +++ b/lib/threads-conf.c @@ -0,0 +1,27 @@ +/* + * The UCW Library -- Threading Helpers + * + * (c) 2006 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include "lib/lib.h" +#include "lib/threads.h" +#include "lib/conf.h" + +uns default_thread_stack_size = 65556; + +static struct cf_section threads_config = { + CF_ITEMS { + CF_UNS("DefaultStackSize", &default_thread_stack_size), + CF_END + } +}; + +static void CONSTRUCTOR +ucwlib_threads_conf_init(void) +{ + cf_declare_section("Threads", &threads_config, 0); +} diff --git a/lib/threads.c b/lib/threads.c new file mode 100644 index 00000000..26033e54 --- /dev/null +++ b/lib/threads.c @@ -0,0 +1,83 @@ +/* + * The UCW Library -- Threading Helpers + * + * (c) 2006 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include "lib/lib.h" +#include "lib/threads.h" + +#ifdef CONFIG_UCW_THREADS + +#include + +static pthread_key_t ucwlib_context_key; +static pthread_mutex_t ucwlib_master_mutex; + +static void CONSTRUCTOR +ucwlib_threads_init(void) +{ + if (pthread_key_create(&ucwlib_context_key, NULL) < 0) + die("Cannot create pthread_key: %m"); + pthread_mutex_init(&ucwlib_master_mutex, NULL); +} + +struct ucwlib_context * +ucwlib_thread_context(void) +{ + struct ucwlib_context *c = pthread_getspecific(ucwlib_context_key); + if (!c) + { + c = xmalloc_zero(sizeof(*c)); + pthread_setspecific(ucwlib_context_key, c); + } + return c; +} + +void +ucwlib_lock(void) +{ + pthread_mutex_lock(&ucwlib_master_mutex); +} + +void +ucwlib_unlock(void) +{ + pthread_mutex_unlock(&ucwlib_master_mutex); +} + +#else + +struct ucwlib_context * +ucw_thread_context(void) +{ + static struct ucwlib_context ucwlib_context; + return &ucwlib_context; +} + +void +ucwlib_lock(void) +{ +} + +void +ucwlib_unlock(void) +{ +} + +#endif + +#ifdef TEST + +int main(void) +{ + ucwlib_lock(); + ucwlib_unlock(); + ucwlib_thread_context(); + return 0; +} + +#endif diff --git a/lib/threads.h b/lib/threads.h new file mode 100644 index 00000000..9e33109e --- /dev/null +++ b/lib/threads.h @@ -0,0 +1,34 @@ +/* + * The UCW Library -- Threading Helpers + * + * (c) 2006 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#ifndef _UCW_THREAD_H +#define _UCW_THREAD_H + +/* This structure holds per-thread data */ + +struct ucwlib_context { + int temp_counter; // Counter for fb-temp.c + struct asio_queue *io_queue; // Async I/O queue for fb-direct.c + sh_sighandler_t *signal_handlers; // Signal handlers for sighandler.c +}; + +struct ucwlib_context *ucwlib_thread_context(void); + +/* Global lock used for initialization, cleanup and other not so frequently accessed global state */ + +void ucwlib_lock(void); +void ucwlib_unlock(void); + +#ifdef CONFIG_UCW_THREADS + +extern uns default_thread_stack_size; + +#endif + +#endif -- 2.39.2