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