dynwind.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003, 2004, 2006, 2008, 2010, 2011 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 <assert.h>
  22. #include "libguile/_scm.h"
  23. #include "libguile/control.h"
  24. #include "libguile/eval.h"
  25. #include "libguile/alist.h"
  26. #include "libguile/fluids.h"
  27. #include "libguile/ports.h"
  28. #include "libguile/smob.h"
  29. #include "libguile/dynwind.h"
  30. /* {Dynamic wind}
  31. Things that can be on the wind list:
  32. #<frame>
  33. #<winder>
  34. #<with-fluids>
  35. #<prompt>
  36. (enter-proc . leave-proc) dynamic-wind
  37. */
  38. SCM
  39. scm_dynamic_wind (SCM in_guard, SCM thunk, SCM out_guard)
  40. #define FUNC_NAME "dynamic-wind"
  41. {
  42. SCM ans, old_winds;
  43. SCM_ASSERT (scm_is_true (scm_thunk_p (out_guard)),
  44. out_guard,
  45. SCM_ARG3, FUNC_NAME);
  46. scm_call_0 (in_guard);
  47. old_winds = scm_i_dynwinds ();
  48. scm_i_set_dynwinds (scm_acons (in_guard, out_guard, old_winds));
  49. ans = scm_call_0 (thunk);
  50. scm_i_set_dynwinds (old_winds);
  51. scm_call_0 (out_guard);
  52. return ans;
  53. }
  54. #undef FUNC_NAME
  55. /* Frames and winders. */
  56. static scm_t_bits tc16_frame;
  57. #define FRAME_P(f) SCM_SMOB_PREDICATE (tc16_frame, (f))
  58. #define FRAME_F_REWINDABLE (1 << 0)
  59. #define FRAME_REWINDABLE_P(f) (SCM_SMOB_FLAGS(f) & FRAME_F_REWINDABLE)
  60. static scm_t_bits tc16_winder;
  61. #define WINDER_P(w) SCM_SMOB_PREDICATE (tc16_winder, (w))
  62. #define WINDER_PROC(w) ((void (*)(void *))SCM_SMOB_DATA (w))
  63. #define WINDER_DATA(w) ((void *)SCM_SMOB_DATA_2 (w))
  64. #define WINDER_F_EXPLICIT (1 << 0)
  65. #define WINDER_F_REWIND (1 << 1)
  66. #define WINDER_F_MARK (1 << 2)
  67. #define WINDER_EXPLICIT_P(w) (SCM_SMOB_FLAGS(w) & WINDER_F_EXPLICIT)
  68. #define WINDER_REWIND_P(w) (SCM_SMOB_FLAGS(w) & WINDER_F_REWIND)
  69. #define WINDER_MARK_P(w) (SCM_SMOB_FLAGS(w) & WINDER_F_MARK)
  70. void
  71. scm_dynwind_begin (scm_t_dynwind_flags flags)
  72. {
  73. SCM f;
  74. SCM_NEWSMOB (f, tc16_frame, 0);
  75. if (flags & SCM_F_DYNWIND_REWINDABLE)
  76. SCM_SET_SMOB_FLAGS (f, FRAME_F_REWINDABLE);
  77. scm_i_set_dynwinds (scm_cons (f, scm_i_dynwinds ()));
  78. }
  79. void
  80. scm_dynwind_end (void)
  81. {
  82. SCM winds;
  83. /* Unwind upto and including the next frame entry. We can only
  84. encounter #<winder> entries on the way.
  85. */
  86. winds = scm_i_dynwinds ();
  87. while (scm_is_pair (winds))
  88. {
  89. SCM entry = SCM_CAR (winds);
  90. winds = SCM_CDR (winds);
  91. scm_i_set_dynwinds (winds);
  92. if (FRAME_P (entry))
  93. return;
  94. assert (WINDER_P (entry));
  95. if (!WINDER_REWIND_P (entry) && WINDER_EXPLICIT_P (entry))
  96. WINDER_PROC(entry) (WINDER_DATA (entry));
  97. }
  98. assert (0);
  99. }
  100. void
  101. scm_dynwind_unwind_handler (void (*proc) (void *), void *data,
  102. scm_t_wind_flags flags)
  103. {
  104. SCM w;
  105. SCM_NEWSMOB2 (w, tc16_winder, (scm_t_bits) proc, (scm_t_bits) data);
  106. if (flags & SCM_F_WIND_EXPLICITLY)
  107. SCM_SET_SMOB_FLAGS (w, WINDER_F_EXPLICIT);
  108. scm_i_set_dynwinds (scm_cons (w, scm_i_dynwinds ()));
  109. }
  110. void
  111. scm_dynwind_rewind_handler (void (*proc) (void *), void *data,
  112. scm_t_wind_flags flags)
  113. {
  114. SCM w;
  115. SCM_NEWSMOB2 (w, tc16_winder, (scm_t_bits) proc, (scm_t_bits) data);
  116. SCM_SET_SMOB_FLAGS (w, WINDER_F_REWIND);
  117. scm_i_set_dynwinds (scm_cons (w, scm_i_dynwinds ()));
  118. if (flags & SCM_F_WIND_EXPLICITLY)
  119. proc (data);
  120. }
  121. void
  122. scm_dynwind_unwind_handler_with_scm (void (*proc) (SCM), SCM data,
  123. scm_t_wind_flags flags)
  124. {
  125. SCM w;
  126. scm_t_bits fl = ((flags&SCM_F_WIND_EXPLICITLY)? WINDER_F_EXPLICIT : 0);
  127. SCM_NEWSMOB2 (w, tc16_winder, (scm_t_bits) proc, SCM_UNPACK (data));
  128. SCM_SET_SMOB_FLAGS (w, fl | WINDER_F_MARK);
  129. scm_i_set_dynwinds (scm_cons (w, scm_i_dynwinds ()));
  130. }
  131. void
  132. scm_dynwind_rewind_handler_with_scm (void (*proc) (SCM), SCM data,
  133. scm_t_wind_flags flags)
  134. {
  135. SCM w;
  136. SCM_NEWSMOB2 (w, tc16_winder, (scm_t_bits) proc, SCM_UNPACK (data));
  137. SCM_SET_SMOB_FLAGS (w, WINDER_F_REWIND | WINDER_F_MARK);
  138. scm_i_set_dynwinds (scm_cons (w, scm_i_dynwinds ()));
  139. if (flags & SCM_F_WIND_EXPLICITLY)
  140. proc (data);
  141. }
  142. void
  143. scm_dynwind_free (void *mem)
  144. {
  145. scm_dynwind_unwind_handler (free, mem, SCM_F_WIND_EXPLICITLY);
  146. }
  147. #ifdef GUILE_DEBUG
  148. SCM_DEFINE (scm_wind_chain, "wind-chain", 0, 0, 0,
  149. (),
  150. "Return the current wind chain. The wind chain contains all\n"
  151. "information required by @code{dynamic-wind} to call its\n"
  152. "argument thunks when entering/exiting its scope.")
  153. #define FUNC_NAME s_scm_wind_chain
  154. {
  155. return scm_i_dynwinds ();
  156. }
  157. #undef FUNC_NAME
  158. #endif
  159. void
  160. scm_swap_bindings (SCM vars, SCM vals)
  161. {
  162. SCM tmp;
  163. while (SCM_NIMP (vals))
  164. {
  165. tmp = SCM_VARIABLE_REF (SCM_CAR (vars));
  166. SCM_VARIABLE_SET (SCM_CAR (vars), SCM_CAR (vals));
  167. SCM_SETCAR (vals, tmp);
  168. vars = SCM_CDR (vars);
  169. vals = SCM_CDR (vals);
  170. }
  171. }
  172. void
  173. scm_dowinds (SCM to, long delta)
  174. {
  175. scm_i_dowinds (to, delta, NULL, NULL);
  176. }
  177. void
  178. scm_i_dowinds (SCM to, long delta, void (*turn_func) (void *), void *data)
  179. {
  180. tail:
  181. if (scm_is_eq (to, scm_i_dynwinds ()))
  182. {
  183. if (turn_func)
  184. turn_func (data);
  185. }
  186. else if (delta < 0)
  187. {
  188. SCM wind_elt;
  189. scm_i_dowinds (SCM_CDR (to), 1 + delta, turn_func, data);
  190. wind_elt = SCM_CAR (to);
  191. if (FRAME_P (wind_elt))
  192. {
  193. if (!FRAME_REWINDABLE_P (wind_elt))
  194. scm_misc_error ("dowinds",
  195. "cannot invoke continuation from this context",
  196. SCM_EOL);
  197. }
  198. else if (WINDER_P (wind_elt))
  199. {
  200. if (WINDER_REWIND_P (wind_elt))
  201. WINDER_PROC (wind_elt) (WINDER_DATA (wind_elt));
  202. }
  203. else if (SCM_WITH_FLUIDS_P (wind_elt))
  204. {
  205. scm_i_swap_with_fluids (wind_elt,
  206. SCM_I_CURRENT_THREAD->dynamic_state);
  207. }
  208. else if (SCM_PROMPT_P (wind_elt))
  209. ; /* pass -- see vm_reinstate_partial_continuation */
  210. else if (scm_is_pair (wind_elt))
  211. scm_call_0 (SCM_CAR (wind_elt));
  212. else
  213. /* trash on the wind list */
  214. abort ();
  215. scm_i_set_dynwinds (to);
  216. }
  217. else
  218. {
  219. SCM wind;
  220. SCM wind_elt;
  221. wind = scm_i_dynwinds ();
  222. wind_elt = SCM_CAR (wind);
  223. scm_i_set_dynwinds (SCM_CDR (wind));
  224. if (FRAME_P (wind_elt))
  225. {
  226. /* Nothing to do. */
  227. }
  228. else if (WINDER_P (wind_elt))
  229. {
  230. if (!WINDER_REWIND_P (wind_elt))
  231. WINDER_PROC (wind_elt) (WINDER_DATA (wind_elt));
  232. }
  233. else if (SCM_WITH_FLUIDS_P (wind_elt))
  234. {
  235. scm_i_swap_with_fluids (wind_elt,
  236. SCM_I_CURRENT_THREAD->dynamic_state);
  237. }
  238. else if (SCM_PROMPT_P (wind_elt))
  239. ; /* pass -- though we could invalidate the prompt */
  240. else if (scm_is_pair (wind_elt))
  241. scm_call_0 (SCM_CDR (wind_elt));
  242. else
  243. /* trash on the wind list */
  244. abort ();
  245. delta--;
  246. goto tail; /* scm_dowinds(to, delta-1); */
  247. }
  248. }
  249. void
  250. scm_init_dynwind ()
  251. {
  252. tc16_frame = scm_make_smob_type ("frame", 0);
  253. tc16_winder = scm_make_smob_type ("winder", 0);
  254. #include "libguile/dynwind.x"
  255. }
  256. /*
  257. Local Variables:
  258. c-file-style: "gnu"
  259. End:
  260. */