summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSebastian Wilhelmi <wilhelmi@ira.uka.de>2000-04-28 12:24:53 +0000
committerSebastian Wilhelmi <wilhelmi@src.gnome.org>2000-04-28 12:24:53 +0000
commit1418fe3a6dd0c76bcdf644c87d956353bd5b940c (patch)
tree4956606935418bbc6c3e3c4cc0297897e17d1523 /tests
parentf0e9f522c21228f05afd15696d74e7f285e1838e (diff)
downloadglib-1418fe3a6dd0c76bcdf644c87d956353bd5b940c.tar.gz
New File implementing an asynchronous queue to be used for asynchronous
2000-04-28 Sebastian Wilhelmi <wilhelmi@ira.uka.de> * gasyncqueue.c: New File implementing an asynchronous queue to be used for asynchronous inter-thread communication. * gthreadpool.c: New File implementing a thread pool to be used for distributing work among several threads. * glib.h: Added the type and function declarations for these two types. * tests/threadpool-test.c: New File implementing a test for the thread pool. This also checks the asynchronous queue underlying the thread pool. * tests/Makefile.am: Changed accordingly.
Diffstat (limited to 'tests')
-rw-r--r--tests/.cvsignore1
-rw-r--r--tests/Makefile.am3
-rw-r--r--tests/threadpool-test.c57
3 files changed, 61 insertions, 0 deletions
diff --git a/tests/.cvsignore b/tests/.cvsignore
index 30e3af867..c6169b2e1 100644
--- a/tests/.cvsignore
+++ b/tests/.cvsignore
@@ -37,5 +37,6 @@ testgdate
testgdateparser
testglib
thread-test
+threadpool-test
tree-test
type-test
diff --git a/tests/Makefile.am b/tests/Makefile.am
index c1e68cadb..a53473021 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -21,6 +21,7 @@ TESTS = \
strfunc-test \
string-test \
thread-test \
+ threadpool-test \
tree-test \
type-test
@@ -40,6 +41,8 @@ strfunc_test_LDADD = $(top_builddir)/libglib.la
string_test_LDADD = $(top_builddir)/libglib.la
thread_test_LDADD = $(top_builddir)/libglib.la \
$(top_builddir)/gthread/libgthread.la @G_THREAD_LIBS@
+threadpool_test_LDADD = $(top_builddir)/libglib.la \
+ $(top_builddir)/gthread/libgthread.la @G_THREAD_LIBS@
tree_test_LDADD = $(top_builddir)/libglib.la
type_test_LDADD = $(top_builddir)/libglib.la
diff --git a/tests/threadpool-test.c b/tests/threadpool-test.c
new file mode 100644
index 000000000..9044e4bbd
--- /dev/null
+++ b/tests/threadpool-test.c
@@ -0,0 +1,57 @@
+#include <glib.h>
+
+#define RUNS 100
+
+G_LOCK_DEFINE_STATIC (thread_counter);
+gulong abs_thread_counter;
+gulong running_thread_counter;
+
+void
+thread_pool_func (gpointer a, gpointer b)
+{
+ G_LOCK (thread_counter);
+ abs_thread_counter++;
+ running_thread_counter++;
+ G_UNLOCK (thread_counter);
+
+ g_usleep (g_random_int_range (0, 4000));
+
+ G_LOCK (thread_counter);
+ running_thread_counter--;
+ G_UNLOCK (thread_counter);
+}
+
+int
+main (int argc,
+ char *argv[])
+{
+ GThreadPool *pool1, *pool2, *pool3;
+ guint i;
+ /* Only run the test, if threads are enabled and a default thread
+ implementation is available */
+#if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
+ g_thread_init (NULL);
+
+ pool1 = g_thread_pool_new (thread_pool_func, 3, 0, FALSE,
+ G_THREAD_PRIORITY_NORMAL, FALSE, NULL);
+ pool2 = g_thread_pool_new (thread_pool_func, 5, 0, FALSE,
+ G_THREAD_PRIORITY_LOW, FALSE, NULL);
+ pool3 = g_thread_pool_new (thread_pool_func, 7, 0, FALSE,
+ G_THREAD_PRIORITY_LOW, TRUE, NULL);
+
+ for (i = 0; i < RUNS; i++)
+ {
+ g_thread_pool_push (pool1, GUINT_TO_POINTER (1));
+ g_thread_pool_push (pool2, GUINT_TO_POINTER (1));
+ g_thread_pool_push (pool3, GUINT_TO_POINTER (1));
+ }
+
+ g_thread_pool_free (pool1, FALSE, TRUE);
+ g_thread_pool_free (pool2, FALSE, TRUE);
+ g_thread_pool_free (pool3, FALSE, TRUE);
+
+ g_assert (RUNS * 3 == abs_thread_counter);
+ g_assert (running_thread_counter == 0);
+#endif
+ return 0;
+}