promises.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011
  2. * Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <alloca.h>
  23. #include "libguile/__scm.h"
  24. #include "libguile/_scm.h"
  25. #include "libguile/alist.h"
  26. #include "libguile/async.h"
  27. #include "libguile/continuations.h"
  28. #include "libguile/debug.h"
  29. #include "libguile/deprecation.h"
  30. #include "libguile/dynwind.h"
  31. #include "libguile/eq.h"
  32. #include "libguile/eval.h"
  33. #include "libguile/feature.h"
  34. #include "libguile/fluids.h"
  35. #include "libguile/goops.h"
  36. #include "libguile/hash.h"
  37. #include "libguile/hashtab.h"
  38. #include "libguile/list.h"
  39. #include "libguile/macros.h"
  40. #include "libguile/memoize.h"
  41. #include "libguile/modules.h"
  42. #include "libguile/ports.h"
  43. #include "libguile/print.h"
  44. #include "libguile/procprop.h"
  45. #include "libguile/programs.h"
  46. #include "libguile/root.h"
  47. #include "libguile/smob.h"
  48. #include "libguile/srcprop.h"
  49. #include "libguile/stackchk.h"
  50. #include "libguile/strings.h"
  51. #include "libguile/threads.h"
  52. #include "libguile/throw.h"
  53. #include "libguile/validate.h"
  54. #include "libguile/values.h"
  55. #include "libguile/promises.h"
  56. scm_t_bits scm_tc16_promise;
  57. SCM_DEFINE (scm_make_promise, "make-promise", 1, 0, 0,
  58. (SCM thunk),
  59. "Create a new promise object.\n\n"
  60. "@code{make-promise} is a procedural form of @code{delay}.\n"
  61. "These two expressions are equivalent:\n"
  62. "@lisp\n"
  63. "(delay @var{exp})\n"
  64. "(make-promise (lambda () @var{exp}))\n"
  65. "@end lisp\n")
  66. #define FUNC_NAME s_scm_make_promise
  67. {
  68. SCM_VALIDATE_THUNK (1, thunk);
  69. SCM_RETURN_NEWSMOB2 (scm_tc16_promise,
  70. SCM_UNPACK (thunk),
  71. SCM_UNPACK (scm_make_recursive_mutex ()));
  72. }
  73. #undef FUNC_NAME
  74. static int
  75. promise_print (SCM exp, SCM port, scm_print_state *pstate)
  76. {
  77. int writingp = SCM_WRITINGP (pstate);
  78. scm_puts_unlocked ("#<promise ", port);
  79. SCM_SET_WRITINGP (pstate, 1);
  80. scm_iprin1 (SCM_PROMISE_DATA (exp), port, pstate);
  81. SCM_SET_WRITINGP (pstate, writingp);
  82. scm_putc_unlocked ('>', port);
  83. return !0;
  84. }
  85. SCM_DEFINE (scm_force, "force", 1, 0, 0,
  86. (SCM promise),
  87. "If @var{promise} has not been computed yet, compute and\n"
  88. "return @var{promise}, otherwise just return the previously computed\n"
  89. "value.")
  90. #define FUNC_NAME s_scm_force
  91. {
  92. SCM_VALIDATE_SMOB (1, promise, promise);
  93. scm_lock_mutex (SCM_PROMISE_MUTEX (promise));
  94. if (!SCM_PROMISE_COMPUTED_P (promise))
  95. {
  96. SCM ans = scm_call_0 (SCM_PROMISE_DATA (promise));
  97. if (!SCM_PROMISE_COMPUTED_P (promise))
  98. {
  99. SCM_SET_PROMISE_DATA (promise, ans);
  100. SCM_SET_PROMISE_COMPUTED (promise);
  101. }
  102. }
  103. scm_unlock_mutex (SCM_PROMISE_MUTEX (promise));
  104. return SCM_PROMISE_DATA (promise);
  105. }
  106. #undef FUNC_NAME
  107. SCM_DEFINE (scm_promise_p, "promise?", 1, 0, 0,
  108. (SCM obj),
  109. "Return true if @var{obj} is a promise, i.e. a delayed computation\n"
  110. "(@pxref{Delayed evaluation,,,r5rs.info,The Revised^5 Report on Scheme}).")
  111. #define FUNC_NAME s_scm_promise_p
  112. {
  113. return scm_from_bool (SCM_TYP16_PREDICATE (scm_tc16_promise, obj));
  114. }
  115. #undef FUNC_NAME
  116. void
  117. scm_init_promises ()
  118. {
  119. scm_tc16_promise = scm_make_smob_type ("promise", 0);
  120. scm_set_smob_print (scm_tc16_promise, promise_print);
  121. #include "libguile/promises.x"
  122. scm_add_feature ("delay");
  123. }
  124. /*
  125. Local Variables:
  126. c-file-style: "gnu"
  127. End:
  128. */