test-pthread-create-secondary.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Copyright (C) 2011, 2013 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. /* Test whether threads created with `pthread_create' work, and whether
  19. a secondary thread can call `scm_with_guile'. (bug #32436). */
  20. #ifdef HAVE_CONFIG_H
  21. # include <config.h>
  22. #endif
  23. #include <pthread.h>
  24. #include <stdlib.h>
  25. #include <libguile.h>
  26. #include <gc/gc.h>
  27. /* Currently, calling `GC_INIT' from a secondary thread is only
  28. supported on some systems, notably Linux-based systems (and not on
  29. FreeBSD, for instance.)
  30. Up to GC 7.2alpha5, calling `GC_INIT' from a secondary thread would
  31. lead to a segfault. This was fixed in BDW-GC on 2011-04-16 by Ivan
  32. Maidanski. See <http://thread.gmane.org/gmane.lisp.guile.bugs/5340>
  33. for details. */
  34. #if defined __linux__ \
  35. && (GC_VERSION_MAJOR > 7 \
  36. || (GC_VERSION_MAJOR == 7 && GC_VERSION_MINOR > 2) \
  37. || (GC_VERSION_MAJOR == 7 && GC_VERSION_MINOR == 2 \
  38. && GC_ALPHA_VERSION > 5))
  39. static void *
  40. do_something (void *arg)
  41. {
  42. scm_list_copy (scm_make_list (scm_from_int (1234), SCM_BOOL_T));
  43. scm_gc ();
  44. return NULL;
  45. }
  46. static void *
  47. thread (void *arg)
  48. {
  49. scm_with_guile (do_something, NULL);
  50. return NULL;
  51. }
  52. int
  53. main (int argc, char *argv[])
  54. {
  55. int i;
  56. for (i = 0; i < 77; i++)
  57. {
  58. pthread_t thr;
  59. pthread_create (&thr, NULL, thread, NULL);
  60. pthread_join (thr, NULL);
  61. }
  62. return EXIT_SUCCESS;
  63. }
  64. #else /* Linux && GC < 7.2alpha5 */
  65. int
  66. main (int argc, char *argv[])
  67. {
  68. /* Skip. */
  69. return 77;
  70. }
  71. #endif /* GC < 7.2 */