root.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000, 2001, 2002, 2006, 2008 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. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include "libguile/_scm.h"
  24. #include "libguile/stackchk.h"
  25. #include "libguile/dynwind.h"
  26. #include "libguile/eval.h"
  27. #include "libguile/smob.h"
  28. #include "libguile/pairs.h"
  29. #include "libguile/throw.h"
  30. #include "libguile/fluids.h"
  31. #include "libguile/ports.h"
  32. #include "libguile/root.h"
  33. SCM scm_sys_protects[SCM_NUM_PROTECTS];
  34. /* {call-with-dynamic-root}
  35. *
  36. * Suspending the current thread to evaluate a thunk on the
  37. * same C stack but under a new root.
  38. *
  39. * Calls to call-with-dynamic-root return exactly once (unless
  40. * the process is somehow exitted). */
  41. /* cwdr fills out both of these structures, and then passes a pointer
  42. to them through scm_internal_catch to the cwdr_body and
  43. cwdr_handler functions, to tell them how to behave and to get
  44. information back from them.
  45. A cwdr is a lot like a catch, except there is no tag (all
  46. exceptions are caught), and the body procedure takes the arguments
  47. passed to cwdr as A1 and ARGS. The handler is also special since
  48. it is not directly run from scm_internal_catch. It is executed
  49. outside the new dynamic root. */
  50. struct cwdr_body_data {
  51. /* Arguments to pass to the cwdr body function. */
  52. SCM a1, args;
  53. /* Scheme procedure to use as body of cwdr. */
  54. SCM body_proc;
  55. };
  56. struct cwdr_handler_data {
  57. /* Do we need to run the handler? */
  58. int run_handler;
  59. /* The tag and args to pass it. */
  60. SCM tag, args;
  61. };
  62. /* Invoke the body of a cwdr, assuming that the throw handler has
  63. already been set up. DATA points to a struct set up by cwdr that
  64. says what proc to call, and what args to apply it to.
  65. With a little thought, we could replace this with scm_body_thunk,
  66. but I don't want to mess with that at the moment. */
  67. static SCM
  68. cwdr_body (void *data)
  69. {
  70. struct cwdr_body_data *c = (struct cwdr_body_data *) data;
  71. return scm_apply (c->body_proc, c->a1, c->args);
  72. }
  73. /* Record the fact that the body of the cwdr has thrown. Record
  74. enough information to invoke the handler later when the dynamic
  75. root has been deestablished. */
  76. static SCM
  77. cwdr_handler (void *data, SCM tag, SCM args)
  78. {
  79. struct cwdr_handler_data *c = (struct cwdr_handler_data *) data;
  80. c->run_handler = 1;
  81. c->tag = tag;
  82. c->args = args;
  83. return SCM_UNSPECIFIED;
  84. }
  85. SCM
  86. scm_internal_cwdr (scm_t_catch_body body, void *body_data,
  87. scm_t_catch_handler handler, void *handler_data,
  88. SCM_STACKITEM *stack_start)
  89. {
  90. struct cwdr_handler_data my_handler_data;
  91. SCM answer, old_winds;
  92. /* Exit caller's dynamic state.
  93. */
  94. old_winds = scm_i_dynwinds ();
  95. scm_dowinds (SCM_EOL, scm_ilength (old_winds));
  96. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  97. scm_dynwind_current_dynamic_state (scm_make_dynamic_state (SCM_UNDEFINED));
  98. my_handler_data.run_handler = 0;
  99. answer = scm_i_with_continuation_barrier (body, body_data,
  100. cwdr_handler, &my_handler_data,
  101. NULL, NULL);
  102. scm_dynwind_end ();
  103. /* Enter caller's dynamic state.
  104. */
  105. scm_dowinds (old_winds, - scm_ilength (old_winds));
  106. /* Now run the real handler iff the body did a throw. */
  107. if (my_handler_data.run_handler)
  108. return handler (handler_data, my_handler_data.tag, my_handler_data.args);
  109. else
  110. return answer;
  111. }
  112. /* The original CWDR for invoking Scheme code with a Scheme handler. */
  113. static SCM
  114. cwdr (SCM proc, SCM a1, SCM args, SCM handler, SCM_STACKITEM *stack_start)
  115. {
  116. struct cwdr_body_data c;
  117. c.a1 = a1;
  118. c.args = args;
  119. c.body_proc = proc;
  120. return scm_internal_cwdr (cwdr_body, &c,
  121. scm_handle_by_proc, &handler,
  122. stack_start);
  123. }
  124. SCM_DEFINE (scm_call_with_dynamic_root, "call-with-dynamic-root", 2, 0, 0,
  125. (SCM thunk, SCM handler),
  126. "Call @var{thunk} with a new dynamic state and within"
  127. "a continuation barrier. The @var{handler} catches all"
  128. "otherwise uncaught throws and executes within the same"
  129. "dynamic context as @var{thunk}.")
  130. #define FUNC_NAME s_scm_call_with_dynamic_root
  131. {
  132. SCM_STACKITEM stack_place;
  133. return cwdr (thunk, SCM_EOL, SCM_EOL, handler, &stack_place);
  134. }
  135. #undef FUNC_NAME
  136. SCM_DEFINE (scm_dynamic_root, "dynamic-root", 0, 0, 0,
  137. (),
  138. "Return an object representing the current dynamic root.\n\n"
  139. "These objects are only useful for comparison using @code{eq?}.\n")
  140. #define FUNC_NAME s_scm_dynamic_root
  141. {
  142. return SCM_I_CURRENT_THREAD->continuation_root;
  143. }
  144. #undef FUNC_NAME
  145. SCM
  146. scm_apply_with_dynamic_root (SCM proc, SCM a1, SCM args, SCM handler)
  147. {
  148. SCM_STACKITEM stack_place;
  149. return cwdr (proc, a1, args, handler, &stack_place);
  150. }
  151. void
  152. scm_init_root ()
  153. {
  154. #include "libguile/root.x"
  155. }
  156. /*
  157. Local Variables:
  158. c-file-style: "gnu"
  159. End:
  160. */