promises.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/smob.h"
  47. #include "libguile/srcprop.h"
  48. #include "libguile/stackchk.h"
  49. #include "libguile/strings.h"
  50. #include "libguile/threads.h"
  51. #include "libguile/throw.h"
  52. #include "libguile/validate.h"
  53. #include "libguile/values.h"
  54. #include "libguile/promises.h"
  55. scm_t_bits scm_tc16_promise;
  56. SCM_DEFINE (scm_make_promise, "make-promise", 1, 0, 0,
  57. (SCM thunk),
  58. "Create a new promise object.\n\n"
  59. "@code{make-promise} is a procedural form of @code{delay}.\n"
  60. "These two expressions are equivalent:\n"
  61. "@lisp\n"
  62. "(delay @var{exp})\n"
  63. "(make-promise (lambda () @var{exp}))\n"
  64. "@end lisp\n")
  65. #define FUNC_NAME s_scm_make_promise
  66. {
  67. SCM_VALIDATE_THUNK (1, thunk);
  68. SCM_RETURN_NEWSMOB2 (scm_tc16_promise,
  69. SCM_UNPACK (thunk),
  70. SCM_UNPACK (scm_make_recursive_mutex ()));
  71. }
  72. #undef FUNC_NAME
  73. static int
  74. promise_print (SCM exp, SCM port, scm_print_state *pstate)
  75. {
  76. int writingp = SCM_WRITINGP (pstate);
  77. scm_puts ("#<promise ", port);
  78. SCM_SET_WRITINGP (pstate, 1);
  79. scm_iprin1 (SCM_PROMISE_DATA (exp), port, pstate);
  80. SCM_SET_WRITINGP (pstate, writingp);
  81. scm_putc ('>', port);
  82. return !0;
  83. }
  84. SCM_DEFINE (scm_force, "force", 1, 0, 0,
  85. (SCM promise),
  86. "If @var{promise} has not been computed yet, compute and\n"
  87. "return @var{promise}, otherwise just return the previously computed\n"
  88. "value.")
  89. #define FUNC_NAME s_scm_force
  90. {
  91. SCM_VALIDATE_SMOB (1, promise, promise);
  92. scm_lock_mutex (SCM_PROMISE_MUTEX (promise));
  93. if (!SCM_PROMISE_COMPUTED_P (promise))
  94. {
  95. SCM ans = scm_call_0 (SCM_PROMISE_DATA (promise));
  96. if (!SCM_PROMISE_COMPUTED_P (promise))
  97. {
  98. SCM_SET_PROMISE_DATA (promise, ans);
  99. SCM_SET_PROMISE_COMPUTED (promise);
  100. }
  101. }
  102. scm_unlock_mutex (SCM_PROMISE_MUTEX (promise));
  103. return SCM_PROMISE_DATA (promise);
  104. }
  105. #undef FUNC_NAME
  106. SCM_DEFINE (scm_promise_p, "promise?", 1, 0, 0,
  107. (SCM obj),
  108. "Return true if @var{obj} is a promise, i.e. a delayed computation\n"
  109. "(@pxref{Delayed evaluation,,,r5rs.info,The Revised^5 Report on Scheme}).")
  110. #define FUNC_NAME s_scm_promise_p
  111. {
  112. return scm_from_bool (SCM_TYP16_PREDICATE (scm_tc16_promise, obj));
  113. }
  114. #undef FUNC_NAME
  115. void
  116. scm_init_promises ()
  117. {
  118. scm_tc16_promise = scm_make_smob_type ("promise", 0);
  119. scm_set_smob_print (scm_tc16_promise, promise_print);
  120. #include "libguile/promises.x"
  121. scm_add_feature ("delay");
  122. }
  123. /*
  124. Local Variables:
  125. c-file-style: "gnu"
  126. End:
  127. */