]> mj.ucw.cz Git - libucw.git/commitdiff
Added threading infrastructure to libucw and CONFIG_UCW_THREADS.
authorMartin Mares <mj@ucw.cz>
Mon, 11 Dec 2006 22:43:37 +0000 (23:43 +0100)
committerMartin Mares <mj@ucw.cz>
Mon, 11 Dec 2006 22:43:37 +0000 (23:43 +0100)
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
lib/threads-conf.c [new file with mode: 0644]
lib/threads.c [new file with mode: 0644]
lib/threads.h [new file with mode: 0644]

index 15dc0a246d60df199676c55244b9cce182ae0052..14018b1c6d6fabda8868bf92ec983a9875ec3aa3 100644 (file)
@@ -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 (file)
index 0000000..f3ac5a2
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ *     The UCW Library -- Threading Helpers
+ *
+ *     (c) 2006 Martin Mares <mj@ucw.cz>
+ *
+ *     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 (file)
index 0000000..26033e5
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ *     The UCW Library -- Threading Helpers
+ *
+ *     (c) 2006 Martin Mares <mj@ucw.cz>
+ *
+ *     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 <pthread.h>
+
+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 (file)
index 0000000..9e33109
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ *     The UCW Library -- Threading Helpers
+ *
+ *     (c) 2006 Martin Mares <mj@ucw.cz>
+ *
+ *     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