test-pthread-create-secondary.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Copyright 2011,2013,2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* Test whether threads created with `pthread_create' work, and whether
  16. a secondary thread can call `scm_with_guile'. (bug #32436). */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include <pthread.h>
  21. #include <stdlib.h>
  22. #include <libguile.h>
  23. #include <gc/gc.h>
  24. /* Currently, calling `GC_INIT' from a secondary thread is only
  25. supported on some systems, notably Linux-based systems (and not on
  26. FreeBSD, for instance.)
  27. Up to GC 7.2alpha5, calling `GC_INIT' from a secondary thread would
  28. lead to a segfault. This was fixed in BDW-GC on 2011-04-16 by Ivan
  29. Maidanski. See <http://thread.gmane.org/gmane.lisp.guile.bugs/5340>
  30. for details. */
  31. #if defined __linux__ \
  32. && (GC_VERSION_MAJOR > 7 \
  33. || (GC_VERSION_MAJOR == 7 && GC_VERSION_MINOR > 2) \
  34. || (GC_VERSION_MAJOR == 7 && GC_VERSION_MINOR == 2 \
  35. && GC_ALPHA_VERSION > 5))
  36. static void *
  37. do_something (void *arg)
  38. {
  39. scm_list_copy (scm_make_list (scm_from_int (1234), SCM_BOOL_T));
  40. scm_gc ();
  41. return NULL;
  42. }
  43. static void *
  44. thread (void *arg)
  45. {
  46. scm_with_guile (do_something, NULL);
  47. return NULL;
  48. }
  49. int
  50. main (int argc, char *argv[])
  51. {
  52. int i;
  53. for (i = 0; i < 77; i++)
  54. {
  55. pthread_t thr;
  56. pthread_create (&thr, NULL, thread, NULL);
  57. pthread_join (thr, NULL);
  58. }
  59. return EXIT_SUCCESS;
  60. }
  61. #else /* Linux && GC < 7.2alpha5 */
  62. int
  63. main (int argc, char *argv[])
  64. {
  65. /* Skip. */
  66. return 77;
  67. }
  68. #endif /* GC < 7.2 */