api-init.texi 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2007
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Initialization
  7. @section Initializing Guile
  8. @cindex Initializing Guile
  9. Each thread that wants to use functions from the Guile API needs to
  10. put itself into guile mode with either @code{scm_with_guile} or
  11. @code{scm_init_guile}. The global state of Guile is initialized
  12. automatically when the first thread enters guile mode.
  13. When a thread wants to block outside of a Guile API function, it
  14. should leave guile mode temporarily with @code{scm_without_guile},
  15. @xref{Blocking}.
  16. Threads that are created by @code{call-with-new-thread} or
  17. @code{scm_spawn_thread} start out in guile mode so you don't need to
  18. initialize them.
  19. @deftypefn {C Function} {void *} scm_with_guile (void *(*func)(void *), void *data)
  20. Call @var{func}, passing it @var{data} and return what @var{func}
  21. returns. While @var{func} is running, the current thread is in guile
  22. mode and can thus use the Guile API.
  23. When @code{scm_with_guile} is called from guile mode, the thread remains
  24. in guile mode when @code{scm_with_guile} returns.
  25. Otherwise, it puts the current thread into guile mode and, if needed,
  26. gives it a Scheme representation that is contained in the list returned
  27. by @code{all-threads}, for example. This Scheme representation is not
  28. removed when @code{scm_with_guile} returns so that a given thread is
  29. always represented by the same Scheme value during its lifetime, if at
  30. all.
  31. When this is the first thread that enters guile mode, the global state
  32. of Guile is initialized before calling @code{func}.
  33. The function @var{func} is called via
  34. @code{scm_with_continuation_barrier}; thus, @code{scm_with_guile}
  35. returns exactly once.
  36. When @code{scm_with_guile} returns, the thread is no longer in guile
  37. mode (except when @code{scm_with_guile} was called from guile mode, see
  38. above). Thus, only @code{func} can store @code{SCM} variables on the
  39. stack and be sure that they are protected from the garbage collector.
  40. See @code{scm_init_guile} for another approach at initializing Guile
  41. that does not have this restriction.
  42. It is OK to call @code{scm_with_guile} while a thread has temporarily
  43. left guile mode via @code{scm_without_guile}. It will then simply
  44. temporarily enter guile mode again.
  45. @end deftypefn
  46. @deftypefn {C Function} void scm_init_guile ()
  47. Arrange things so that all of the code in the current thread executes as
  48. if from within a call to @code{scm_with_guile}. That is, all functions
  49. called by the current thread can assume that @code{SCM} values on their
  50. stack frames are protected from the garbage collector (except when the
  51. thread has explicitely left guile mode, of course).
  52. When @code{scm_init_guile} is called from a thread that already has been
  53. in guile mode once, nothing happens. This behavior matters when you
  54. call @code{scm_init_guile} while the thread has only temporarily left
  55. guile mode: in that case the thread will not be in guile mode after
  56. @code{scm_init_guile} returns. Thus, you should not use
  57. @code{scm_init_guile} in such a scenario.
  58. When a uncaught throw happens in a thread that has been put into guile
  59. mode via @code{scm_init_guile}, a short message is printed to the
  60. current error port and the thread is exited via @code{scm_pthread_exit
  61. (NULL)}. No restrictions are placed on continuations.
  62. The function @code{scm_init_guile} might not be available on all
  63. platforms since it requires some stack-bounds-finding magic that might
  64. not have been ported to all platforms that Guile runs on. Thus, if you
  65. can, it is better to use @code{scm_with_guile} or its variation
  66. @code{scm_boot_guile} instead of this function.
  67. @end deftypefn
  68. @deftypefn {C Function} void scm_boot_guile (int @var{argc}, char **@var{argv}, void (*@var{main_func}) (void *@var{data}, int @var{argc}, char **@var{argv}), void *@var{data})
  69. Enter guile mode as with @code{scm_with_guile} and call @var{main_func},
  70. passing it @var{data}, @var{argc}, and @var{argv} as indicated. When
  71. @var{main_func} returns, @code{scm_boot_guile} calls @code{exit (0)};
  72. @code{scm_boot_guile} never returns. If you want some other exit value,
  73. have @var{main_func} call @code{exit} itself. If you don't want to exit
  74. at all, use @code{scm_with_guile} instead of @code{scm_boot_guile}.
  75. The function @code{scm_boot_guile} arranges for the Scheme
  76. @code{command-line} function to return the strings given by @var{argc}
  77. and @var{argv}. If @var{main_func} modifies @var{argc} or @var{argv},
  78. it should call @code{scm_set_program_arguments} with the final list, so
  79. Scheme code will know which arguments have been processed
  80. (@pxref{Runtime Environment}).
  81. @end deftypefn
  82. @deftypefn {C Function} void scm_shell (int @var{argc}, char **@var{argv})
  83. Process command-line arguments in the manner of the @code{guile}
  84. executable. This includes loading the normal Guile initialization
  85. files, interacting with the user or running any scripts or expressions
  86. specified by @code{-s} or @code{-e} options, and then exiting.
  87. @xref{Invoking Guile}, for more details.
  88. Since this function does not return, you must do all
  89. application-specific initialization before calling this function.
  90. @end deftypefn