From 4a235623391dd0d3d46f87dcf00d152e1787c191 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ludovic=20Court=C3=A8s?= Date: Fri, 22 Apr 2011 12:29:50 +0200 Subject: [PATCH] Add pthread-related tests. * test-suite/standalone/Makefile.am (test_scm_spawn_thread_CFLAGS, test_scm_spawn_thread_LDADD, test_pthread_create_CFLAGS, test_pthread_create_LDADD, test_pthread_create_secondary_CFLAGS, test_pthread_create_secondary_LDADD): New variables. (check_PROGRAMS)[BUILD_PTHREAD_SUPPORT]: Add `test-scm-spawn-thread', `test-pthread_create', `test-pthread_create-secondary'. (TESTS)[BUILD_PTHREAD_SUPPORT]: Likewise. * test-suite/standalone/test-scm-spawn-thread.c, test-suite/standalone/test-pthread-create.c, test-suite/standalone/test-pthread-create-secondary.c: New files. --- .gitignore | 3 + test-suite/standalone/Makefile.am | 15 ++++ .../test-pthread-create-secondary.c | 85 +++++++++++++++++++ test-suite/standalone/test-pthread-create.c | 63 ++++++++++++++ test-suite/standalone/test-scm-spawn-thread.c | 62 ++++++++++++++ 5 files changed, 228 insertions(+) create mode 100644 test-suite/standalone/test-pthread-create-secondary.c create mode 100644 test-suite/standalone/test-pthread-create.c create mode 100644 test-suite/standalone/test-scm-spawn-thread.c diff --git a/.gitignore b/.gitignore index f24e589e5..a0eeeca76 100644 --- a/.gitignore +++ b/.gitignore @@ -139,3 +139,6 @@ INSTALL /.sc-start-* /lib/math.h /lib/sys/time.h +/test-suite/standalone/test-scm-spawn-thread +/test-suite/standalone/test-pthread-create +/test-suite/standalone/test-pthread-create-secondary diff --git a/test-suite/standalone/Makefile.am b/test-suite/standalone/Makefile.am index b21edd20d..cf1fc4fe3 100644 --- a/test-suite/standalone/Makefile.am +++ b/test-suite/standalone/Makefile.am @@ -198,6 +198,21 @@ test_scm_with_guile_LDADD = $(LIBGUILE_LDADD) check_PROGRAMS += test-scm-with-guile TESTS += test-scm-with-guile +test_scm_spawn_thread_CFLAGS = ${test_cflags} +test_scm_spawn_thread_LDADD = $(LIBGUILE_LDADD) +check_PROGRAMS += test-scm-spawn-thread +TESTS += test-scm-spawn-thread + +test_pthread_create_CFLAGS = ${test_cflags} +test_pthread_create_LDADD = $(LIBGUILE_LDADD) +check_PROGRAMS += test-pthread-create +TESTS += test-pthread-create + +test_pthread_create_secondary_CFLAGS = ${test_cflags} $(BDW_GC_CFLAGS) +test_pthread_create_secondary_LDADD = $(LIBGUILE_LDADD) +check_PROGRAMS += test-pthread-create-secondary +TESTS += test-pthread-create-secondary + else EXTRA_DIST += test-with-guile-module.c test-scm-with-guile.c diff --git a/test-suite/standalone/test-pthread-create-secondary.c b/test-suite/standalone/test-pthread-create-secondary.c new file mode 100644 index 000000000..fe39c2a26 --- /dev/null +++ b/test-suite/standalone/test-pthread-create-secondary.c @@ -0,0 +1,85 @@ +/* Copyright (C) 2011 Free Software Foundation, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 3 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +/* Test whether threads created with `pthread_create' work, and whether + a secondary thread can call `scm_with_guile'. (bug #32436). */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include +#include + +#include + + +/* Up to GC 7.2alpha5, calling `GC_INIT' from a secondary thread would + lead to a segfault. This was fixed in BDW-GC on 2011-04-16 by Ivan + Maidanski. See + for details. */ + +#if (GC_VERSION_MAJOR > 7) \ + || ((GC_VERSION_MAJOR == 7) && (GC_VERSION_MINOR > 2)) \ + || ((GC_VERSION_MAJOR == 7) && (GC_VERSION_MINOR == 2) \ + && (GC_ALPHA_VERSION > 5)) + +static void * +do_something (void *arg) +{ + scm_list_copy (scm_make_list (scm_from_int (1234), SCM_BOOL_T)); + scm_gc (); + return NULL; +} + +static void * +thread (void *arg) +{ + scm_with_guile (do_something, NULL); + return NULL; +} + + +int +main (int argc, char *argv[]) +{ + int i; + + for (i = 0; i < 77; i++) + { + pthread_t thr; + + pthread_create (&thr, NULL, thread, NULL); + pthread_join (thr, NULL); + } + + return EXIT_SUCCESS; +} + + +#else /* GC < 7.2 */ + +int +main (int argc, char *argv[]) +{ + /* Skip. */ + return 77; +} + +#endif /* GC < 7.2 */ diff --git a/test-suite/standalone/test-pthread-create.c b/test-suite/standalone/test-pthread-create.c new file mode 100644 index 000000000..8d9617b58 --- /dev/null +++ b/test-suite/standalone/test-pthread-create.c @@ -0,0 +1,63 @@ +/* Copyright (C) 2011 Free Software Foundation, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 3 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +/* Test whether threads created with `pthread_create' work (bug #32436) + when then main thread is the one that initializes Guile. */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include +#include + +static void * +do_something (void *arg) +{ + scm_list_copy (scm_make_list (scm_from_int (1234), SCM_BOOL_T)); + scm_gc (); + return NULL; +} + +static void * +thread (void *arg) +{ + scm_with_guile (do_something, NULL); + return NULL; +} + + +int +main (int argc, char *argv[]) +{ + int i; + pthread_t thr; + + scm_init_guile (); + + do_something (NULL); + + for (i = 0; i < 77; i++) + { + pthread_create (&thr, NULL, thread, NULL); + pthread_join (thr, NULL); + } + + return EXIT_SUCCESS; +} diff --git a/test-suite/standalone/test-scm-spawn-thread.c b/test-suite/standalone/test-scm-spawn-thread.c new file mode 100644 index 000000000..b632ab0fe --- /dev/null +++ b/test-suite/standalone/test-scm-spawn-thread.c @@ -0,0 +1,62 @@ +/* Copyright (C) 2011 Free Software Foundation, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 3 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +/* Test whether a thread created with `scm_spawn_thread' can be joined. + See for the + original report. */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include + +#include +#include + +static SCM +thread_main (void *data) +{ + return SCM_BOOL_T; +} + +static SCM +thread_handler (void *data, SCM key, SCM args) +{ + return SCM_BOOL_T; +} + +static void * +inner_main (void *data) +{ + SCM thread, timeout; + + thread = scm_spawn_thread (thread_main, 0, thread_handler, 0); + timeout = scm_from_unsigned_integer (time (NULL) + 10); + return (void *) scm_join_thread_timed (thread, timeout, SCM_BOOL_F); +} + + +int +main (int argc, char **argv) +{ + SCM result; + + result = PTR2SCM (scm_with_guile (inner_main, 0)); + return scm_is_true (result) ? EXIT_SUCCESS : EXIT_FAILURE; +} -- 2.20.1