continuations.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifndef SCM_CONTINUATIONS_H
  2. #define SCM_CONTINUATIONS_H
  3. /* Copyright 1995-1996,2000-2001,2006,2008-2010,2012-2014,2018
  4. Free Software Foundation, Inc.
  5. This file is part of Guile.
  6. Guile is free software: you can redistribute it and/or modify it
  7. under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Guile is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with Guile. If not, see
  16. <https://www.gnu.org/licenses/>. */
  17. #include <setjmp.h>
  18. #include "libguile/programs.h"
  19. #include "libguile/throw.h"
  20. #define SCM_CONTINUATIONP(x) \
  21. (SCM_PROGRAM_P (x) && SCM_PROGRAM_IS_CONTINUATION (x))
  22. /* a continuation SCM is a non-immediate pointing to a heap cell with:
  23. word 0: bits 0-15: smob type tag: scm_tc16_continuation.
  24. bits 16-31: unused.
  25. word 1: malloc block containing an scm_t_contregs structure with a
  26. tail array of SCM_STACKITEM. the size of the array is stored
  27. in the num_stack_items field of the structure.
  28. */
  29. typedef struct
  30. {
  31. jmp_buf jmpbuf;
  32. #if SCM_HAVE_AUXILIARY_STACK
  33. void *auxiliary_stack;
  34. unsigned long auxiliary_stack_size;
  35. #endif
  36. size_t num_stack_items; /* size of the saved stack. */
  37. SCM root; /* continuation root identifier. */
  38. SCM vm_cont; /* vm's stack and regs */
  39. /* The offset from the live stack location to this copy. This is
  40. used to adjust pointers from within the copied stack to the stack
  41. itself.
  42. Thus, when you read a pointer from the copied stack that points
  43. into the live stack, you need to add OFFSET so that it points
  44. into the copy.
  45. */
  46. ptrdiff_t offset;
  47. SCM_STACKITEM stack[1]; /* copied stack of size num_stack_items. */
  48. } scm_t_contregs;
  49. SCM_INTERNAL SCM scm_i_make_continuation (scm_thread *thread, SCM vm_cont);
  50. SCM_INTERNAL void scm_i_reinstate_continuation (SCM cont,
  51. uint8_t *mra) SCM_NORETURN;
  52. SCM_INTERNAL int scm_i_continuation_to_frame (SCM cont,
  53. struct scm_frame *frame);
  54. SCM_INTERNAL scm_t_contregs* scm_i_contregs (SCM contregs);
  55. SCM_API void *scm_c_with_continuation_barrier (void *(*func)(void*), void *);
  56. SCM_API SCM scm_with_continuation_barrier (SCM proc);
  57. SCM_INTERNAL SCM
  58. scm_i_with_continuation_barrier (scm_t_catch_body body,
  59. void *body_data,
  60. scm_t_catch_handler handler,
  61. void *handler_data,
  62. scm_t_catch_handler pre_unwind_handler,
  63. void *pre_unwind_handler_data);
  64. SCM_INTERNAL void scm_init_continuations (void);
  65. #endif /* SCM_CONTINUATIONS_H */