about summary refs log tree commit homepage
path: root/thrpool.c
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2012-01-11 21:46:04 +0000
committerEric Wong <normalperson@yhbt.net>2012-01-11 21:46:04 +0000
commit301b41b6f1350806a750794d615e3468735757a6 (patch)
tree54deb2b4cb0060a54746e3635746d0f338c294f5 /thrpool.c
downloadcmogstored-301b41b6f1350806a750794d615e3468735757a6.tar.gz
Nuked old history since it was missing copyright/GPLv3 notices.
Diffstat (limited to 'thrpool.c')
-rw-r--r--thrpool.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/thrpool.c b/thrpool.c
new file mode 100644
index 0000000..fcd5a04
--- /dev/null
+++ b/thrpool.c
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2012, Eric Wong <normalperson@yhbt.net>
+ * License: GPLv3 or later (see COPYING for details)
+ */
+#include "cmogstored.h"
+
+/* we can lower this if we can test with lower values, but NPTL is 16K */
+#define MOG_THR_STACK_SIZE ((16 * 1024))
+
+#if defined(PTHREAD_STACK_MIN) && (PTHREAD_STACK_MIN > MOG_THR_STACK_SIZE)
+#  undef MOG_THR_STACK_SIZE
+#  define MOG_THR_STACK_SIZE PTHREAD_STACK_MIN
+#endif
+
+static const size_t stacksize = (size_t)MOG_THR_STACK_SIZE;
+
+void
+mog_thrpool_start(struct mog_thrpool *tp, size_t n,
+                  void *(*start_fn)(void *), void *arg)
+{
+        size_t i;
+        pthread_t *thr;
+
+        tp->threads = thr = xmalloc(sizeof(pthread_t) * n);
+        tp->n_threads = n;
+
+        for (i = 0; i < n; i++, thr++) {
+                pthread_attr_t attr;
+
+                CHECK(int, 0, pthread_attr_init(&attr));
+                CHECK(int, 0, pthread_attr_setstacksize(&attr, stacksize));
+                CHECK(int, 0, pthread_create(thr, &attr, start_fn, arg));
+                CHECK(int, 0, pthread_attr_destroy(&attr));
+        }
+}
+
+void mog_thrpool_quit(struct mog_thrpool *tp)
+{
+        pthread_t *thr = tp->threads;
+
+        for (; tp->n_threads--; thr++) {
+                CHECK(int, 0, pthread_cancel(*thr));
+                CHECK(int, 0, pthread_join(*thr, NULL));
+        }
+        mog_free_and_null(&tp->threads);
+}