]> mj.ucw.cz Git - libucw.git/commitdiff
Added a simple threading helper module.
authorMartin Mares <mj@ucw.cz>
Sat, 9 Dec 2006 20:55:02 +0000 (21:55 +0100)
committerMartin Mares <mj@ucw.cz>
Sat, 9 Dec 2006 20:55:02 +0000 (21:55 +0100)
lib/Makefile
lib/threads.c [new file with mode: 0644]
lib/threads.h [new file with mode: 0644]

index 8f25ba2d1d271351fdf6409d67db4667228e7e2e..d75761a5518da2b8eba16de7faf90df13fff965e 100644 (file)
@@ -7,6 +7,7 @@ PROGS+=$(o)/lib/db-tool
 endif
 
 LIBUCW_MODS= \
+       threads \
        alloc alloc_str realloc bigalloc 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 \
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..3c8b03a
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ *     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 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);
+
+#endif