Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / test-suite / standalone / test-pthread-create-secondary.c
1 /* Copyright (C) 2011 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
17 */
18
19 /* Test whether threads created with `pthread_create' work, and whether
20 a secondary thread can call `scm_with_guile'. (bug #32436). */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <pthread.h>
27 #include <stdlib.h>
28 #include <libguile.h>
29
30 #include <gc/gc.h>
31
32
33 /* Up to GC 7.2alpha5, calling `GC_INIT' from a secondary thread would
34 lead to a segfault. This was fixed in BDW-GC on 2011-04-16 by Ivan
35 Maidanski. See <http://thread.gmane.org/gmane.lisp.guile.bugs/5340>
36 for details. */
37
38 #if (GC_VERSION_MAJOR > 7) \
39 || ((GC_VERSION_MAJOR == 7) && (GC_VERSION_MINOR > 2)) \
40 || ((GC_VERSION_MAJOR == 7) && (GC_VERSION_MINOR == 2) \
41 && (GC_ALPHA_VERSION > 5))
42
43 static void *
44 do_something (void *arg)
45 {
46 scm_list_copy (scm_make_list (scm_from_int (1234), SCM_BOOL_T));
47 scm_gc ();
48 return NULL;
49 }
50
51 static void *
52 thread (void *arg)
53 {
54 scm_with_guile (do_something, NULL);
55 return NULL;
56 }
57
58 \f
59 int
60 main (int argc, char *argv[])
61 {
62 int i;
63
64 for (i = 0; i < 77; i++)
65 {
66 pthread_t thr;
67
68 pthread_create (&thr, NULL, thread, NULL);
69 pthread_join (thr, NULL);
70 }
71
72 return EXIT_SUCCESS;
73 }
74
75 \f
76 #else /* GC < 7.2 */
77
78 int
79 main (int argc, char *argv[])
80 {
81 /* Skip. */
82 return 77;
83 }
84
85 #endif /* GC < 7.2 */