fluids.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* Copyright (C) 1996,1997,2000,2001 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include "libguile/_scm.h"
  42. #include "libguile/print.h"
  43. #include "libguile/smob.h"
  44. #include "libguile/dynwind.h"
  45. #include "libguile/fluids.h"
  46. #include "libguile/alist.h"
  47. #include "libguile/eval.h"
  48. #include "libguile/ports.h"
  49. #include "libguile/deprecation.h"
  50. #define INITIAL_FLUIDS 10
  51. #include "libguile/validate.h"
  52. static volatile long n_fluids;
  53. scm_t_bits scm_tc16_fluid;
  54. SCM
  55. scm_make_initial_fluids ()
  56. {
  57. return scm_c_make_vector (INITIAL_FLUIDS, SCM_BOOL_F);
  58. }
  59. static void
  60. grow_fluids (scm_root_state *root_state, int new_length)
  61. {
  62. SCM old_fluids, new_fluids;
  63. long old_length, i;
  64. old_fluids = root_state->fluids;
  65. old_length = SCM_VECTOR_LENGTH (old_fluids);
  66. new_fluids = scm_c_make_vector (new_length, SCM_BOOL_F);
  67. i = 0;
  68. while (i < old_length)
  69. {
  70. SCM_VELTS(new_fluids)[i] = SCM_VELTS(old_fluids)[i];
  71. i++;
  72. }
  73. while (i < new_length)
  74. {
  75. SCM_VELTS(new_fluids)[i] = SCM_BOOL_F;
  76. i++;
  77. }
  78. root_state->fluids = new_fluids;
  79. }
  80. void
  81. scm_copy_fluids (scm_root_state *root_state)
  82. {
  83. grow_fluids (root_state, SCM_VECTOR_LENGTH (root_state->fluids));
  84. }
  85. static int
  86. fluid_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  87. {
  88. scm_puts ("#<fluid ", port);
  89. scm_intprint ((int) SCM_FLUID_NUM (exp), 10, port);
  90. scm_putc ('>', port);
  91. return 1;
  92. }
  93. static long
  94. next_fluid_num ()
  95. {
  96. long n;
  97. SCM_CRITICAL_SECTION_START;
  98. n = n_fluids++;
  99. SCM_CRITICAL_SECTION_END;
  100. return n;
  101. }
  102. SCM_DEFINE (scm_make_fluid, "make-fluid", 0, 0, 0,
  103. (),
  104. "Return a newly created fluid.\n"
  105. "Fluids are objects of a certain type (a smob) that can hold one SCM\n"
  106. "value per dynamic root. That is, modifications to this value are\n"
  107. "only visible to code that executes within the same dynamic root as\n"
  108. "the modifying code. When a new dynamic root is constructed, it\n"
  109. "inherits the values from its parent. Because each thread executes\n"
  110. "in its own dynamic root, you can use fluids for thread local storage.")
  111. #define FUNC_NAME s_scm_make_fluid
  112. {
  113. long n;
  114. n = next_fluid_num ();
  115. SCM_RETURN_NEWSMOB (scm_tc16_fluid, n);
  116. }
  117. #undef FUNC_NAME
  118. SCM_DEFINE (scm_fluid_p, "fluid?", 1, 0, 0,
  119. (SCM obj),
  120. "Return @code{#t} iff @var{obj} is a fluid; otherwise, return\n"
  121. "@code{#f}.")
  122. #define FUNC_NAME s_scm_fluid_p
  123. {
  124. return SCM_BOOL(SCM_FLUIDP (obj));
  125. }
  126. #undef FUNC_NAME
  127. SCM_DEFINE (scm_fluid_ref, "fluid-ref", 1, 0, 0,
  128. (SCM fluid),
  129. "Return the value associated with @var{fluid} in the current\n"
  130. "dynamic root. If @var{fluid} has not been set, then return\n"
  131. "@code{#f}.")
  132. #define FUNC_NAME s_scm_fluid_ref
  133. {
  134. unsigned long int n;
  135. SCM_VALIDATE_FLUID (1, fluid);
  136. n = SCM_FLUID_NUM (fluid);
  137. if (SCM_VECTOR_LENGTH (scm_root->fluids) <= n)
  138. grow_fluids (scm_root, n+1);
  139. return SCM_VELTS (scm_root->fluids)[n];
  140. }
  141. #undef FUNC_NAME
  142. SCM_DEFINE (scm_fluid_set_x, "fluid-set!", 2, 0, 0,
  143. (SCM fluid, SCM value),
  144. "Set the value associated with @var{fluid} in the current dynamic root.")
  145. #define FUNC_NAME s_scm_fluid_set_x
  146. {
  147. unsigned long int n;
  148. SCM_VALIDATE_FLUID (1, fluid);
  149. n = SCM_FLUID_NUM (fluid);
  150. if (SCM_VECTOR_LENGTH (scm_root->fluids) <= n)
  151. grow_fluids (scm_root, n+1);
  152. SCM_VELTS (scm_root->fluids)[n] = value;
  153. return SCM_UNSPECIFIED;
  154. }
  155. #undef FUNC_NAME
  156. void
  157. scm_swap_fluids (SCM fluids, SCM vals)
  158. {
  159. while (!SCM_NULLP (fluids))
  160. {
  161. SCM fl = SCM_CAR (fluids);
  162. SCM old_val = scm_fluid_ref (fl);
  163. scm_fluid_set_x (fl, SCM_CAR (vals));
  164. SCM_SETCAR (vals, old_val);
  165. fluids = SCM_CDR (fluids);
  166. vals = SCM_CDR (vals);
  167. }
  168. }
  169. /* Swap the fluid values in reverse order. This is important when the
  170. same fluid appears multiple times in the fluids list. */
  171. void
  172. scm_swap_fluids_reverse (SCM fluids, SCM vals)
  173. {
  174. if (!SCM_NULLP (fluids))
  175. {
  176. SCM fl, old_val;
  177. scm_swap_fluids_reverse (SCM_CDR (fluids), SCM_CDR (vals));
  178. fl = SCM_CAR (fluids);
  179. old_val = scm_fluid_ref (fl);
  180. scm_fluid_set_x (fl, SCM_CAR (vals));
  181. SCM_SETCAR (vals, old_val);
  182. }
  183. }
  184. static SCM
  185. apply_thunk (void *thunk)
  186. {
  187. return scm_call_0 (SCM_PACK (thunk));
  188. }
  189. SCM_DEFINE (scm_with_fluids, "with-fluids*", 3, 0, 0,
  190. (SCM fluids, SCM values, SCM thunk),
  191. "Set @var{fluids} to @var{values} temporary, and call @var{thunk}.\n"
  192. "@var{fluids} must be a list of fluids and @var{values} must be the same\n"
  193. "number of their values to be applied. Each substitution is done\n"
  194. "one after another. @var{thunk} must be a procedure with no argument.")
  195. #define FUNC_NAME s_scm_with_fluids
  196. {
  197. return scm_c_with_fluids (fluids, values, apply_thunk, (void *) SCM_UNPACK (thunk));
  198. }
  199. #undef FUNC_NAME
  200. SCM
  201. scm_c_with_fluids (SCM fluids, SCM values, SCM (*cproc) (), void *cdata)
  202. #define FUNC_NAME "scm_c_with_fluids"
  203. {
  204. SCM ans;
  205. long flen, vlen;
  206. SCM_VALIDATE_LIST_COPYLEN (1, fluids, flen);
  207. SCM_VALIDATE_LIST_COPYLEN (2, values, vlen);
  208. if (flen != vlen)
  209. scm_out_of_range (s_scm_with_fluids, values);
  210. scm_swap_fluids (fluids, values);
  211. scm_dynwinds = scm_acons (fluids, values, scm_dynwinds);
  212. ans = cproc (cdata);
  213. scm_dynwinds = SCM_CDR (scm_dynwinds);
  214. scm_swap_fluids_reverse (fluids, values);
  215. return ans;
  216. }
  217. #undef FUNC_NAME
  218. SCM
  219. scm_c_with_fluid (SCM fluid, SCM value, SCM (*cproc) (), void *cdata)
  220. #define FUNC_NAME "scm_c_with_fluid"
  221. {
  222. return scm_c_with_fluids (scm_list_1 (fluid), scm_list_1 (value),
  223. cproc, cdata);
  224. }
  225. #undef FUNC_NAME
  226. void
  227. scm_init_fluids ()
  228. {
  229. scm_tc16_fluid = scm_make_smob_type ("fluid", 0);
  230. scm_set_smob_print (scm_tc16_fluid, fluid_print);
  231. #include "libguile/fluids.x"
  232. }
  233. #if SCM_DEBUG_DEPRECATED == 0
  234. SCM
  235. scm_internal_with_fluids (SCM fluids, SCM values, SCM (*cproc) (), void *cdata)
  236. {
  237. scm_c_issue_deprecation_warning ("`scm_internal_with_fluids' is deprecated. "
  238. "Use `scm_c_with_fluids' instead.");
  239. return scm_c_with_fluids (fluids, values, cproc, cdata);
  240. }
  241. #endif
  242. /*
  243. Local Variables:
  244. c-file-style: "gnu"
  245. End:
  246. */